EPUB writer: Fix section numbering.

Previously the numbering restarted from 1 in each chapter
(with `--number-sections`), though the numbers in the table
of contents were correct.

Note that this fix is a bit hackish and possibly fragile:
if the rendering of section numbers in HTML changes in the
future, it may break.  But it works, without needing
changes in other modules.
This commit is contained in:
John MacFarlane 2013-02-16 17:36:39 -08:00
parent 14b64ed46c
commit eca8c6043b

View file

@ -147,6 +147,7 @@ writeEPUB opts doc@(Pandoc meta _) = do
let chapToEntry :: Int -> [Block] -> Entry
chapToEntry num bs = mkEntry (showChapter num)
$ fixSectionNumbers num
$ renderHtml
$ writeHtml opts'
$ case bs of
@ -230,7 +231,7 @@ writeEPUB opts doc@(Pandoc meta _) = do
let navPointNode :: (Int -> String -> String -> [Element] -> Element)
-> Shared.Element -> State Int Element
navPointNode formatter (Sec _ nums (ident,classes,keyvals) ils children) = do
navPointNode formatter (Sec _ nums (ident,_,_) ils children) = do
n <- get
modify (+1)
let showNums :: [Int] -> String
@ -477,3 +478,9 @@ replaceRefs refTable = bottomUp replaceOneRef
Just url -> Link lab (url,tit)
Nothing -> x
replaceOneRef x = x
-- This is ugly and inefficient.
fixSectionNumbers :: Int -> B8.ByteString -> B8.ByteString
fixSectionNumbers num = B8.pack . go . B8.unpack
where go = substitute "<span class=\"header-section-number\">1"
("<span class=\"header-section-number\">" ++ show num)