Skip to content

Commit

Permalink
Respect empty LineBlock lines in plain writer
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
silby committed Nov 11, 2024
1 parent be734d2 commit 0c3463f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
22 changes: 15 additions & 7 deletions src/Text/Pandoc/Writers/Markdown.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions test/command/jabberwocky.md
Original file line number Diff line number Diff line change
@@ -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!”
```

0 comments on commit 0c3463f

Please sign in to comment.