From fba4f1f37eecb9b4b835b485a6ed92dd909cdcc0 Mon Sep 17 00:00:00 2001
From: John MacFarlane <fiddlosopher@gmail.com>
Date: Sun, 18 Dec 2011 12:33:44 -0800
Subject: [PATCH] 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
- [ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
- , "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">"
- , "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
- , "<head>"
- , "<title>$title$</title>"
- , "$if(coverimage)$"
- , "<style type=\"text/css\">img{ max-width: 100%; }</style>"
- , "$endif$"
- , "<link href=\"stylesheet.css\" type=\"text/css\" rel=\"stylesheet\" />"
- , "</head>"
- , "<body>"
- , "$if(coverimage)$"
- , "<div id=\"cover-image\">"
- , "<img src=\"$coverimage$\" alt=\"$title$\" />"
- , "</div>"
- , "$else$"
- , "$if(titlepage)$"
- , "<h1 class=\"title\">$title$</h1>"
- , "$for(author)$"
- , "<h2 class=\"author\">$author$</h2>"
- , "$endfor$"
- , "$if(date)$"
- , "<h3 class=\"date\">$date$</h3>"
- , "$endif$"
- , "$else$"
- , "<h1>$title$</h1>"
- , "$if(toc)$"
- , "<div id=\"$idprefix$TOC\">"
- , "$toc$"
- , "</div>"
- , "$endif$"
- , "$endif$"
- , "$body$"
- , "$endif$"
- , "</body>"
- , "</html>"
- ]
-
diff --git a/templates b/templates
index e2e8191e9..15fb6a91b 160000
--- a/templates
+++ b/templates
@@ -1 +1 @@
-Subproject commit e2e8191e907d036b143478318e014bb8ab7318ef
+Subproject commit 15fb6a91bb91b10f4e50e50bb2e76b0adac554bb