summaryrefslogtreecommitdiff
path: root/src/Text/Pandoc/Readers/EPUB.hs
diff options
context:
space:
mode:
authorJonas Smedegaard <dr@jones.dk>2016-06-06 22:32:09 +0200
committerJonas Smedegaard <dr@jones.dk>2016-06-06 22:32:09 +0200
commitf8389d48da9921672da3896b85f7ed444ede714d (patch)
treed01ffee40dfdfa03f9b6a18980e5beb0c5dc262e /src/Text/Pandoc/Readers/EPUB.hs
parent29583a109043c7b2128b37f13135a326add16996 (diff)
Imported Upstream version 1.17.1~dfsg
Diffstat (limited to 'src/Text/Pandoc/Readers/EPUB.hs')
-rw-r--r--src/Text/Pandoc/Readers/EPUB.hs18
1 files changed, 12 insertions, 6 deletions
diff --git a/src/Text/Pandoc/Readers/EPUB.hs b/src/Text/Pandoc/Readers/EPUB.hs
index 07d282708..b8a0b47e7 100644
--- a/src/Text/Pandoc/Readers/EPUB.hs
+++ b/src/Text/Pandoc/Readers/EPUB.hs
@@ -14,12 +14,13 @@ import Text.Pandoc.Walk (walk, query)
import Text.Pandoc.Readers.HTML (readHtml)
import Text.Pandoc.Options ( ReaderOptions(..), readerTrace)
import Text.Pandoc.Shared (escapeURI, collapseFilePath, addMetaField)
+import Network.URI (unEscapeString)
import Text.Pandoc.MediaBag (MediaBag, insertMedia)
import Text.Pandoc.Compat.Except (MonadError, throwError, runExcept, Except)
import Text.Pandoc.Compat.Monoid ((<>))
import Text.Pandoc.MIME (MimeType)
import qualified Text.Pandoc.Builder as B
-import Codec.Archive.Zip ( Archive (..), toArchive, fromEntry
+import Codec.Archive.Zip ( Archive (..), toArchiveOrFail, fromEntry
, findEntryByPath, Entry)
import qualified Data.ByteString.Lazy as BL (ByteString)
import System.FilePath ( takeFileName, (</>), dropFileName, normalise
@@ -39,7 +40,9 @@ import Text.Pandoc.Error
type Items = M.Map String (FilePath, MimeType)
readEPUB :: ReaderOptions -> BL.ByteString -> Either PandocError (Pandoc, MediaBag)
-readEPUB opts bytes = runEPUB (archiveToEPUB opts $ toArchive bytes)
+readEPUB opts bytes = case toArchiveOrFail bytes of
+ Right archive -> runEPUB $ archiveToEPUB opts $ archive
+ Left _ -> Left $ ParseFailure "Couldn't extract ePub file"
runEPUB :: Except PandocError a -> Either PandocError a
runEPUB = runExcept
@@ -72,14 +75,15 @@ archiveToEPUB os archive = do
let docSpan = B.doc $ B.para $ B.spanWith (takeFileName path, [], []) mempty
return $ docSpan <> doc
mimeToReader :: MonadError PandocError m => MimeType -> FilePath -> FilePath -> m Pandoc
- mimeToReader "application/xhtml+xml" (normalise -> root) (normalise -> path) = do
+ mimeToReader "application/xhtml+xml" (unEscapeString -> root)
+ (unEscapeString -> path) = do
fname <- findEntryByPathE (root </> path) archive
html <- either throwError return .
readHtml os' .
UTF8.toStringLazy $
fromEntry fname
return $ fixInternalReferences path html
- mimeToReader s _ path
+ mimeToReader s _ (unEscapeString -> path)
| s `elem` imageMimes = return $ imageToPandoc path
| otherwise = return $ mempty
@@ -190,8 +194,10 @@ fixInlineIRs s (Span as v) =
Span (fixAttrs s as) v
fixInlineIRs s (Code as code) =
Code (fixAttrs s as) code
-fixInlineIRs s (Link attr t ('#':url, tit)) =
- Link attr t (addHash s url, tit)
+fixInlineIRs s (Link as is ('#':url, tit)) =
+ Link (fixAttrs s as) is (addHash s url, tit)
+fixInlineIRs s (Link as is t) =
+ Link (fixAttrs s as) is t
fixInlineIRs _ v = v
prependHash :: [String] -> Inline -> Inline