@@ -60,6 +60,9 @@ import Text.Blaze.Html.Renderer.Utf8 (renderHtml)
6060
6161data EPUBVersion = EPUB2 | EPUB3 deriving Eq
6262
63+-- TODO - make an option
64+chapterHeaderLevel = 1
65+
6366writeEPUB2, writeEPUB3 :: WriterOptions -- ^ Writer options
6467 -> Pandoc -- ^ Document to convert
6568 -> IO B.ByteString
@@ -136,19 +139,22 @@ writeEPUB version opts doc@(Pandoc meta _) = do
136139 let reftable = correlateRefs blocks'
137140 let blocks'' = replaceRefs reftable blocks'
138141
139− let addToChunk :: [[Block]] -> Block -> [[Block]]
140− addToChunk (c:cs) (Header 1 xs) = [Header 1 xs] : c : cs
141− addToChunk (c:cs) x = (c ++ [x]) : cs
142− addToChunk [] x = [[x]]
142+ let isChapterHeader (Header n _) = n <= chapterHeaderLevel
143+ isChapterHeader _ = False
144+
145+ let toChunks :: [Block] -> [[Block]]
146+ toChunks [] = []
147+ toChunks (b:bs) = (b:xs) : toChunks ys
148+ where (xs,ys) = break isChapterHeader bs
143149
144− let chunks = reverse $ foldl addToChunk [] blocks''
150+ let chunks = toChunks blocks''
145151
146152 let chapToEntry :: Int -> [Block] -> Entry
147153 chapToEntry num bs = mkEntry (showChapter num)
148154 $ renderHtml
149155 $ writeHtml opts'
150156 $ case bs of
151− (Header 1 xs : _) -> Pandoc (Meta xs [] []) bs
157+ (Header _ xs : _) -> Pandoc (Meta xs [] []) bs
152158 _ -> Pandoc (Meta [] [] []) bs
153159
154160 let chapterEntries = zipWith chapToEntry [1..] chunks
@@ -445,13 +451,13 @@ correlateRefs bs = identTable $ execState (mapM_ go bs)
445451 , identTable = [] }
446452 where go :: Block -> State IdentState ()
447453 go (Header n ils) = do
448− when (n == 1) $
454+ when (n <= chapterHeaderLevel) $
449455 modify $ \s -> s{ chapterNumber = chapterNumber s + 1
450456 , chapterIdents = [] }
451457 st <- get
452458 let runningid = uniqueIdent ils (runningIdents st)
453459 let chapterid = showChapter (chapterNumber st) ++
454− if n == 1
460+ if n <= chapterHeaderLevel
455461 then ""
456462 else '#' : uniqueIdent ils (chapterIdents st)
457463 modify $ \s -> s{ runningIdents = runningid : runningIdents st