git.iko.soy/typeable/pandoc
on master help sign in
EPUB Reader: Improved robustness of image extraction

We now maintain the invariant that when fetchImages is called, all images have absolute paths. This patch fixes several bugs relating to this as there are three places where images can be introduced. (1) During the HTML parse (2) As spine elements (3) As a cover image For (1), the paths are corrected by the transformation renameImages For (2) and (3), we need to append the "root" to the path we parse from the spine

commitcfd8c0214c3f369d0f8c6f033325c343b78c7659 authorMatthew Pickering <matthewtpickering@gmail.com> dateFri 8 Aug 2014, 23:04 (12 years ago) parent40ae8ef
1 file changed+9−7
src/Text/Pandoc/Readers/EPUB.hs+9−7
src/Text/Pandoc/Readers/EPUB.hs+9−7
@@ -52,10 +52,12 @@ runEPUB = either error id . runExcept
5252--
5353archiveToEPUB :: (MonadError String m) => ReaderOptions -> Archive -> m (Pandoc, MediaBag)
5454archiveToEPUB (setEPUBOptions -> os) archive = do
55+ -- root is path to folder with manifest file in
5556 (root, content) <- getManifest archive
5657 meta <- parseMeta content
5758 (cover, items) <- parseManifest content
58− let coverDoc = fromMaybe mempty (imageToPandoc <$> cover)
59+ -- No need to collapse here as the image path is from the manifest file
60+ let coverDoc = fromMaybe mempty (imageToPandoc . (root </>) <$> cover)
5961 spine <- parseSpine items content
6062 let escapedSpine = map (escapeURI . takeFileName . fst) spine
6163 Pandoc _ bs <-
@@ -68,17 +70,17 @@ archiveToEPUB (setEPUBOptions -> os) archive = do
6870 parseSpineElem :: MonadError String m => FilePath -> (FilePath, MIME) -> m Pandoc
6971 parseSpineElem (normalise -> r) (normalise -> path, mime) = do
7072 when (readerTrace os) (traceM path)
71− doc <- mimeToReader mime r path
73+ doc <- mimeToReader mime (r </> path)
7274 let docSpan = B.doc $ B.para $ B.spanWith (takeFileName path, [], []) mempty
7375 return $ docSpan <> doc
74− mimeToReader :: MonadError String m => MIME -> FilePath -> FilePath -> m Pandoc
75− mimeToReader "application/xhtml+xml" r path = do
76− fname <- findEntryByPathE (r </> path) archive
77− return $ fixInternalReferences (r </> path) .
76+ mimeToReader :: MonadError String m => MIME -> FilePath -> m Pandoc
77+ mimeToReader "application/xhtml+xml" (normalise -> path) = do
78+ fname <- findEntryByPathE path archive
79+ return $ fixInternalReferences path .
7880 readHtml os .
7981 UTF8.toStringLazy $
8082 fromEntry fname
81− mimeToReader s _ path
83+ mimeToReader s path
8284 | s `elem` imageMimes = return $ imageToPandoc path
8385 | otherwise = return $ mempty
8486