Turn any technical book PDF into a Claude Code skill — ready to study, reference, and use while you work.
Python agent-skillsai-agentsbook-to-skillcontext-engineeringdocument-processingedtechknowledge-baseknowledge-managementllmpdf-to-markdownragself-studystudy-tools
fix(epub): resolve encoded manifest hrefs (#231)
Stamina96d2c5397da81
diff --git a/book_to_skill/parsers/epub.py b/book_to_skill/parsers/epub.pyindex 9031094..1cb3f65 100644--- a/book_to_skill/parsers/epub.py+++ b/book_to_skill/parsers/epub.py@@ -1,9 +1,12 @@from __future__ import annotations+import htmlimport posixpathimport re-import zipfileimport sys+import zipfile+from urllib.parse import unquote, urlsplit+from book_to_skill.parsers.html import _HTMLTextExtractor@@ -60,6 +63,17 @@ def _find_opf_path(zf: zipfile.ZipFile) -> str | None:return opf_files[0] if opf_files else None+def _resolve_manifest_href(href: str, opf_dir: str) -> str:+ """Map an OPF manifest IRI to its ZIP member name."""+ # Manifest hrefs are IRIs, not literal archive names. XML entities have+ # already been decoded by a real XML parser; mirror that here before+ # discarding query/fragment components and decoding percent escapes.+ archive_path = unquote(urlsplit(html.unescape(href)).path)+ if opf_dir:+ archive_path = posixpath.join(opf_dir, archive_path)+ return posixpath.normpath(archive_path)++def extract_with_zipfile(epub_path: str) -> str | None:"""stdlib-only EPUB extractor: unzip → parse HTML files."""try:@@ -86,8 +100,7 @@ def extract_with_zipfile(epub_path: str) -> str | None:id_m = re.search(r'\bid=["\']([^"\']+)["\']', item_tag)href_m = re.search(r'\bhref=["\']([^"\']+)["\']', item_tag)if id_m and href_m:- href = href_m.group(1)- resolved = posixpath.normpath(posixpath.join(opf_dir, href)) if opf_dir else href+ resolved = _resolve_manifest_href(href_m.group(1), opf_dir)manifest[id_m.group(1)] = resolved# Spine: ordered idrefs -> hrefs (true reading order).diff --git a/tests/test_book_to_skill.py b/tests/test_book_to_skill.pyindex e125296..e25ed42 100644--- a/tests/test_book_to_skill.py+++ b/tests/test_book_to_skill.py@@ -1899,6 +1899,30 @@ class TestEpubSpineOrder:)assert "SUBDIR" in out+ def test_manifest_href_fragment_is_not_part_of_archive_name(self, tmp_path):+ opf = (+ '<package xmlns="http://www.idpf.org/2007/opf" version="3.0"><manifest>'+ '<item id="c1" href="chapter.xhtml#section-2" '+ 'media-type="application/xhtml+xml"/>'+ '</manifest><spine><itemref idref="c1"/></spine></package>'+ )+ files = {"chapter.xhtml": self._doc("FRAGMENT")}+ out = extract_with_zipfile(self._make_epub(tmp_path, opf, files))+ assert "FRAGMENT" in out++ def test_manifest_href_percent_encoding_maps_to_archive_name(self, tmp_path):+ opf = (+ '<package xmlns="http://www.idpf.org/2007/opf" version="3.0"><manifest>'+ '<item id="c1" href="Text/Chapter%201.xhtml" '+ 'media-type="application/xhtml+xml"/>'+ '</manifest><spine><itemref idref="c1"/></spine></package>'+ )+ files = {"OEBPS/Text/Chapter 1.xhtml": self._doc("ENCODED")}+ out = extract_with_zipfile(+ self._make_epub(tmp_path, opf, files, opf_name="OEBPS/content.opf")+ )+ assert "ENCODED" in out+def test_non_self_closing_item_tag(self, tmp_path):# <item ...></item> (non-self-closing) is parsed via its opening tag.opf = (