git.iko.soy/typeable/pandoc
on master help sign in
Implement --ascii for Markdown writer.
commit26341c163225c142d19af425af08049cb2eef446 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 1 Nov 2018, 16:31 (8 years ago) parentf379edc
3 files changed+48−28
MANUAL.txt+5−5 src/Text/Pandoc/Writers/Markdown.hs+35−23 test/command/ascii.md+8
MANUAL.txt+5−5
@@ -860,11 +860,11 @@ Options affecting specific writers {.options}
860860
861861: Use only ASCII characters in output. Currently supported for
862862 XML and HTML formats (which use entities instead of
863− UTF-8 when this option is selected), groff ms
864− (which use hexadecimal escapes), and to a limited degree
865− LaTeX (which uses standard commands for accented
866− characters when possible). Groff man output uses ASCII
867− by default.
863+ UTF-8 when this option is selected), Markdown (which uses
864+ entities), groff ms (which use hexadecimal escapes), and to
865+ a limited degree LaTeX (which uses standard commands for
866+ accented characters when possible). Groff man output uses
867+ ASCII by default.
868868
869869`--reference-links`
870870
src/Text/Pandoc/Writers/Markdown.hs+35−23
@@ -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 -> "&lt;" ++ escapeString opts cs
292+ '\\' : '<' : go cs
293+ | otherwise -> "&lt;" ++ go cs
288294 '>' | isEnabled Ext_all_symbols_escapable opts ->
289− '\\' : '>' : escapeString opts cs
290− | otherwise -> "&gt;" ++ escapeString opts cs
295+ '\\' : '>' : go cs
296+ | otherwise -> "&gt;" ++ 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 "&lsquo;" <> contents <> "&rsquo;"
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 "&ldquo;" <> contents <> "&rdquo;"
1089+ else "“" <> contents <> "”"
10781090inlineToMarkdown opts (Code attr str) = do
10791091 let tickGroups = filter (\s -> '`' `elem` s) $ group str
10801092 let longest = if null tickGroups
test/command/ascii.md+8
@@ -43,3 +43,11 @@ pandoc -t jats --ascii
4343^D
4444<p>&#228;&#233;&#305;&#229;</p>
4545```
46+
47+```
48+pandoc -t markdown-smart --ascii
49+"äéıå"
50+^D
51+&ldquo;&auml;&eacute;&inodot;&aring;&rdquo;
52+```
53+