Add spine getter
This commit is contained in:
parent
092ef7b8f1
commit
9938b51ba1
1 changed files with 9 additions and 3 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue