git.iko.soy/typeable/pandoc
on master help sign in
Merge pull request #1702 from tarleb/org-comment-trees
a1b5997John MacFarlane
src/Text/Pandoc/Readers/Org.hs+26−1
@@ -69,7 +69,32 @@ parseOrg = do
69 blocks' <- parseBlocks
69 blocks' <- parseBlocks
70 st <- getState
70 st <- getState
71 let meta = runF (orgStateMeta' st) st
71 let meta = runF (orgStateMeta' st) st
72+ let removeUnwantedBlocks = dropCommentTrees . filter (/= Null)
72− return $ Pandoc meta $ filter (/= Null) (B.toList $ runF blocks' st)
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
73
98
74--
99--
75-- Parser State for Org
100-- Parser State for Org
tests/Tests/Readers/Org.hs+12
@@ -450,6 +450,18 @@ tests =
450 , header 2 ("walk" <> space <> "dog")
450 , header 2 ("walk" <> space <> "dog")
451 ]
451 ]
452
452
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+
453 , "Paragraph starting with an asterisk" =:
465 , "Paragraph starting with an asterisk" =:
454 "*five" =?>
466 "*five" =?>
455 para "*five"
467 para "*five"