diff --git a/shelfmark/metadata_providers/googlebooks.py b/shelfmark/metadata_providers/googlebooks.py index d7d24652..bfcdf27c 100644 --- a/shelfmark/metadata_providers/googlebooks.py +++ b/shelfmark/metadata_providers/googlebooks.py @@ -6,6 +6,8 @@ Requires a free API key from Google Cloud Console (~1000 requests/day quota). API Documentation: https://developers.google.com/books/docs/v1/using """ +import html +import re from contextlib import suppress from dataclasses import replace from http import HTTPStatus @@ -43,6 +45,36 @@ from shelfmark.metadata_providers import ( logger = setup_logger(__name__) +# A tag starts with a letter after "<" or " b" is left alone. +_HTML_TAG_RE = re.compile(r"]*>") +# A br with attributes is still a line break; the word boundary keeps out any other tag +# whose name only starts with "br". +_LINE_BREAK_RE = re.compile(r"]*>", re.IGNORECASE) +_BLOCK_END_RE = re.compile(r"", re.IGNORECASE) +_TRAILING_SPACE_RE = re.compile(r"[ \t]+\n") +_EXTRA_BLANK_LINES_RE = re.compile(r"\n{3,}") + + +def _description_to_text(description: object) -> str | None: + """Turn a Google Books description, which is HTML, into plain text. + + The UI renders a description as text, never as markup, so leaving the HTML in showed + every description with its tags, starting with a literal "

". Converting it here + keeps the paragraph and line breaks, which the details view preserves, and avoids + putting markup from the API into the page. + """ + if not isinstance(description, str): + return None + text = _LINE_BREAK_RE.sub("\n", description) + text = _BLOCK_END_RE.sub("\n\n", text) + text = _HTML_TAG_RE.sub("", text) + # Entities are decoded after the tags are gone, so an escaped "<b>" stays text. + text = html.unescape(text) + text = _TRAILING_SPACE_RE.sub("\n", text) + text = _EXTRA_BLANK_LINES_RE.sub("\n\n", text).strip() + return text or None + + _HTTP_STATUS_FORBIDDEN = HTTPStatus.FORBIDDEN _HTTP_STATUS_BAD_REQUEST = HTTPStatus.BAD_REQUEST _HTTP_STATUS_NOT_FOUND = HTTPStatus.NOT_FOUND @@ -341,8 +373,8 @@ class GoogleBooksProvider(MetadataProvider): # Genres/categories (limit to 5) genres = volume_info.get("categories", [])[:5] - # Description (may contain HTML - leave as-is for UI to sanitize) - description = volume_info.get("description") + # Description: the API sends HTML, the UI shows text + description = _description_to_text(volume_info.get("description")) # Source URL source_url = volume_info.get("infoLink") diff --git a/tests/metadata/test_googlebooks_parse.py b/tests/metadata/test_googlebooks_parse.py index ee719dbc..56766c64 100644 --- a/tests/metadata/test_googlebooks_parse.py +++ b/tests/metadata/test_googlebooks_parse.py @@ -174,6 +174,47 @@ class TestGoogleBooksParseVolume: assert result is None + @staticmethod + def _description_of(description): + provider = GoogleBooksProvider(api_key="test-key") + result = provider._parse_volume( + { + "id": "volume-3", + "volumeInfo": {"title": "Described Book", "description": description}, + } + ) + assert result is not None + return result.description + + def test_parse_volume_turns_html_description_into_plain_text(self): + """Google Books sends HTML and the UI renders it as text: users saw a literal

.""" + description = self._description_of( + "

First paragraph.

Second, with bold & italic." + "
A new line.

" + ) + + assert description == "First paragraph.\n\nSecond, with bold & italic.\nA new line." + + def test_parse_volume_keeps_a_line_break_written_with_attributes(self): + """A `
` was dropped as a plain tag and joined the lines ("FirstSecond").""" + assert self._description_of('First
Second') == "First\nSecond" + + def test_parse_volume_keeps_an_uppercase_self_closing_line_break(self): + """Tag names are case-insensitive in HTML, and `
` is common in older blurbs.""" + assert self._description_of("First
Second") == "First\nSecond" + + def test_parse_volume_keeps_angle_brackets_that_are_not_tags(self): + """Only something shaped like a tag is removed, never text around a < or >. + + A looser `<[^>]+>` would eat "< 3 and a >" here along with the real tags. + """ + assert self._description_of("

2 < 3 and a > b

") == "2 < 3 and a > b" + + def test_parse_volume_reports_an_empty_description_as_missing(self): + """A description with nothing left to show must not render as a blank block.""" + assert self._description_of("") is None + assert self._description_of("


") is None + class _FullPageSession: def __init__(self, count):