diff --git a/docreader/parser/mhtml_parser.py b/docreader/parser/mhtml_parser.py index 8103d11f..d79ecdd4 100644 --- a/docreader/parser/mhtml_parser.py +++ b/docreader/parser/mhtml_parser.py @@ -31,6 +31,23 @@ _AD_DOMAINS = ( _UNKNOWN_8BIT = frozenset({"unknown-8bit", "unknown"}) +# Inline elements that markdownify renders as "" when their text is only +# whitespace, taking that whitespace with them. +_WHITESPACE_DROPPING_INLINE_TAGS = ( + "a", + "b", + "code", + "del", + "em", + "i", + "kbd", + "s", + "samp", + "strong", + "sub", + "sup", +) + def _header_str(value) -> str: """Normalize a MIME header to str, recovering 8-bit UTF-8 bytes. @@ -270,6 +287,7 @@ class MHTMLParser(BaseParser): self._strip_internal_links(soup) if image_aliases: self._rewrite_image_sources(soup, image_aliases, base_location) + self._unwrap_whitespace_only_inline(soup) text_fallback = soup.get_text(separator="\n", strip=True) markdown_text = md(str(soup), heading_style="ATX") result = self._normalize_markdown(markdown_text) @@ -295,6 +313,19 @@ class MHTMLParser(BaseParser): """Backward-compatible wrapper for existing internal callers and tests.""" return self.html_to_markdown(html_content, image_aliases, base_location) + @staticmethod + def _unwrap_whitespace_only_inline(soup: BeautifulSoup) -> None: + """Keep the whitespace held by an otherwise empty inline element. + + markdownify drops such an element together with its whitespace, so + ``further reference`` became ``furtherreference``. + Unwrapping leaves the whitespace as plain text. Innermost elements go + first, so `` `` is unwrapped completely. + """ + for tag in reversed(soup.find_all(_WHITESPACE_DROPPING_INLINE_TAGS)): + if tag.find(True) is None and not tag.get_text().strip(): + tag.unwrap() + @staticmethod def _normalize_markdown(markdown_text: str) -> str: text = markdown_text.replace("\r\n", "\n").replace("\r", "\n") diff --git a/docreader/tests/test_html_parser.py b/docreader/tests/test_html_parser.py index 0694f7da..68edd901 100644 --- a/docreader/tests/test_html_parser.py +++ b/docreader/tests/test_html_parser.py @@ -58,6 +58,13 @@ class HTMLParserTest(unittest.TestCase): self.assertIn("[Guide](./guide.html)", document.content) self.assertIn("[Section](#section)", document.content) + def test_parse_keeps_space_held_by_whitespace_only_inline_element(self): + html = b"
further reference
" + + document = HTMLParser(file_name="space.html", file_type="html").parse(html) + + self.assertEqual("further reference", document.content) + def test_registry_routes_html_extensions_to_html_parser(self): self.assertIs(registry.get_parser_class("", "html"), HTMLParser) self.assertIs(registry.get_parser_class("", "htm"), HTMLParser) diff --git a/docreader/tests/test_mhtml_parser.py b/docreader/tests/test_mhtml_parser.py index 1d98fa76..a0bd050b 100644 --- a/docreader/tests/test_mhtml_parser.py +++ b/docreader/tests/test_mhtml_parser.py @@ -242,6 +242,38 @@ class MHTMLParserTest(unittest.TestCase): self.assertEqual(markdown, "alpha \nbeta") + def test_html_to_markdown_keeps_whitespace_of_whitespace_only_inline_elements(self): + parser = MHTMLParser(file_name="article.mhtml", file_type="mhtml") + cases = { + "further reference
": "further reference", + "First Last
": "**First** **Last**", + "Hello world
one two
": "one\xa0two", + '': "see here", + "a b
": "a b", + "x y z w
": "x y z w", + "v w x y
bold code em "
+ 'gone link '
+ "keepempty "
+ '