HtmlRewriter: title is cut at the first `&` (entity or bare ampersand)
I maintainer di solito rispondono entro 1 giorno
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 77/100
Direzione di ricerca
La issue individua la gestione dei titoli in HtmlRewriter e indica tests/rewriting/test_html_rewriting.py per i test. Leggi la gestione dei titoli in handle_data, handle_entityref, handle_charref e handle_endtag("title"), poi esegui i test esistenti sull’estrazione dei titoli. L’obiettivo è che i titoli mantengano il testo e le entità decodificate dopo la prima e commerciale, che il primo title continui a prevalere e che il contenuto riscritto rimanga invariato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
HtmlRewriter.rewrite(...).title only keeps the text before the first & in <title>.
from zimscraperlib.html import find_title_in
from zimscraperlib.rewriting.html import HtmlRewriter
from zimscraperlib.rewriting.url_rewriting import ArticleUrlRewriter, HttpUrl
def title(html: str) -> str:
url_rewriter = ArticleUrlRewriter(article_url=HttpUrl("https://www.example.com/page.html"))
return HtmlRewriter(url_rewriter, None, None, None).rewrite(html).title
for html in (
"<html><head><title>Tom & Jerry</title></head></html>",
"<html><head><title>Rock & Roll – My Blog</title></head></html>",
"<html><head><title>L'Illustre Maurin</title></head></html>",
):
print(repr(title(html)), "| find_title_in:", repr(find_title_in(html, "text/html")))
<title> |
HtmlRewriter(...).rewrite().title |
find_title_in (same library) |
|---|---|---|
Tom & Jerry |
'Tom' |
'Tom & Jerry' |
Rock & Roll – My Blog |
'Rock' |
'Rock & Roll – My Blog' |
L'Illustre Maurin |
'L' |
"L'Illustre Maurin" |
Tested on main (d1deb81, 5.5.3-dev0), Python 3.14.
Why it matters
warc2zim uses this value as the entry title: Rewriter.rewrite_html() returns HtmlRewriter(...).rewrite(...) and items.py does self.title, self.content = rewriter.rewrite(...). So any page whose title contains &, ', and so on gets a truncated title in the ZIM, which is what title search and suggestions show. French and Italian titles with an escaped apostrophe are an easy way to hit it.
Cause
The parser runs with convert_charrefs=False, so HTMLParser delivers a title in pieces: handle_data("Tom "), handle_entityref("amp"), handle_data(" Jerry"). handle_data stores the first piece and then ignores the rest because self.title is no longer None:
if self.html_rewrite_context == "title" and self.title is None:
self.title = data.strip()
Proposed fix
Collect the pieces while inside the first <title> and join them when it closes:
- in
handle_data, appenddatato a list instead of assigning the title; - in
handle_entityref/handle_charref, append the decoded reference (html.unescape(f"&{name};")/html.unescape(f"&#{name};")); - in
handle_endtag("title"), setself.title = "".join(parts).strip()if it isn't set yet.
The rewritten HTML is not affected, only the extracted title. "First <title> wins" stays as it is, so test_extract_title still holds. I tried this as a subclass outside the repo and it gives the same titles as find_title_in for the cases above, with identical content.
I can open a PR with this and tests in tests/rewriting/test_html_rewriting.py if the approach looks right to you.
Disclosure: I found this with the help of an AI assistant (Claude Code). The reproduction above was run locally against main.
- Lingua principale
- Python
- Stelle
- 33
- Fork
- 34
- Merge medio
- 1g 8h
- PR unite (30g)
- 24
Preparare l'ambiente
Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di openzim/python-scraperlib
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
openzim/python-scraperlib#414 ·
I maintainer di solito rispondono entro 1 giorno
-
[next major] Remove deprecated 'convert' parameter for optimize_image function inside optimization.pyForse già presa @Sriram-Nambiar l’ha presa 40 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
openzim/python-scraperlib#292 · 1 commento · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
compute_tags keeps empty tagsForse già presa @ahmershahdev l’ha presa 1 giorno fa. Aperta
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 32/100
openzim/python-scraperlib#412 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Release 5.7.0Forse già presa @benoit74 l’ha presa 4 giorni fa. Aperta
openzim/python-scraperlib#411 · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
-
colorthief is unmaintained and will break with Pillow 14Forse già presa @anshuman83-40 l’ha presa 26 giorni fa. Aperta
Difficoltà 4/5 3-5 giorni Idoneità per principianti 48/100
openzim/python-scraperlib#365 · 2 commenti · 1 assegnatario ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di openzim/python-scraperlib
Issue simili
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 74/100
vllm-project/vllm#69791 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
BCStudentSoftwareDevTeam/cas#481 ·
I maintainer di solito rispondono entro 7 giorni
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 68/100
I maintainer di solito rispondono entro 1 giorno