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