aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <fiddlosopher@gmail.com>2013-02-23 20:21:45 -0800
committerJohn MacFarlane <fiddlosopher@gmail.com>2013-02-23 20:21:45 -0800
commitdd48e06a4d59c39a418548e0d88a55691fa01d42 (patch)
treeeb26a3ec65b48dbf05fcdf19b8b5a9f48d2cef31
parentca6cb0450918d3981850b8dfb24b65e4ca0cc8a3 (diff)
downloadpandoc-dd48e06a4d59c39a418548e0d88a55691fa01d42.tar.gz
EPUB writer: Improved behavior with `--number-sections`.
* Headers with "unnumbered" attribute are not numbered. (Nor do they cause an increment in running numbering.) * Section numbers now work properly, even when there is material before the first numbered section.
-rw-r--r--src/Text/Pandoc/Writers/EPUB.hs31
1 files changed, 24 insertions, 7 deletions
diff --git a/src/Text/Pandoc/Writers/EPUB.hs b/src/Text/Pandoc/Writers/EPUB.hs
index 72262a884..cf6f19e8f 100644
--- a/src/Text/Pandoc/Writers/EPUB.hs
+++ b/src/Text/Pandoc/Writers/EPUB.hs
@@ -62,8 +62,8 @@ import Text.Blaze.Html.Renderer.Utf8 (renderHtml)
import Text.Blaze.Renderer.Utf8 (renderHtml)
#endif
--- A Chapter includes a list of blocks and maybe a display
--- number. Note, some chapters are unnumbered. The display
+-- A Chapter includes a list of blocks and maybe a section
+-- number offset. Note, some chapters are unnumbered. The section
-- number is different from the index number, which will be used
-- in filenames, chapter0003.xhtml.
data Chapter = Chapter (Maybe [Int]) [Block]
@@ -147,16 +147,33 @@ writeEPUB opts doc@(Pandoc meta _) = do
let toChapters :: [Block] -> State [Int] [Chapter]
toChapters [] = return []
- toChapters (b:bs) = (Chapter Nothing (b:xs) :) `fmap` toChapters ys
- where (xs,ys) = break isChapterHeader bs
-
- let chapters = evalState (toChapters blocks'') [0,0,0,0,0,0]
+ toChapters (Header n attr@(_,classes,_) ils : bs) = do
+ nums <- get
+ mbnum <- if "unnumbered" `elem` classes
+ then return Nothing
+ else case splitAt (n - 1) nums of
+ (ks, (m:_)) -> do
+ let nums' = ks ++ [m+1]
+ put nums'
+ return $ Just (ks ++ [m])
+ -- note, this is the offset not the sec number
+ (ks, []) -> do
+ let nums' = ks ++ [1]
+ put nums'
+ return $ Just ks
+ let (xs,ys) = break isChapterHeader bs
+ (Chapter mbnum (Header n attr ils : xs) :) `fmap` toChapters ys
+ toChapters (b:bs) = do
+ let (xs,ys) = break isChapterHeader bs
+ (Chapter Nothing (b:xs) :) `fmap` toChapters ys
+
+ let chapters = evalState (toChapters blocks'') []
let chapToEntry :: Int -> Chapter -> Entry
chapToEntry num (Chapter mbnum bs) = mkEntry (showChapter num)
$ renderHtml
$ writeHtml opts'{ writerNumberOffset =
- maybe [] (map (\x -> x - 1)) mbnum }
+ maybe [] id mbnum }
$ case bs of
(Header _ _ xs : _) -> Pandoc (Meta xs [] []) bs
_ -> Pandoc (Meta [] [] []) bs