From fba4f1f37eecb9b4b835b485a6ed92dd909cdcc0 Mon Sep 17 00:00:00 2001 From: John MacFarlane Date: Sun, 18 Dec 2011 12:33:44 -0800 Subject: EPUB: Added customizable templates for epub pages. epub-page.html, epub-coverimage.html, epub-titlepage.html. --- pandoc.cabal | 4 ++- src/Text/Pandoc/Writers/EPUB.hs | 59 +++++++++-------------------------------- templates | 2 +- 3 files changed, 17 insertions(+), 48 deletions(-) diff --git a/pandoc.cabal b/pandoc.cabal index 92e9f6f07..6c6251b63 100644 --- a/pandoc.cabal +++ b/pandoc.cabal @@ -45,7 +45,9 @@ Data-Files: templates/default.mediawiki, templates/default.rtf, templates/default.s5, templates/default.slidy, templates/default.dzslides, templates/default.asciidoc, - templates/default.textile, templates/default.org + templates/default.textile, templates/default.org, + templates/epub-titlepage.html, templates/epub-page.html, + templates/epub-coverimage.html, -- data for ODT writer reference.odt, -- stylesheet for EPUB writer diff --git a/src/Text/Pandoc/Writers/EPUB.hs b/src/Text/Pandoc/Writers/EPUB.hs index c3b0c95f2..55da4554d 100644 --- a/src/Text/Pandoc/Writers/EPUB.hs +++ b/src/Text/Pandoc/Writers/EPUB.hs @@ -32,7 +32,7 @@ import Data.IORef import Data.Maybe ( fromMaybe, isNothing ) import Data.List ( findIndices, isPrefixOf ) import System.Environment ( getEnv ) -import System.FilePath ( (), takeBaseName, takeExtension ) +import System.FilePath ( (), (<.>), takeBaseName, takeExtension ) import qualified Data.ByteString.Lazy as B import Data.ByteString.Lazy.UTF8 ( fromString ) import Codec.Archive.Zip @@ -63,6 +63,15 @@ writeEPUB mbStylesheet opts doc@(Pandoc meta _) = do let vars = writerVariables opts' let mbCoverImage = lookup "epub-cover-image" vars + titlePageTemplate <- readDataFile (writerUserDataDir opts) + $ "templates" "epub-titlepage" <.> "html" + + coverImageTemplate <- readDataFile (writerUserDataDir opts) + $ "templates" "epub-coverimage" <.> "html" + + pageTemplate <- readDataFile (writerUserDataDir opts) + $ "templates" "epub-page" <.> "html" + -- cover page (cpgEntry, cpicEntry) <- case mbCoverImage of @@ -70,9 +79,8 @@ writeEPUB mbStylesheet opts doc@(Pandoc meta _) = do Just img -> do let coverImage = "cover-image" ++ takeExtension img let cpContent = fromString $ writeHtmlString - opts'{writerTemplate = pageTemplate - ,writerVariables = - ("coverimage",coverImage):vars} + opts'{writerTemplate = coverImageTemplate, + writerVariables = ("coverimage",coverImage):vars} (Pandoc meta []) imgContent <- B.readFile img return ( [mkEntry "cover.xhtml" cpContent] @@ -80,8 +88,7 @@ writeEPUB mbStylesheet opts doc@(Pandoc meta _) = do -- title page let tpContent = fromString $ writeHtmlString - opts'{writerTemplate = pageTemplate - ,writerVariables = ("titlepage","yes"):vars} + opts'{writerTemplate = titlePageTemplate} (Pandoc meta []) let tpEntry = mkEntry "title_page.xhtml" tpContent @@ -298,43 +305,3 @@ imageTypeOf x = case drop 1 (map toLower (takeExtension x)) of "svg" -> Just "image/svg+xml" _ -> Nothing -pageTemplate :: String -pageTemplate = unlines - [ "" - , "" - , "" - , "" - , "$title$" - , "$if(coverimage)$" - , "" - , "$endif$" - , "" - , "" - , "" - , "$if(coverimage)$" - , "
" - , "\"$title$\"" - , "
" - , "$else$" - , "$if(titlepage)$" - , "

$title$

" - , "$for(author)$" - , "

$author$

" - , "$endfor$" - , "$if(date)$" - , "

$date$

" - , "$endif$" - , "$else$" - , "

$title$

" - , "$if(toc)$" - , "
" - , "$toc$" - , "
" - , "$endif$" - , "$endif$" - , "$body$" - , "$endif$" - , "" - , "" - ] - diff --git a/templates b/templates index e2e8191e9..15fb6a91b 160000 --- a/templates +++ b/templates @@ -1 +1 @@ -Subproject commit e2e8191e907d036b143478318e014bb8ab7318ef +Subproject commit 15fb6a91bb91b10f4e50e50bb2e76b0adac554bb -- cgit v1.2.3