From 313d2e40b8b6d4457a70dc22e14da1e48e0bd6b1 Mon Sep 17 00:00:00 2001
From: fiddlosopher <fiddlosopher@788f1e2b-df1e-0410-8736-df70ead52e1b>
Date: Sun, 1 Nov 2009 02:38:18 +0000
Subject: [PATCH] Changed heuristic in compactify.

compactify has to decide whether a Para that ends a list is a Para
intentionally, or just because of the blank lines at the end of
every list.  In the latter case the Para is turned to a Plain.

The old heuristic was:  change final Para to Plain iff the other
items all end in Plain.  This produces bad results when, for example,
an item contains just a Plain and an HTML comment, as it

- a
<!--
- b
-->
-c

The new heuristic:  change final Para to Plain iff the other items
don't contain a Para.

git-svn-id: https://pandoc.googlecode.com/svn/trunk@1616 788f1e2b-df1e-0410-8736-df70ead52e1b
---
 src/Text/Pandoc/Shared.hs | 32 ++++++++++++++------------------
 1 file changed, 14 insertions(+), 18 deletions(-)

diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs
index b67e169c8..f920c79aa 100644
--- a/src/Text/Pandoc/Shared.hs
+++ b/src/Text/Pandoc/Shared.hs
@@ -856,29 +856,25 @@ normalizeSpaces list =
                                 else lst
     in  removeLeading $ removeTrailing $ removeDoubles list
 
--- | Change final list item from @Para@ to @Plain@ if the list should 
--- be compact.
+-- | Change final list item from @Para@ to @Plain@ if the list contains
+-- no other @Para@ blocks.
 compactify :: [[Block]]  -- ^ List of list items (each a list of blocks)
            -> [[Block]]
 compactify [] = []
 compactify items =
-    let final  = last items
-        others = init items
-    in  case last final of
-          Para a  -> if all endsWithPlain others && not (null final)
-                        then others ++ [init final ++ [Plain a]]
-                        else items
-          _       -> items
+  case (init items, last items) of
+       (_,[])          -> items
+       (others, final) ->
+            case last final of
+                 Para a -> case (filter isPara $ concat items) of
+                                -- if this is only Para, change to Plain
+                                [_] -> others ++ [init final ++ [Plain a]]
+                                _   -> items
+                 _      -> items
 
-endsWithPlain :: [Block] -> Bool
-endsWithPlain [] = False
-endsWithPlain blocks =
-  case last blocks of
-       Plain _                  -> True
-       (BulletList (x:xs))      -> endsWithPlain $ last (x:xs)
-       (OrderedList _ (x:xs))   -> endsWithPlain $ last (x:xs)
-       (DefinitionList (x:xs))  -> endsWithPlain $ last $ map snd (x:xs)
-       _                        -> False
+isPara :: Block -> Bool
+isPara (Para _) = True
+isPara _        = False
 
 -- | Data structure for defining hierarchical Pandoc documents
 data Element = Blk Block