git.iko.soy/typeable/pandoc
on master help sign in
Org reader: subject full doc tree to headline transformations

Emacs parses org documents into a tree structure, which is then post-processed during exporting. The reader is changed to do the same, turning the document into a single tree of headlines starting at level 0. Fixes: #3695

commitbf93c07267bf138f4f4cab7625ff273fa2ac67cd authorAlbert Krewinkel <albert@zeitkraut.de> dateSat 27 May 2017, 15:24 (9 years ago) parent8ec03cf
3 files changed+53−8
src/Text/Pandoc/Readers/Org/Blocks.hs+5−5 src/Text/Pandoc/Readers/Org/DocumentTree.hs+32−1 test/Tests/Readers/Org.hs+16−2
src/Text/Pandoc/Readers/Org/Blocks.hs+5−5
@@ -33,7 +33,7 @@ module Text.Pandoc.Readers.Org.Blocks
3333 ) where
3434
3535import Text.Pandoc.Readers.Org.BlockStarts
36−import Text.Pandoc.Readers.Org.DocumentTree (headline, headlineToBlocks)
36+import Text.Pandoc.Readers.Org.DocumentTree (documentTree, headlineToBlocks)
3737import Text.Pandoc.Readers.Org.Inlines
3838import Text.Pandoc.Readers.Org.Meta (metaExport, metaKey, metaLine)
3939import Text.Pandoc.Readers.Org.ParserState
@@ -62,11 +62,11 @@ import Data.Monoid ((<>))
6262-- | Get a list of blocks.
6363blockList :: PandocMonad m => OrgParser m [Block]
6464blockList = do
65− initialBlocks <- blocks
66− headlines <- sequence <$> manyTill (headline blocks inline 1) eof
65+ headlines <- documentTree blocks inline
6766 st <- getState
68− headlineBlocks <- fmap mconcat . mapM headlineToBlocks $ runF headlines st
69− return . B.toList $ (runF initialBlocks st) <> headlineBlocks
67+ headlineBlocks <- headlineToBlocks $ runF headlines st
68+ -- ignore first headline, it's the document's title
69+ return . drop 1 . B.toList $ headlineBlocks
7070
7171-- | Get the meta information saved in the state.
7272meta :: Monad m => OrgParser m Meta
src/Text/Pandoc/Readers/Org/DocumentTree.hs+32−1
@@ -28,7 +28,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2828Parsers for org-mode headlines and document subtrees
2929-}
3030module Text.Pandoc.Readers.Org.DocumentTree
31− ( headline
31+ ( documentTree
3232 , headlineToBlocks
3333 ) where
3434
@@ -43,11 +43,42 @@ import Text.Pandoc.Readers.Org.BlockStarts
4343import Text.Pandoc.Readers.Org.Parsing
4444import Text.Pandoc.Readers.Org.ParserState
4545
46+import qualified Data.Map as Map
4647import qualified Text.Pandoc.Builder as B
4748
4849--
4950-- Org headers
5051--
52+
53+-- | Parse input as org document tree.
54+documentTree :: PandocMonad m
55+ => OrgParser m (F Blocks)
56+ -> OrgParser m (F Inlines)
57+ -> OrgParser m (F Headline)
58+documentTree blocks inline = do
59+ initialBlocks <- blocks
60+ headlines <- sequence <$> manyTill (headline blocks inline 1) eof
61+ title <- fmap (getTitle . unMeta) . orgStateMeta <$> getState
62+ return $ do
63+ headlines' <- headlines
64+ initialBlocks' <- initialBlocks
65+ title' <- title
66+ return Headline
67+ { headlineLevel = 0
68+ , headlineTodoMarker = Nothing
69+ , headlineText = B.fromList title'
70+ , headlineTags = mempty
71+ , headlineProperties = mempty
72+ , headlineContents = initialBlocks'
73+ , headlineChildren = headlines'
74+ }
75+ where
76+ getTitle :: Map.Map String MetaValue -> [Inline]
77+ getTitle metamap =
78+ case Map.lookup "title" metamap of
79+ Just (MetaInlines inlns) -> inlns
80+ _ -> []
81+
5182newtype Tag = Tag { fromTag :: String }
5283 deriving (Show, Eq)
5384
test/Tests/Readers/Org.hs+16−2
@@ -708,16 +708,30 @@ tests =
708708
709709 , "limit headline depth" =:
710710 unlines [ "#+OPTIONS: H:2"
711− , "* section"
711+ , "* top-level section"
712712 , "** subsection"
713713 , "*** list item 1"
714714 , "*** list item 2"
715715 ] =?>
716− mconcat [ headerWith ("section", [], []) 1 "section"
716+ mconcat [ headerWith ("top-level-section", [], []) 1 "top-level section"
717717 , headerWith ("subsection", [], []) 2 "subsection"
718718 , orderedList [ para "list item 1", para "list item 2" ]
719719 ]
720720
721+ , "turn all headlines into lists" =:
722+ unlines [ "#+OPTIONS: H:0"
723+ , "first block"
724+ , "* top-level section 1"
725+ , "** subsection"
726+ , "* top-level section 2"
727+ ] =?>
728+ mconcat [ para "first block"
729+ , orderedList
730+ [ (para "top-level section 1" <>
731+ orderedList [ para "subsection" ])
732+ , para "top-level section 2" ]
733+ ]
734+
721735 , "disable author export" =:
722736 unlines [ "#+OPTIONS: author:nil"
723737 , "#+AUTHOR: ShyGuy"