git.iko.soy/typeable/pandoc
on master help sign in
EPUB writer: Parameterize header level for splitting chapters.
0654c4cJohn MacFarlane
src/Text/Pandoc/Writers/EPUB.hs+14−8
@@ -60,6 +60,9 @@ import Text.Blaze.Html.Renderer.Utf8 (renderHtml)
60
60
61data EPUBVersion = EPUB2 | EPUB3 deriving Eq
61data EPUBVersion = EPUB2 | EPUB3 deriving Eq
62
62
63+-- TODO - make an option
64+chapterHeaderLevel = 1
65+
63writeEPUB2, writeEPUB3 :: WriterOptions -- ^ Writer options
66writeEPUB2, writeEPUB3 :: WriterOptions -- ^ Writer options
64 -> Pandoc -- ^ Document to convert
67 -> Pandoc -- ^ Document to convert
65 -> IO B.ByteString
68 -> IO B.ByteString
@@ -136,19 +139,22 @@ writeEPUB version opts doc@(Pandoc meta _) = do
136 let reftable = correlateRefs blocks'
139 let reftable = correlateRefs blocks'
137 let blocks'' = replaceRefs reftable blocks'
140 let blocks'' = replaceRefs reftable blocks'
138
141
142+ let isChapterHeader (Header n _) = n <= chapterHeaderLevel
143+ isChapterHeader _ = False
144+
139− let addToChunk :: [[Block]] -> Block -> [[Block]]
145+ let toChunks :: [Block] -> [[Block]]
140− addToChunk (c:cs) (Header 1 xs) = [Header 1 xs] : c : cs
146+ toChunks [] = []
141− addToChunk (c:cs) x = (c ++ [x]) : cs
147+ toChunks (b:bs) = (b:xs) : toChunks ys
142− addToChunk [] x = [[x]]
148+ where (xs,ys) = break isChapterHeader bs
143
149
144− let chunks = reverse $ foldl addToChunk [] blocks''
150+ let chunks = toChunks blocks''
145
151
146 let chapToEntry :: Int -> [Block] -> Entry
152 let chapToEntry :: Int -> [Block] -> Entry
147 chapToEntry num bs = mkEntry (showChapter num)
153 chapToEntry num bs = mkEntry (showChapter num)
148 $ renderHtml
154 $ renderHtml
149 $ writeHtml opts'
155 $ writeHtml opts'
150 $ case bs of
156 $ case bs of
151− (Header 1 xs : _) -> Pandoc (Meta xs [] []) bs
157+ (Header _ xs : _) -> Pandoc (Meta xs [] []) bs
152 _ -> Pandoc (Meta [] [] []) bs
158 _ -> Pandoc (Meta [] [] []) bs
153
159
154 let chapterEntries = zipWith chapToEntry [1..] chunks
160 let chapterEntries = zipWith chapToEntry [1..] chunks
@@ -445,13 +451,13 @@ correlateRefs bs = identTable $ execState (mapM_ go bs)
445 , identTable = [] }
451 , identTable = [] }
446 where go :: Block -> State IdentState ()
452 where go :: Block -> State IdentState ()
447 go (Header n ils) = do
453 go (Header n ils) = do
448− when (n == 1) $
454+ when (n <= chapterHeaderLevel) $
449 modify $ \s -> s{ chapterNumber = chapterNumber s + 1
455 modify $ \s -> s{ chapterNumber = chapterNumber s + 1
450 , chapterIdents = [] }
456 , chapterIdents = [] }
451 st <- get
457 st <- get
452 let runningid = uniqueIdent ils (runningIdents st)
458 let runningid = uniqueIdent ils (runningIdents st)
453 let chapterid = showChapter (chapterNumber st) ++
459 let chapterid = showChapter (chapterNumber st) ++
454− if n == 1
460+ if n <= chapterHeaderLevel
455 then ""
461 then ""
456 else '#' : uniqueIdent ils (chapterIdents st)
462 else '#' : uniqueIdent ils (chapterIdents st)
457 modify $ \s -> s{ runningIdents = runningid : runningIdents st
463 modify $ \s -> s{ runningIdents = runningid : runningIdents st