git.iko.soy/typeable/pandoc
on master help sign in
Move `compactify'DL` from Markdown reader into Shared

The function `compactify'DL`, used to change the final definition item of a definition list into a `Plain` iff all other items are `Plain`s as well, is useful in many parsers and hence moved into Text.Pandoc.Shared.

commitefebade38b3aef30b5fbf04e60f1fc2bd76e6c6c authorAlbert Krewinkel <tarleb@moltkeplatz.de> dateSat 19 Apr 2014, 14:48 (13 years ago) parent8e91d36
2 files changed+17−20
src/Text/Pandoc/Readers/Markdown.hs−17 src/Text/Pandoc/Shared.hs+17−3
src/Text/Pandoc/Readers/Markdown.hs−17
@@ -861,22 +861,6 @@ definitionList = do
861861 items <- fmap sequence $ many1 definitionListItem
862862 return $ B.definitionList <$> fmap compactify'DL items
863863
864−compactify'DL :: [(Inlines, [Blocks])] -> [(Inlines, [Blocks])]
865−compactify'DL items =
866− let defs = concatMap snd items
867− defBlocks = reverse $ concatMap B.toList defs
868− isPara (Para _) = True
869− isPara _ = False
870− in case defBlocks of
871− (Para x:_) -> if not $ any isPara (drop 1 defBlocks)
872− then let (t,ds) = last items
873− lastDef = B.toList $ last ds
874− ds' = init ds ++
875− [B.fromList $ init lastDef ++ [Plain x]]
876− in init items ++ [(t, ds')]
877− else items
878− _ -> items
879−
880864--
881865-- paragraph block
882866--
@@ -1892,4 +1876,3 @@ doubleQuoted = try $ do
18921876 (withQuoteContext InDoubleQuote $ doubleQuoteEnd >> return
18931877 (fmap B.doubleQuoted . trimInlinesF $ contents))
18941878 <|> (return $ return (B.str "\8220") <> contents)
1895−
src/Text/Pandoc/Shared.hs+17−3
@@ -56,6 +56,7 @@ module Text.Pandoc.Shared (
5656 stringify,
5757 compactify,
5858 compactify',
59+ compactify'DL,
5960 Element (..),
6061 hierarchicalize,
6162 uniqueIdent,
@@ -82,7 +83,7 @@ module Text.Pandoc.Shared (
8283import Text.Pandoc.Definition
8384import Text.Pandoc.Walk
8485import Text.Pandoc.Generic
85−import Text.Pandoc.Builder (Blocks, ToMetaValue(..))
86+import Text.Pandoc.Builder (Inlines, Blocks, ToMetaValue(..))
8687import qualified Text.Pandoc.Builder as B
8788import qualified Text.Pandoc.UTF8 as UTF8
8889import System.Environment (getProgName)
@@ -435,6 +436,21 @@ compactify' items =
435436 _ -> items
436437 _ -> items
437438
439+-- | Like @compactify'@, but akts on items of definition lists.
440+compactify'DL :: [(Inlines, [Blocks])] -> [(Inlines, [Blocks])]
441+compactify'DL items =
442+ let defs = concatMap snd items
443+ defBlocks = reverse $ concatMap B.toList defs
444+ in case defBlocks of
445+ (Para x:_) -> if not $ any isPara (drop 1 defBlocks)
446+ then let (t,ds) = last items
447+ lastDef = B.toList $ last ds
448+ ds' = init ds ++
449+ [B.fromList $ init lastDef ++ [Plain x]]
450+ in init items ++ [(t, ds')]
451+ else items
452+ _ -> items
453+
438454isPara :: Block -> Bool
439455isPara (Para _) = True
440456isPara _ = False
@@ -698,5 +714,3 @@ safeRead s = case reads s of
698714 (d,x):_
699715 | all isSpace x -> return d
700716 _ -> fail $ "Could not read `" ++ s ++ "'"
701−
702−