git.iko.soy/typeable/pandoc
on master help sign in
Shared: Export compactify', formerly in Markdown reader.
commit5c06322ab2cc6707ec1e00f9b6c17283cd0fb347 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 27 Sep 2012, 17:22 (14 years ago) parent7633d51
2 files changed+25−21
src/Text/Pandoc/Readers/Markdown.hs+7−21 src/Text/Pandoc/Shared.hs+18
src/Text/Pandoc/Readers/Markdown.hs+7−21
@@ -39,7 +39,7 @@ import Text.Pandoc.Definition
3939import qualified Text.Pandoc.Builder as B
4040import Text.Pandoc.Builder (Inlines, Blocks, trimInlines, (<>))
4141import Text.Pandoc.Options
42−import Text.Pandoc.Shared hiding (compactify)
42+import Text.Pandoc.Shared
4343import Text.Pandoc.Parsing hiding (tableWith)
4444import Text.Pandoc.Readers.LaTeX ( rawLaTeXInline, rawLaTeXBlock )
4545import Text.Pandoc.Readers.HTML ( htmlTag, htmlInBalanced, isInlineTag, isBlockTag,
@@ -635,26 +635,12 @@ orderedList = try $ do
635635 skipNonindentSpaces
636636 orderedListMarker style delim )
637637 start' <- option 1 $ guardEnabled Ext_startnum >> return start
638− return $ B.orderedListWith (start', style, delim) <$> fmap compactify items
639−
640−-- | Change final list item from @Para@ to @Plain@ if the list contains
641−-- no other @Para@ blocks. (From Shared, modified for Blocks rather than [Block].)
642−compactify :: [Blocks] -- ^ List of list items (each a list of blocks)
643− -> [Blocks]
644−compactify [] = []
645−compactify items =
646− let (others, final) = (init items, last items)
647− in case reverse (B.toList final) of
648− (Para a:xs) -> case [Para x | Para x <- concatMap B.toList items] of
649− -- if this is only Para, change to Plain
650− [_] -> others ++ [B.fromList (reverse $ Plain a : xs)]
651− _ -> items
652− _ -> items
638+ return $ B.orderedListWith (start', style, delim) <$> fmap compactify' items
653639
654640bulletList :: Parser [Char] ParserState (F Blocks)
655641bulletList = do
656642 items <- fmap sequence $ many1 $ listItem bulletListStart
657− return $ B.bulletList <$> fmap compactify items
643+ return $ B.bulletList <$> fmap compactify' items
658644
659645-- definition lists
660646
@@ -699,10 +685,10 @@ defRawBlock = try $ do
699685definitionList :: Parser [Char] ParserState (F Blocks)
700686definitionList = do
701687 items <- fmap sequence $ many1 definitionListItem
702− return $ B.definitionList <$> fmap compactifyDL items
688+ return $ B.definitionList <$> fmap compactify'DL items
703689
704−compactifyDL :: [(Inlines, [Blocks])] -> [(Inlines, [Blocks])]
705−compactifyDL items =
690+compactify'DL :: [(Inlines, [Blocks])] -> [(Inlines, [Blocks])]
691+compactify'DL items =
706692 let defs = concatMap snd items
707693 defBlocks = reverse $ concatMap B.toList defs
708694 isPara (Para _) = True
@@ -1041,7 +1027,7 @@ gridTableRow indices = do
10411027 colLines <- many1 (gridTableRawLine indices)
10421028 let cols = map ((++ "\n") . unlines . removeOneLeadingSpace) $
10431029 transpose colLines
1044− fmap compactify <$> fmap sequence (mapM (parseFromString block) cols)
1030+ fmap compactify' <$> fmap sequence (mapM (parseFromString block) cols)
10451031
10461032removeOneLeadingSpace :: [String] -> [String]
10471033removeOneLeadingSpace xs =
src/Text/Pandoc/Shared.hs+18
@@ -54,6 +54,7 @@ module Text.Pandoc.Shared (
5454 normalize,
5555 stringify,
5656 compactify,
57+ compactify',
5758 Element (..),
5859 hierarchicalize,
5960 uniqueIdent,
@@ -74,6 +75,8 @@ module Text.Pandoc.Shared (
7475
7576import Text.Pandoc.Definition
7677import Text.Pandoc.Generic
78+import Text.Pandoc.Builder (Blocks)
79+import qualified Text.Pandoc.Builder as B
7780import qualified Text.Pandoc.UTF8 as UTF8
7881import System.Environment (getProgName)
7982import System.Exit (exitWith, ExitCode(..))
@@ -378,6 +381,21 @@ compactify items =
378381 _ -> items
379382 _ -> items
380383
384+-- | Change final list item from @Para@ to @Plain@ if the list contains
385+-- no other @Para@ blocks. Like compactify, but operates on @Blocks@ rather
386+-- than @[Block]@.
387+compactify' :: [Blocks] -- ^ List of list items (each a list of blocks)
388+ -> [Blocks]
389+compactify' [] = []
390+compactify' items =
391+ let (others, final) = (init items, last items)
392+ in case reverse (B.toList final) of
393+ (Para a:xs) -> case [Para x | Para x <- concatMap B.toList items] of
394+ -- if this is only Para, change to Plain
395+ [_] -> others ++ [B.fromList (reverse $ Plain a : xs)]
396+ _ -> items
397+ _ -> items
398+
381399isPara :: Block -> Bool
382400isPara (Para _) = True
383401isPara _ = False