git.iko.soy/typeable/pandoc
on master help sign in
+ Added a 'substitute' function to Shared.hs. This is a generic
8f07505fiddlosopher
src/Text/Pandoc/Shared.hs+12−2
@@ -31,6 +31,7 @@ module Text.Pandoc.Shared (
31 -- * List processing
31 -- * List processing
32 splitBy,
32 splitBy,
33 splitByIndices,
33 splitByIndices,
34+ substitute,
34 -- * Text processing
35 -- * Text processing
35 gsub,
36 gsub,
36 joinWithSep,
37 joinWithSep,
@@ -79,8 +80,8 @@ import Text.Pandoc.Entities ( decodeEntities, encodeEntities, characterEntity )
79import Text.Regex ( matchRegexAll, mkRegex, subRegex, Regex )
80import Text.Regex ( matchRegexAll, mkRegex, subRegex, Regex )
80import Text.PrettyPrint.HughesPJ as PP ( text, char, (<>), ($$), nest, Doc,
81import Text.PrettyPrint.HughesPJ as PP ( text, char, (<>), ($$), nest, Doc,
81 isEmpty )
82 isEmpty )
82−import Char ( toLower )
83+import Data.Char ( toLower )
83−import List ( find, groupBy )
84+import Data.List ( find, groupBy, isPrefixOf )
84
85
85-- | Parse a string with a given parser and state.
86-- | Parse a string with a given parser and state.
86readWith :: GenParser Char ParserState a -- ^ parser
87readWith :: GenParser Char ParserState a -- ^ parser
@@ -288,6 +289,15 @@ removeTrailingSpace = reverse . removeLeadingSpace . reverse
288stripFirstAndLast str =
289stripFirstAndLast str =
289 drop 1 $ take ((length str) - 1) str
290 drop 1 $ take ((length str) - 1) str
290
291
292+-- | Replace each occurrence of one sublist in a list with another.
293+substitute :: (Eq a) => [a] -> [a] -> [a] -> [a]
294+substitute _ _ [] = []
295+substitute [] _ lst = lst
296+substitute target replacement lst =
297+ if isPrefixOf target lst
298+ then replacement ++ (substitute target replacement $ drop (length target) lst)
299+ else (head lst):(substitute target replacement $ tail lst)
300+
291-- | Split list into groups separated by sep.
301-- | Split list into groups separated by sep.
292splitBy :: (Eq a) => a -> [a] -> [[a]]
302splitBy :: (Eq a) => a -> [a] -> [[a]]
293splitBy _ [] = []
303splitBy _ [] = []
src/Text/Pandoc/Writers/LaTeX.hs+6−6
@@ -78,12 +78,12 @@ latexHeader notes options (Meta title authors date) =
78escapeBrackets = backslashEscape "{}"
78escapeBrackets = backslashEscape "{}"
79escapeSpecial = backslashEscape "$%&~_#"
79escapeSpecial = backslashEscape "$%&~_#"
80
80
81−escapeBackslash = gsub "\\\\" "\\\\textbackslash{}"
81+escapeBackslash = substitute "\\" "\\textbackslash{}"
82−fixBackslash = gsub "\\\\textbackslash\\\\\\{\\\\\\}" "\\\\textbackslash{}"
82+fixBackslash = substitute "\\textbackslash\\{\\}" "\\textbackslash{}"
83−escapeHat = gsub "\\^" "\\\\^{}"
83+escapeHat = substitute "^" "\\^{}"
84−escapeBar = gsub "\\|" "\\\\textbar{}"
84+escapeBar = substitute "|" "\\textbar{}"
85−escapeLt = gsub "<" "\\\\textless{}"
85+escapeLt = substitute "<" "\\textless{}"
86−escapeGt = gsub ">" "\\\\textgreater{}"
86+escapeGt = substitute ">" "\\textgreater{}"
87
87
88-- | Escape string for LaTeX
88-- | Escape string for LaTeX
89stringToLaTeX :: String -> String
89stringToLaTeX :: String -> String