Add tag support

This commit is contained in:
Marko Andjelic 2026-01-25 03:06:12 +00:00
commit 8d3848451c

View file

@ -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