git.iko.soy/typeable/pandoc
on master help sign in
+ Added a 'substitute' function to Shared.hs. This is a generic

list function that can be used to substitute one substring for another in a string, like 'gsub' except without regular expressions. + Use 'substitute' instead of 'gsub' in the LaTeX writer. This avoids what appears to be a bug in Text.Regex, whereby "\\^" matches "\350". There seems to be a slight speed improvement as well. (Note: If this works, it would be good to replace other uses of gsub that don't employ regexs with 'substitute'.) git-svn-id: https://pandoc.googlecode.com/svn/trunk@500 788f1e2b-df1e-0410-8736-df70ead52e1b

commit8f0750574ac1702d27bf9a98ccce3962ad0b587c authorfiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b> dateMon 22 Jan 2007, 21:28 (20 years ago) parenta7839a1
2 files changed+18−8
src/Text/Pandoc/Shared.hs+12−2 src/Text/Pandoc/Writers/LaTeX.hs+6−6
src/Text/Pandoc/Shared.hs+12−2
@@ -31,6 +31,7 @@ module Text.Pandoc.Shared (
3131 -- * List processing
3232 splitBy,
3333 splitByIndices,
34+ substitute,
3435 -- * Text processing
3536 gsub,
3637 joinWithSep,
@@ -79,8 +80,8 @@ import Text.Pandoc.Entities ( decodeEntities, encodeEntities, characterEntity )
7980import Text.Regex ( matchRegexAll, mkRegex, subRegex, Regex )
8081import Text.PrettyPrint.HughesPJ as PP ( text, char, (<>), ($$), nest, Doc,
8182 isEmpty )
82−import Char ( toLower )
83−import List ( find, groupBy )
83+import Data.Char ( toLower )
84+import Data.List ( find, groupBy, isPrefixOf )
8485
8586-- | Parse a string with a given parser and state.
8687readWith :: GenParser Char ParserState a -- ^ parser
@@ -288,6 +289,15 @@ removeTrailingSpace = reverse . removeLeadingSpace . reverse
288289stripFirstAndLast str =
289290 drop 1 $ take ((length str) - 1) str
290291
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+
291301-- | Split list into groups separated by sep.
292302splitBy :: (Eq a) => a -> [a] -> [[a]]
293303splitBy _ [] = []
src/Text/Pandoc/Writers/LaTeX.hs+6−6
@@ -78,12 +78,12 @@ latexHeader notes options (Meta title authors date) =
7878escapeBrackets = backslashEscape "{}"
7979escapeSpecial = backslashEscape "$%&~_#"
8080
81−escapeBackslash = gsub "\\\\" "\\\\textbackslash{}"
82−fixBackslash = gsub "\\\\textbackslash\\\\\\{\\\\\\}" "\\\\textbackslash{}"
83−escapeHat = gsub "\\^" "\\\\^{}"
84−escapeBar = gsub "\\|" "\\\\textbar{}"
85−escapeLt = gsub "<" "\\\\textless{}"
86−escapeGt = gsub ">" "\\\\textgreater{}"
81+escapeBackslash = substitute "\\" "\\textbackslash{}"
82+fixBackslash = substitute "\\textbackslash\\{\\}" "\\textbackslash{}"
83+escapeHat = substitute "^" "\\^{}"
84+escapeBar = substitute "|" "\\textbar{}"
85+escapeLt = substitute "<" "\\textless{}"
86+escapeGt = substitute ">" "\\textgreater{}"
8787
8888-- | Escape string for LaTeX
8989stringToLaTeX :: String -> String