diff options
-rw-r--r-- | src/Text/Pandoc/Shared.hs | 14 | ||||
-rw-r--r-- | src/Text/Pandoc/Writers/LaTeX.hs | 12 |
2 files changed, 18 insertions, 8 deletions
diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs index 8ee990827..adaffb9da 100644 --- a/src/Text/Pandoc/Shared.hs +++ b/src/Text/Pandoc/Shared.hs @@ -31,6 +31,7 @@ module Text.Pandoc.Shared ( -- * List processing splitBy, splitByIndices, + substitute, -- * Text processing gsub, joinWithSep, @@ -79,8 +80,8 @@ import Text.Pandoc.Entities ( decodeEntities, encodeEntities, characterEntity ) import Text.Regex ( matchRegexAll, mkRegex, subRegex, Regex ) import Text.PrettyPrint.HughesPJ as PP ( text, char, (<>), ($$), nest, Doc, isEmpty ) -import Char ( toLower ) -import List ( find, groupBy ) +import Data.Char ( toLower ) +import Data.List ( find, groupBy, isPrefixOf ) -- | Parse a string with a given parser and state. readWith :: GenParser Char ParserState a -- ^ parser @@ -288,6 +289,15 @@ removeTrailingSpace = reverse . removeLeadingSpace . reverse stripFirstAndLast str = drop 1 $ take ((length str) - 1) str +-- | Replace each occurrence of one sublist in a list with another. +substitute :: (Eq a) => [a] -> [a] -> [a] -> [a] +substitute _ _ [] = [] +substitute [] _ lst = lst +substitute target replacement lst = + if isPrefixOf target lst + then replacement ++ (substitute target replacement $ drop (length target) lst) + else (head lst):(substitute target replacement $ tail lst) + -- | Split list into groups separated by sep. splitBy :: (Eq a) => a -> [a] -> [[a]] splitBy _ [] = [] diff --git a/src/Text/Pandoc/Writers/LaTeX.hs b/src/Text/Pandoc/Writers/LaTeX.hs index db7af223d..2badf2f24 100644 --- a/src/Text/Pandoc/Writers/LaTeX.hs +++ b/src/Text/Pandoc/Writers/LaTeX.hs @@ -78,12 +78,12 @@ latexHeader notes options (Meta title authors date) = escapeBrackets = backslashEscape "{}" escapeSpecial = backslashEscape "$%&~_#" -escapeBackslash = gsub "\\\\" "\\\\textbackslash{}" -fixBackslash = gsub "\\\\textbackslash\\\\\\{\\\\\\}" "\\\\textbackslash{}" -escapeHat = gsub "\\^" "\\\\^{}" -escapeBar = gsub "\\|" "\\\\textbar{}" -escapeLt = gsub "<" "\\\\textless{}" -escapeGt = gsub ">" "\\\\textgreater{}" +escapeBackslash = substitute "\\" "\\textbackslash{}" +fixBackslash = substitute "\\textbackslash\\{\\}" "\\textbackslash{}" +escapeHat = substitute "^" "\\^{}" +escapeBar = substitute "|" "\\textbar{}" +escapeLt = substitute "<" "\\textless{}" +escapeGt = substitute ">" "\\textgreater{}" -- | Escape string for LaTeX stringToLaTeX :: String -> String |