@@ -35,8 +35,8 @@ module Text.Pandoc.Shared (
35 -- * Text processing
35 -- * Text processing
36 joinWithSep,
36 joinWithSep,
37 tabsToSpaces,
37 tabsToSpaces,
38− backslashEscape,
38+ backslashEscapes,
39− escapeCharAsString,
39+ escapeStringUsing,
41 stripTrailingNewlines,
41 stripTrailingNewlines,
42 removeLeadingTrailingSpace,
42 removeLeadingTrailingSpace,
@@ -260,22 +260,21 @@ tabsInLine num tabstop (c:cs) =
260 else nextnumraw in
260 else nextnumraw in
261 replacement ++ (tabsInLine nextnum tabstop cs)
261 replacement ++ (tabsInLine nextnum tabstop cs)
263−-- | Escape designated characters with backslash.
263+-- | Returns an association list of backslash escapes for the
264+-- designated characters.
264−backslashEscape :: [Char] -- ^ list of special characters to escape
265+backslashEscapes :: [Char] -- ^ list of special characters to escape
265− -> String -- ^ string input
266+ -> [(Char, String)]
266− -> String
267+backslashEscapes = map (\ch -> (ch, ['\\',ch]))
267−backslashEscape special [] = []
268+
268−backslashEscape special (x:xs) = if x `elem` special
269+-- | Escape a string of characters, using an association list of
269− then '\\':x:(backslashEscape special xs)
270+-- characters and strings.
270− else x:(backslashEscape special xs)
271+escapeStringUsing :: [(Char, String)] -> String -> String
271−
272+escapeStringUsing escapeTable "" = ""
272−-- | Escape a character as a string
273+escapeStringUsing escapeTable (x:xs) =
273−escapeCharAsString ch str "" = ""
274+ case (lookup x escapeTable) of
274−escapeCharAsString ch str (x:xs) | x == ch =
275+ Just str -> str ++ rest
275− str ++ escapeCharAsString ch str xs
276+ Nothing -> x:rest
276−escapeCharAsString ch str xs =
277+ where rest = escapeStringUsing escapeTable xs
277− let (a,b) = break (== ch) xs in
278− a ++ escapeCharAsString ch str b
280-- | Returns @True@ if string ends with given character.
279-- | Returns @True@ if string ends with given character.
281endsWith :: Char -> [Char] -> Bool
280endsWith :: Char -> [Char] -> Bool