From 9938b51ba144d6580ad1e4b81f3ff8c5bea64dc4 Mon Sep 17 00:00:00 2001 From: Marko Andjelic Date: Sat, 24 Jan 2026 01:15:24 +0000 Subject: [PATCH] Add spine getter --- app/EpubParser.hs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/app/EpubParser.hs b/app/EpubParser.hs index 7d6e08c..56be6d2 100644 --- a/app/EpubParser.hs +++ b/app/EpubParser.hs @@ -4,7 +4,7 @@ import Codec.Archive.Zip (toArchive, findEntryByPath, fromEntry, Archive) import qualified Data.ByteString.Lazy as BL import qualified Data.ByteString.Lazy.Char8 as C8 import Text.HTML.TagSoup (parseTags, Tag) -import Text.HTML.Scalpel (scrape, text, tagSelector, attr) +import Text.HTML.Scalpel (scrape, text, tagSelector, attr, attrs) import System.FilePath (takeDirectory) import Control.Monad.Reader import qualified Data.Text as T @@ -16,6 +16,7 @@ data EpubEnv = EpubEnv { title :: T.Text , author :: T.Text , rootPrefix :: FilePath + , spine :: [T.Text] } deriving (Show) myEnv :: Archive -> [Tag String] -> EpubEnv @@ -28,7 +29,8 @@ myEnv archive tags = EpubEnv { title = getTagText "dc:title" tags , author = getTagText "dc:creator" tags - , rootPrefix = prefix + , rootPrefix = prefix + , spine = getSpine tags } openEpub :: FilePath -> IO (Either String Archive) @@ -38,7 +40,6 @@ getOpfPath :: Archive -> Maybe FilePath getOpfPath archive = do entry <- findEntryByPath "META-INF/container.xml" archive let tags = parseTags $ C8.unpack (fromEntry entry) - -- Logic: scrape (the_scraper) (the_tags) scrape (attr "full-path" (tagSelector "rootfile")) tags getOpfTags :: Archive -> FilePath -> [Tag String] @@ -47,6 +48,7 @@ getOpfTags archive opfpath = Just entry -> parseTags $ C8.unpack (fromEntry entry) Nothing -> [] +-- Makes it easier so we can just use this function and concatenate relative paths getRootPrefix :: FilePath -> FilePath getRootPrefix path = let dir = takeDirectory path @@ -57,3 +59,7 @@ getTagText tagName tags = let scraper = text (tagSelector tagName) in T.pack $ fromMaybe "" (scrape scraper tags) + +getSpine :: [Tag String] -> [T.Text] +getSpine tags = + map T.pack . fromMaybe [] $ scrape (attrs "idref" (tagSelector "itemref")) tags