@@ -62,8 +62,8 @@ import Text.Blaze.Html.Renderer.Utf8 (renderHtml)
6262import Text.Blaze.Renderer.Utf8 (renderHtml)
6363#endif
6464
65−-- A Chapter includes a list of blocks and maybe a display
66−-- number. Note, some chapters are unnumbered. The display
65+-- A Chapter includes a list of blocks and maybe a section
66+-- number offset. Note, some chapters are unnumbered. The section
6767-- number is different from the index number, which will be used
6868-- in filenames, chapter0003.xhtml.
6969data Chapter = Chapter (Maybe [Int]) [Block]
@@ -147,16 +147,33 @@ writeEPUB opts doc@(Pandoc meta _) = do
147147
148148 let toChapters :: [Block] -> State [Int] [Chapter]
149149 toChapters [] = return []
150− toChapters (b:bs) = (Chapter Nothing (b:xs) :) `fmap` toChapters ys
151− where (xs,ys) = break isChapterHeader bs
152−
153− let chapters = evalState (toChapters blocks'') [0,0,0,0,0,0]
150+ toChapters (Header n attr@(_,classes,_) ils : bs) = do
151+ nums <- get
152+ mbnum <- if "unnumbered" `elem` classes
153+ then return Nothing
154+ else case splitAt (n - 1) nums of
155+ (ks, (m:_)) -> do
156+ let nums' = ks ++ [m+1]
157+ put nums'
158+ return $ Just (ks ++ [m])
159+ -- note, this is the offset not the sec number
160+ (ks, []) -> do
161+ let nums' = ks ++ [1]
162+ put nums'
163+ return $ Just ks
164+ let (xs,ys) = break isChapterHeader bs
165+ (Chapter mbnum (Header n attr ils : xs) :) `fmap` toChapters ys
166+ toChapters (b:bs) = do
167+ let (xs,ys) = break isChapterHeader bs
168+ (Chapter Nothing (b:xs) :) `fmap` toChapters ys
169+
170+ let chapters = evalState (toChapters blocks'') []
154171
155172 let chapToEntry :: Int -> Chapter -> Entry
156173 chapToEntry num (Chapter mbnum bs) = mkEntry (showChapter num)
157174 $ renderHtml
158175 $ writeHtml opts'{ writerNumberOffset =
159− maybe [] (map (\x -> x - 1)) mbnum }
176+ maybe [] id mbnum }
160177 $ case bs of
161178 (Header _ _ xs : _) -> Pandoc (Meta xs [] []) bs
162179 _ -> Pandoc (Meta [] [] []) bs