forked from marko/svitak
epub version awareness
This commit is contained in:
parent
acd221be28
commit
fd05214fee
1 changed files with 18 additions and 7 deletions
|
|
@ -16,7 +16,7 @@ import qualified Codec.Epub.Data.Spine as DSpin
|
|||
import Codec.Epub.Parse (getManifest, getMetadata, getSpine)
|
||||
import Control.Applicative ((<|>))
|
||||
import Control.Monad (guard)
|
||||
import Control.Monad.Except (runExceptT)
|
||||
import Control.Monad.Except (runExceptT, ExceptT)
|
||||
import Control.Monad.Reader (MonadReader (ask), liftIO, runReaderT)
|
||||
import qualified Data.ByteString.Base64 as B64
|
||||
import qualified Data.ByteString.Lazy as B
|
||||
|
|
@ -28,8 +28,9 @@ import System.FilePath (normalise, takeDirectory, (</>))
|
|||
import Text.HTML.Scalpel (Scraper, ScraperT, anySelector, attr, chroot, chroots, html, innerHTML, scrapeStringLike, text)
|
||||
import Types (Chapter (..), EpubAction, EpubEnv (..), InternalPath(..), ChapterIndex(..), BookInfo(..), EpubType(..))
|
||||
import Navigation (navFinder, lndmkScraper)
|
||||
import Codec.Epub.Parse (getPackage)
|
||||
import Codec.Epub.Data.Metadata (metaTitles, titleText, titleType, Creator (creatorText), Metadata (metaCreators))
|
||||
|
||||
import Codec.Epub.Data.Package (Package(..))
|
||||
instance BookInfo EpubEnv where
|
||||
getTitle env =
|
||||
let titles = metaTitles (eMetadata env)
|
||||
|
|
@ -71,11 +72,13 @@ openEpub path = do
|
|||
|
||||
metaResult <- runExceptT $ getMetadata xml
|
||||
manResult <- runExceptT $ getManifest xml
|
||||
verResult <- runExceptT $ detectVersion xml
|
||||
|
||||
case (metaResult, manResult) of
|
||||
(Left err, _) -> pure $ Left $ "Metadata parse error: " ++ err
|
||||
(_, Left err) -> pure $ Left $ "Manifest parse error: " ++ err
|
||||
(Right meta, Right man) ->
|
||||
case (metaResult, manResult, verResult) of
|
||||
(Left err, _, _) -> pure $ Left $ "Metadata parse error: " ++ err
|
||||
(_, Left err, _) -> pure $ Left $ "Manifest parse error: " ++ err
|
||||
(_, _, Left err) -> pure $ Left $ "Version parse error: " ++ err
|
||||
(Right meta, Right man, Right v) ->
|
||||
pure $
|
||||
Right $
|
||||
EpubEnv
|
||||
|
|
@ -84,9 +87,14 @@ openEpub path = do
|
|||
baseDir = takeDirectory opfPath,
|
||||
bookPath = path,
|
||||
eMetadata = meta,
|
||||
eManifest = man
|
||||
eManifest = man,
|
||||
eVersion = T.pack v
|
||||
}
|
||||
|
||||
(Left err, _, _) -> pure $ Left $ "Metadata error: " ++ err
|
||||
(_, Left err, _) -> pure $ Left $ "Manifest error: " ++ err
|
||||
(_, _, Left err) -> pure $ Left $ "Version error: " ++ err
|
||||
|
||||
getOpfPath :: Archive -> Maybe String
|
||||
getOpfPath arch =
|
||||
maybe Nothing (\e -> getRootPath (TE.decodeUtf8 . B.toStrict $ fromEntry e)) (findEntryByPath "META-INF/container.xml" arch)
|
||||
|
|
@ -183,3 +191,6 @@ chapterScraper = do
|
|||
title <- chTitleScraper <|> pure "Untitled Chapter"
|
||||
content <- chroot "body" (innerHTML anySelector) <|> innerHTML anySelector
|
||||
pure (title, content)
|
||||
|
||||
detectVersion :: String -> ExceptT String IO String
|
||||
detectVersion = fmap pkgVersion . getPackage
|
||||
|
|
|
|||
Loading…
Reference in a new issue