Add chapter getter
This commit is contained in:
parent
a9e774676e
commit
f6c7d9065e
1 changed files with 15 additions and 4 deletions
|
|
@ -19,12 +19,13 @@ data EpubEnv = EpubEnv
|
||||||
, rootPrefix :: FilePath
|
, rootPrefix :: FilePath
|
||||||
, spine :: [T.Text]
|
, spine :: [T.Text]
|
||||||
, manifestMap :: M.Map T.Text T.Text
|
, manifestMap :: M.Map T.Text T.Text
|
||||||
|
, archive :: Archive
|
||||||
} deriving (Show)
|
} deriving (Show)
|
||||||
|
|
||||||
myEnv :: Archive -> [Tag String] -> EpubEnv
|
myEnv :: Archive -> [Tag String] -> EpubEnv
|
||||||
myEnv archive tags =
|
myEnv arch tags =
|
||||||
let
|
let
|
||||||
opfPath = fromMaybe "" (getOpfPath archive)
|
opfPath = fromMaybe "" (getOpfPath arch)
|
||||||
prefix = getRootPrefix opfPath
|
prefix = getRootPrefix opfPath
|
||||||
|
|
||||||
in
|
in
|
||||||
|
|
@ -34,6 +35,7 @@ myEnv archive tags =
|
||||||
, rootPrefix = prefix
|
, rootPrefix = prefix
|
||||||
, spine = getSpine tags
|
, spine = getSpine tags
|
||||||
, manifestMap = getManifest tags
|
, manifestMap = getManifest tags
|
||||||
|
, archive = arch
|
||||||
}
|
}
|
||||||
|
|
||||||
openEpub :: FilePath -> IO (Either String Archive)
|
openEpub :: FilePath -> IO (Either String Archive)
|
||||||
|
|
@ -83,5 +85,14 @@ resolvePath spineId = do
|
||||||
pure $ fmap (\href -> p </> T.unpack href) (M.lookup spineId m)
|
pure $ fmap (\href -> p </> T.unpack href) (M.lookup spineId m)
|
||||||
|
|
||||||
resolveSpine :: EpubAction [FilePath]
|
resolveSpine :: EpubAction [FilePath]
|
||||||
resolveSpine =
|
resolveSpine = catMaybes <$> (asks spine >>= mapM resolvePath)
|
||||||
catMaybes <$> (asks spine >>= mapM resolvePath)
|
|
||||||
|
getChapter :: FilePath -> EpubAction T.Text
|
||||||
|
getChapter path = do
|
||||||
|
arch <- asks archive
|
||||||
|
case findEntryByPath path arch of
|
||||||
|
Nothing -> pure $ T.pack "Error: Chapter not found."
|
||||||
|
Just entry -> do
|
||||||
|
let content = C8.unpack (fromEntry entry)
|
||||||
|
let rawText = scrape (text anySelector) (parseTags content)
|
||||||
|
pure $ T.pack $ fromMaybe "" rawText
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue