Files
BSN-Chatsystem/fix_article_format.py
T

68 lines
2.7 KiB
Python

#!/usr/bin/env python3
"""Fix article HTML format for Article-Server compatibility.
Ensures: <p class="meta"> date, <meta name="description">, <meta name="keywords">
Run: python3 fix_article_format.py /home/hermes/workspace/artikel.html
"""
import sys, re, os
from datetime import datetime
def fix_article(filepath):
with open(filepath, 'r') as f:
content = f.read()
changed = False
# 1. Fix <div class="meta"> → <p class="meta">
if re.search(r'<div class="meta">([^<]+)</div>', content):
content = re.sub(r'<div class="meta">([^<]+)</div>', r'<p class="meta">\1</p>', content)
changed = True
print(f" ✓ <div class=\"meta\"> → <p class=\"meta\">")
# 2. Add <p class="meta"> if missing (using dateline or default)
if '<p class="meta">' not in content:
date_str = datetime.now().strftime("%d. %B %Y")
# Try to extract date from dateline
dl = re.search(r'<p class="dateline">([^<]+)</p>', content)
if dl:
date_str = dl.group(1).split('·')[0].strip()
# Insert after <body> or after <h1>
meta_line = f'<p class="meta">{date_str} · Lesezeit: ca. 5 Minuten</p>\n'
if '<h1>' in content:
content = re.sub(r'(</h1>\n)', r'\1' + meta_line, content, count=1)
else:
content = re.sub(r'(<body>\n)', r'\1' + meta_line, content, count=1)
changed = True
print(f" ✓ <p class=\"meta\"> hinzugefügt: {date_str}")
# 3. Add <meta name="description"> if missing
if '<meta name="description" content="' not in content:
title_m = re.search(r'<title>(.*?)</title>', content)
if title_m:
title = title_m.group(1).strip()
desc = title[:160] + ('' if len(title) > 160 else '')
desc_tag = f'<meta name="description" content="{desc}">\n'
content = re.sub(r'(<title>.*?</title>\n)', r'\1' + desc_tag, content, count=1)
changed = True
print(f" ✓ <meta name=\"description\"> hinzugefügt")
# 4. Add <meta name="keywords"> if missing
if '<meta name="keywords" content="' not in content:
kw_tag = '<meta name="keywords" content="Brettspiel, News, brettspiel-news.de">\n'
content = re.sub(r'(<meta name="description".*?>\n)', r'\1' + kw_tag, content, count=1)
changed = True
print(f" ✓ <meta name=\"keywords\"> hinzugefügt")
if changed:
with open(filepath, 'w') as f:
f.write(content)
return True
return False
if __name__ == '__main__':
for f in sys.argv[1:]:
print(f"{os.path.basename(f)}:")
if fix_article(f):
print(f" ✅ Gefixt")
else:
print(f" ✓ Bereits korrekt")