aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2019-09-19 18:08:49 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2019-09-19 18:17:23 -0700
commit8408e584742ae5e3c6e3d752501992fe604ab7c5 (patch)
tree97164952af32bad0364117db386bd53f3eb1507a
parent5a85789185eac1e9753c1090c89e065f28be2392 (diff)
downloadpandoc-8408e584742ae5e3c6e3d752501992fe604ab7c5.tar.gz
EPUB writer: improve splitting into chapters.
+ Use makeSection from T.P.Shared. This deals better with embedded divs. (Closes #5761.) + Remove chapter-title class from chapter h1, for now. (Reverts one change made earlier; we may revisit this in light of #5749.) + Avoid issuing warning multiple times when title not set (see #5760).
-rw-r--r--src/Text/Pandoc/Shared.hs9
-rw-r--r--src/Text/Pandoc/Writers/EPUB.hs59
2 files changed, 29 insertions, 39 deletions
diff --git a/src/Text/Pandoc/Shared.hs b/src/Text/Pandoc/Shared.hs
index e169ccb82..7c8a2e2a8 100644
--- a/src/Text/Pandoc/Shared.hs
+++ b/src/Text/Pandoc/Shared.hs
@@ -510,9 +510,12 @@ makeSections numbering mbBaseLevel bs =
S.modify $ \(_, ln) -> (mbLevel, ln)
rest' <- go rest
let divattr = (ident, ["section"], [])
- let attr = ("",classes,kvs ++
- [("number", intercalate "." (map show newnum))
- | numbering])
+ let attr = ("",classes,
+ -- don't touch number if already present
+ case lookup "number" kvs of
+ Nothing | numbering ->
+ ("number", intercalate "." (map show newnum)) : kvs
+ _ -> kvs)
return $
Div divattr (Header level' attr title' : sectionContents') : rest'
go (Div (dident,dclasses,dkvs)
diff --git a/src/Text/Pandoc/Writers/EPUB.hs b/src/Text/Pandoc/Writers/EPUB.hs
index f4125318b..2ed8d5155 100644
--- a/src/Text/Pandoc/Writers/EPUB.hs
+++ b/src/Text/Pandoc/Writers/EPUB.hs
@@ -21,12 +21,13 @@ import Codec.Archive.Zip (Entry, addEntryToArchive, eRelativePath, emptyArchive,
import Control.Applicative ( (<|>) )
import Control.Monad (mplus, unless, when, zipWithM)
import Control.Monad.Except (catchError, throwError)
-import Control.Monad.State.Strict (State, StateT, evalState, evalStateT, get,
- gets, lift, modify, put)
+import Control.Monad.State.Strict (StateT, evalState, evalStateT, get,
+ gets, lift, modify)
import qualified Data.ByteString.Lazy as B
import qualified Data.ByteString.Lazy.Char8 as B8
import Data.Char (isAlphaNum, isAscii, isDigit, toLower)
import Data.List (isInfixOf, isPrefixOf)
+import Data.List.Split (splitWhen)
import qualified Data.Map as M
import Data.Maybe (fromMaybe, isNothing, mapMaybe, isJust)
import qualified Data.Set as Set
@@ -513,38 +514,24 @@ pandocToEPUB version opts doc = do
let chapterHeaderLevel = writerEpubChapterLevel opts
- let isChapterHeader (Header n _ _) = n <= chapterHeaderLevel
- isChapterHeader (Div ("refs",_,_) (Header n _ _:_)) =
- n <= chapterHeaderLevel
+ let isChapterHeader (Div _ (Header n _ _:_)) = n <= chapterHeaderLevel
isChapterHeader _ = False
- let toChapters :: [Block] -> State [Int] [Chapter]
- toChapters [] = return []
- toChapters (Div _ bs@(Header 1 _ _:_) : rest) =
- toChapters (bs ++ rest)
- toChapters (Header n (ident,classes,kvs) 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 (ident,"chapter-title":classes,kvs) ils : xs) :) <$>
- toChapters ys
- toChapters (b:bs) = do
- let (xs,ys) = break isChapterHeader bs
- (Chapter Nothing (b:xs) :) <$> toChapters ys
-
- let chapters' = evalState (toChapters blocks') []
+ let secsToChapters :: [Block] -> [Chapter]
+ secsToChapters [] = []
+ secsToChapters (d@(Div (_,"section":_,_) bs) : rest)
+ | isChapterHeader d =
+ Chapter mbnum [d] : secsToChapters rest
+ where mbnum = case bs of
+ (Header _ (_,_,kvs) _ : _) ->
+ map (fromMaybe 0 . safeRead) .
+ splitWhen (=='.') <$> lookup "number" kvs
+ _ -> Nothing
+ secsToChapters bs =
+ Chapter Nothing xs : secsToChapters ys
+ where (xs, ys) = break isChapterHeader bs
+
+ let chapters' = secsToChapters $ makeSections True Nothing blocks'
let extractLinkURL' :: Int -> Inline -> [(String, String)]
extractLinkURL' num (Span (ident, _, _) _)
@@ -576,11 +563,11 @@ pandocToEPUB version opts doc = do
Chapter mbnum $ walk fixInternalReferences bs)
chapters'
- let chapToEntry num (Chapter mbnum bs) =
+ let chapToEntry num (Chapter _ bs) =
mkEntry ("text/" ++ showChapter num) =<<
- writeHtml opts'{ writerNumberOffset = fromMaybe [] mbnum
- , writerVariables = ("body-type", bodyType) :
- cssvars True ++ vars } pdoc
+ writeHtml opts'{ writerVariables = ("body-type", bodyType) :
+ ("pagetitle", showChapter num) :
+ cssvars True ++ vars } pdoc
where (pdoc, bodyType) =
case bs of
(Header _ (_,_,kvs) xs : _) ->