@@ -27,7 +27,6 @@ import qualified Data.ByteString.Lazy as B
27import qualified Data.ByteString.Lazy.Char8 as B8
27import qualified Data.ByteString.Lazy.Char8 as B8
28import Data.Char (isAlphaNum, isAscii, isDigit, toLower)
28import Data.Char (isAlphaNum, isAscii, isDigit, toLower)
29import Data.List (isInfixOf, isPrefixOf)
29import Data.List (isInfixOf, isPrefixOf)
30−import Data.List.Split (splitWhen)
31import qualified Data.Map as M
30import qualified Data.Map as M
32import Data.Maybe (fromMaybe, isNothing, mapMaybe, isJust)
31import Data.Maybe (fromMaybe, isNothing, mapMaybe, isJust)
33import qualified Data.Set as Set
32import qualified Data.Set as Set
@@ -60,11 +59,8 @@ import Text.XML.Light (Attr (..), Element (..), Node (..), QName (..),
60 ppElement, showElement, strContent, unode, unqual)
59 ppElement, showElement, strContent, unode, unqual)
61import Text.Pandoc.XML (escapeStringForXML)
60import Text.Pandoc.XML (escapeStringForXML)
63−-- A Chapter includes a list of blocks and maybe a section
62+-- A Chapter includes a list of blocks.
64−-- number offset. Note, some chapters are unnumbered. The section
65−-- number is different from the index number, which will be used
66−-- in filenames, chapter0003.xhtml.
67−data Chapter = Chapter (Maybe [Int]) [Block]
63+data Chapter = Chapter [Block]
69data EPUBState = EPUBState {
65data EPUBState = EPUBState {
70 stMediaPaths :: [(FilePath, (FilePath, Maybe Entry))]
66 stMediaPaths :: [(FilePath, (FilePath, Maybe Entry))]
@@ -519,16 +515,16 @@ pandocToEPUB version opts doc = do
520 let secsToChapters :: [Block] -> [Chapter]
516 let secsToChapters :: [Block] -> [Chapter]
521 secsToChapters [] = []
517 secsToChapters [] = []
522− secsToChapters (d@(Div (_,"section":_,_) bs) : rest)
518+ secsToChapters (d@(Div attr@(_,"section":_,_)
523− | isChapterHeader d =
524− Chapter mbnum [d] : secsToChapters rest
525− where mbnum = case bs of
526− (Header _ (_,_,kvs) _ : _) ->
519+ (h@(Header lvl _ _) : bs)) : rest)
527− map (fromMaybe 0 . safeRead) .
520+ | chapterHeaderLevel == lvl =
528− splitWhen (=='.') <$> lookup "number" kvs
521+ Chapter [d] : secsToChapters rest
529− _ -> Nothing
522+ | chapterHeaderLevel > lvl =
523+ Chapter [Div attr (h:xs)] :
524+ secsToChapters ys ++ secsToChapters rest
525+ where (xs, ys) = break isChapterHeader bs
530 secsToChapters bs =
526 secsToChapters bs =
531− Chapter Nothing xs : secsToChapters ys
527+ Chapter xs : secsToChapters ys
532 where (xs, ys) = break isChapterHeader bs
528 where (xs, ys) = break isChapterHeader bs
534 let chapters' = secsToChapters $ makeSections True Nothing blocks'
530 let chapters' = secsToChapters $ makeSections True Nothing blocks'
@@ -545,7 +541,7 @@ pandocToEPUB version opts doc = do
545 | not (null ident) = [(ident, showChapter num ++ ('#':ident))]
541 | not (null ident) = [(ident, showChapter num ++ ('#':ident))]
546 extractLinkURL num b = query (extractLinkURL' num) b
542 extractLinkURL num b = query (extractLinkURL' num) b
548− let reftable = concat $ zipWith (\(Chapter _ bs) num ->
544+ let reftable = concat $ zipWith (\(Chapter bs) num ->
549 query (extractLinkURL num) bs)
545 query (extractLinkURL num) bs)
550 chapters' [1..]
546 chapters' [1..]
@@ -559,11 +555,11 @@ pandocToEPUB version opts doc = do
559 -- internal reference IDs change when we chunk the file,
555 -- internal reference IDs change when we chunk the file,
560 -- so that '#my-header-1' might turn into 'chap004.xhtml#my-header'.
556 -- so that '#my-header-1' might turn into 'chap004.xhtml#my-header'.
561 -- this fixes that:
557 -- this fixes that:
562− let chapters = map (\(Chapter mbnum bs) ->
558+ let chapters = map (\(Chapter bs) ->
563− Chapter mbnum $ walk fixInternalReferences bs)
559+ Chapter $ walk fixInternalReferences bs)
564 chapters'
560 chapters'
566− let chapToEntry num (Chapter _ bs) =
562+ let chapToEntry num (Chapter bs) =
567 mkEntry ("text/" ++ showChapter num) =<<
563 mkEntry ("text/" ++ showChapter num) =<<
568 writeHtml opts'{ writerVariables = ("body-type", bodyType) :
564 writeHtml opts'{ writerVariables = ("body-type", bodyType) :
569 ("pagetitle", showChapter num) :
565 ("pagetitle", showChapter num) :