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
|
||||
, spine :: [T.Text]
|
||||
, manifestMap :: M.Map T.Text T.Text
|
||||
, archive :: Archive
|
||||
} deriving (Show)
|
||||
|
||||
myEnv :: Archive -> [Tag String] -> EpubEnv
|
||||
myEnv archive tags =
|
||||
myEnv arch tags =
|
||||
let
|
||||
opfPath = fromMaybe "" (getOpfPath archive)
|
||||
opfPath = fromMaybe "" (getOpfPath arch)
|
||||
prefix = getRootPrefix opfPath
|
||||
|
||||
in
|
||||
|
|
@ -34,6 +35,7 @@ myEnv archive tags =
|
|||
, rootPrefix = prefix
|
||||
, spine = getSpine tags
|
||||
, manifestMap = getManifest tags
|
||||
, archive = arch
|
||||
}
|
||||
|
||||
openEpub :: FilePath -> IO (Either String Archive)
|
||||
|
|
@ -83,5 +85,14 @@ resolvePath spineId = do
|
|||
pure $ fmap (\href -> p </> T.unpack href) (M.lookup spineId m)
|
||||
|
||||
resolveSpine :: EpubAction [FilePath]
|
||||
resolveSpine =
|
||||
catMaybes <$> (asks spine >>= mapM resolvePath)
|
||||
resolveSpine = 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