git.iko.soy/typeable/pandoc
on master help sign in
Org reader: support headline levels export setting
5ffa4abAlbert Krewinkel
src/Text/Pandoc/Readers/Org/Blocks.hs+27−6
@@ -111,7 +111,7 @@ headline lvl = try $ do
111 newline
111 newline
112 properties <- option mempty propertiesDrawer
112 properties <- option mempty propertiesDrawer
113 contents <- blocks
113 contents <- blocks
114− children <- many (headline (lvl + 1))
114+ children <- many (headline (level + 1))
115 return $ do
115 return $ do
116 title' <- title
116 title' <- title
117 contents' <- contents
117 contents' <- contents
@@ -135,12 +135,14 @@ headline lvl = try $ do
135
135
136-- | Convert an Org mode headline (i.e. a document tree) into pandoc's Blocks
136-- | Convert an Org mode headline (i.e. a document tree) into pandoc's Blocks
137headlineToBlocks :: Headline -> OrgParser Blocks
137headlineToBlocks :: Headline -> OrgParser Blocks
138−headlineToBlocks hdln@(Headline {..}) =
138+headlineToBlocks hdln@(Headline {..}) = do
139+ maxHeadlineLevels <- getExportSetting exportHeadlineLevels
139 case () of
140 case () of
140− _ | any isNoExportTag headlineTags -> return mempty
141+ _ | any isNoExportTag headlineTags -> return mempty
141− _ | any isArchiveTag headlineTags -> archivedHeadlineToBlocks hdln
142+ _ | any isArchiveTag headlineTags -> archivedHeadlineToBlocks hdln
142− _ | isCommentTitle headlineText -> return mempty
143+ _ | isCommentTitle headlineText -> return mempty
144+ _ | headlineLevel >= maxHeadlineLevels -> headlineToHeaderWithList hdln
143− _ -> headlineToHeaderWithContents hdln
145+ _ -> headlineToHeaderWithContents hdln
144
146
145isNoExportTag :: Tag -> Bool
147isNoExportTag :: Tag -> Bool
146isNoExportTag = (== toTag "noexport")
148isNoExportTag = (== toTag "noexport")
@@ -163,6 +165,25 @@ archivedHeadlineToBlocks hdln = do
163 ArchivedTreesExport -> headlineToHeaderWithContents hdln
165 ArchivedTreesExport -> headlineToHeaderWithContents hdln
164 ArchivedTreesHeadlineOnly -> headlineToHeader hdln
166 ArchivedTreesHeadlineOnly -> headlineToHeader hdln
165
167
168+headlineToHeaderWithList :: Headline -> OrgParser Blocks
169+headlineToHeaderWithList hdln@(Headline {..}) = do
170+ maxHeadlineLevels <- getExportSetting exportHeadlineLevels
171+ header <- headlineToHeader hdln
172+ listElements <- sequence (map headlineToBlocks headlineChildren)
173+ let listBlock = if null listElements
174+ then mempty
175+ else B.orderedList listElements
176+ let headerText = if maxHeadlineLevels == headlineLevel
177+ then header
178+ else flattenHeader header
179+ return $ headerText <> headlineContents <> listBlock
180+ where
181+ flattenHeader :: Blocks -> Blocks
182+ flattenHeader blks =
183+ case B.toList blks of
184+ (Header _ _ inlns:_) -> B.para (B.fromList inlns)
185+ _ -> mempty
186+
166headlineToHeaderWithContents :: Headline -> OrgParser Blocks
187headlineToHeaderWithContents :: Headline -> OrgParser Blocks
167headlineToHeaderWithContents hdln@(Headline {..}) = do
188headlineToHeaderWithContents hdln@(Headline {..}) = do
168 header <- headlineToHeader hdln
189 header <- headlineToHeader hdln
src/Text/Pandoc/Readers/Org/ExportSettings.hs+10−2
@@ -32,8 +32,9 @@ module Text.Pandoc.Readers.Org.ExportSettings
32import Text.Pandoc.Readers.Org.ParserState
32import Text.Pandoc.Readers.Org.ParserState
33import Text.Pandoc.Readers.Org.Parsing
33import Text.Pandoc.Readers.Org.Parsing
34
34
35−import Control.Monad ( void )
35+import Control.Monad ( mzero, void )
36import Data.Char ( toLower )
36import Data.Char ( toLower )
37+import Data.Maybe ( listToMaybe )
37
38
38-- | Read and handle space separated org-mode export settings.
39-- | Read and handle space separated org-mode export settings.
39exportSettings :: OrgParser ()
40exportSettings :: OrgParser ()
@@ -61,7 +62,7 @@ exportSetting = choice
61 , ignoredSetting "e"
62 , ignoredSetting "e"
62 , ignoredSetting "email"
63 , ignoredSetting "email"
63 , ignoredSetting "f"
64 , ignoredSetting "f"
64− , ignoredSetting "H"
65+ , integerSetting "H" (\val es -> es { exportHeadlineLevels = val })
65 , ignoredSetting "inline"
66 , ignoredSetting "inline"
66 , ignoredSetting "num"
67 , ignoredSetting "num"
67 , ignoredSetting "p"
68 , ignoredSetting "p"
@@ -94,6 +95,13 @@ genericExportSetting optionParser settingIdentifier setter = try $ do
94booleanSetting :: String -> ExportSettingSetter Bool -> OrgParser ()
95booleanSetting :: String -> ExportSettingSetter Bool -> OrgParser ()
95booleanSetting = genericExportSetting elispBoolean
96booleanSetting = genericExportSetting elispBoolean
96
97
98+-- | An integer-valued option.
99+integerSetting :: String -> ExportSettingSetter Int -> OrgParser ()
100+integerSetting = genericExportSetting parseInt
101+ where
102+ parseInt = try $
103+ many1 digit >>= maybe mzero (return . fst) . listToMaybe . reads
104+
97-- | Either the string "headline" or an elisp boolean and treated as an
105-- | Either the string "headline" or an elisp boolean and treated as an
98-- @ArchivedTreesOption@.
106-- @ArchivedTreesOption@.
99archivedTreeSetting :: String
107archivedTreeSetting :: String
src/Text/Pandoc/Readers/Org/ParserState.hs+3
@@ -158,6 +158,8 @@ data ExportSettings = ExportSettings
158 -- explicitly excluded from the resulting output while @Right@ means that
158 -- explicitly excluded from the resulting output while @Right@ means that
159 -- only the listed drawer names should be included.
159 -- only the listed drawer names should be included.
160 , exportEmphasizedText :: Bool -- ^ Parse emphasized text
160 , exportEmphasizedText :: Bool -- ^ Parse emphasized text
161+ , exportHeadlineLevels :: Int
162+ -- ^ Maximum depth of headlines, deeper headlines are convert to list
161 , exportSmartQuotes :: Bool -- ^ Parse quotes smartly
163 , exportSmartQuotes :: Bool -- ^ Parse quotes smartly
162 , exportSpecialStrings :: Bool -- ^ Parse ellipses and dashes smartly
164 , exportSpecialStrings :: Bool -- ^ Parse ellipses and dashes smartly
163 , exportSubSuperscripts :: Bool -- ^ TeX-like syntax for sub- and superscripts
165 , exportSubSuperscripts :: Bool -- ^ TeX-like syntax for sub- and superscripts
@@ -171,6 +173,7 @@ defaultExportSettings = ExportSettings
171 { exportArchivedTrees = ArchivedTreesHeadlineOnly
173 { exportArchivedTrees = ArchivedTreesHeadlineOnly
172 , exportDrawers = Left ["LOGBOOK"]
174 , exportDrawers = Left ["LOGBOOK"]
173 , exportEmphasizedText = True
175 , exportEmphasizedText = True
176+ , exportHeadlineLevels = 3
174 , exportSmartQuotes = True
177 , exportSmartQuotes = True
175 , exportSpecialStrings = True
178 , exportSpecialStrings = True
176 , exportSubSuperscripts = True
179 , exportSubSuperscripts = True
tests/Tests/Readers/Org.hs+12
@@ -611,6 +611,18 @@ tests =
611 ] =?>
611 ] =?>
612 let tagSpan t = spanWith ("", ["tag"], [("data-tag-name", t)]) mempty
612 let tagSpan t = spanWith ("", ["tag"], [("data-tag-name", t)]) mempty
613 in headerWith ("old", [], mempty) 1 ("old" <> tagSpan "ARCHIVE")
613 in headerWith ("old", [], mempty) 1 ("old" <> tagSpan "ARCHIVE")
614+
615+ , "Export option: limit headline depth" =:
616+ unlines [ "#+OPTIONS: H:2"
617+ , "* section"
618+ , "** subsection"
619+ , "*** list item 1"
620+ , "*** list item 2"
621+ ] =?>
622+ mconcat [ headerWith ("section", [], []) 1 "section"
623+ , headerWith ("subsection", [], []) 2 "subsection"
624+ , orderedList [ para "list item 1", para "list item 2" ]
625+ ]
614 ]
626 ]
615
627
616 , testGroup "Basic Blocks" $
628 , testGroup "Basic Blocks" $