git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #1702 from tarleb/org-comment-trees

Org reader: Drop COMMENT document trees

commita1b5997af499252cde722a6250657f21e960a50a authorJohn MacFarlane <jgm@berkeley.edu> dateSat 18 Oct 2014, 13:19 (12 years ago) parent8b60d43e3c36ed
2 files changed+38−1
src/Text/Pandoc/Readers/Org.hs+26−1 tests/Tests/Readers/Org.hs+12
src/Text/Pandoc/Readers/Org.hs+26−1
@@ -69,7 +69,32 @@ parseOrg = do
6969 blocks' <- parseBlocks
7070 st <- getState
7171 let meta = runF (orgStateMeta' st) st
72− return $ Pandoc meta $ filter (/= Null) (B.toList $ runF blocks' st)
72+ let removeUnwantedBlocks = dropCommentTrees . filter (/= Null)
73+ return $ Pandoc meta $ removeUnwantedBlocks (B.toList $ runF blocks' st)
74+
75+-- | Drop COMMENT headers and the document tree below those headers.
76+dropCommentTrees :: [Block] -> [Block]
77+dropCommentTrees [] = []
78+dropCommentTrees blks@(b:bs) =
79+ maybe blks (flip dropUntilHeaderAboveLevel bs) $ commentHeaderLevel b
80+
81+-- | Return the level of a header starting a comment tree and Nothing
82+-- otherwise.
83+commentHeaderLevel :: Block -> Maybe Int
84+commentHeaderLevel blk =
85+ case blk of
86+ (Header level _ ((Str "COMMENT"):_)) -> Just level
87+ _ -> Nothing
88+
89+-- | Drop blocks until a header on or above the given level is seen
90+dropUntilHeaderAboveLevel :: Int -> [Block] -> [Block]
91+dropUntilHeaderAboveLevel n = dropWhile (not . isHeaderLevelLowerEq n)
92+
93+isHeaderLevelLowerEq :: Int -> Block -> Bool
94+isHeaderLevelLowerEq n blk =
95+ case blk of
96+ (Header level _ _) -> n >= level
97+ _ -> False
7398
7499--
75100-- Parser State for Org
tests/Tests/Readers/Org.hs+12
@@ -450,6 +450,18 @@ tests =
450450 , header 2 ("walk" <> space <> "dog")
451451 ]
452452
453+ , "Comment Trees" =:
454+ unlines [ "* COMMENT A comment tree"
455+ , " Not much going on here"
456+ , "** This will be dropped"
457+ , "* Comment tree above"
458+ ] =?>
459+ header 1 "Comment tree above"
460+
461+ , "Nothing but a COMMENT header" =:
462+ "* COMMENT Test" =?>
463+ (mempty::Blocks)
464+
453465 , "Paragraph starting with an asterisk" =:
454466 "*five" =?>
455467 para "*five"