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.
This commit is contained in:
Evan Silberman
2024-11-11 13:29:42 -08:00
committed by John MacFarlane
parent be734d2543
commit fdef25eb6e
2 changed files with 36 additions and 7 deletions
+14 -7
View File
@@ -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,19 @@ 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
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
+22
View File
@@ -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!”
```