git.iko.soy/typeable/pandoc
on master help sign in
Added --epub-cover-image option.

API change: Added a parameter for the cover image path to writeEPUB. Followed best practices outlined in http://blog.threepress.org/2009/11/20/best-practices-in-epub-cover-images/

commitf5cbb68534c52b292c57aaf741ab94442ddadd7a authorJohn MacFarlane <jgm@berkeley.edu> dateTue 8 Mar 2011, 23:25 (16 years ago) parent403bb52
3 files changed+59−10
README+4 src/Text/Pandoc/Writers/EPUB.hs+44−9 src/pandoc.hs+11−1
README+4
@@ -379,6 +379,10 @@ Options
379379 user data directory (see `--data-dir`, below). If it is not
380380 found there, sensible defaults will be used.
381381
382+`--epub-cover-image=`*FILE*
383+: Use the specified image as the EPUB cover. It is recommended
384+ that the image be less than 1000px in width and height.
385+
382386`--epub-metadata=`*FILE*
383387: Look in the specified XML file for metadata for the EPUB.
384388 The file should contain a series of Dublin Core elements,
src/Text/Pandoc/Writers/EPUB.hs+44−9
@@ -46,22 +46,40 @@ import Text.Pandoc.UUID
4646import Text.Pandoc.Writers.HTML
4747import Text.Pandoc.Writers.Markdown ( writePlain )
4848import Data.Char ( toLower )
49+import System.Directory ( copyFile )
4950
5051-- | Produce an EPUB file from a Pandoc document.
51−writeEPUB :: Maybe String -- ^ EPUB stylesheet specified at command line
52+writeEPUB :: Maybe FilePath -- ^ Path of cover image
53+ -> Maybe String -- ^ EPUB stylesheet specified at command line
5254 -> WriterOptions -- ^ Writer options
5355 -> Pandoc -- ^ Document to convert
5456 -> IO B.ByteString
55−writeEPUB mbStylesheet opts doc@(Pandoc meta _) = do
57+writeEPUB mbCoverImage mbStylesheet opts doc@(Pandoc meta _) = do
5658 (TOD epochtime _) <- getClockTime
5759 let mkEntry path content = toEntry path epochtime content
5860 let opts' = opts{ writerEmailObfuscation = NoObfuscation
5961 , writerStandalone = True
6062 , writerWrapText = False }
6163 let sourceDir = writerSourceDirectory opts'
64+ let vars = writerVariables opts'
65+
66+ -- cover page
67+ (cpgEntry, cpicEntry) <-
68+ case mbCoverImage of
69+ Nothing -> return ([],[])
70+ Just img -> do
71+ let coverImage = "cover-image" ++ takeExtension img
72+ copyFile img coverImage
73+ let cpContent = fromString $ writeHtmlString
74+ opts'{writerTemplate = pageTemplate
75+ ,writerVariables =
76+ ("coverimage",coverImage):vars}
77+ (Pandoc meta [])
78+ imgContent <- B.readFile img
79+ return ( [mkEntry "cover.xhtml" cpContent]
80+ , [mkEntry coverImage imgContent] )
6281
6382 -- title page
64− let vars = writerVariables opts'
6583 let tpContent = fromString $ writeHtmlString
6684 opts'{writerTemplate = pageTemplate
6785 ,writerVariables = ("titlepage","yes"):vars}
@@ -124,10 +142,14 @@ writeEPUB mbStylesheet opts doc@(Pandoc meta _) = do
124142 , unode "item" ! [("id","style"), ("href","stylesheet.css")
125143 ,("media-type","text/css")] $ ()
126144 ] ++
127− map chapterNode (tpEntry : chapterEntries) ++
128− map pictureNode picEntries
145+ map chapterNode (cpgEntry ++ (tpEntry : chapterEntries)) ++
146+ map pictureNode (cpicEntry ++ picEntries)
129147 , unode "spine" ! [("toc","ncx")] $
130− map chapterRefNode (tpEntry : chapterEntries)
148+ case mbCoverImage of
149+ Nothing -> []
150+ Just _ -> [ unode "itemref" !
151+ [("idref", "cover"),("linear","no")] $ () ]
152+ ++ map chapterRefNode (tpEntry : chapterEntries)
131153 ]
132154 let contentsEntry = mkEntry "content.opf" contentsData
133155
@@ -142,7 +164,7 @@ writeEPUB mbStylesheet opts doc@(Pandoc meta _) = do
142164 let tocData = fromString $ ppTopElement $
143165 unode "ncx" ! [("version","2005-1")
144166 ,("xmlns","http://www.daisy.org/z3986/2005/ncx/")] $
145− [ unode "head"
167+ [ unode "head" $
146168 [ unode "meta" ! [("name","dtb:uid")
147169 ,("content", show uuid)] $ ()
148170 , unode "meta" ! [("name","dtb:depth")
@@ -151,7 +173,10 @@ writeEPUB mbStylesheet opts doc@(Pandoc meta _) = do
151173 ,("content", "0")] $ ()
152174 , unode "meta" ! [("name","dtb:maxPageNumber")
153175 ,("content", "0")] $ ()
154− ]
176+ ] ++ case mbCoverImage of
177+ Nothing -> []
178+ Just _ -> [unode "meta" ! [("name","cover"),
179+ ("content","cover-image")] $ ()]
155180 , unode "docTitle" $ unode "text" $ plainTitle
156181 , unode "navMap" $ zipWith3 navPointNode (tpEntry : chapterEntries)
157182 [1..(length chapterEntries + 1)]
@@ -181,7 +206,8 @@ writeEPUB mbStylesheet opts doc@(Pandoc meta _) = do
181206 -- construct archive
182207 let archive = foldr addEntryToArchive emptyArchive
183208 (mimetypeEntry : containerEntry : stylesheetEntry : tpEntry :
184− contentsEntry : tocEntry : (picEntries ++ chapterEntries) )
209+ contentsEntry : tocEntry :
210+ (picEntries ++ cpicEntry ++ cpgEntry ++ chapterEntries) )
185211 return $ fromArchive archive
186212
187213metadataElement :: String -> UUID -> String -> String -> [String] -> Element
@@ -266,9 +292,17 @@ pageTemplate = unlines
266292 , "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
267293 , "<head>"
268294 , "<title>$title$</title>"
295+ , "$if(coverimage)$"
296+ , "<style type=\"text/css\">img{ max-width: 100%; }</style>"
297+ , "$endif$"
269298 , "<link href=\"stylesheet.css\" type=\"text/css\" rel=\"stylesheet\" />"
270299 , "</head>"
271300 , "<body>"
301+ , "$if(coverimage)$"
302+ , "<div id=\"cover-image\">"
303+ , "<img src=\"$coverimage$\" alt=\"$title$\" />"
304+ , "</div>"
305+ , "$else$"
272306 , "$if(titlepage)$"
273307 , "<h1 class=\"title\">$title$</h1>"
274308 , "$for(author)$"
@@ -279,6 +313,7 @@ pageTemplate = unlines
279313 , "$if(toc)$"
280314 , "$toc$"
281315 , "$endif$"
316+ , "$endif$"
282317 , "$body$"
283318 , "$endif$"
284319 , "</body>"
src/pandoc.hs+11−1
@@ -107,6 +107,7 @@ data Opt = Opt
107107 , optHTMLMathMethod :: HTMLMathMethod -- ^ Method to print HTML math
108108 , optReferenceODT :: Maybe FilePath -- ^ Path of reference.odt
109109 , optEPUBStylesheet :: Maybe String -- ^ EPUB stylesheet
110+ , optEPUBCoverImage :: Maybe FilePath -- ^ Path of epub cover image
110111 , optEPUBMetadata :: String -- ^ EPUB metadata
111112 , optDumpArgs :: Bool -- ^ Output command-line arguments
112113 , optIgnoreArgs :: Bool -- ^ Ignore command-line arguments
@@ -151,6 +152,7 @@ defaultOpts = Opt
151152 , optHTMLMathMethod = PlainMath
152153 , optReferenceODT = Nothing
153154 , optEPUBStylesheet = Nothing
155+ , optEPUBCoverImage = Nothing
154156 , optEPUBMetadata = ""
155157 , optDumpArgs = False
156158 , optIgnoreArgs = False
@@ -490,6 +492,13 @@ options =
490492 "FILENAME")
491493 "" -- "Path of epub.css"
492494
495+ , Option "" ["epub-cover-image"]
496+ (ReqArg
497+ (\arg opt ->
498+ return opt { optEPUBCoverImage = Just arg })
499+ "FILENAME")
500+ "" -- "Path of epub cover image"
501+
493502 , Option "" ["epub-metadata"]
494503 (ReqArg
495504 (\arg opt -> do
@@ -674,6 +683,7 @@ main = do
674683 , optHTMLMathMethod = mathMethod
675684 , optReferenceODT = referenceODT
676685 , optEPUBStylesheet = epubStylesheet
686+ , optEPUBCoverImage = epubCoverImage
677687 , optEPUBMetadata = epubMetadata
678688 , optDumpArgs = dumpArgs
679689 , optIgnoreArgs = ignoreArgs
@@ -858,7 +868,7 @@ main = do
858868
859869 case lookup writerName' writers of
860870 Nothing | writerName' == "epub" ->
861− writeEPUB epubStylesheet writerOptions doc2
871+ writeEPUB epubCoverImage epubStylesheet writerOptions doc2
862872 >>= B.writeFile (encodeString outputFile)
863873 Nothing | writerName' == "odt" ->
864874 writeODT referenceODT writerOptions doc2