diff options
Diffstat (limited to 'src/Text/Pandoc/Writers/EPUB.hs')
-rw-r--r-- | src/Text/Pandoc/Writers/EPUB.hs | 37 |
1 files changed, 28 insertions, 9 deletions
diff --git a/src/Text/Pandoc/Writers/EPUB.hs b/src/Text/Pandoc/Writers/EPUB.hs index 82b6e8221..0f4e338e6 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 (..), @@ -67,6 +68,7 @@ data Chapter = Chapter (Maybe [Int]) [Block] data EPUBState = EPUBState { stMediaPaths :: [(FilePath, (FilePath, Maybe Entry))] + , stMediaNextId :: Int , stEpubSubdir :: String } @@ -390,7 +392,7 @@ writeEPUB epubVersion opts doc = do -- sanity check on epubSubdir unless (all (\c -> isAscii c && isAlphaNum c) epubSubdir) $ throwError $ PandocEpubSubdirectoryError epubSubdir - let initState = EPUBState { stMediaPaths = [], stEpubSubdir = epubSubdir } + let initState = EPUBState { stMediaPaths = [], stMediaNextId = 0, stEpubSubdir = epubSubdir } evalStateT (pandocToEPUB epubVersion opts doc) initState pandocToEPUB :: PandocMonad m @@ -450,14 +452,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 @@ -994,17 +1005,25 @@ modifyMediaRef oldsrc = do Just (n,_) -> return n Nothing -> catchError (do (img, mbMime) <- P.fetchItem oldsrc - let new = "media/file" ++ show (length media) ++ - fromMaybe (takeExtension (takeWhile (/='?') oldsrc)) - (('.':) <$> (mbMime >>= extensionFromMimeType)) - entry <- mkEntry new (B.fromChunks . (:[]) $ img) + let ext = fromMaybe (takeExtension (takeWhile (/='?') oldsrc)) + (('.':) <$> (mbMime >>= extensionFromMimeType)) + newName <- getMediaNextNewName ext + let newPath = "media/" ++ newName + entry <- mkEntry newPath (B.fromChunks . (:[]) $ img) modify $ \st -> st{ stMediaPaths = - (oldsrc, (new, Just entry)):media} - return new) + (oldsrc, (newPath, Just entry)):media} + return newPath) (\e -> do report $ CouldNotFetchResource oldsrc (show e) return oldsrc) +getMediaNextNewName :: PandocMonad m => String -> E m String +getMediaNextNewName ext = do + nextId <- gets stMediaNextId + modify $ \st -> st { stMediaNextId = nextId + 1 } + let nextName = "file" ++ show nextId ++ ext + (P.fetchItem nextName >> getMediaNextNewName ext) `catchError` const (return nextName) + transformBlock :: PandocMonad m => Block -> E m Block |