summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2017-02-04 21:06:42 +0100
committerJohn MacFarlane <jgm@berkeley.edu>2017-02-04 21:06:42 +0100
commite0abe18bb92b4d57cf0364486010de9acd8b8d71 (patch)
tree4cef9cdd104882c6e8aa619d83a631039b954e8c
parentcb1b0bcba7963a3d5becf57af0dcc72e82c82aed (diff)
Markdown writer: Better escaping when +smart.
-rw-r--r--src/Text/Pandoc/Writers/Markdown.hs38
1 files changed, 21 insertions, 17 deletions
diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs
index 7826c4bdd..8327ea9bc 100644
--- a/src/Text/Pandoc/Writers/Markdown.hs
+++ b/src/Text/Pandoc/Writers/Markdown.hs
@@ -267,23 +267,27 @@ noteToMarkdown opts num blocks = do
-- | Escape special characters for Markdown.
escapeString :: WriterOptions -> String -> String
-escapeString opts = escapeStringUsing markdownEscapes
- where markdownEscapes = ('<', "&lt;") : ('>', "&gt;") :
- backslashEscapes specialChars
- specialChars =
- (if isEnabled Ext_superscript opts
- then ('^':)
- else id) .
- (if isEnabled Ext_subscript opts
- then ('~':)
- else id) .
- (if isEnabled Ext_tex_math_dollars opts
- then ('$':)
- else id) $
- "\\`*_[]#" ++
- if isEnabled Ext_smart opts
- then "\"'"
- else ""
+escapeString _ [] = []
+escapeString opts (c:cs) =
+ case c of
+ '<' -> "&lt;" ++ escapeString opts cs
+ '>' -> "&gt;" ++ escapeString opts cs
+ _ | c `elem` ['\\','`','*','_','[',']','#'] ->
+ '\\':c:escapeString opts cs
+ '^' | isEnabled Ext_superscript opts -> '\\':'^':escapeString opts cs
+ '~' | isEnabled Ext_subscript opts -> '\\':'~':escapeString opts cs
+ '$' | isEnabled Ext_tex_math_dollars opts -> '\\':'$':escapeString opts cs
+ '\'' | isEnabled Ext_smart opts -> '\\':'\'':escapeString opts cs
+ '"' | isEnabled Ext_smart opts -> '\\':'"':escapeString opts cs
+ '-' | isEnabled Ext_smart opts ->
+ case cs of
+ '-':_ -> '\\':'-':escapeString opts cs
+ _ -> '-':escapeString opts cs
+ '.' | isEnabled Ext_smart opts ->
+ case cs of
+ '.':'.':rest -> '\\':'.':'.':'.':escapeString opts rest
+ _ -> '.':escapeString opts cs
+ _ -> c : escapeString opts cs
-- | Construct table of contents from list of header blocks.
tableOfContents :: PandocMonad m => WriterOptions -> [Block] -> m Doc