From 30361308e7913eb82eb5e6f0cb7339153bd4ea32 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Fri, 4 Jan 2013 22:29:41 -0800 Subject: Added `--epub-chapter-level` and `--epub-toc-level` options. Also added writerEpubChapterLevel and writerEpubTOCLevel fields to WriterOptions. --- src/Text/Pandoc/Options.hs | 4 ++++ src/Text/Pandoc/Writers/EPUB.hs | 24 +++++++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/Text/Pandoc/Options.hs b/src/Text/Pandoc/Options.hs index 86b1f5b99..0424b434f 100644 --- a/src/Text/Pandoc/Options.hs +++ b/src/Text/Pandoc/Options.hs @@ -237,6 +237,8 @@ data WriterOptions = WriterOptions , writerTeXLigatures :: Bool -- ^ Use tex ligatures quotes, dashes in latex , writerEpubStylesheet :: Maybe String -- ^ EPUB stylesheet specified at command line , writerEpubFonts :: [FilePath] -- ^ Paths to fonts to embed + , writerEpubChapterLevel :: Int -- ^ Header level for chapters (separate files) + , writerEpubTOCLevel :: Int -- ^ Number of levels to include in TOC , writerReferenceODT :: Maybe FilePath -- ^ Path to reference ODT if specified , writerReferenceDocx :: Maybe FilePath -- ^ Ptah to reference DOCX if specified } deriving Show @@ -275,6 +277,8 @@ instance Default WriterOptions where , writerTeXLigatures = True , writerEpubStylesheet = Nothing , writerEpubFonts = [] + , writerEpubChapterLevel = 1 + , writerEpubTOCLevel = 3 , writerReferenceODT = Nothing , writerReferenceDocx = Nothing } diff --git a/src/Text/Pandoc/Writers/EPUB.hs b/src/Text/Pandoc/Writers/EPUB.hs index 5d3325ba9..024823b38 100644 --- a/src/Text/Pandoc/Writers/EPUB.hs +++ b/src/Text/Pandoc/Writers/EPUB.hs @@ -60,9 +60,6 @@ import Text.Blaze.Html.Renderer.Utf8 (renderHtml) data EPUBVersion = EPUB2 | EPUB3 deriving Eq --- TODO - make an option -chapterHeaderLevel = 1 - writeEPUB2, writeEPUB3 :: WriterOptions -- ^ Writer options -> Pandoc -- ^ Document to convert -> IO B.ByteString @@ -133,10 +130,12 @@ writeEPUB version opts doc@(Pandoc meta _) = do (Header 1 _ : _) -> blocks _ -> Header 1 (docTitle meta) : blocks + let chapterHeaderLevel = writerEpubChapterLevel opts + -- internal reference IDs change when we chunk the file, -- so that '#my-header-1' might turn into 'chap004.xhtml#my-header'. -- the next two lines fix that: - let reftable = correlateRefs blocks' + let reftable = correlateRefs chapterHeaderLevel blocks' let blocks'' = replaceRefs reftable blocks' let isChapterHeader (Header n _) = n <= chapterHeaderLevel @@ -230,6 +229,8 @@ writeEPUB version opts doc@(Pandoc meta _) = do -- toc.ncx let secs = hierarchicalize blocks'' + let tocLevel = writerEpubTOCLevel opts + let navPointNode :: (Int -> String -> String -> [Element] -> Element) -> Shared.Element -> State Int Element navPointNode formatter (Sec _ nums ident ils children) = do @@ -244,7 +245,7 @@ writeEPUB version opts doc@(Pandoc meta _) = do let src = case lookup ident reftable of Just x -> x Nothing -> error (ident ++ " not found in reftable") - let isSec (Sec lev _ _ _ _) = lev <= 3 -- only includes levels 1-3 + let isSec (Sec lev _ _ _ _) = lev <= tocLevel isSec _ = False let subsecs = filter isSec children subs <- mapM (navPointNode formatter) subsecs @@ -443,12 +444,13 @@ showChapter = printf "ch%03d.xhtml" -- that would be used in a normal pandoc document with -- new URLs to be used in the EPUB. For example, what -- was "header-1" might turn into "ch006.xhtml#header". -correlateRefs :: [Block] -> [(String,String)] -correlateRefs bs = identTable $ execState (mapM_ go bs) - IdentState{ chapterNumber = 0 - , runningIdents = [] - , chapterIdents = [] - , identTable = [] } +correlateRefs :: Int -> [Block] -> [(String,String)] +correlateRefs chapterHeaderLevel bs = + identTable $ execState (mapM_ go bs) + IdentState{ chapterNumber = 0 + , runningIdents = [] + , chapterIdents = [] + , identTable = [] } where go :: Block -> State IdentState () go (Header n ils) = do when (n <= chapterHeaderLevel) $ -- cgit v1.2.3