From 8d3848451c5a9c8e5fef4823935b06b78fe055c9 Mon Sep 17 00:00:00 2001 From: Marko Andjelic Date: Sun, 25 Jan 2026 03:06:12 +0000 Subject: [PATCH] Add tag support --- app/EpubParser.hs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/app/EpubParser.hs b/app/EpubParser.hs index 2d4cff9..cd69dab 100644 --- a/app/EpubParser.hs +++ b/app/EpubParser.hs @@ -19,7 +19,7 @@ import qualified Data.Text.Encoding as TE import Control.Monad.Reader ( ReaderT, MonadReader(ask) ) import Text.HTML.TagSoup ( parseTags, Tag(..) ) import System.FilePath (takeDirectory, (), normalise) - +import Data.Maybe (mapMaybe, isJust) type EpubAction a = ReaderT EpubEnv IO a data EpubEnv = EpubEnv @@ -60,10 +60,10 @@ resolveSpine :: EpubAction [FilePath] resolveSpine = do env <- ask let tags = manifest env - let itemRefs = [ idRef | TagOpen "itemref" attrs <- tags, let idRef = lookup "idref" attrs, Just idRef /= Nothing ] + let itemRefs = [ idRef | TagOpen "itemref" attrs <- tags, let idRef = lookup "idref" attrs, isJust $ Just idRef ] - let files = map (findFileById tags) itemRefs - return [ f | Just f <- files ] + -- let files = map (findFileById tags) itemRefs + pure $ mapMaybe (findFileById tags) itemRefs findFileById :: [Tag String] -> Maybe String -> Maybe FilePath findFileById _ Nothing = Nothing @@ -73,19 +73,14 @@ findFileById tags (Just myId) = (x:_) -> x [] -> Nothing -getChapter :: FilePath -> EpubAction T.Text +getChapter :: FilePath -> EpubAction [Tag T.Text] getChapter filename = do env <- ask - let fullPath = normalise $ (baseDir env) filename + let fullPath = normalise $ baseDir env filename let fileEntry = findEntryByPath fullPath (archive env) case fileEntry of - Nothing -> return "" + Nothing -> pure [] Just file -> do - let content = fromEntry file - return $ stripHtml $ TE.decodeUtf8 $ B.toStrict content -stripHtml :: T.Text -> T.Text -stripHtml raw = - let tags = parseTags (T.unpack raw) - in T.pack $ unwords [ txt | TagText txt <- tags ] + pure $ parseTags $ TE.decodeUtf8 $ B.toStrict $ fromEntry file