Add spine getter

This commit is contained in:
Marko Andjelic 2026-01-24 01:15:24 +00:00
commit 9938b51ba1

View file

@ -4,7 +4,7 @@ import Codec.Archive.Zip (toArchive, findEntryByPath, fromEntry, Archive)
import qualified Data.ByteString.Lazy as BL import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Lazy.Char8 as C8 import qualified Data.ByteString.Lazy.Char8 as C8
import Text.HTML.TagSoup (parseTags, Tag) 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 System.FilePath (takeDirectory)
import Control.Monad.Reader import Control.Monad.Reader
import qualified Data.Text as T import qualified Data.Text as T
@ -16,6 +16,7 @@ data EpubEnv = EpubEnv
{ title :: T.Text { title :: T.Text
, author :: T.Text , author :: T.Text
, rootPrefix :: FilePath , rootPrefix :: FilePath
, spine :: [T.Text]
} deriving (Show) } deriving (Show)
myEnv :: Archive -> [Tag String] -> EpubEnv myEnv :: Archive -> [Tag String] -> EpubEnv
@ -28,7 +29,8 @@ myEnv archive tags =
EpubEnv EpubEnv
{ title = getTagText "dc:title" tags { title = getTagText "dc:title" tags
, author = getTagText "dc:creator" tags , author = getTagText "dc:creator" tags
, rootPrefix = prefix , rootPrefix = prefix
, spine = getSpine tags
} }
openEpub :: FilePath -> IO (Either String Archive) openEpub :: FilePath -> IO (Either String Archive)
@ -38,7 +40,6 @@ getOpfPath :: Archive -> Maybe FilePath
getOpfPath archive = do getOpfPath archive = do
entry <- findEntryByPath "META-INF/container.xml" archive entry <- findEntryByPath "META-INF/container.xml" archive
let tags = parseTags $ C8.unpack (fromEntry entry) let tags = parseTags $ C8.unpack (fromEntry entry)
-- Logic: scrape (the_scraper) (the_tags)
scrape (attr "full-path" (tagSelector "rootfile")) tags scrape (attr "full-path" (tagSelector "rootfile")) tags
getOpfTags :: Archive -> FilePath -> [Tag String] getOpfTags :: Archive -> FilePath -> [Tag String]
@ -47,6 +48,7 @@ getOpfTags archive opfpath =
Just entry -> parseTags $ C8.unpack (fromEntry entry) Just entry -> parseTags $ C8.unpack (fromEntry entry)
Nothing -> [] Nothing -> []
-- Makes it easier so we can just use this function and concatenate relative paths
getRootPrefix :: FilePath -> FilePath getRootPrefix :: FilePath -> FilePath
getRootPrefix path = getRootPrefix path =
let dir = takeDirectory path let dir = takeDirectory path
@ -57,3 +59,7 @@ getTagText tagName tags =
let scraper = text (tagSelector tagName) let scraper = text (tagSelector tagName)
in T.pack $ fromMaybe "" (scrape scraper tags) in T.pack $ fromMaybe "" (scrape scraper tags)
getSpine :: [Tag String] -> [T.Text]
getSpine tags =
map T.pack . fromMaybe [] $ scrape (attrs "idref" (tagSelector "itemref")) tags