From 2645ce7acacc9050643cad1dbdf0d9759270765c Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Wed, 8 Mar 2017 12:06:08 +0100 Subject: Markdown writer: escape unordered list markers at beginning of paragraph to avoid false interpretation as a list. Also handle `|`, and refactor code for escaping `%`. Closes #3497. --- src/Text/Pandoc/Writers/Markdown.hs | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs index 96d46ae5b..844df3345 100644 --- a/src/Text/Pandoc/Writers/Markdown.hs +++ b/src/Text/Pandoc/Writers/Markdown.hs @@ -398,18 +398,26 @@ blockToMarkdown' opts (Plain inlines) = do then Just $ writerColumns opts else Nothing let rendered = render colwidth contents - let escapeDelimiter (x:xs) | x `elem` (".()" :: String) = '\\':x:xs - | otherwise = x : escapeDelimiter xs - escapeDelimiter [] = [] - let contents' = if isEnabled Ext_all_symbols_escapable opts && - not isPlain && beginsWithOrderedListMarker rendered - then text $ escapeDelimiter rendered - else contents - -- escape if para starts with % - return $ - if isEnabled Ext_pandoc_title_block opts && take 1 rendered == "%" - then "\\" <> contents' <> cr - else contents' <> cr + let escapeMarker (x:xs) | x `elem` (".()" :: String) = '\\':x:xs + | otherwise = x : escapeMarker xs + escapeMarker [] = [] + let contents' = + case rendered of + '%':_ | isEnabled Ext_pandoc_title_block opts && + isEnabled Ext_all_symbols_escapable opts -> + "\\" <> contents + '+':s:_ | not isPlain && isSpace s -> "\\" <> contents + '*':s:_ | not isPlain && isSpace s -> "\\" <> contents + '-':s:_ | not isPlain && isSpace s -> "\\" <> contents + '|':_ | (isEnabled Ext_line_blocks opts || + isEnabled Ext_pipe_tables opts) + && isEnabled Ext_all_symbols_escapable opts + -> "\\" <> contents + _ | not isPlain && beginsWithOrderedListMarker rendered + && isEnabled Ext_all_symbols_escapable opts + -> text $ escapeMarker rendered + | otherwise -> contents + return $ contents' <> cr -- title beginning with fig: indicates figure blockToMarkdown' opts (Para [Image attr alt (src,'f':'i':'g':':':tit)]) = blockToMarkdown opts (Para [Image attr alt (src,tit)]) -- cgit v1.2.3