From f0fb4c496b89530f74f4ad17612383563511a27b Mon Sep 17 00:00:00 2001
From: fiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b>
Date: Sat, 21 Jul 2007 21:34:13 +0000
Subject: [PATCH] Changes to functions for character escaping: + Removed
 escapeCharAsString + Added escapeStringUsing + Changed backslashEscape to
 backslashEscapes, which   now returns an association list of escapes to be  
 passed to escapeStringUsing

git-svn-id: https://pandoc.googlecode.com/svn/trunk@748 788f1e2b-df1e-0410-8736-df70ead52e1b
---
 src/Text/Pandoc/Shared.hs | 33 ++++++++++++++++-----------------
 1 file changed, 16 insertions(+), 17 deletions(-)

diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs
index 737282b97..06c1b89c3 100644
--- a/src/Text/Pandoc/Shared.hs
+++ b/src/Text/Pandoc/Shared.hs
@@ -35,8 +35,8 @@ module Text.Pandoc.Shared (
                      -- * Text processing
                      joinWithSep,
                      tabsToSpaces,
-                     backslashEscape,
-                     escapeCharAsString,
+                     backslashEscapes,
+                     escapeStringUsing,
                      endsWith,
                      stripTrailingNewlines,
                      removeLeadingTrailingSpace,
@@ -260,22 +260,21 @@ tabsInLine num tabstop (c:cs) =
                      else nextnumraw in
     replacement ++ (tabsInLine nextnum tabstop cs)
 
--- | Escape designated characters with backslash.
-backslashEscape :: [Char]    -- ^ list of special characters to escape
-                -> String    -- ^ string input
-                -> String 
-backslashEscape special [] = []
-backslashEscape special (x:xs) = if x `elem` special
-    then '\\':x:(backslashEscape special xs)
-    else x:(backslashEscape special xs)
+-- | Returns an association list of backslash escapes for the
+-- designated characters.
+backslashEscapes :: [Char]    -- ^ list of special characters to escape
+                 -> [(Char, String)]
+backslashEscapes = map (\ch -> (ch, ['\\',ch]))
 
--- | Escape a character as a string
-escapeCharAsString ch str "" = ""
-escapeCharAsString ch str (x:xs) | x == ch = 
-  str ++ escapeCharAsString ch str xs
-escapeCharAsString ch str xs =
-  let (a,b) = break (== ch) xs in
-      a ++ escapeCharAsString ch str b
+-- | Escape a string of characters, using an association list of
+-- characters and strings.
+escapeStringUsing :: [(Char, String)] -> String -> String
+escapeStringUsing escapeTable "" = ""
+escapeStringUsing escapeTable (x:xs) = 
+  case (lookup x escapeTable) of
+       Just str  -> str ++ rest
+       Nothing   -> x:rest
+  where rest = escapeStringUsing escapeTable xs
 
 -- | Returns @True@ if string ends with given character.
 endsWith :: Char -> [Char] -> Bool