git.iko.soy/typeable/pandoc
on master help sign in
Markdown writer: Better escaping when +smart.
commite0abe18bb92b4d57cf0364486010de9acd8b8d71 authorJohn MacFarlane <jgm@berkeley.edu> dateSat 4 Feb 2017, 21:06 (10 years ago) parentcb1b0bc
1 file changed+21−17
src/Text/Pandoc/Writers/Markdown.hs+21−17
src/Text/Pandoc/Writers/Markdown.hs+21−17
@@ -267,23 +267,27 @@ noteToMarkdown opts num blocks = do
267267
268268-- | Escape special characters for Markdown.
269269escapeString :: WriterOptions -> String -> String
270−escapeString opts = escapeStringUsing markdownEscapes
271− where markdownEscapes = ('<', "&lt;") : ('>', "&gt;") :
272− backslashEscapes specialChars
273− specialChars =
274− (if isEnabled Ext_superscript opts
275− then ('^':)
276− else id) .
277− (if isEnabled Ext_subscript opts
278− then ('~':)
279− else id) .
280− (if isEnabled Ext_tex_math_dollars opts
281− then ('$':)
282− else id) $
283− "\\`*_[]#" ++
284− if isEnabled Ext_smart opts
285− then "\"'"
286− else ""
270+escapeString _ [] = []
271+escapeString opts (c:cs) =
272+ case c of
273+ '<' -> "&lt;" ++ escapeString opts cs
274+ '>' -> "&gt;" ++ escapeString opts cs
275+ _ | c `elem` ['\\','`','*','_','[',']','#'] ->
276+ '\\':c:escapeString opts cs
277+ '^' | isEnabled Ext_superscript opts -> '\\':'^':escapeString opts cs
278+ '~' | isEnabled Ext_subscript opts -> '\\':'~':escapeString opts cs
279+ '$' | isEnabled Ext_tex_math_dollars opts -> '\\':'$':escapeString opts cs
280+ '\'' | isEnabled Ext_smart opts -> '\\':'\'':escapeString opts cs
281+ '"' | isEnabled Ext_smart opts -> '\\':'"':escapeString opts cs
282+ '-' | isEnabled Ext_smart opts ->
283+ case cs of
284+ '-':_ -> '\\':'-':escapeString opts cs
285+ _ -> '-':escapeString opts cs
286+ '.' | isEnabled Ext_smart opts ->
287+ case cs of
288+ '.':'.':rest -> '\\':'.':'.':'.':escapeString opts rest
289+ _ -> '.':escapeString opts cs
290+ _ -> c : escapeString opts cs
287291
288292-- | Construct table of contents from list of header blocks.
289293tableOfContents :: PandocMonad m => WriterOptions -> [Block] -> m Doc