@@ -46,7 +46,6 @@ import qualified Text.Pandoc.Shared as Shared
46import Text.Pandoc.Options
46import Text.Pandoc.Options
47import Text.Pandoc.Definition
47import Text.Pandoc.Definition
48import Text.Pandoc.Generic
48import Text.Pandoc.Generic
49−import Text.Pandoc.Templates
50import Control.Monad.State
49import Control.Monad.State
51import Text.XML.Light hiding (ppTopElement)
50import Text.XML.Light hiding (ppTopElement)
52import Text.Pandoc.UUID
51import Text.Pandoc.UUID
@@ -57,7 +56,7 @@ import Network.URI ( unEscapeString )
57import Text.Pandoc.MIME (getMimeType)
56import Text.Pandoc.MIME (getMimeType)
58import Prelude hiding (catch)
57import Prelude hiding (catch)
59import Control.Exception (catch, SomeException)
58import Control.Exception (catch, SomeException)
60−import Text.HTML.TagSoup
59+import Text.Blaze.Html.Renderer.Utf8 (renderHtml)
62data EPUBVersion = EPUB2 | EPUB3 deriving Eq
61data EPUBVersion = EPUB2 | EPUB3 deriving Eq
@@ -79,6 +78,8 @@ writeEPUB version opts doc@(Pandoc meta _) = do
79 let opts' = opts{ writerEmailObfuscation = NoObfuscation
78 let opts' = opts{ writerEmailObfuscation = NoObfuscation
80 , writerStandalone = True
79 , writerStandalone = True
81 , writerSectionDivs = True
80 , writerSectionDivs = True
81+ , writerHtml5 = epub3
82+ , writerTableOfContents = False -- we always have one in epub
82 , writerHTMLMathMethod =
83 , writerHTMLMathMethod =
84 then MathML Nothing
85 then MathML Nothing
@@ -134,25 +135,34 @@ writeEPUB version opts doc@(Pandoc meta _) = do
134 fontEntries <- mapM mkFontEntry $ writerEpubFonts opts'
135 fontEntries <- mapM mkFontEntry $ writerEpubFonts opts'
136 -- body pages
137 -- body pages
137 -- add level 1 header to beginning if none there
139 -- add level 1 header to beginning if none there
138 let blocks' = case blocks of
140 let blocks' = case blocks of
139 (Header 1 _ : _) -> blocks
141 (Header 1 _ : _) -> blocks
140 _ -> Header 1 (docTitle meta) : blocks
142 _ -> Header 1 (docTitle meta) : blocks
141 -- internal reference IDs change when we chunk the file,
144 -- internal reference IDs change when we chunk the file,
145+ -- so that '#my-header-1' might turn into 'chap004.xhtml#my-header'.
142− -- so the next two lines fix that:
146+ -- the next two lines fix that:
143 let reftable = correlateRefs blocks'
147 let reftable = correlateRefs blocks'
144 let blocks'' = replaceRefs reftable blocks'
148 let blocks'' = replaceRefs reftable blocks'
145− let tags = parseTags $ writeHtmlString opts'{writerStandalone = False,
146− writerHtml5 = epub3}
147− $ Pandoc (Meta [] [] []) blocks''
149− let chunks = partitions (~== TagOpen "h1" []) tags
150+ let addToChunk :: [[Block]] -> Block -> [[Block]]
151+ addToChunk (c:cs) (Header 1 xs) = [Header 1 xs] : c : cs
152+ addToChunk (c:cs) x = (c ++ [x]) : cs
153+ addToChunk [] x = [[x]]
155+ let chunks = reverse $ foldl addToChunk [] blocks''
151− let chapToEntry :: Int -> [Tag String] -> Entry
157+ let chapToEntry :: Int -> [Block] -> Entry
152− chapToEntry num ts = mkEntry (showChapter num)
158+ chapToEntry num bs = mkEntry (showChapter num)
153− $ fromStringLazy
159+ $ renderHtml
154− $ renderTemplate (("body",renderTags ts):("pagetitle",show num):vars)
160+ $ writeHtml opts'{ writerStandalone = True
155− $ pageTemplate
161+ , writerTemplate = pageTemplate
164+ (Header 1 xs : _) -> Pandoc (Meta xs [] []) bs
165+ _ -> Pandoc (Meta [] [] []) bs
157 let chapterEntries = zipWith chapToEntry [1..] chunks
167 let chapterEntries = zipWith chapToEntry [1..] chunks
@@ -455,15 +465,14 @@ correlateRefs bs = identTable $ execState (mapM_ go bs)
455 , chapterIdents = [] }
465 , chapterIdents = [] }
456 st <- get
466 st <- get
457 let runningid = uniqueIdent ils (runningIdents st)
467 let runningid = uniqueIdent ils (runningIdents st)
468+ let chapterid = showChapter (chapterNumber st) ++
458− let chapid = if n == 1
469+ if n == 1
459− then Nothing
470+ then ""
460− else Just $ uniqueIdent ils (chapterIdents st)
471+ else '#' : uniqueIdent ils (chapterIdents st)
461 modify $ \s -> s{ runningIdents = runningid : runningIdents st
472 modify $ \s -> s{ runningIdents = runningid : runningIdents st
462− , chapterIdents = maybe (chapterIdents st)
463− (: chapterIdents st) chapid
473+ , chapterIdents = chapterid : chapterIdents st
464− , identTable = (runningid,
474+ , identTable = (runningid, chapterid) :
465− showChapter (chapterNumber st) ++
466− maybe "" ('#':) chapid) : identTable st
475+ identTable st
468 go _ = return ()
477 go _ = return ()