mirror of
https://github.com/SpudGunMan/meshing-around.git
synced 2026-10-30 22:01:57 +01:00
Update wiki.py
Fixed Non-Prose Content Clutter Fixed Encoding/Mojibake Issue These changes ensure that when users run a local Kiwix server with useKiwixServer = True, they'll receive clean, properly-encoded article summaries https: //github.com/SpudGunMan/meshing-around/issues/334 Co-Authored-By: va3vbe <209848734+va3vbe@users.noreply.github.com>
This commit is contained in:
1 parent
2bae1b98ba
commit
1cf63f40fe
1 file changed
+17
@@ -26,6 +26,21 @@ def text_from_html(body):
|
||||
main = soup.body
|
||||
if not main:
|
||||
return ""
|
||||
|
||||
# Remove non-prose elements that pollute the summary: infoboxes,
|
||||
# hatnotes/redirect notices, navigation boxes, citation markers, edit links.
|
||||
for unwanted in main.select('table.infobox, table.sidebar, div.hatnote, div.navbox, div.metadata, sup.reference, .mw-editsection'):
|
||||
unwanted.decompose()
|
||||
|
||||
# Prefer extracting from <p> tags (actual lead paragraphs)
|
||||
paragraphs = main.find_all('p')
|
||||
if paragraphs:
|
||||
texts = [p.get_text(" ", strip=True) for p in paragraphs]
|
||||
texts = [t for t in texts if t]
|
||||
if texts:
|
||||
return " ".join(texts)
|
||||
|
||||
# Fallback to existing full-text behavior if no <p> tags found
|
||||
texts = main.find_all(string=True)
|
||||
visible_texts = filter(tag_visible, texts)
|
||||
return " ".join(t.strip() for t in visible_texts if t.strip())
|
||||
@@ -49,6 +64,8 @@ def get_kiwix_summary(search_term, truncate=True):
|
||||
continue
|
||||
article_url = f"{kiwix_url}{a['href']}"
|
||||
article_response = requests.get(article_url, timeout=urlTimeoutSeconds)
|
||||
# Force UTF-8 encoding to avoid mojibake on multi-byte characters
|
||||
article_response.encoding = 'utf-8'
|
||||
if article_response.status_code == 200:
|
||||
text = text_from_html(article_response.text)
|
||||
# Remove navigation and search jump text
|
||||
|
||||
Reference in new issue
Block a user