git.iko.soy/typeable/pandoc
on master help sign in
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.

commit5f495eaace3ce4cf15a89b6b3cfb54df6d1658d8 authorJohn MacFarlane <jgm@berkeley.edu> dateSat 13 Jul 2019, 15:20 (7 years ago) parent6cf5c3f
3 files changed+28−2
data/templates/default.epub2+8 data/templates/default.epub3+8 src/Text/Pandoc/Writers/EPUB.hs+12−2
data/templates/default.epub2+8
@@ -46,6 +46,13 @@ $if(rights)$
4646 <div class="rights">$rights$</div>
4747$endif$
4848$else$
49+$if(coverpage)$
50+<div id="cover-image">
51+<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">
52+<image width="$cover-image-width$" height="$cover-image-height$" xlink:href="../media/$cover-image$" />
53+</svg>
54+</div>
55+$else$
4956$for(include-before)$
5057$include-before$
5158$endfor$
@@ -54,6 +61,7 @@ $for(include-after)$
5461$include-after$
5562$endfor$
5663$endif$
64+$endif$
5765</body>
5866</html>
5967
data/templates/default.epub3+8
@@ -47,6 +47,13 @@ $if(rights)$
4747$endif$
4848</section>
4949$else$
50+$if(coverpage)$
51+<div id="cover-image">
52+<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">
53+<image width="$cover-image-width$" height="$cover-image-height$" xlink:href="../media/$cover-image$" />
54+</svg>
55+</div>
56+$else$
5057$for(include-before)$
5158$include-before$
5259$endfor$
@@ -55,6 +62,7 @@ $for(include-after)$
5562$include-after$
5663$endfor$
5764$endif$
65+$endif$
5866</body>
5967</html>
6068
src/Text/Pandoc/Writers/EPUB.hs+12−2
@@ -41,6 +41,7 @@ import qualified Text.Pandoc.Class as P
4141import Data.Time
4242import Text.Pandoc.Definition
4343import Text.Pandoc.Error
44+import Text.Pandoc.ImageSize
4445import Text.Pandoc.Logging
4546import Text.Pandoc.MIME (MimeType, extensionFromMimeType, getMimeType)
4647import Text.Pandoc.Options (EPUBVersion (..), HTMLMathMethod (..),
@@ -450,14 +451,23 @@ pandocToEPUB version opts doc = do
450451 Nothing -> return ([],[])
451452 Just img -> do
452453 let coverImage = takeFileName img
454+ imgContent <- lift $ P.readFileLazy img
455+ (coverImageWidth, coverImageHeight) <-
456+ case imageSize opts' (B.toStrict imgContent) of
457+ Right sz -> return $ sizeInPixels sz
458+ Left err' -> (0, 0) <$ report
459+ (CouldNotDetermineImageSize img err')
453460 cpContent <- lift $ writeHtml
454461 opts'{ writerVariables =
455462 ("coverpage","true"):
456463 ("pagetitle",
457464 escapeStringForXML plainTitle):
465+ ("cover-image", coverImage):
466+ ("cover-image-width", show coverImageWidth):
467+ ("cover-image-height",
468+ show coverImageHeight):
458469 cssvars True ++ vars }
459− (Pandoc meta [RawBlock (Format "html") $ "<div id=\"cover-image\">\n<img src=\"../media/" ++ coverImage ++ "\" alt=\"cover image\" />\n</div>"])
460− imgContent <- lift $ P.readFileLazy img
470+ (Pandoc meta [])
461471 coverEntry <- mkEntry "text/cover.xhtml" cpContent
462472 coverImageEntry <- mkEntry ("media/" ++ coverImage)
463473 imgContent