From 76c5ebf14118a70debdb083d26d158f37be1a831 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sun, 3 Mar 2013 08:42:45 -0800 Subject: EPUB writer: Include html TOC even in epub2. * The TOC is included in ``, but `linear` is set to `no` unless the `--toc` option is specified. * Include `` element in OPF. * This should allow the TOC to be useable in Kindles when converted with kindlegen. * Results validate with epubcheck 3.0 for both epub and epub3 output. * Closes #773. --- src/Text/Pandoc/Writers/EPUB.hs | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) (limited to 'src/Text') diff --git a/src/Text/Pandoc/Writers/EPUB.hs b/src/Text/Pandoc/Writers/EPUB.hs index cf6f19e8f..c7b9d6ae7 100644 --- a/src/Text/Pandoc/Writers/EPUB.hs +++ b/src/Text/Pandoc/Writers/EPUB.hs @@ -229,12 +229,10 @@ writeEPUB opts doc@(Pandoc meta _) = do ,("media-type","application/x-dtbncx+xml")] $ () , unode "item" ! [("id","style"), ("href","stylesheet.css") ,("media-type","text/css")] $ () - ] ++ - [ unode "item" ! [("id","nav") - ,("href","nav.xhtml") - ,("properties","nav") - ,("media-type","application/xhtml+xml")] $ () - | version == EPUB3 + , unode "item" ! ([("id","nav") + ,("href","nav.xhtml") + ,("media-type","application/xhtml+xml")] ++ + [("properties","nav") | epub3 ]) $ () ] ++ map chapterNode (cpgEntry ++ (tpEntry : chapterEntries)) ++ map pictureNode (cpicEntry ++ picEntries) ++ @@ -244,7 +242,15 @@ writeEPUB opts doc@(Pandoc meta _) = do Nothing -> [] Just _ -> [ unode "itemref" ! [("idref", "cover"),("linear","no")] $ () ] - ++ map chapterRefNode (tpEntry : chapterEntries) + ++ ((unode "itemref" ! [("idref", "title_page"),("linear","yes")] $ ()) : + (unode "itemref" ! [("idref", "nav") + ,("linear", if writerTableOfContents opts + then "yes" + else "no")] $ ()) : + map chapterRefNode chapterEntries) + , unode "guide" $ + unode "reference" ! + [("type","toc"),("title",plainTitle),("href","nav.xhtml")] $ () ] let contentsEntry = mkEntry "content.opf" contentsData @@ -315,16 +321,19 @@ writeEPUB opts doc@(Pandoc meta _) = do $ (unode "span" tit)) : case subs of [] -> [] - (_:_) -> [unode "ol" subs] + (_:_) -> [unode "ol" ! [("class","toc")] $ subs] + let navtag = if epub3 then "nav" else "div" let navData = fromStringLazy $ ppTopElement $ unode "html" ! [("xmlns","http://www.w3.org/1999/xhtml") ,("xmlns:epub","http://www.idpf.org/2007/ops")] $ - [ unode "head" $ unode "title" plainTitle + [ unode "head" $ + [ unode "title" plainTitle + , unode "link" ! [("rel","stylesheet"),("type","text/css"),("href","stylesheet.css")] $ () ] , unode "body" $ - unode "nav" ! [("epub:type","toc")] $ + unode navtag ! [("epub:type","toc") | epub3] $ [ unode "h1" plainTitle - , unode "ol" $ evalState (mapM (navPointNode navXhtmlFormatter) secs) 1] + , unode "ol" ! [("class","toc")] $ evalState (mapM (navPointNode navXhtmlFormatter) secs) 1] ] let navEntry = mkEntry "nav.xhtml" navData @@ -357,9 +366,8 @@ writeEPUB opts doc@(Pandoc meta _) = do -- construct archive let archive = foldr addEntryToArchive emptyArchive (mimetypeEntry : containerEntry : appleEntry : stylesheetEntry : tpEntry : - contentsEntry : tocEntry : - ([navEntry | version == EPUB3] ++ picEntries ++ cpicEntry ++ cpgEntry ++ - chapterEntries ++ fontEntries) ) + contentsEntry : tocEntry : navEntry : + (picEntries ++ cpicEntry ++ cpgEntry ++ chapterEntries ++ fontEntries)) return $ fromArchive archive metadataElement :: EPUBVersion -> String -> UUID -> String -> String -> [String] @@ -385,7 +393,7 @@ metadataElement version metadataXML uuid lang title authors date currentTime mbC $ a | a <- authors ] ++ [ unode "dc:date" date | not (elt `contains` "date") ] ++ [ unode "meta" ! [("property", "dcterms:modified")] $ - (showDateTimeISO8601 currentTime) | version == EPUB3 ] ++ + (showDateTimeISO8601 currentTime) | version == EPUB3] ++ [ unode "meta" ! [("name","cover"), ("content","cover-image")] $ () | not (isNothing mbCoverImage) ] in elt{ elContent = elContent elt ++ map Elem newNodes } -- cgit v1.2.3