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.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
@ -29,6 +30,7 @@ myEnv archive tags =
{ title = getTagText "dc:title" tags
, author = getTagText "dc:creator" tags
, 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