From ec47012b5f3791b175abef8917cfed4358b9b4e5 Mon Sep 17 00:00:00 2001 From: Marko Andjelic Date: Fri, 6 Mar 2026 01:52:15 +0000 Subject: [PATCH] add epub3 navigation parsing --- app/Navigation.hs | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/app/Navigation.hs b/app/Navigation.hs index 1703954..996657d 100644 --- a/app/Navigation.hs +++ b/app/Navigation.hs @@ -6,12 +6,16 @@ module Navigation ) where -import Text.HTML.Scalpel (Scraper, attr, (@:), (@=), chroot, chroots, text, match) -import Types (NavItem(..), EpubType (..), InternalPath(..)) +import Text.HTML.Scalpel (Scraper, attr, (@:), (@=), chroot, chroots, text, match, scrapeStringLike, scrape) +import Types (NavItem(..), EpubType (..), InternalPath(..), EpubEnv(..)) import Data.Maybe (fromMaybe) +import qualified Data.ByteString.Lazy as B import Control.Applicative (optional) import Control.Monad (guard) +import Codec.Archive.Zip import qualified Data.Text as T +import qualified Data.Text.Encoding as TE +import System.FilePath (normalise, takeDirectory, ()) -- ------------------------------------------------------------ -- EPub 3 navigation @@ -39,3 +43,24 @@ lndmkScraper labels = where navPredicate "epub:type" xs = EpubType (T.pack xs) `elem` labels 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