From 61968047e49f2710499028d71b12c783a53c7658 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sat, 8 Jan 2022 23:09:06 -0800 Subject: [PATCH] Avoid blank lines after tight sublists in org, haddock. T.P.Writers.Shared `endsWithPlain` now returns True if the list ends with a list which ends with a Plain. See #7810. --- src/Text/Pandoc/Writers/Org.hs | 4 ++-- src/Text/Pandoc/Writers/RST.hs | 11 +++++++++-- src/Text/Pandoc/Writers/Shared.hs | 4 +++- test/writer.haddock | 1 - test/writer.org | 1 - 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Text/Pandoc/Writers/Org.hs b/src/Text/Pandoc/Writers/Org.hs index d6f3d6e37..22c229de9 100644 --- a/src/Text/Pandoc/Writers/Org.hs +++ b/src/Text/Pandoc/Writers/Org.hs @@ -219,7 +219,7 @@ bulletListItemToOrg :: PandocMonad m => [Block] -> Org m (Doc Text) bulletListItemToOrg items = do exts <- gets $ writerExtensions . stOptions contents <- blockListToOrg (taskListItemToOrg exts items) - return $ hang 2 "- " contents $$ + return $ hang 2 "- " (chomp contents) $$ if null items || endsWithPlain items then cr else blankline @@ -237,7 +237,7 @@ orderedListItemToOrg marker counter items = do (\n -> space <> literal "[@" <> literal (tshow n) <> literal "]") counter return $ hang (T.length marker + 1) - (literal marker <> cookie <> space) contents $$ + (literal marker <> cookie <> space) (chomp contents) $$ if null items || endsWithPlain items then cr else blankline diff --git a/src/Text/Pandoc/Writers/RST.hs b/src/Text/Pandoc/Writers/RST.hs index 549f0f369..08e4bfa53 100644 --- a/src/Text/Pandoc/Writers/RST.hs +++ b/src/Text/Pandoc/Writers/RST.hs @@ -32,6 +32,7 @@ import Text.Pandoc.Shared import Text.Pandoc.Templates (renderTemplate) import Text.Pandoc.Writers.Shared import Text.Pandoc.Walk +import Safe (lastMay) type Refs = [([Inline], Target)] @@ -365,7 +366,7 @@ bulletListItemToRST :: PandocMonad m => [Block] -> RST m (Doc Text) bulletListItemToRST items = do contents <- blockListToRST items return $ hang 3 "- " contents $$ - if null items || endsWithPlain items + if null items || (endsWithPlain items && not (endsWithList items)) then cr else blankline @@ -378,10 +379,16 @@ orderedListItemToRST marker items = do contents <- blockListToRST items let marker' = marker <> " " return $ hang (T.length marker') (literal marker') contents $$ - if null items || endsWithPlain items + if null items || (endsWithPlain items && not (endsWithList items)) then cr else blankline +endsWithList :: [Block] -> Bool +endsWithList bs = case lastMay bs of + Just (BulletList{}) -> True + Just (OrderedList{}) -> True + _ -> False + -- | Convert definition list item (label, list of blocks) to RST. definitionListItemToRST :: PandocMonad m => ([Inline], [[Block]]) -> RST m (Doc Text) definitionListItemToRST (label, defs) = do diff --git a/src/Text/Pandoc/Writers/Shared.hs b/src/Text/Pandoc/Writers/Shared.hs index 4a1a39520..f9bb5b1eb 100644 --- a/src/Text/Pandoc/Writers/Shared.hs +++ b/src/Text/Pandoc/Writers/Shared.hs @@ -456,7 +456,9 @@ endsWithPlain :: [Block] -> Bool endsWithPlain xs = case lastMay xs of Just Plain{} -> True - _ -> False + Just (BulletList is) -> maybe False endsWithPlain (lastMay is) + Just (OrderedList _ is) -> maybe False endsWithPlain (lastMay is) + _ -> False -- | Convert the relevant components of a new-style table (with block -- caption, row headers, row and column spans, and so on) to those of diff --git a/test/writer.haddock b/test/writer.haddock index 82c45f358..81865b2e8 100644 --- a/test/writer.haddock +++ b/test/writer.haddock @@ -206,7 +206,6 @@ Here’s another: - Fee - Fie - Foe - 3. Third Same thing but with paragraphs: diff --git a/test/writer.org b/test/writer.org index 3b0abef2b..7dbecd1f3 100644 --- a/test/writer.org +++ b/test/writer.org @@ -241,7 +241,6 @@ Here's another: - Fee - Fie - Foe - 3. Third Same thing but with paragraphs: