From 51f6f0e3a1533a0e1bb94538f55bdc9db380a028 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Fri, 3 Dec 2021 17:52:47 -0800 Subject: Improve Markdown writer escaping. This fixes escaping for '#' in particular. Closes #7726. --- src/Text/Pandoc/Writers/Markdown/Inline.hs | 37 +++++++++++++++--------------- test/command/3792.md | 2 +- test/command/4164.md | 2 +- test/command/7208.md | 2 +- test/command/7726.md | 22 ++++++++++++++++++ test/command/biblatex-sigfridsson.md | 2 +- test/writer.opml | 2 +- 7 files changed, 46 insertions(+), 23 deletions(-) create mode 100644 test/command/7726.md diff --git a/src/Text/Pandoc/Writers/Markdown/Inline.hs b/src/Text/Pandoc/Writers/Markdown/Inline.hs index be22f8d9f..e774b5cc4 100644 --- a/src/Text/Pandoc/Writers/Markdown/Inline.hs +++ b/src/Text/Pandoc/Writers/Markdown/Inline.hs @@ -44,32 +44,35 @@ import Text.Pandoc.Writers.Markdown.Types (MarkdownVariant(..), -- | Escape special characters for Markdown. escapeText :: WriterOptions -> Text -> Text -escapeText opts = T.pack . go . T.unpack +escapeText opts = T.pack . go' . T.unpack where startsWithSpace (' ':_) = True startsWithSpace ('\t':_) = True startsWithSpace [] = True startsWithSpace _ = False + go' ('#':cs) + | isEnabled Ext_space_in_atx_header opts + = if startsWithSpace (dropWhile (=='#') cs) + then '\\':'#':go cs + else '#':go cs + | otherwise = '\\':'#':go cs + go' ('@':cs) + | isEnabled Ext_citations opts = + case cs of + (d:_) + | isAlphaNum d || d == '_' || d == '{' + -> '\\':'@':go cs + _ -> '@':go cs + go' cs = go cs go [] = [] go (c:cs) = case c of - '<' | isEnabled Ext_all_symbols_escapable opts -> - '\\' : '<' : go cs - | otherwise -> "<" ++ go cs - '>' | isEnabled Ext_all_symbols_escapable opts -> - '\\' : '>' : go cs - | otherwise -> ">" ++ go cs - '@' | isEnabled Ext_citations opts -> - case cs of - (d:_) - | isAlphaNum d || d == '_' || d == '{' - -> '\\':'@':go cs - _ -> '@':go cs - '#' | isEnabled Ext_space_in_atx_header opts - , startsWithSpace cs - -> '\\':'#':go cs _ | c `elem` ['\\','`','*','_','[',']'] -> '\\':c:go cs + '>' | isEnabled Ext_all_symbols_escapable opts -> '\\':'>':go cs + | otherwise -> ">" ++ go cs + '<' | isEnabled Ext_all_symbols_escapable opts -> '\\':'<':go cs + | otherwise -> "<" ++ go cs '|' | isEnabled Ext_pipe_tables opts -> '\\':'|':go cs '^' | isEnabled Ext_superscript opts -> '\\':'^':go cs '~' | isEnabled Ext_subscript opts || @@ -90,8 +93,6 @@ escapeText opts = T.pack . go . T.unpack | isEnabled Ext_intraword_underscores opts , isAlphaNum c , isAlphaNum x -> c : '_' : x : go xs - '#':xs -> c : '#' : go xs - '>':xs -> c : '>' : go xs _ -> c : go cs attrsToMarkdown :: Attr -> Doc Text diff --git a/test/command/3792.md b/test/command/3792.md index eff26d517..eb109b9cc 100644 --- a/test/command/3792.md +++ b/test/command/3792.md @@ -6,7 +6,7 @@ and properly escaped. ok ^D --- -title: \ \*that\* +title: \ \*that\* --- ok diff --git a/test/command/4164.md b/test/command/4164.md index 68cbd0584..4e7b7e285 100644 --- a/test/command/4164.md +++ b/test/command/4164.md @@ -26,6 +26,6 @@ Here is inline html: Here is inline html: -\
\ bla bla \
+\ \ bla bla \ ``` diff --git a/test/command/7208.md b/test/command/7208.md index fe02ec32e..e65943ade 100644 --- a/test/command/7208.md +++ b/test/command/7208.md @@ -2,5 +2,5 @@ % pandoc -t gfm \ ^D -\ +\ ``` diff --git a/test/command/7726.md b/test/command/7726.md new file mode 100644 index 000000000..bab11ca04 --- /dev/null +++ b/test/command/7726.md @@ -0,0 +1,22 @@ +``` +% pandoc -t markdown +\# Hi + +\## Hi + +\### Hi + +\#### hi + +and #hi +^D +\# Hi + +\## Hi + +\### Hi + +\#### hi + +and #hi +``` diff --git a/test/command/biblatex-sigfridsson.md b/test/command/biblatex-sigfridsson.md index e042a8762..f83c35622 100644 --- a/test/command/biblatex-sigfridsson.md +++ b/test/command/biblatex-sigfridsson.md @@ -90,7 +90,7 @@ references: - family: Ryde given: Ulf container-title: Journal of Computational Chemistry - doi: "10.1002/(SICI)1096-987X(199803)19:4\\<377::AID-JCC1>3.0.CO;2-P" + doi: "10.1002/(SICI)1096-987X(199803)19:4\\<377::AID-JCC1\\>3.0.CO;2-P" id: sigfridsson issue: 4 issued: 1998 diff --git a/test/writer.opml b/test/writer.opml index bfe1e5de6..6bdcb882e 100644 --- a/test/writer.opml +++ b/test/writer.opml @@ -52,7 +52,7 @@ - + -- cgit v1.2.3