@@ -14,7 +14,7 @@ import Text.Pandoc.Walk (walk, query)
14import Text.Pandoc.Generic(bottomUp)
14import Text.Pandoc.Generic(bottomUp)
15import Text.Pandoc.Readers.HTML (readHtml)
15import Text.Pandoc.Readers.HTML (readHtml)
16import Text.Pandoc.Options ( ReaderOptions(..), readerTrace)
16import Text.Pandoc.Options ( ReaderOptions(..), readerTrace)
17−import Text.Pandoc.Shared (escapeURI)
17+import Text.Pandoc.Shared (escapeURI, collapseFilePath)
18import Text.Pandoc.MediaBag (MediaBag, insertMedia)
18import Text.Pandoc.MediaBag (MediaBag, insertMedia)
19import Text.Pandoc.Compat.Except (MonadError, throwError, runExcept, Except)
19import Text.Pandoc.Compat.Except (MonadError, throwError, runExcept, Except)
20import qualified Text.Pandoc.Builder as B
20import qualified Text.Pandoc.Builder as B
@@ -22,7 +22,7 @@ import Codec.Archive.Zip ( Archive (..), toArchive, fromEntry
22 , findEntryByPath, Entry)
22 , findEntryByPath, Entry)
23import qualified Data.ByteString.Lazy as BL (ByteString)
23import qualified Data.ByteString.Lazy as BL (ByteString)
24import System.FilePath ( takeFileName, (</>), dropFileName, normalise
24import System.FilePath ( takeFileName, (</>), dropFileName, normalise
25− , joinPath, dropFileName, splitDirectories
25+ , dropFileName
26 , splitFileName )
26 , splitFileName )
27import qualified Text.Pandoc.UTF8 as UTF8 (toStringLazy)
27import qualified Text.Pandoc.UTF8 as UTF8 (toStringLazy)
28import Control.Applicative ((<$>))
28import Control.Applicative ((<$>))
@@ -50,50 +50,52 @@ runEPUB = either error id . runExcept
51archiveToEPUB :: (MonadError String m) => ReaderOptions -> Archive -> m (Pandoc, MediaBag)
51archiveToEPUB :: (MonadError String m) => ReaderOptions -> Archive -> m (Pandoc, MediaBag)
52archiveToEPUB os archive = do
52archiveToEPUB os archive = do
53+ -- root is path to folder with manifest file in
53 (root, content) <- getManifest archive
54 (root, content) <- getManifest archive
54 meta <- parseMeta content
55 meta <- parseMeta content
55 (cover, items) <- parseManifest content
56 (cover, items) <- parseManifest content
57+ -- No need to collapse here as the image path is from the manifest file
56− let coverDoc = fromMaybe mempty (imageToPandoc <$> cover)
58+ let coverDoc = fromMaybe mempty (imageToPandoc . (root </>) <$> cover)
57 spine <- parseSpine items content
59 spine <- parseSpine items content
58 let escapedSpine = map (escapeURI . takeFileName . fst) spine
60 let escapedSpine = map (escapeURI . takeFileName . fst) spine
59 Pandoc _ bs <-
61 Pandoc _ bs <-
60 foldM' (\a b -> ((a <>) . bottomUp (prependHash escapedSpine))
62 foldM' (\a b -> ((a <>) . bottomUp (prependHash escapedSpine))
61 `liftM` parseSpineElem root b) mempty spine
63 `liftM` parseSpineElem root b) mempty spine
62 let ast = coverDoc <> (Pandoc meta bs)
64 let ast = coverDoc <> (Pandoc meta bs)
63− let mediaBag = fetchImages (M.elems items) root archive ast
65+ let mediaBag = fetchImages (M.elems items) archive ast
64 return $ (ast, mediaBag)
66 return $ (ast, mediaBag)
66 os' = os {readerParseRaw = True}
68 os' = os {readerParseRaw = True}
67 parseSpineElem :: MonadError String m => FilePath -> (FilePath, MIME) -> m Pandoc
69 parseSpineElem :: MonadError String m => FilePath -> (FilePath, MIME) -> m Pandoc
68 parseSpineElem (normalise -> r) (normalise -> path, mime) = do
70 parseSpineElem (normalise -> r) (normalise -> path, mime) = do
69 when (readerTrace os) (traceM path)
71 when (readerTrace os) (traceM path)
70− doc <- mimeToReader mime r path
72+ doc <- mimeToReader mime (r </> path)
71 let docSpan = B.doc $ B.para $ B.spanWith (takeFileName path, [], []) mempty
73 let docSpan = B.doc $ B.para $ B.spanWith (takeFileName path, [], []) mempty
72 return $ docSpan <> doc
74 return $ docSpan <> doc
73− mimeToReader :: MonadError String m => MIME -> FilePath -> FilePath -> m Pandoc
75+ mimeToReader :: MonadError String m => MIME -> FilePath -> m Pandoc
74− mimeToReader "application/xhtml+xml" r path = do
76+ mimeToReader "application/xhtml+xml" (normalise -> path) = do
75− fname <- findEntryByPathE (r </> path) archive
77+ fname <- findEntryByPathE path archive
76− return $ fixInternalReferences (r </> path) .
78+ return $ fixInternalReferences path .
77 readHtml os' .
79 readHtml os' .
78 UTF8.toStringLazy $
80 UTF8.toStringLazy $
79 fromEntry fname
81 fromEntry fname
80− mimeToReader s _ path
82+ mimeToReader s path
81 | s `elem` imageMimes = return $ imageToPandoc path
83 | s `elem` imageMimes = return $ imageToPandoc path
82 | otherwise = return $ mempty
84 | otherwise = return $ mempty
86+-- paths should be absolute when this function is called
87+-- renameImages should do this
84fetchImages :: [(FilePath, MIME)]
88fetchImages :: [(FilePath, MIME)]
86 -> Archive
89 -> Archive
88 -> MediaBag
91 -> MediaBag
89−fetchImages mimes root arc (query iq -> links) =
92+fetchImages mimes arc (query iq -> links) =
90 foldr (uncurry3 insertMedia) mempty
93 foldr (uncurry3 insertMedia) mempty
91 (mapMaybe getEntry links)
94 (mapMaybe getEntry links)
93− getEntry (normalise -> l) =
96+ getEntry link =
94− let mediaPos = normalise (root </> l) in
95− (l , lookup mediaPos mimes, ) . fromEntry
97+ (link , lookup link mimes, ) . fromEntry
96− <$> findEntryByPath mediaPos arc
98+ <$> findEntryByPath link arc
98iq :: Inline -> [FilePath]
100iq :: Inline -> [FilePath]
99iq (Image _ (url, _)) = [url]
101iq (Image _ (url, _)) = [url]
@@ -101,20 +103,9 @@ iq _ = []
102-- Remove relative paths
104-- Remove relative paths
103renameImages :: FilePath -> Inline -> Inline
105renameImages :: FilePath -> Inline -> Inline
104−renameImages root (Image a (url, b)) = Image a (collapse (root </> url), b)
106+renameImages root (Image a (url, b)) = Image a (collapseFilePath (root </> url), b)
105renameImages _ x = x
107renameImages _ x = x
107−collapse :: FilePath -> FilePath
108−collapse = joinPath . reverse . foldl go [] . splitDirectories
111− go r@(p:rs) ".." = case p of
118imageToPandoc :: FilePath -> Pandoc
109imageToPandoc :: FilePath -> Pandoc
119imageToPandoc s = B.doc . B.para $ B.image s "" mempty
110imageToPandoc s = B.doc . B.para $ B.image s "" mempty