@@ -65,6 +65,7 @@ import Text.Pandoc.Walk
6565import Text.Pandoc.Writers.HTML (writeHtml5String)
6666import Text.Pandoc.Writers.Math (texMathToInlines)
6767import Text.Pandoc.Writers.Shared
68+import Text.Pandoc.XML (toHtml5Entities)
6869
6970type Notes = [[Block]]
7071type Ref = (Doc, Target, Attr)
@@ -279,39 +280,44 @@ noteToMarkdown opts num blocks = do
279280
280281-- | Escape special characters for Markdown.
281282escapeString :: WriterOptions -> String -> String
282−escapeString _ [] = []
283−escapeString opts (c:cs) =
284− case c of
283+escapeString opts =
284+ (if writerPreferAscii opts
285+ then T.unpack . toHtml5Entities . T.pack
286+ else id) . go
287+ where
288+ go [] = []
289+ go (c:cs) =
290+ case c of
285291 '<' | isEnabled Ext_all_symbols_escapable opts ->
286− '\\' : '<' : escapeString opts cs
287− | otherwise -> "<" ++ escapeString opts cs
292+ '\\' : '<' : go cs
293+ | otherwise -> "<" ++ go cs
288294 '>' | isEnabled Ext_all_symbols_escapable opts ->
289− '\\' : '>' : escapeString opts cs
290− | otherwise -> ">" ++ escapeString opts cs
295+ '\\' : '>' : go cs
296+ | otherwise -> ">" ++ go cs
291297 '@' | isEnabled Ext_citations opts ->
292298 case cs of
293299 (d:_)
294300 | isAlphaNum d || d == '_'
295− -> '\\':'@':escapeString opts cs
296− _ -> '@':escapeString opts cs
301+ -> '\\':'@':go cs
302+ _ -> '@':go cs
297303 _ | c `elem` ['\\','`','*','_','[',']','#'] ->
298− '\\':c:escapeString opts cs
299− '|' | isEnabled Ext_pipe_tables opts -> '\\':'|':escapeString opts cs
300− '^' | isEnabled Ext_superscript opts -> '\\':'^':escapeString opts cs
304+ '\\':c:go cs
305+ '|' | isEnabled Ext_pipe_tables opts -> '\\':'|':go cs
306+ '^' | isEnabled Ext_superscript opts -> '\\':'^':go cs
301307 '~' | isEnabled Ext_subscript opts ||
302− isEnabled Ext_strikeout opts -> '\\':'~':escapeString opts cs
303− '$' | isEnabled Ext_tex_math_dollars opts -> '\\':'$':escapeString opts cs
304− '\'' | isEnabled Ext_smart opts -> '\\':'\'':escapeString opts cs
305− '"' | isEnabled Ext_smart opts -> '\\':'"':escapeString opts cs
308+ isEnabled Ext_strikeout opts -> '\\':'~':go cs
309+ '$' | isEnabled Ext_tex_math_dollars opts -> '\\':'$':go cs
310+ '\'' | isEnabled Ext_smart opts -> '\\':'\'':go cs
311+ '"' | isEnabled Ext_smart opts -> '\\':'"':go cs
306312 '-' | isEnabled Ext_smart opts ->
307313 case cs of
308− '-':_ -> '\\':'-':escapeString opts cs
309− _ -> '-':escapeString opts cs
314+ '-':_ -> '\\':'-':go cs
315+ _ -> '-':go cs
310316 '.' | isEnabled Ext_smart opts ->
311317 case cs of
312− '.':'.':rest -> '\\':'.':'.':'.':escapeString opts rest
313− _ -> '.':escapeString opts cs
314− _ -> c : escapeString opts cs
318+ '.':'.':rest -> '\\':'.':'.':'.':go rest
319+ _ -> '.':go cs
320+ _ -> c : go cs
315321
316322-- | Construct table of contents from list of header blocks.
317323tableOfContents :: PandocMonad m => WriterOptions -> [Block] -> MD m Doc
@@ -1069,12 +1075,18 @@ inlineToMarkdown opts (Quoted SingleQuote lst) = do
10691075 contents <- inlineListToMarkdown opts lst
10701076 return $ if isEnabled Ext_smart opts
10711077 then "'" <> contents <> "'"
1072− else "‘" <> contents <> "’"
1078+ else
1079+ if writerPreferAscii opts
1080+ then "‘" <> contents <> "’"
1081+ else "‘" <> contents <> "’"
10731082inlineToMarkdown opts (Quoted DoubleQuote lst) = do
10741083 contents <- inlineListToMarkdown opts lst
10751084 return $ if isEnabled Ext_smart opts
10761085 then "\"" <> contents <> "\""
1077− else "“" <> contents <> "”"
1086+ else
1087+ if writerPreferAscii opts
1088+ then "“" <> contents <> "”"
1089+ else "“" <> contents <> "”"
10781090inlineToMarkdown opts (Code attr str) = do
10791091 let tickGroups = filter (\s -> '`' `elem` s) $ group str
10801092 let longest = if null tickGroups