git.iko.soy/typeable/pandoc
on master help sign in
Bug fixes to EPUB chapter splitting.
commit6d4b35dfaf7779cf3503f8cd152aaa992e180935 authorJohn MacFarlane <jgm@berkeley.edu> dateFri 20 Sep 2019, 17:20 (7 years ago) parenta69d52c
1 file changed+16−15
src/Text/Pandoc/Writers/EPUB.hs+16−15
src/Text/Pandoc/Writers/EPUB.hs+16−15
@@ -61,6 +61,7 @@ import Text.Pandoc.XML (escapeStringForXML)
6161
6262-- A Chapter includes a list of blocks.
6363data Chapter = Chapter [Block]
64+ deriving (Show)
6465
6566data EPUBState = EPUBState {
6667 stMediaPaths :: [(FilePath, (FilePath, Maybe Entry))]
@@ -501,13 +502,6 @@ pandocToEPUB version opts doc = do
501502
502503 -- body pages
503504
504− -- add level 1 header to beginning if none there
505− let blocks' = addIdentifiers opts
506− $ case blocks of
507− (Header 1 _ _ : _) -> blocks
508− _ -> Header 1 ("",["unnumbered"],[])
509− (docTitle' meta) : blocks
510−
511505 let chapterHeaderLevel = writerEpubChapterLevel opts
512506
513507 let isChapterHeader (Div _ (Header n _ _:_)) = n <= chapterHeaderLevel
@@ -515,19 +509,28 @@ pandocToEPUB version opts doc = do
515509
516510 let secsToChapters :: [Block] -> [Chapter]
517511 secsToChapters [] = []
518− secsToChapters (d@(Div attr@(_,"section":_,_)
519− (h@(Header lvl _ _) : bs)) : rest)
512+ secsToChapters (d@(Div attr (h@(Header lvl _ _) : bs)) : rest)
520513 | chapterHeaderLevel == lvl =
521− Chapter [d] : secsToChapters rest
514+ Chapter [d] : secsToChapters rest
522515 | chapterHeaderLevel > lvl =
523516 Chapter [Div attr (h:xs)] :
524517 secsToChapters ys ++ secsToChapters rest
525518 where (xs, ys) = break isChapterHeader bs
526519 secsToChapters bs =
527− Chapter xs : secsToChapters ys
520+ (if null xs then id else (Chapter xs :)) $ secsToChapters ys
528521 where (xs, ys) = break isChapterHeader bs
529522
530− let chapters' = secsToChapters $ makeSections True Nothing blocks'
523+ -- add level 1 header to beginning if none there
524+ let secs = makeSections True Nothing
525+ $ addIdentifiers opts
526+ $ case blocks of
527+ (Div _
528+ (Header{}:_) : _) -> blocks
529+ (Header 1 _ _ : _) -> blocks
530+ _ -> Header 1 ("",["unnumbered"],[])
531+ (docTitle' meta) : blocks
532+
533+ let chapters' = secsToChapters secs
531534
532535 let extractLinkURL' :: Int -> Inline -> [(String, String)]
533536 extractLinkURL' num (Span (ident, _, _) _)
@@ -696,14 +699,12 @@ pandocToEPUB version opts doc = do
696699 contentsEntry <- mkEntry "content.opf" contentsData
697700
698701 -- toc.ncx
699− let secs = makeSections True (Just 1) blocks'
700−
701702 let tocLevel = writerTOCDepth opts
702703
703704 let navPointNode :: PandocMonad m
704705 => (Int -> [Inline] -> String -> [Element] -> Element)
705706 -> Block -> StateT Int m [Element]
706− navPointNode formatter (Div (ident,"section":_,_)
707+ navPointNode formatter (Div (ident,_,_)
707708 (Header lvl (_,_,kvs) ils : children)) = do
708709 if lvl > tocLevel
709710 then return []