git.iko.soy/typeable/pandoc
on master help sign in
Added `--epub-chapter-level` and `--epub-toc-level` options.

Also added writerEpubChapterLevel and writerEpubTOCLevel fields to WriterOptions.

commit30361308e7913eb82eb5e6f0cb7339153bd4ea32 authorJohn MacFarlane <fiddlosopher@gmail.com> dateFri 4 Jan 2013, 22:29 (14 years ago) parent1d16349
4 files changed+61−11
README+14 pandoc.hs+30 src/Text/Pandoc/Options.hs+4 src/Text/Pandoc/Writers/EPUB.hs+13−11
README+14
@@ -512,6 +512,20 @@ Options affecting specific writers
512512 }
513513 body { font-family: "DejaVuSans"; }
514514
515+`--epub-chapter-level=`*NUMBER*
516+: Specify the header level at which to split the EPUB into separate
517+ "chapter" files. The default is to split into chapters at level 1
518+ headers. This option only affects the internal composition of the
519+ EPUB, not the way chapters and sections are displayed to users. Some
520+ readers may be slow if the chapter files are too large, so for large
521+ documents with few level 1 headers, one might want to use a chapter
522+ level of 2 or 3.
523+
524+`--epub-toc-level=`*NUMBER*
525+: Specify the number of section levels to include in an EPUB's table
526+ of contents. The default is 3 (which means that level 1, 2, and 3
527+ headers will be listed in the contents).
528+
515529`--latex-engine=`*pdflatex|lualatex|xelatex*
516530: Use the specified LaTeX engine when producing PDF output.
517531 The default is `pdflatex`. If the engine is not in your PATH,
pandoc.hs+30
@@ -114,6 +114,8 @@ data Opt = Opt
114114 , optEPUBStylesheet :: Maybe String -- ^ EPUB stylesheet
115115 , optEPUBMetadata :: String -- ^ EPUB metadata
116116 , optEPUBFonts :: [FilePath] -- ^ EPUB fonts to embed
117+ , optEPUBChapterLevel :: Int -- ^ Header level at which to split chapters
118+ , optEPUBTOCLevel :: Int -- ^ Number of levels to include in TOC
117119 , optDumpArgs :: Bool -- ^ Output command-line arguments
118120 , optIgnoreArgs :: Bool -- ^ Ignore command-line arguments
119121 , optReferenceLinks :: Bool -- ^ Use reference links in writing markdown, rst
@@ -166,6 +168,8 @@ defaultOpts = Opt
166168 , optEPUBStylesheet = Nothing
167169 , optEPUBMetadata = ""
168170 , optEPUBFonts = []
171+ , optEPUBChapterLevel = 1
172+ , optEPUBTOCLevel = 3
169173 , optDumpArgs = False
170174 , optIgnoreArgs = False
171175 , optReferenceLinks = False
@@ -554,6 +558,28 @@ options =
554558 "FILE")
555559 "" -- "Directory of fonts to embed"
556560
561+ , Option "" ["epub-chapter-level"]
562+ (ReqArg
563+ (\arg opt -> do
564+ case safeRead arg of
565+ Just t | t >= 1 && t <= 6 ->
566+ return opt { optEPUBChapterLevel = t }
567+ _ -> err 59 $
568+ "chapter level must be a number between 1 and 6")
569+ "NUMBER")
570+ "" -- "Header level at which to split chapters in EPUB"
571+
572+ , Option "" ["epub-toc-level"]
573+ (ReqArg
574+ (\arg opt -> do
575+ case safeRead arg of
576+ Just t | t >= 1 && t <= 6 ->
577+ return opt { optEPUBTOCLevel = t }
578+ _ -> err 57 $
579+ "TOC level must be a number between 1 and 6")
580+ "NUMBER")
581+ "" -- "Number of levels to include in EPUB TOC"
582+
557583 , Option "" ["latex-engine"]
558584 (ReqArg
559585 (\arg opt -> do
@@ -803,6 +829,8 @@ main = do
803829 , optEPUBStylesheet = epubStylesheet
804830 , optEPUBMetadata = epubMetadata
805831 , optEPUBFonts = epubFonts
832+ , optEPUBChapterLevel = epubChapterLevel
833+ , optEPUBTOCLevel = epubTOCLevel
806834 , optDumpArgs = dumpArgs
807835 , optIgnoreArgs = ignoreArgs
808836 , optReferenceLinks = referenceLinks
@@ -992,6 +1020,8 @@ main = do
9921020 writerTeXLigatures = texLigatures,
9931021 writerEpubStylesheet = epubStylesheet,
9941022 writerEpubFonts = epubFonts,
1023+ writerEpubChapterLevel = epubChapterLevel,
1024+ writerEpubTOCLevel = epubTOCLevel,
9951025 writerReferenceODT = referenceODT,
9961026 writerReferenceDocx = referenceDocx
9971027 }
src/Text/Pandoc/Options.hs+4
@@ -237,6 +237,8 @@ data WriterOptions = WriterOptions
237237 , writerTeXLigatures :: Bool -- ^ Use tex ligatures quotes, dashes in latex
238238 , writerEpubStylesheet :: Maybe String -- ^ EPUB stylesheet specified at command line
239239 , writerEpubFonts :: [FilePath] -- ^ Paths to fonts to embed
240+ , writerEpubChapterLevel :: Int -- ^ Header level for chapters (separate files)
241+ , writerEpubTOCLevel :: Int -- ^ Number of levels to include in TOC
240242 , writerReferenceODT :: Maybe FilePath -- ^ Path to reference ODT if specified
241243 , writerReferenceDocx :: Maybe FilePath -- ^ Ptah to reference DOCX if specified
242244 } deriving Show
@@ -275,6 +277,8 @@ instance Default WriterOptions where
275277 , writerTeXLigatures = True
276278 , writerEpubStylesheet = Nothing
277279 , writerEpubFonts = []
280+ , writerEpubChapterLevel = 1
281+ , writerEpubTOCLevel = 3
278282 , writerReferenceODT = Nothing
279283 , writerReferenceDocx = Nothing
280284 }
src/Text/Pandoc/Writers/EPUB.hs+13−11
@@ -60,9 +60,6 @@ import Text.Blaze.Html.Renderer.Utf8 (renderHtml)
6060
6161data EPUBVersion = EPUB2 | EPUB3 deriving Eq
6262
63−-- TODO - make an option
64−chapterHeaderLevel = 1
65−
6663writeEPUB2, writeEPUB3 :: WriterOptions -- ^ Writer options
6764 -> Pandoc -- ^ Document to convert
6865 -> IO B.ByteString
@@ -133,10 +130,12 @@ writeEPUB version opts doc@(Pandoc meta _) = do
133130 (Header 1 _ : _) -> blocks
134131 _ -> Header 1 (docTitle meta) : blocks
135132
133+ let chapterHeaderLevel = writerEpubChapterLevel opts
134+
136135 -- internal reference IDs change when we chunk the file,
137136 -- so that '#my-header-1' might turn into 'chap004.xhtml#my-header'.
138137 -- the next two lines fix that:
139− let reftable = correlateRefs blocks'
138+ let reftable = correlateRefs chapterHeaderLevel blocks'
140139 let blocks'' = replaceRefs reftable blocks'
141140
142141 let isChapterHeader (Header n _) = n <= chapterHeaderLevel
@@ -230,6 +229,8 @@ writeEPUB version opts doc@(Pandoc meta _) = do
230229 -- toc.ncx
231230 let secs = hierarchicalize blocks''
232231
232+ let tocLevel = writerEpubTOCLevel opts
233+
233234 let navPointNode :: (Int -> String -> String -> [Element] -> Element)
234235 -> Shared.Element -> State Int Element
235236 navPointNode formatter (Sec _ nums ident ils children) = do
@@ -244,7 +245,7 @@ writeEPUB version opts doc@(Pandoc meta _) = do
244245 let src = case lookup ident reftable of
245246 Just x -> x
246247 Nothing -> error (ident ++ " not found in reftable")
247− let isSec (Sec lev _ _ _ _) = lev <= 3 -- only includes levels 1-3
248+ let isSec (Sec lev _ _ _ _) = lev <= tocLevel
248249 isSec _ = False
249250 let subsecs = filter isSec children
250251 subs <- mapM (navPointNode formatter) subsecs
@@ -443,12 +444,13 @@ showChapter = printf "ch%03d.xhtml"
443444-- that would be used in a normal pandoc document with
444445-- new URLs to be used in the EPUB. For example, what
445446-- was "header-1" might turn into "ch006.xhtml#header".
446−correlateRefs :: [Block] -> [(String,String)]
447−correlateRefs bs = identTable $ execState (mapM_ go bs)
448− IdentState{ chapterNumber = 0
449− , runningIdents = []
450− , chapterIdents = []
451− , identTable = [] }
447+correlateRefs :: Int -> [Block] -> [(String,String)]
448+correlateRefs chapterHeaderLevel bs =
449+ identTable $ execState (mapM_ go bs)
450+ IdentState{ chapterNumber = 0
451+ , runningIdents = []
452+ , chapterIdents = []
453+ , identTable = [] }
452454 where go :: Block -> State IdentState ()
453455 go (Header n ils) = do
454456 when (n <= chapterHeaderLevel) $