forked from marko/svitak
add epub3 navigation parsing
This commit is contained in:
parent
2fd5982020
commit
ec47012b5f
1 changed files with 27 additions and 2 deletions
|
|
@ -6,12 +6,16 @@ module Navigation
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
|
|
||||||
import Text.HTML.Scalpel (Scraper, attr, (@:), (@=), chroot, chroots, text, match)
|
import Text.HTML.Scalpel (Scraper, attr, (@:), (@=), chroot, chroots, text, match, scrapeStringLike, scrape)
|
||||||
import Types (NavItem(..), EpubType (..), InternalPath(..))
|
import Types (NavItem(..), EpubType (..), InternalPath(..), EpubEnv(..))
|
||||||
import Data.Maybe (fromMaybe)
|
import Data.Maybe (fromMaybe)
|
||||||
|
import qualified Data.ByteString.Lazy as B
|
||||||
import Control.Applicative (optional)
|
import Control.Applicative (optional)
|
||||||
import Control.Monad (guard)
|
import Control.Monad (guard)
|
||||||
|
import Codec.Archive.Zip
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
|
import qualified Data.Text.Encoding as TE
|
||||||
|
import System.FilePath (normalise, takeDirectory, (</>))
|
||||||
|
|
||||||
-- ------------------------------------------------------------
|
-- ------------------------------------------------------------
|
||||||
-- EPub 3 navigation
|
-- EPub 3 navigation
|
||||||
|
|
@ -39,3 +43,24 @@ lndmkScraper labels =
|
||||||
where
|
where
|
||||||
navPredicate "epub:type" xs = EpubType (T.pack xs) `elem` labels
|
navPredicate "epub:type" xs = EpubType (T.pack xs) `elem` labels
|
||||||
navPredicate _ _ = False
|
navPredicate _ _ = False
|
||||||
|
|
||||||
|
--------------------------------------------------------
|
||||||
|
-- dispatching --
|
||||||
|
--------------------------------------------------------
|
||||||
|
|
||||||
|
getLandmarks :: EpubEnv -> [NavItem]
|
||||||
|
getLandmarks env =
|
||||||
|
case eVersion env of
|
||||||
|
"2.0" -> []
|
||||||
|
"3.0" ->
|
||||||
|
case scrapeStringLike (opfXml env) navFinder of
|
||||||
|
Nothing -> []
|
||||||
|
Just relPath ->
|
||||||
|
let fullPath = normalise $ (baseDir env) </> relPath
|
||||||
|
in case findEntryByPath fullPath (archive env) of
|
||||||
|
Nothing -> []
|
||||||
|
Just entry ->
|
||||||
|
let content = TE.decodeUtf8 . B.toStrict $ fromEntry entry
|
||||||
|
types = map EpubType ["toc", "landmarks", "bodymatter"]
|
||||||
|
scrp = lndmkScraper types
|
||||||
|
in fromMaybe [] $ scrapeStringLike content scrp
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue