Allow specification of epub-cover-image in YAML metadata.

This commit is contained in:
John MacFarlane 2013-11-30 15:17:38 -08:00
parent 2f4bf9dfa4
commit 96b678d823
2 changed files with 13 additions and 10 deletions

6
README
View file

@ -541,7 +541,9 @@ Options affecting specific writers
`--epub-cover-image=`*FILE* `--epub-cover-image=`*FILE*
: Use the specified image as the EPUB cover. It is recommended : Use the specified image as the EPUB cover. It is recommended
that the image be less than 1000px in width and height. that the image be less than 1000px in width and height. Note that
in a markdown source document you can also specify `epub-cover-image`
in a YAML metadata block (see [EPUB Metadata], below).
`--epub-metadata=`*FILE* `--epub-metadata=`*FILE*
: Look in the specified XML file for metadata for the EPUB. : Look in the specified XML file for metadata for the EPUB.
@ -2818,6 +2820,8 @@ The following fields are recognized:
~ A string value. ~ A string value.
`rights` `rights`
~ A string value. ~ A string value.
`epub-cover-image`
~ A string value (path to cover image).
Literate Haskell support Literate Haskell support
======================== ========================

View file

@ -279,8 +279,7 @@ metadataFromMeta opts meta = EPUBMetadata{
relation = metaValueToString <$> lookupMeta "relation" meta relation = metaValueToString <$> lookupMeta "relation" meta
coverage = metaValueToString <$> lookupMeta "coverage" meta coverage = metaValueToString <$> lookupMeta "coverage" meta
rights = metaValueToString <$> lookupMeta "rights" meta rights = metaValueToString <$> lookupMeta "rights" meta
coverImage = fmap (const "cover-image") $ coverImage = lookup "epub-cover-image" (writerVariables opts)
lookup "epub-cover-image" $ writerVariables opts
-- | Produce an EPUB file from a Pandoc document. -- | Produce an EPUB file from a Pandoc document.
writeEPUB :: WriterOptions -- ^ Writer options writeEPUB :: WriterOptions -- ^ Writer options
@ -305,11 +304,11 @@ writeEPUB opts doc@(Pandoc meta _) = do
then MathML Nothing then MathML Nothing
else writerHTMLMathMethod opts else writerHTMLMathMethod opts
, writerWrapText = False } , writerWrapText = False }
let mbCoverImage = lookup "epub-cover-image" vars metadata <- getEPUBMetadata opts' meta
-- cover page -- cover page
(cpgEntry, cpicEntry) <- (cpgEntry, cpicEntry) <-
case mbCoverImage of case epubCoverImage metadata of
Nothing -> return ([],[]) Nothing -> return ([],[])
Just img -> do Just img -> do
let coverImage = "cover-image" ++ takeExtension img let coverImage = "cover-image" ++ takeExtension img
@ -421,7 +420,6 @@ writeEPUB opts doc@(Pandoc meta _) = do
[("id", takeBaseName $ eRelativePath ent), [("id", takeBaseName $ eRelativePath ent),
("href", eRelativePath ent), ("href", eRelativePath ent),
("media-type", maybe "" id $ getMimeType $ eRelativePath ent)] $ () ("media-type", maybe "" id $ getMimeType $ eRelativePath ent)] $ ()
metadata <- getEPUBMetadata opts' meta
let plainTitle = case docTitle meta of let plainTitle = case docTitle meta of
[] -> case epubTitle metadata of [] -> case epubTitle metadata of
[] -> "UNTITLED" [] -> "UNTITLED"
@ -452,7 +450,7 @@ writeEPUB opts doc@(Pandoc meta _) = do
map pictureNode (cpicEntry ++ picEntries) ++ map pictureNode (cpicEntry ++ picEntries) ++
map fontNode fontEntries map fontNode fontEntries
, unode "spine" ! [("toc","ncx")] $ , unode "spine" ! [("toc","ncx")] $
case mbCoverImage of case epubCoverImage metadata of
Nothing -> [] Nothing -> []
Just _ -> [ unode "itemref" ! Just _ -> [ unode "itemref" !
[("idref", "cover"),("linear","no")] $ () ] [("idref", "cover"),("linear","no")] $ () ]
@ -471,7 +469,7 @@ writeEPUB opts doc@(Pandoc meta _) = do
("href","nav.xhtml")] $ () ("href","nav.xhtml")] $ ()
] ++ ] ++
[ unode "reference" ! [ unode "reference" !
[("type","cover"),("title","Cover"),("href","cover.xhtml")] $ () | mbCoverImage /= Nothing [("type","cover"),("title","Cover"),("href","cover.xhtml")] $ () | epubCoverImage metadata /= Nothing
] ]
] ]
let contentsEntry = mkEntry "content.opf" contentsData let contentsEntry = mkEntry "content.opf" contentsData
@ -526,7 +524,7 @@ writeEPUB opts doc@(Pandoc meta _) = do
,("content", "0")] $ () ,("content", "0")] $ ()
, unode "meta" ! [("name","dtb:maxPageNumber") , unode "meta" ! [("name","dtb:maxPageNumber")
,("content", "0")] $ () ,("content", "0")] $ ()
] ++ case mbCoverImage of ] ++ case epubCoverImage metadata of
Nothing -> [] Nothing -> []
Just _ -> [unode "meta" ! [("name","cover"), Just _ -> [unode "meta" ! [("name","cover"),
("content","cover-image")] $ ()] ("content","cover-image")] $ ()]
@ -623,7 +621,8 @@ metadataElement version md currentTime =
coverageNodes = maybe [] (dcTag' "coverage") $ epubCoverage md coverageNodes = maybe [] (dcTag' "coverage") $ epubCoverage md
rightsNodes = maybe [] (dcTag' "rights") $ epubRights md rightsNodes = maybe [] (dcTag' "rights") $ epubRights md
coverImageNodes = maybe [] coverImageNodes = maybe []
(\ci -> [unode "meta" ! [("name","cover"), ("content",ci)] $ ()]) (const $ [unode "meta" ! [("name","cover"),
("content","cover-image")] $ ()])
$ epubCoverImage md $ epubCoverImage md
modifiedNodes = [ unode "meta" ! [("property", "dcterms:modified")] $ modifiedNodes = [ unode "meta" ! [("property", "dcterms:modified")] $
(showDateTimeISO8601 currentTime) | version == EPUB3 ] (showDateTimeISO8601 currentTime) | version == EPUB3 ]