@@ -45,14 +45,14 @@ module Text.Pandoc.Shared (
4545 toRomanNumeral,
4646 escapeURI,
4747 unescapeURI,
48+ tabFilter,
49+ -- * Prettyprinting
4850 wrapped,
4951 wrapIfNeeded,
5052 wrappedTeX,
5153 wrapTeXIfNeeded,
5254 BlockWrapper (..),
5355 wrappedBlocksToDoc,
54− tabFilter,
55− -- * Prettyprinting
5656 hang',
5757 -- * Pandoc block and inline list processing
5858 orderedListMarkers,
@@ -195,6 +195,30 @@ unescapeURI :: String -> String
195195unescapeURI = escapeURIString (\c -> isAllowedInURI c || not (isAscii c)) .
196196 decodeString . unEscapeString
197197
198+-- | Convert tabs to spaces and filter out DOS line endings.
199+-- Tabs will be preserved if tab stop is set to 0.
200+tabFilter :: Int -- ^ Tab stop
201+ -> String -- ^ Input
202+ -> String
203+tabFilter tabStop =
204+ let go _ [] = ""
205+ go _ ('\n':xs) = '\n' : go tabStop xs
206+ go _ ('\r':'\n':xs) = '\n' : go tabStop xs
207+ go _ ('\r':xs) = '\n' : go tabStop xs
208+ go spsToNextStop ('\t':xs) =
209+ if tabStop == 0
210+ then '\t' : go tabStop xs
211+ else replicate spsToNextStop ' ' ++ go tabStop xs
212+ go 1 (x:xs) =
213+ x : go tabStop xs
214+ go spsToNextStop (x:xs) =
215+ x : go (spsToNextStop - 1) xs
216+ in go tabStop
217+
218+--
219+-- Prettyprinting
220+--
221+
198222-- | Wrap inlines to line length.
199223wrapped :: Monad m => ([Inline] -> m Doc) -> [Inline] -> m Doc
200224wrapped listWriter sect = (mapM listWriter $ splitBy Space sect) >>=
@@ -264,30 +288,6 @@ wrappedBlocksToDoc = foldr addBlock empty
264288 addBlock (Pad d) accum = d $$ text "" $$ accum
265289 addBlock (Reg d) accum = d $$ accum
266290
267−-- | Convert tabs to spaces and filter out DOS line endings.
268−-- Tabs will be preserved if tab stop is set to 0.
269−tabFilter :: Int -- ^ Tab stop
270− -> String -- ^ Input
271− -> String
272−tabFilter tabStop =
273− let go _ [] = ""
274− go _ ('\n':xs) = '\n' : go tabStop xs
275− go _ ('\r':'\n':xs) = '\n' : go tabStop xs
276− go _ ('\r':xs) = '\n' : go tabStop xs
277− go spsToNextStop ('\t':xs) =
278− if tabStop == 0
279− then '\t' : go tabStop xs
280− else replicate spsToNextStop ' ' ++ go tabStop xs
281− go 1 (x:xs) =
282− x : go tabStop xs
283− go spsToNextStop (x:xs) =
284− x : go (spsToNextStop - 1) xs
285− in go tabStop
286−
287−--
288−-- Prettyprinting
289−--
290−
291291-- | A version of hang that works like the version in pretty-1.0.0.0
292292hang' :: Doc -> Int -> Doc -> Doc
293293hang' d1 n d2 = d1 $$ (nest n d2)