From 4a55d773c6c9d2b00a21d3373156cf6633d3410e Mon Sep 17 00:00:00 2001 From: Marko Andjelic Date: Tue, 27 Jan 2026 21:21:15 +0000 Subject: [PATCH] improve chapter detection --- app/EpubParser.hs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/app/EpubParser.hs b/app/EpubParser.hs index 3065d22..7b25c6e 100644 --- a/app/EpubParser.hs +++ b/app/EpubParser.hs @@ -120,16 +120,7 @@ embedImages arch bdir = map processTag b64 = TE.decodeUtf8 $ B64.encode rawData in "data:" <> getMimeType fullPath <> ";base64," <> b64 --- TODO: Implement this, as it's not called yet -getChapterTitle :: [Tag T.Text] -> T.Text -getChapterTitle tags = - case dropWhile (not . isHeading) tags of - (_ : TagText t : _) -> T.strip t - _ -> "Untitled Chapter" - where - isHeading (TagOpen "h1" _) = True - isHeading (TagOpen "h2" _) = True - isHeading _ = False + filterJunk :: [Tag T.Text] -> [Tag T.Text] filterJunk = go @@ -142,3 +133,17 @@ filterJunk = go dropUntilClose _ [] = [] dropUntilClose name (TagClose n : xs) | n == name = xs dropUntilClose name (_ : xs) = dropUntilClose name xs + +-- TODO: Implement this, as it's not called yet +getChapterTitle :: [Tag T.Text] -> T.Text +getChapterTitle tags = + case dropWhile (not . ishding) tags of + (TagOpen x _ : xs ) -> T.strip $ innerText (takeWhile (not . isclose x) xs) -- The heading content, the outer tags will be detected such as h1 but this makes sure that every other inner tag is ignored yet the inner text will still be visible + + _ -> "Untitled Chapter" + + where + ishding (TagOpen n _ ) = n `elem` ["h1", "h2", "h3"] + ishding _ = False + isclose n (TagClose n') = n == n' + isclose _ _ = False