From f6c7d9065e037102e0310a65eb359e3376285457 Mon Sep 17 00:00:00 2001 From: Marko Andjelic Date: Sat, 24 Jan 2026 18:22:13 +0000 Subject: [PATCH] Add chapter getter --- app/EpubParser.hs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/app/EpubParser.hs b/app/EpubParser.hs index 15c386f..746f8c4 100644 --- a/app/EpubParser.hs +++ b/app/EpubParser.hs @@ -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