git.iko.soy/typeable/pandoc
on master help sign in
Renamed removedLeadingTrailingSpace to trim.
93e92a4John MacFarlane
src/Text/Pandoc/Parsing.hs+2−3
@@ -610,7 +610,7 @@ gridTableWith blocks headless =
610
610
611gridTableSplitLine :: [Int] -> String -> [String]
611gridTableSplitLine :: [Int] -> String -> [String]
612gridTableSplitLine indices line = map removeFinalBar $ tail $
612gridTableSplitLine indices line = map removeFinalBar $ tail $
613− splitStringByIndices (init indices) $ removeTrailingSpace line
613+ splitStringByIndices (init indices) $ trimr line
614
614
615gridPart :: Char -> Parser [Char] st (Int, Int)
615gridPart :: Char -> Parser [Char] st (Int, Int)
616gridPart ch = do
616gridPart ch = do
@@ -652,8 +652,7 @@ gridTableHeader headless blocks = try $ do
652 then replicate (length dashes) ""
652 then replicate (length dashes) ""
653 else map (intercalate " ") $ transpose
653 else map (intercalate " ") $ transpose
654 $ map (gridTableSplitLine indices) rawContent
654 $ map (gridTableSplitLine indices) rawContent
655− heads <- mapM (parseFromString blocks) $
655+ heads <- mapM (parseFromString blocks) $ map trim rawHeads
656− map removeLeadingTrailingSpace rawHeads
657 return (heads, aligns, indices)
656 return (heads, aligns, indices)
658
657
659gridTableRawLine :: [Int] -> Parser [Char] ParserState [String]
658gridTableRawLine :: [Int] -> Parser [Char] ParserState [String]
src/Text/Pandoc/Readers/LaTeX.hs−3
@@ -146,9 +146,6 @@ braced = bgroup *> (concat <$> manyTill
146bracketed :: Monoid a => LP a -> LP a
146bracketed :: Monoid a => LP a -> LP a
147bracketed parser = try $ char '[' *> (mconcat <$> manyTill parser (char ']'))
147bracketed parser = try $ char '[' *> (mconcat <$> manyTill parser (char ']'))
148
148
149−trim :: String -> String
150−trim = removeLeadingTrailingSpace
151−
152mathDisplay :: LP String -> LP Inlines
149mathDisplay :: LP String -> LP Inlines
153mathDisplay p = displayMath <$> (try p >>= applyMacros' . trim)
150mathDisplay p = displayMath <$> (try p >>= applyMacros' . trim)
154
151
src/Text/Pandoc/Readers/Markdown.hs+9−9
@@ -239,7 +239,7 @@ referenceKey = try $ do
239 src <- try betweenAngles <|> sourceURL
239 src <- try betweenAngles <|> sourceURL
240 tit <- option "" referenceTitle
240 tit <- option "" referenceTitle
241 blanklines
241 blanklines
242− let target = (escapeURI $ removeTrailingSpace src, tit)
242+ let target = (escapeURI $ trimr src, tit)
243 st <- getState
243 st <- getState
244 let oldkeys = stateKeys st
244 let oldkeys = stateKeys st
245 updateState $ \s -> s { stateKeys = M.insert (toKey raw) target oldkeys }
245 updateState $ \s -> s { stateKeys = M.insert (toKey raw) target oldkeys }
@@ -848,7 +848,7 @@ simpleTableHeader headless = try $ do
848 else rawHeads
848 else rawHeads
849 heads <- fmap sequence
849 heads <- fmap sequence
850 $ mapM (parseFromString (mconcat <$> many plain))
850 $ mapM (parseFromString (mconcat <$> many plain))
851− $ map removeLeadingTrailingSpace rawHeads'
851+ $ map trim rawHeads'
852 return (heads, aligns, indices)
852 return (heads, aligns, indices)
853
853
854-- Returns an alignment type for a table, based on a list of strings
854-- Returns an alignment type for a table, based on a list of strings
@@ -859,7 +859,7 @@ alignType :: [String]
859 -> Alignment
859 -> Alignment
860alignType [] _ = AlignDefault
860alignType [] _ = AlignDefault
861alignType strLst len =
861alignType strLst len =
862− let nonempties = filter (not . null) $ map removeTrailingSpace strLst
862+ let nonempties = filter (not . null) $ map trimr strLst
863 (leftSpace, rightSpace) =
863 (leftSpace, rightSpace) =
864 case sortBy (comparing length) nonempties of
864 case sortBy (comparing length) nonempties of
865 (x:_) -> (head x `elem` " \t", length x < len)
865 (x:_) -> (head x `elem` " \t", length x < len)
@@ -884,7 +884,7 @@ rawTableLine :: [Int]
884rawTableLine indices = do
884rawTableLine indices = do
885 notFollowedBy' (blanklines <|> tableFooter)
885 notFollowedBy' (blanklines <|> tableFooter)
886 line <- many1Till anyChar newline
886 line <- many1Till anyChar newline
887− return $ map removeLeadingTrailingSpace $ tail $
887+ return $ map trim $ tail $
888 splitStringByIndices (init indices) line
888 splitStringByIndices (init indices) line
889
889
890-- Parse a table line and return a list of lists of blocks (columns).
890-- Parse a table line and return a list of lists of blocks (columns).
@@ -957,7 +957,7 @@ multilineTableHeader headless = try $ do
957 else map (intercalate " ") rawHeadsList
957 else map (intercalate " ") rawHeadsList
958 heads <- fmap sequence $
958 heads <- fmap sequence $
959 mapM (parseFromString (mconcat <$> many plain)) $
959 mapM (parseFromString (mconcat <$> many plain)) $
960− map removeLeadingTrailingSpace rawHeads
960+ map trim rawHeads
961 return (heads, aligns, indices)
961 return (heads, aligns, indices)
962
962
963-- Parse a grid table: starts with row of '-' on top, then header
963-- Parse a grid table: starts with row of '-' on top, then header
@@ -972,7 +972,7 @@ gridTable headless =
972
972
973gridTableSplitLine :: [Int] -> String -> [String]
973gridTableSplitLine :: [Int] -> String -> [String]
974gridTableSplitLine indices line = map removeFinalBar $ tail $
974gridTableSplitLine indices line = map removeFinalBar $ tail $
975− splitStringByIndices (init indices) $ removeTrailingSpace line
975+ splitStringByIndices (init indices) $ trimr line
976
976
977gridPart :: Char -> Parser [Char] st (Int, Int)
977gridPart :: Char -> Parser [Char] st (Int, Int)
978gridPart ch = do
978gridPart ch = do
@@ -1014,7 +1014,7 @@ gridTableHeader headless = try $ do
1014 else map (intercalate " ") $ transpose
1014 else map (intercalate " ") $ transpose
1015 $ map (gridTableSplitLine indices) rawContent
1015 $ map (gridTableSplitLine indices) rawContent
1016 heads <- fmap sequence $ mapM (parseFromString block) $
1016 heads <- fmap sequence $ mapM (parseFromString block) $
1017− map removeLeadingTrailingSpace rawHeads
1017+ map trim rawHeads
1018 return (heads, aligns, indices)
1018 return (heads, aligns, indices)
1019
1019
1020gridTableRawLine :: [Int] -> Parser [Char] ParserState [String]
1020gridTableRawLine :: [Int] -> Parser [Char] ParserState [String]
@@ -1228,7 +1228,7 @@ code = try $ do
1228 notFollowedBy (char '`')))
1228 notFollowedBy (char '`')))
1229 attr <- option ([],[],[]) (try $ guardEnabled Ext_inline_code_attributes >>
1229 attr <- option ([],[],[]) (try $ guardEnabled Ext_inline_code_attributes >>
1230 optional whitespace >> attributes)
1230 optional whitespace >> attributes)
1231− return $ return $ B.codeWith attr $ removeLeadingTrailingSpace $ concat result
1231+ return $ return $ B.codeWith attr $ trim $ concat result
1232
1232
1233math :: Parser [Char] ParserState (F Inlines)
1233math :: Parser [Char] ParserState (F Inlines)
1234math = (return . B.displayMath <$> (mathDisplay >>= applyMacros'))
1234math = (return . B.displayMath <$> (mathDisplay >>= applyMacros'))
@@ -1416,7 +1416,7 @@ source' = do
1416 tit <- option "" linkTitle
1416 tit <- option "" linkTitle
1417 skipSpaces
1417 skipSpaces
1418 eof
1418 eof
1419− return (escapeURI $ removeTrailingSpace src, tit)
1419+ return (escapeURI $ trimr src, tit)
1420
1420
1421linkTitle :: Parser [Char] ParserState String
1421linkTitle :: Parser [Char] ParserState String
1422linkTitle = try $ do
1422linkTitle = try $ do
src/Text/Pandoc/Readers/RST.hs+11−11
@@ -256,15 +256,15 @@ imageBlock = try $ do
256imageDef :: Inlines -> RSTParser Inlines
256imageDef :: Inlines -> RSTParser Inlines
257imageDef defaultAlt = try $ do
257imageDef defaultAlt = try $ do
258 string "image:: "
258 string "image:: "
259− src <- escapeURI . removeLeadingTrailingSpace <$> manyTill anyChar newline
259+ src <- escapeURI . trim <$> manyTill anyChar newline
260 fields <- try $ do indent <- lookAhead $ many (oneOf " /t")
260 fields <- try $ do indent <- lookAhead $ many (oneOf " /t")
261 many $ rawFieldListItem indent
261 many $ rawFieldListItem indent
262 optional blanklines
262 optional blanklines
263− let alt = maybe defaultAlt (\x -> B.str $ removeTrailingSpace x)
263+ let alt = maybe defaultAlt (\x -> B.str $ trimr x)
264 $ lookup "alt" fields
264 $ lookup "alt" fields
265 let img = B.image src "" alt
265 let img = B.image src "" alt
266 return $ case lookup "target" fields of
266 return $ case lookup "target" fields of
267− Just t -> B.link (escapeURI $ removeLeadingTrailingSpace t)
267+ Just t -> B.link (escapeURI $ trim t)
268 "" img
268 "" img
269 Nothing -> img
269 Nothing -> img
270
270
@@ -381,7 +381,7 @@ customCodeBlock = try $ do
381figureBlock :: RSTParser Blocks
381figureBlock :: RSTParser Blocks
382figureBlock = try $ do
382figureBlock = try $ do
383 string ".. figure::"
383 string ".. figure::"
384− src <- escapeURI . removeLeadingTrailingSpace <$> manyTill anyChar newline
384+ src <- escapeURI . trim <$> manyTill anyChar newline
385 body <- indentedBlock
385 body <- indentedBlock
386 caption <- parseFromString extractCaption body
386 caption <- parseFromString extractCaption body
387 return $ B.para $ B.image src "" caption
387 return $ B.para $ B.image src "" caption
@@ -540,7 +540,7 @@ defaultRoleBlock :: RSTParser Blocks
540defaultRoleBlock = try $ do
540defaultRoleBlock = try $ do
541 string ".. default-role::"
541 string ".. default-role::"
542 -- doesn't enforce any restrictions on the role name; embedded spaces shouldn't be allowed, for one
542 -- doesn't enforce any restrictions on the role name; embedded spaces shouldn't be allowed, for one
543− role <- manyTill anyChar newline >>= return . removeLeadingTrailingSpace
543+ role <- manyTill anyChar newline >>= return . trim
544 updateState $ \s -> s { stateRstDefaultRole =
544 updateState $ \s -> s { stateRstDefaultRole =
545 if null role
545 if null role
546 then stateRstDefaultRole defaultParserState
546 then stateRstDefaultRole defaultParserState
@@ -587,7 +587,7 @@ directive = try $ do
587-- divide string by blanklines
587-- divide string by blanklines
588toChunks :: String -> [String]
588toChunks :: String -> [String]
589toChunks = dropWhile null
589toChunks = dropWhile null
590− . map (removeLeadingTrailingSpace . unlines)
590+ . map (trim . unlines)
591 . splitBy (all (`elem` " \t")) . lines
591 . splitBy (all (`elem` " \t")) . lines
592
592
593---
593---
@@ -674,7 +674,7 @@ targetURI = do
674 contents <- many1 (try (many spaceChar >> newline >>
674 contents <- many1 (try (many spaceChar >> newline >>
675 many1 spaceChar >> noneOf " \t\n") <|> noneOf "\n")
675 many1 spaceChar >> noneOf " \t\n") <|> noneOf "\n")
676 blanklines
676 blanklines
677− return $ escapeURI $ removeLeadingTrailingSpace $ contents
677+ return $ escapeURI $ trim $ contents
678
678
679imageKey :: RSTParser ()
679imageKey :: RSTParser ()
680imageKey = try $ do
680imageKey = try $ do
@@ -758,7 +758,7 @@ simpleTableRow indices = do
758
758
759simpleTableSplitLine :: [Int] -> String -> [String]
759simpleTableSplitLine :: [Int] -> String -> [String]
760simpleTableSplitLine indices line =
760simpleTableSplitLine indices line =
761− map removeLeadingTrailingSpace
761+ map trim
762 $ tail $ splitByIndices (init indices) line
762 $ tail $ splitByIndices (init indices) line
763
763
764simpleTableHeader :: Bool -- ^ Headerless table
764simpleTableHeader :: Bool -- ^ Headerless table
@@ -777,7 +777,7 @@ simpleTableHeader headless = try $ do
777 then replicate (length dashes) ""
777 then replicate (length dashes) ""
778 else simpleTableSplitLine indices rawContent
778 else simpleTableSplitLine indices rawContent
779 heads <- mapM (parseFromString (B.toList . mconcat <$> many plain)) $
779 heads <- mapM (parseFromString (B.toList . mconcat <$> many plain)) $
780− map removeLeadingTrailingSpace rawHeads
780+ map trim rawHeads
781 return (heads, aligns, indices)
781 return (heads, aligns, indices)
782
782
783-- Parse a simple table.
783-- Parse a simple table.
@@ -845,7 +845,7 @@ code = try $ do
845 string "``"
845 string "``"
846 result <- manyTill anyChar (try (string "``"))
846 result <- manyTill anyChar (try (string "``"))
847 return $ B.code
847 return $ B.code
848− $ removeLeadingTrailingSpace $ unwords $ lines result
848+ $ trim $ unwords $ lines result
849
849
850-- succeeds only if we're not right after a str (ie. in middle of word)
850-- succeeds only if we're not right after a str (ie. in middle of word)
851atStart :: RSTParser a -> RSTParser a
851atStart :: RSTParser a -> RSTParser a
@@ -932,7 +932,7 @@ explicitLink = try $ do
932 src <- manyTill (noneOf ">\n") (char '>')
932 src <- manyTill (noneOf ">\n") (char '>')
933 skipSpaces
933 skipSpaces
934 string "`_"
934 string "`_"
935− return $ B.link (escapeURI $ removeLeadingTrailingSpace src) "" label'
935+ return $ B.link (escapeURI $ trim src) "" label'
936
936
937referenceLink :: RSTParser Inlines
937referenceLink :: RSTParser Inlines
938referenceLink = try $ do
938referenceLink = try $ do
src/Text/Pandoc/Shared.hs+9−9
@@ -38,9 +38,9 @@ module Text.Pandoc.Shared (
38 backslashEscapes,
38 backslashEscapes,
39 escapeStringUsing,
39 escapeStringUsing,
40 stripTrailingNewlines,
40 stripTrailingNewlines,
41− removeLeadingTrailingSpace,
41+ trim,
42− removeLeadingSpace,
42+ triml,
43− removeTrailingSpace,
43+ trimr,
44 stripFirstAndLast,
44 stripFirstAndLast,
45 camelCaseToHyphenated,
45 camelCaseToHyphenated,
46 toRomanNumeral,
46 toRomanNumeral,
@@ -161,16 +161,16 @@ stripTrailingNewlines :: String -> String
161stripTrailingNewlines = reverse . dropWhile (== '\n') . reverse
161stripTrailingNewlines = reverse . dropWhile (== '\n') . reverse
162
162
163-- | Remove leading and trailing space (including newlines) from string.
163-- | Remove leading and trailing space (including newlines) from string.
164−removeLeadingTrailingSpace :: String -> String
164+trim :: String -> String
165−removeLeadingTrailingSpace = removeLeadingSpace . removeTrailingSpace
165+trim = triml . trimr
166
166
167-- | Remove leading space (including newlines) from string.
167-- | Remove leading space (including newlines) from string.
168−removeLeadingSpace :: String -> String
168+triml :: String -> String
169−removeLeadingSpace = dropWhile (`elem` " \r\n\t")
169+triml = dropWhile (`elem` " \r\n\t")
170
170
171-- | Remove trailing space (including newlines) from string.
171-- | Remove trailing space (including newlines) from string.
172−removeTrailingSpace :: String -> String
172+trimr :: String -> String
173−removeTrailingSpace = reverse . removeLeadingSpace . reverse
173+trimr = reverse . triml . reverse
174
174
175-- | Strip leading and trailing characters from string
175-- | Strip leading and trailing characters from string
176stripFirstAndLast :: String -> String
176stripFirstAndLast :: String -> String
src/Text/Pandoc/Writers/Docbook.hs+2−2
@@ -49,7 +49,7 @@ authorToDocbook opts name' =
49 in if ',' `elem` name
49 in if ',' `elem` name
50 then -- last name first
50 then -- last name first
51 let (lastname, rest) = break (==',') name
51 let (lastname, rest) = break (==',') name
52− firstname = removeLeadingSpace rest in
52+ firstname = triml rest in
53 inTagsSimple "firstname" (text $ escapeStringForXML firstname) <>
53 inTagsSimple "firstname" (text $ escapeStringForXML firstname) <>
54 inTagsSimple "surname" (text $ escapeStringForXML lastname)
54 inTagsSimple "surname" (text $ escapeStringForXML lastname)
55 else -- last name last
55 else -- last name last
@@ -74,7 +74,7 @@ writeDocbook opts (Pandoc (Meta tit auths dat) blocks) =
74 else Nothing
74 else Nothing
75 render' = render colwidth
75 render' = render colwidth
76 opts' = if "/book>" `isSuffixOf`
76 opts' = if "/book>" `isSuffixOf`
77− (removeTrailingSpace $ writerTemplate opts)
77+ (trimr $ writerTemplate opts)
78 then opts{ writerChapters = True }
78 then opts{ writerChapters = True }
79 else opts
79 else opts
80 startLvl = if writerChapters opts' then 0 else 1
80 startLvl = if writerChapters opts' then 0 else 1
src/Text/Pandoc/Writers/EPUB.hs+2−2
@@ -151,7 +151,7 @@ writeEPUB opts doc@(Pandoc meta _) = do
151 [("id", takeBaseName $ eRelativePath ent),
151 [("id", takeBaseName $ eRelativePath ent),
152 ("href", eRelativePath ent),
152 ("href", eRelativePath ent),
153 ("media-type", maybe "" id $ getMimeType $ eRelativePath ent)] $ ()
153 ("media-type", maybe "" id $ getMimeType $ eRelativePath ent)] $ ()
154− let plainify t = removeTrailingSpace $
154+ let plainify t = trimr $
155 writePlain opts'{ writerStandalone = False } $
155 writePlain opts'{ writerStandalone = False } $
156 Pandoc meta [Plain t]
156 Pandoc meta [Plain t]
157 let plainTitle = plainify $ docTitle meta
157 let plainTitle = plainify $ docTitle meta
@@ -289,7 +289,7 @@ transformInlines _ sourceDir picsRef (Image lab (src,tit) : xs) = do
289 return new
289 return new
290 return $ Image lab (newsrc, tit) : xs
290 return $ Image lab (newsrc, tit) : xs
291transformInlines (MathML _) _ _ (x@(Math _ _) : xs) = do
291transformInlines (MathML _) _ _ (x@(Math _ _) : xs) = do
292− let writeHtmlInline opts z = removeTrailingSpace $
292+ let writeHtmlInline opts z = trimr $
293 writeHtmlString opts $ Pandoc (Meta [] [] []) [Plain [z]]
293 writeHtmlString opts $ Pandoc (Meta [] [] []) [Plain [z]]
294 mathml = writeHtmlInline def{writerHTMLMathMethod = MathML Nothing } x
294 mathml = writeHtmlInline def{writerHTMLMathMethod = MathML Nothing } x
295 fallback = writeHtmlInline def{writerHTMLMathMethod = PlainMath } x
295 fallback = writeHtmlInline def{writerHTMLMathMethod = PlainMath } x
src/Text/Pandoc/Writers/Man.hs+1−2
@@ -63,8 +63,7 @@ pandocToMan opts (Pandoc (Meta title authors date) blocks) = do
63 (text (reverse xs), char d)
63 (text (reverse xs), char d)
64 xs -> (text (reverse xs), doubleQuotes empty)
64 xs -> (text (reverse xs), doubleQuotes empty)
65 let description = hsep $
65 let description = hsep $
66− map (doubleQuotes . text . removeLeadingTrailingSpace) $
66+ map (doubleQuotes . text . trim) $ splitBy (== '|') rest
67− splitBy (== '|') rest
68 body <- blockListToMan opts blocks
67 body <- blockListToMan opts blocks
69 notes <- liftM stNotes get
68 notes <- liftM stNotes get
70 notes' <- notesToMan opts (reverse notes)
69 notes' <- notesToMan opts (reverse notes)
tests/Tests/Helpers.hs+2−2
@@ -17,7 +17,7 @@ import Test.Framework
17import Test.Framework.Providers.HUnit
17import Test.Framework.Providers.HUnit
18import Test.Framework.Providers.QuickCheck2
18import Test.Framework.Providers.QuickCheck2
19import Test.HUnit (assertBool)
19import Test.HUnit (assertBool)
20−import Text.Pandoc.Shared (normalize, removeTrailingSpace)
20+import Text.Pandoc.Shared (normalize, trimr)
21import Text.Pandoc.Options
21import Text.Pandoc.Options
22import Text.Pandoc.Writers.Native (writeNative)
22import Text.Pandoc.Writers.Native (writeNative)
23import Language.Haskell.TH.Quote (QuasiQuoter(..))
23import Language.Haskell.TH.Quote (QuasiQuoter(..))
@@ -94,7 +94,7 @@ instance ToString Blocks where
94 toString = writeNative def . toPandoc
94 toString = writeNative def . toPandoc
95
95
96instance ToString Inlines where
96instance ToString Inlines where
97− toString = removeTrailingSpace . writeNative def . toPandoc
97+ toString = trimr . writeNative def . toPandoc
98
98
99instance ToString String where
99instance ToString String where
100 toString = id
100 toString = id