mirror of
https://github.com/calibrain/shelfmark.git
synced 2026-11-16 22:48:12 +00:00
fix(googlebooks): show descriptions as plain text, not HTML (#1723)
With Google Books as the metadata provider, every description shows its
HTML, starting with a literal `<p>`. The API returns HTML,
`_parse_volume` passes it on with the comment "leave as-is for UI to
sanitize", and the UI renders `{book.description}` as text in both
`ReleaseModal.tsx` and `DetailsModal.tsx`.
This turns the description into plain text in the provider: `<br>`
becomes a line break, the end of a block element a blank line, other
tags are dropped and entities decoded. The details modal uses
`whitespace-pre-line`, so the breaks survive there. Text that only looks
like a tag is left alone (`2 < 3 and a > b`).
Tests are in `tests/metadata/test_googlebooks_parse.py`. With the source
change reverted all three fail. `make python-checks` is clean and
`tests/metadata` passes.
I did not reuse `_blurb_to_text` from the DNB provider: it is specific
to DNB blurb pages.
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->
## Summary by CodeRabbit
* **Bug Fixes**
* Google Books descriptions are cleaned up for display: HTML formatting
is removed, entities are decoded, and paragraph breaks are preserved.
Empty descriptions are treated as missing.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
1 parent
5472c36c56
commit
7f4d20ed9d
2 files changed
+75
-2
No files matched your search
@@ -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 "</", so "2 < 3 and a > b" is left alone.
|
||||
_HTML_TAG_RE = re.compile(r"</?[a-zA-Z][^<>]*>")
|
||||
# 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"<br\b[^<>]*>", re.IGNORECASE)
|
||||
_BLOCK_END_RE = re.compile(r"</(?:p|div|li|ul|ol|h[1-6]|blockquote)\s*>", 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 "<p>". 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")
|
||||
|
||||
@@ -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 <p>."""
|
||||
description = self._description_of(
|
||||
"<p>First paragraph.</p><p>Second, with <b>bold</b> & <i>italic</i>."
|
||||
"<br>A new line.</p>"
|
||||
)
|
||||
|
||||
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 `<br class="x">` was dropped as a plain tag and joined the lines ("FirstSecond")."""
|
||||
assert self._description_of('First<br class="x">Second') == "First\nSecond"
|
||||
|
||||
def test_parse_volume_keeps_an_uppercase_self_closing_line_break(self):
|
||||
"""Tag names are case-insensitive in HTML, and `<BR/>` is common in older blurbs."""
|
||||
assert self._description_of("First<BR/>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("<p>2 < 3 and a > b</p>") == "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("<p> </p><br/>") is None
|
||||
|
||||
|
||||
class _FullPageSession:
|
||||
def __init__(self, count):
|
||||
|
||||
Reference in new issue
Block a user