aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <fiddlosopher@gmail.com>2012-11-04 18:03:11 -0800
committerJohn MacFarlane <fiddlosopher@gmail.com>2012-11-04 18:06:28 -0800
commitae6028d442df4ddc4ceb1cd7b01d7e0178d1d8e5 (patch)
treedb99dc7b23545931b2b1c236926badc5da97fec1
parentb69792e2ed73e4a572a3f2d75d46e02a7d190b5c (diff)
downloadpandoc-ae6028d442df4ddc4ceb1cd7b01d7e0178d1d8e5.tar.gz
EPUB writer: Better TOC and footnote handling.
* Here we go back to splitting the document at the Pandoc AST level, and converting each chapter to HTML separately. * Fixes bug in footnote processing introduced by commit 01d109e2efb880d9d2b8256d2e19ed4954076754. The bug caused all footnotes to appear in the last chapter, so that footnote links from other chapters would not work. Closes #651. * We also disable writerTableOfContents, since TOC is automatic for epub.
-rw-r--r--src/Text/Pandoc/Writers/EPUB.hs49
1 files changed, 29 insertions, 20 deletions
diff --git a/src/Text/Pandoc/Writers/EPUB.hs b/src/Text/Pandoc/Writers/EPUB.hs
index 13c5478e6..6631ef349 100644
--- a/src/Text/Pandoc/Writers/EPUB.hs
+++ b/src/Text/Pandoc/Writers/EPUB.hs
@@ -46,7 +46,6 @@ import qualified Text.Pandoc.Shared as Shared
import Text.Pandoc.Options
import Text.Pandoc.Definition
import Text.Pandoc.Generic
-import Text.Pandoc.Templates
import Control.Monad.State
import Text.XML.Light hiding (ppTopElement)
import Text.Pandoc.UUID
@@ -57,7 +56,7 @@ import Network.URI ( unEscapeString )
import Text.Pandoc.MIME (getMimeType)
import Prelude hiding (catch)
import Control.Exception (catch, SomeException)
-import Text.HTML.TagSoup
+import Text.Blaze.Html.Renderer.Utf8 (renderHtml)
data EPUBVersion = EPUB2 | EPUB3 deriving Eq
@@ -79,6 +78,8 @@ writeEPUB version opts doc@(Pandoc meta _) = do
let opts' = opts{ writerEmailObfuscation = NoObfuscation
, writerStandalone = True
, writerSectionDivs = True
+ , writerHtml5 = epub3
+ , writerTableOfContents = False -- we always have one in epub
, writerHTMLMathMethod =
if epub3
then MathML Nothing
@@ -134,25 +135,34 @@ writeEPUB version opts doc@(Pandoc meta _) = do
fontEntries <- mapM mkFontEntry $ writerEpubFonts opts'
-- body pages
+
-- add level 1 header to beginning if none there
let blocks' = case blocks of
(Header 1 _ : _) -> blocks
_ -> Header 1 (docTitle meta) : blocks
+
-- internal reference IDs change when we chunk the file,
- -- so the next two lines fix that:
+ -- so that '#my-header-1' might turn into 'chap004.xhtml#my-header'.
+ -- the next two lines fix that:
let reftable = correlateRefs blocks'
let blocks'' = replaceRefs reftable blocks'
- let tags = parseTags $ writeHtmlString opts'{writerStandalone = False,
- writerHtml5 = epub3}
- $ Pandoc (Meta [] [] []) blocks''
- let chunks = partitions (~== TagOpen "h1" []) tags
+ let addToChunk :: [[Block]] -> Block -> [[Block]]
+ addToChunk (c:cs) (Header 1 xs) = [Header 1 xs] : c : cs
+ addToChunk (c:cs) x = (c ++ [x]) : cs
+ addToChunk [] x = [[x]]
+
+ let chunks = reverse $ foldl addToChunk [] blocks''
- let chapToEntry :: Int -> [Tag String] -> Entry
- chapToEntry num ts = mkEntry (showChapter num)
- $ fromStringLazy
- $ renderTemplate (("body",renderTags ts):("pagetitle",show num):vars)
- $ pageTemplate
+ let chapToEntry :: Int -> [Block] -> Entry
+ chapToEntry num bs = mkEntry (showChapter num)
+ $ renderHtml
+ $ writeHtml opts'{ writerStandalone = True
+ , writerTemplate = pageTemplate
+ }
+ $ case bs of
+ (Header 1 xs : _) -> Pandoc (Meta xs [] []) bs
+ _ -> Pandoc (Meta [] [] []) bs
let chapterEntries = zipWith chapToEntry [1..] chunks
@@ -455,15 +465,14 @@ correlateRefs bs = identTable $ execState (mapM_ go bs)
, chapterIdents = [] }
st <- get
let runningid = uniqueIdent ils (runningIdents st)
- let chapid = if n == 1
- then Nothing
- else Just $ uniqueIdent ils (chapterIdents st)
+ let chapterid = showChapter (chapterNumber st) ++
+ if n == 1
+ then ""
+ else '#' : uniqueIdent ils (chapterIdents st)
modify $ \s -> s{ runningIdents = runningid : runningIdents st
- , chapterIdents = maybe (chapterIdents st)
- (: chapterIdents st) chapid
- , identTable = (runningid,
- showChapter (chapterNumber st) ++
- maybe "" ('#':) chapid) : identTable st
+ , chapterIdents = chapterid : chapterIdents st
+ , identTable = (runningid, chapterid) :
+ identTable st
}
go _ = return ()