From 0c3463f1d4378a21a41118e9327d63f3d3f5f28b Mon Sep 17 00:00:00 2001 From: Evan Silberman Date: Mon, 11 Nov 2024 13:29:42 -0800 Subject: [PATCH] Respect empty LineBlock lines in plain writer The plain writer behaved as a markdown variant with Ext_line_blocks turned off, and so empty lines in a line block would get eliminated. This is surprising, since if there's anything where the intent can be preserved in plain text output it's empty lines. It's still a bit surprising to have nbsps in plain text output, as in the test, where the distinction doesn't really matter, but that'd be an orthogonal change. --- src/Text/Pandoc/Writers/Markdown.hs | 22 +++++++++++++++------- test/command/jabberwocky.md | 22 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 test/command/jabberwocky.md diff --git a/src/Text/Pandoc/Writers/Markdown.hs b/src/Text/Pandoc/Writers/Markdown.hs index 62cbfba6c3ca..ff85f5911c8f 100644 --- a/src/Text/Pandoc/Writers/Markdown.hs +++ b/src/Text/Pandoc/Writers/Markdown.hs @@ -21,7 +21,7 @@ module Text.Pandoc.Writers.Markdown ( writeCommonMark, writeMarkua, writePlain) where -import Control.Monad (foldM, zipWithM, MonadPlus(..), when) +import Control.Monad (foldM, zipWithM, MonadPlus(..), when, liftM) import Control.Monad.Reader ( asks, MonadReader(local) ) import Control.Monad.State.Strict ( gets, modify ) import Data.Default @@ -447,12 +447,20 @@ blockToMarkdown' opts (Plain inlines) = do return $ contents <> cr blockToMarkdown' opts (Para inlines) = (<> blankline) `fmap` blockToMarkdown opts (Plain inlines) -blockToMarkdown' opts (LineBlock lns) = - if isEnabled Ext_line_blocks opts - then do - mdLines <- mapM (inlineListToMarkdown opts) lns - return $ vcat (map (hang 2 (literal "| ")) mdLines) <> blankline - else blockToMarkdown opts $ linesToPara lns +blockToMarkdown' opts (LineBlock lns) = do + variant <- asks envVariant + case variant of + PlainText -> do + let emptyToBlank l = if isEmpty l then blankline else l + mdLines <- mapM (liftM emptyToBlank . inlineListToMarkdown opts) lns + trace $ tshow mdLines + return $ vcat mdLines <> blankline + _ -> + if isEnabled Ext_line_blocks opts + then do + mdLines <- mapM (inlineListToMarkdown opts) lns + return $ vcat (map (hang 2 (literal "| ")) mdLines) <> blankline + else blockToMarkdown opts $ linesToPara lns blockToMarkdown' opts b@(RawBlock f str) = do variant <- asks envVariant let Format fmt = f diff --git a/test/command/jabberwocky.md b/test/command/jabberwocky.md new file mode 100644 index 000000000000..442534f40c47 --- /dev/null +++ b/test/command/jabberwocky.md @@ -0,0 +1,22 @@ +``` +% pandoc -t plain -f markdown +| 'Twas brillig, and the slithy toves +| Did gyre and gimble in the wabe: +| All mimsy were the borogoves, +| And the mome raths outgrabe. +| +| "Beware the Jabberwock, my son! +| The jaws that bite, the claws that catch! +| Beware the Jubjub bird, and shun +| The frumious Bandersnatch!" +^D +’Twas brillig, and the slithy toves +      Did gyre and gimble in the wabe: +All mimsy were the borogoves, +      And the mome raths outgrabe. + +“Beware the Jabberwock, my son! +      The jaws that bite, the claws that catch! +Beware the Jubjub bird, and shun +      The frumious Bandersnatch!” +```