fix(news): unwrap CDATA titles at ingest and on /api/news
Excelsior rows store the feed's CDATA wrapper as the title text, including the surrounding whitespace. Strip a whole-title wrapper before insert and again on GET /api/news so existing rows read clean without an UPDATE. Plain titles and real HTML are unchanged.
This commit is contained in:
parent
02a9a5626b
commit
13a4049137
6 changed files with 154 additions and 1 deletions
33
app/news_title.py
Normal file
33
app/news_title.py
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
"""News title cleanup for the read path.
|
||||
|
||||
Same rule as ``news/scraper/newsScraper/feed_util.py:unwrap_cdata_title``.
|
||||
The API image does not include the scraper package, so the function is
|
||||
copied rather than imported. Existing rows are fixed here on read; this
|
||||
module does not write.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import re
|
||||
|
||||
# Whole-title CDATA wrapper only. Leading/trailing whitespace around the
|
||||
# markers belongs to the wrapper, not the title.
|
||||
_CDATA_WRAPPER = re.compile(
|
||||
r"\A\s*<!\[CDATA\[(.*?)\]\]>\s*\Z",
|
||||
re.DOTALL,
|
||||
)
|
||||
|
||||
|
||||
def unwrap_cdata_title(title: str | None) -> str | None:
|
||||
"""Strip a surrounding CDATA wrapper. Anything else is unchanged.
|
||||
|
||||
``<![CDATA[Hello]]>`` and ``\\n <![CDATA[Hello]]>`` become
|
||||
``Hello``. Real HTML (``<b>Hello</b>``) and a title that merely contains
|
||||
the markers are returned as-is. Non-strings pass through.
|
||||
"""
|
||||
if not isinstance(title, str):
|
||||
return title
|
||||
match = _CDATA_WRAPPER.match(title)
|
||||
if match is None:
|
||||
return title
|
||||
return match.group(1).strip()
|
||||
Loading…
Reference in a new issue