git.iko.soy/typeable/pandoc
on master help sign in
EPUB writer: Use svg tag wrapper for cover image.
5f495eaJohn MacFarlane
data/templates/default.epub2+8
@@ -46,6 +46,13 @@ $if(rights)$
46 <div class="rights">$rights$</div>
46 <div class="rights">$rights$</div>
47$endif$
47$endif$
48$else$
48$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$
49$for(include-before)$
56$for(include-before)$
50$include-before$
57$include-before$
51$endfor$
58$endfor$
@@ -54,6 +61,7 @@ $for(include-after)$
54$include-after$
61$include-after$
55$endfor$
62$endfor$
56$endif$
63$endif$
64+$endif$
57</body>
65</body>
58</html>
66</html>
59
67
data/templates/default.epub3+8
@@ -47,6 +47,13 @@ $if(rights)$
47$endif$
47$endif$
48</section>
48</section>
49$else$
49$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$
50$for(include-before)$
57$for(include-before)$
51$include-before$
58$include-before$
52$endfor$
59$endfor$
@@ -55,6 +62,7 @@ $for(include-after)$
55$include-after$
62$include-after$
56$endfor$
63$endfor$
57$endif$
64$endif$
65+$endif$
58</body>
66</body>
59</html>
67</html>
60
68
src/Text/Pandoc/Writers/EPUB.hs+12−2
@@ -41,6 +41,7 @@ import qualified Text.Pandoc.Class as P
41import Data.Time
41import Data.Time
42import Text.Pandoc.Definition
42import Text.Pandoc.Definition
43import Text.Pandoc.Error
43import Text.Pandoc.Error
44+import Text.Pandoc.ImageSize
44import Text.Pandoc.Logging
45import Text.Pandoc.Logging
45import Text.Pandoc.MIME (MimeType, extensionFromMimeType, getMimeType)
46import Text.Pandoc.MIME (MimeType, extensionFromMimeType, getMimeType)
46import Text.Pandoc.Options (EPUBVersion (..), HTMLMathMethod (..),
47import Text.Pandoc.Options (EPUBVersion (..), HTMLMathMethod (..),
@@ -450,14 +451,23 @@ pandocToEPUB version opts doc = do
450 Nothing -> return ([],[])
451 Nothing -> return ([],[])
451 Just img -> do
452 Just img -> do
452 let coverImage = takeFileName img
453 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')
453 cpContent <- lift $ writeHtml
460 cpContent <- lift $ writeHtml
454 opts'{ writerVariables =
461 opts'{ writerVariables =
455 ("coverpage","true"):
462 ("coverpage","true"):
456 ("pagetitle",
463 ("pagetitle",
457 escapeStringForXML plainTitle):
464 escapeStringForXML plainTitle):
465+ ("cover-image", coverImage):
466+ ("cover-image-width", show coverImageWidth):
467+ ("cover-image-height",
468+ show coverImageHeight):
458 cssvars True ++ vars }
469 cssvars True ++ vars }
459− (Pandoc meta [RawBlock (Format "html") $ "<div id=\"cover-image\">\n<img src=\"../media/" ++ coverImage ++ "\" alt=\"cover image\" />\n</div>"])
470+ (Pandoc meta [])
460− imgContent <- lift $ P.readFileLazy img
461 coverEntry <- mkEntry "text/cover.xhtml" cpContent
471 coverEntry <- mkEntry "text/cover.xhtml" cpContent
462 coverImageEntry <- mkEntry ("media/" ++ coverImage)
472 coverImageEntry <- mkEntry ("media/" ++ coverImage)
463 imgContent
473 imgContent