git.iko.soy/typeable/pandoc
on master help sign in
Allow to overwrite top-level division type heuristics (#3258)

Pandoc uses heuristics to determine the most resonable top-level division type when emitting LaTeX or Docbook markup. It is now possible to overwrite this implicitly set top-level division via the `top-level-division` command line parameter. API change (`Text.Pandoc.Options`): the type of the `writerTopLevelDivision` field in of the `WriterOptions` data type is altered from `Division` to `Maybe Division`. The field's default value is changed from `Section` to `Nothing`. Closes: #3197

commitbaa25362a40fe905368f60222ebd8a533bcfe0f9 authorAlbert Krewinkel <albert+github@zeitkraut.de> dateSat 26 Nov 2016, 21:43 (10 years ago) parent2873cd8
8 files changed+26−20
pandoc.hs+4−4 src/Text/Pandoc/Options.hs+2−2 src/Text/Pandoc/Writers/ConTeXt.hs+6−5 src/Text/Pandoc/Writers/Docbook.hs+4−3 src/Text/Pandoc/Writers/LaTeX.hs+5−2 src/Text/Pandoc/Writers/TEI.hs+2−1 tests/Tests/Writers/Docbook.hs+1−1 tests/Tests/Writers/LaTeX.hs+2−2
pandoc.hs+4−4
@@ -181,7 +181,7 @@ data Opt = Opt
181181 , optHtmlQTags :: Bool -- ^ Use <q> tags in HTML
182182 , optHighlight :: Bool -- ^ Highlight source code
183183 , optHighlightStyle :: Style -- ^ Style to use for highlighted code
184− , optTopLevelDivision :: Division -- ^ Type of the top-level divisions
184+ , optTopLevelDivision :: Maybe Division -- ^ Type of the top-level divisions
185185 , optHTMLMathMethod :: HTMLMathMethod -- ^ Method to print HTML math
186186 , optReferenceODT :: Maybe FilePath -- ^ Path of reference.odt
187187 , optReferenceDocx :: Maybe FilePath -- ^ Path of reference.docx
@@ -246,7 +246,7 @@ defaultOpts = Opt
246246 , optHtmlQTags = False
247247 , optHighlight = True
248248 , optHighlightStyle = pygments
249− , optTopLevelDivision = Section
249+ , optTopLevelDivision = Nothing
250250 , optHTMLMathMethod = PlainMath
251251 , optReferenceODT = Nothing
252252 , optReferenceDocx = Nothing
@@ -598,13 +598,13 @@ options =
598598 (NoArg
599599 (\opt -> do warn $ "--chapters is deprecated. " ++
600600 "Use --top-level-division=chapter instead."
601− return opt { optTopLevelDivision = Chapter }))
601+ return opt { optTopLevelDivision = Just Chapter }))
602602 "" -- "Use chapter for top-level sections in LaTeX, DocBook"
603603
604604 , Option "" ["top-level-division"]
605605 (ReqArg
606606 (\arg opt -> case safeRead (uppercaseFirstLetter arg) of
607− Just dvsn -> return opt { optTopLevelDivision = dvsn }
607+ Just dvsn -> return opt { optTopLevelDivision = Just dvsn }
608608 _ -> err 76 "Top-level division must be section, chapter, or part")
609609 "[section|chapter|part]")
610610 "" -- "Use top-level division type in LaTeX, ConTeXt, DocBook"
src/Text/Pandoc/Options.hs+2−2
@@ -382,7 +382,7 @@ data WriterOptions = WriterOptions
382382 , writerHtmlQTags :: Bool -- ^ Use @<q>@ tags for quotes in HTML
383383 , writerBeamer :: Bool -- ^ Produce beamer LaTeX slide show
384384 , writerSlideLevel :: Maybe Int -- ^ Force header level of slides
385− , writerTopLevelDivision :: Division -- ^ Type of top-level divisions
385+ , writerTopLevelDivision :: Maybe Division -- ^ Type of top-level divisions
386386 , writerListings :: Bool -- ^ Use listings package for code
387387 , writerHighlight :: Bool -- ^ Highlight source code
388388 , writerHighlightStyle :: Style -- ^ Style to use for highlighting
@@ -430,7 +430,7 @@ instance Default WriterOptions where
430430 , writerHtmlQTags = False
431431 , writerBeamer = False
432432 , writerSlideLevel = Nothing
433− , writerTopLevelDivision = Section
433+ , writerTopLevelDivision = Nothing
434434 , writerListings = False
435435 , writerHighlight = False
436436 , writerHighlightStyle = pygments
src/Text/Pandoc/Writers/ConTeXt.hs+6−5
@@ -37,7 +37,7 @@ import Text.Pandoc.Walk (query)
3737import Text.Printf ( printf )
3838import Data.List ( intercalate, intersperse )
3939import Data.Char ( ord )
40−import Data.Maybe ( catMaybes )
40+import Data.Maybe ( catMaybes, fromMaybe )
4141import Control.Monad.State
4242import Text.Pandoc.Pretty
4343import Text.Pandoc.ImageSize
@@ -84,9 +84,10 @@ pandocToConTeXt options (Pandoc meta blocks) = do
8484 let context = defField "toc" (writerTableOfContents options)
8585 $ defField "placelist" (intercalate ("," :: String) $
8686 take (writerTOCDepth options +
87− if writerTopLevelDivision options < Section
88− then 0
89− else 1)
87+ case writerTopLevelDivision options of
88+ Just Part -> 0
89+ Just Chapter -> 0
90+ _ -> 1)
9091 ["chapter","section","subsection","subsubsection",
9192 "subsubsubsection","subsubsubsubsection"])
9293 $ defField "body" main
@@ -422,7 +423,7 @@ sectionHeader (ident,classes,_) hdrLevel lst = do
422423 contents <- inlineListToConTeXt lst
423424 st <- get
424425 let opts = stOptions st
425− let level' = case writerTopLevelDivision opts of
426+ let level' = case fromMaybe Section (writerTopLevelDivision opts) of
426427 Part -> hdrLevel - 2
427428 Chapter -> hdrLevel - 1
428429 Section -> hdrLevel
src/Text/Pandoc/Writers/Docbook.hs+4−3
@@ -39,6 +39,7 @@ import Text.Pandoc.Templates (renderTemplate')
3939import Text.Pandoc.Readers.TeXMath
4040import Data.List ( stripPrefix, isPrefixOf, intercalate, isSuffixOf )
4141import Data.Char ( toLower )
42+import Data.Maybe ( fromMaybe, isNothing )
4243import Data.Monoid ( Any(..) )
4344import Text.Pandoc.Highlighting ( languages, languagesByExtension )
4445import Text.Pandoc.Pretty
@@ -81,11 +82,11 @@ writeDocbook opts (Pandoc meta blocks) =
8182 else Nothing
8283 render' = render colwidth
8384 opts' = if ("/book>" `isSuffixOf` (trimr $ writerTemplate opts) &&
84− writerTopLevelDivision opts >= Section)
85− then opts{ writerTopLevelDivision = Chapter }
85+ isNothing (writerTopLevelDivision opts))
86+ then opts{ writerTopLevelDivision = Just Chapter }
8687 else opts
8788 -- The numbering here follows LaTeX's internal numbering
88− startLvl = case writerTopLevelDivision opts' of
89+ startLvl = case fromMaybe Section (writerTopLevelDivision opts') of
8990 Part -> -1
9091 Chapter -> 0
9192 Section -> 1
src/Text/Pandoc/Writers/LaTeX.hs+5−2
@@ -88,7 +88,10 @@ writeLaTeX options document =
8888 stTable = False, stStrikeout = False,
8989 stUrl = False, stGraphics = False,
9090 stLHS = False,
91− stBook = writerTopLevelDivision options < Section,
91+ stBook = (case writerTopLevelDivision options of
92+ Just Part -> True
93+ Just Chapter -> True
94+ _ -> False),
9295 stCsquotes = False, stHighlighting = False,
9396 stIncremental = writerIncremental options,
9497 stInternalLinks = [], stUsesEuro = False }
@@ -758,7 +761,7 @@ sectionHeader unnumbered ident level lst = do
758761 <> braces (text plain))
759762 book <- gets stBook
760763 opts <- gets stOptions
761− let topLevelDivision = min (if book then Chapter else Section)
764+ let topLevelDivision = fromMaybe (if book then Chapter else Section)
762765 (writerTopLevelDivision opts)
763766 let level' = if writerBeamer opts && topLevelDivision < Section
764767 -- beamer has parts but no chapters
src/Text/Pandoc/Writers/TEI.hs+2−1
@@ -36,6 +36,7 @@ import Text.Pandoc.Writers.Shared
3636import Text.Pandoc.Options
3737import Text.Pandoc.Templates (renderTemplate')
3838import Data.List ( stripPrefix, isPrefixOf )
39+import Data.Maybe ( fromMaybe )
3940import Data.Char ( toLower )
4041import Text.Pandoc.Highlighting ( languages, languagesByExtension )
4142import Text.Pandoc.Pretty
@@ -60,7 +61,7 @@ writeTEI opts (Pandoc meta blocks) =
6061 then Just $ writerColumns opts
6162 else Nothing
6263 render' = render colwidth
63− startLvl = case writerTopLevelDivision opts of
64+ startLvl = case fromMaybe Section (writerTopLevelDivision opts) of
6465 Part -> -1
6566 Chapter -> 0
6667 Section -> 1
tests/Tests/Writers/Docbook.hs+1−1
@@ -238,7 +238,7 @@ tests = [ testGroup "line blocks"
238238
239239 docbookTopLevelDiv :: (ToPandoc a) => Division -> a -> String
240240 docbookTopLevelDiv division =
241− docbookWithOpts def{ writerTopLevelDivision = division }
241+ docbookWithOpts def{ writerTopLevelDivision = Just division }
242242 in
243243 [ test (docbookTopLevelDiv Section) "sections as top-level" $ headers =?>
244244 unlines [ "<sect1>"
tests/Tests/Writers/LaTeX.hs+2−2
@@ -90,11 +90,11 @@ tests = [ testGroup "code blocks"
9090
9191 latexTopLevelDiv :: (ToPandoc a) => Division -> a -> String
9292 latexTopLevelDiv division =
93− latexWithOpts def{ writerTopLevelDivision = division }
93+ latexWithOpts def{ writerTopLevelDivision = Just division }
9494
9595 beamerTopLevelDiv :: (ToPandoc a) => Division -> a -> String
9696 beamerTopLevelDiv division =
97− latexWithOpts def { writerTopLevelDivision = division
97+ latexWithOpts def { writerTopLevelDivision = Just division
9898 , writerBeamer = True }
9999 in
100100 [ test (latexTopLevelDiv Section) "sections as top-level" $ headers =?>