diff options
author | John MacFarlane <jgm@berkeley.edu> | 2019-07-13 15:20:14 -0700 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2019-07-13 15:20:14 -0700 |
commit | 5f495eaace3ce4cf15a89b6b3cfb54df6d1658d8 (patch) | |
tree | 596e8f154d4be90a363fa42f0398e7c1bb5ce77f | |
parent | 6cf5c3f6ac267c93fe7557add482307d494a8c75 (diff) | |
download | pandoc-5f495eaace3ce4cf15a89b6b3cfb54df6d1658d8.tar.gz |
EPUB writer: Use svg tag wrapper for cover image.
In addition, the code generating the image has been moved
to the template, to make it more customizable.
Those who use custom EPUB templates will need to adjust their
templates, adding the code to generate the cover image.
(Previously this was just inserted into 'body'.)
Closes #5643.
-rw-r--r-- | data/templates/default.epub2 | 8 | ||||
-rw-r--r-- | data/templates/default.epub3 | 8 | ||||
-rw-r--r-- | src/Text/Pandoc/Writers/EPUB.hs | 14 |
3 files changed, 28 insertions, 2 deletions
diff --git a/data/templates/default.epub2 b/data/templates/default.epub2 index afcf96a3e..f440134df 100644 --- a/data/templates/default.epub2 +++ b/data/templates/default.epub2 @@ -46,6 +46,13 @@ $if(rights)$ <div class="rights">$rights$</div> $endif$ $else$ +$if(coverpage)$ +<div id="cover-image"> +<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="100%" height="100%" viewBox="0 0 $cover-image-width$ $cover-image-height$" preserveAspectRatio="none"> +<image width="$cover-image-width$" height="$cover-image-height$" xlink:href="../media/$cover-image$" /> +</svg> +</div> +$else$ $for(include-before)$ $include-before$ $endfor$ @@ -54,6 +61,7 @@ $for(include-after)$ $include-after$ $endfor$ $endif$ +$endif$ </body> </html> diff --git a/data/templates/default.epub3 b/data/templates/default.epub3 index f0feb147a..4f5bd6641 100644 --- a/data/templates/default.epub3 +++ b/data/templates/default.epub3 @@ -47,6 +47,13 @@ $if(rights)$ $endif$ </section> $else$ +$if(coverpage)$ +<div id="cover-image"> +<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="100%" height="100%" viewBox="0 0 $cover-image-width$ $cover-image-height$" preserveAspectRatio="none"> +<image width="$cover-image-width$" height="$cover-image-height$" xlink:href="../media/$cover-image$" /> +</svg> +</div> +$else$ $for(include-before)$ $include-before$ $endfor$ @@ -55,6 +62,7 @@ $for(include-after)$ $include-after$ $endfor$ $endif$ +$endif$ </body> </html> diff --git a/src/Text/Pandoc/Writers/EPUB.hs b/src/Text/Pandoc/Writers/EPUB.hs index 82b6e8221..fdcab1442 100644 --- a/src/Text/Pandoc/Writers/EPUB.hs +++ b/src/Text/Pandoc/Writers/EPUB.hs @@ -41,6 +41,7 @@ import qualified Text.Pandoc.Class as P import Data.Time import Text.Pandoc.Definition import Text.Pandoc.Error +import Text.Pandoc.ImageSize import Text.Pandoc.Logging import Text.Pandoc.MIME (MimeType, extensionFromMimeType, getMimeType) import Text.Pandoc.Options (EPUBVersion (..), HTMLMathMethod (..), @@ -450,14 +451,23 @@ pandocToEPUB version opts doc = do Nothing -> return ([],[]) Just img -> do let coverImage = takeFileName img + imgContent <- lift $ P.readFileLazy img + (coverImageWidth, coverImageHeight) <- + case imageSize opts' (B.toStrict imgContent) of + Right sz -> return $ sizeInPixels sz + Left err' -> (0, 0) <$ report + (CouldNotDetermineImageSize img err') cpContent <- lift $ writeHtml opts'{ writerVariables = ("coverpage","true"): ("pagetitle", escapeStringForXML plainTitle): + ("cover-image", coverImage): + ("cover-image-width", show coverImageWidth): + ("cover-image-height", + show coverImageHeight): cssvars True ++ vars } - (Pandoc meta [RawBlock (Format "html") $ "<div id=\"cover-image\">\n<img src=\"../media/" ++ coverImage ++ "\" alt=\"cover image\" />\n</div>"]) - imgContent <- lift $ P.readFileLazy img + (Pandoc meta []) coverEntry <- mkEntry "text/cover.xhtml" cpContent coverImageEntry <- mkEntry ("media/" ++ coverImage) imgContent |