module Text.Pandoc.Readers.Org ( readOrg ) where
import Text.Pandoc.Readers.Org.Blocks ( blockList, meta )
import Text.Pandoc.Readers.Org.Parsing ( OrgParser, readWithM )
import Text.Pandoc.Readers.Org.ParserState ( optionsToParserState )
import Text.Pandoc.Definition
import Text.Pandoc.Error
import Text.Pandoc.Options
import Control.Monad.Reader ( runReader )
readOrg :: ReaderOptions
-> String
-> Either PandocError Pandoc
readOrg opts s = flip runReader def $
readWithM parseOrg (optionsToParserState opts) (s ++ "\n\n")
parseOrg :: OrgParser Pandoc
parseOrg = do
blocks' <- blockList
meta' <- meta
return . Pandoc meta' $ removeUnwantedBlocks blocks'
where
removeUnwantedBlocks :: [Block] -> [Block]
removeUnwantedBlocks = dropCommentTrees . filter (/= Null)
dropCommentTrees :: [Block] -> [Block]
dropCommentTrees [] = []
dropCommentTrees (b:bs) =
maybe (b:dropCommentTrees bs)
(dropCommentTrees . flip dropUntilHeaderAboveLevel bs)
(commentHeaderLevel b)
commentHeaderLevel :: Block -> Maybe Int
commentHeaderLevel blk =
case blk of
(Header level _ ((Str "COMMENT"):_)) -> Just level
(Header level _ title) | hasNoExportTag title -> Just level
_ -> Nothing
where
hasNoExportTag :: [Inline] -> Bool
hasNoExportTag = any isNoExportTag
isNoExportTag :: Inline -> Bool
isNoExportTag (Span ("", ["tag"], [("data-tag-name", "noexport")]) []) = True
isNoExportTag _ = False
dropUntilHeaderAboveLevel :: Int -> [Block] -> [Block]
dropUntilHeaderAboveLevel n = dropWhile (not . isHeaderLevelLowerEq n)
isHeaderLevelLowerEq :: Int -> Block -> Bool
isHeaderLevelLowerEq n blk =
case blk of
(Header level _ _) -> n >= level
_ -> False