Skip to content

Turn any technical book PDF into a Claude Code skill — ready to study, reference, and use while you work.

fix(html): decode entities once in the stdlib parser (#60)

Shubham Srivastavad09d44da64b8
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b0eb785..32461b5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -28,6 +28,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- The dependency-free RTF fallback (used when `striprtf` is not installed) now
decodes `\uN` unicode escapes — smart quotes, dashes, accented letters — instead
of dropping them and leaving only the ASCII fallback character.
+- The stdlib HTML parser (the fallback for HTML files and EPUB extraction when
+ BeautifulSoup is not installed) no longer decodes HTML entities twice, so
+ double-encoded entities such as `&` survive intact.
## [1.2.0] — 2026-06-17
diff --git a/book_to_skill/parsers/html.py b/book_to_skill/parsers/html.py
index 72af1a1..f454363 100644
--- a/book_to_skill/parsers/html.py
+++ b/book_to_skill/parsers/html.py
@@ -31,7 +31,10 @@ class _HTMLTextExtractor(html.parser.HTMLParser):
self._parts.append(data)
def get_text(self) -> str:
- return html.unescape("".join(self._parts))
+ # HTMLParser(convert_charrefs=True) already decoded entities in
+ # handle_data; do NOT unescape again or double-encoded entities
+ # (e.g. "&") collapse incorrectly.
+ return "".join(self._parts)
def extract_html_content(raw_html: str) -> str:
diff --git a/tests/test_book_to_skill.py b/tests/test_book_to_skill.py
index 4303b69..e8cfdef 100644
--- a/tests/test_book_to_skill.py
+++ b/tests/test_book_to_skill.py
@@ -1033,3 +1033,35 @@ class TestRtfUnicodeFallback:
# Two adjacent \uN escapes, each with a \'XX hex fallback, decode cleanly.
text = self._BS + "u8220" + self._BS + "'93Hi" + self._BS + "u8221" + self._BS + "'94"
assert strip_rtf_fallback(text) == "“Hi”"
+
+
+class TestHtmlEntityDecoding:
+ """The stdlib HTML parser decodes entities exactly once (not twice)."""
+
+ def _text(self, fragment):
+ # Feed a raw fragment (no block tags) through a fresh stdlib parser.
+ from book_to_skill.parsers.html import _HTMLTextExtractor
+ p = _HTMLTextExtractor()
+ p.feed(fragment)
+ return p.get_text()
+
+ def test_double_encoded_ampersand(self):
+ # The bug: this used to collapse to "&" (decoded twice).
+ assert self._text("&") == "&"
+
+ def test_double_encoded_tag(self):
+ assert self._text("<tag>") == "<tag>"
+
+ def test_single_entities_still_decode(self):
+ assert self._text("&lt;b&gt;") == "<b>"
+ assert self._text("&amp;") == "&"
+
+ def test_numeric_and_named_entities(self):
+ assert self._text("&#233;") == "é" # decimal numeric
+ assert self._text("&#xE9;") == "é" # hex numeric
+ assert self._text("&copy;") == "©" # non-ASCII named entity
+ assert self._text("hello") == "hello" # plain text
+
+ def test_skip_tag_content_excluded(self):
+ # Confirms the change didn't disturb skip-tag handling.
+ assert self._text("<style>x{}</style>keep") == "keep"