Remove redundant step numbers from recipe instructions in UI and database

This commit is contained in:
2026-10-17 17:08:07 +02:00
parent b86cbbeaa0
commit 814fd54d75
7 changed files with 129 additions and 2 deletions
+36
View File
@@ -0,0 +1,36 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import re
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from app.database import get_connection
sys.stdout.reconfigure(encoding='utf-8')
conn = get_connection()
cur = conn.cursor()
cur.execute("SELECT id, title, instructions FROM recipes ORDER BY id")
recipes = cur.fetchall()
def clean_instructions(text: str) -> str:
cleaned_lines = []
for line in text.split("\n"):
line_s = line.strip()
if not line_s:
continue
cleaned = re.sub(r'^(?:Schritt\s*)?\d+[\.\)\:\-]?\s*', '', line_s, flags=re.IGNORECASE)
cleaned_lines.append(cleaned)
return "\n".join(cleaned_lines)
print(f"Total recipes to preview: {len(recipes)}")
for r in recipes[:10]:
lines_before = [l.strip() for l in r['instructions'].split('\n') if l.strip()]
lines_after = [l.strip() for l in clean_instructions(r['instructions']).split('\n') if l.strip()]
print(f"\n=== ID {r['id']}: {r['title']} ===")
for b, a in zip(lines_before[:2], lines_after[:2]):
print(f" VORHER: {b[:65]}")
print(f" NACHHER: {a[:65]}")
conn.close()
+56
View File
@@ -0,0 +1,56 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Strip leading step numbers from instructions in MariaDB to avoid duplicate numbers in the UI.
"""
import os
import sys
import re
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from app.database import get_connection
sys.stdout.reconfigure(encoding='utf-8')
STEP_REGEX = re.compile(r'^(?:Schritt\s*\d+[:.]?|\d+[\.\)\:])\s*', re.IGNORECASE)
def clean_instructions_text(text: str) -> str:
if not text:
return ""
cleaned_lines = []
for line in text.split("\n"):
line_s = line.strip()
if not line_s:
continue
cleaned = STEP_REGEX.sub('', line_s)
cleaned_lines.append(cleaned)
return "\n".join(cleaned_lines)
def main():
conn = get_connection()
cur = conn.cursor()
cur.execute("SELECT id, title, instructions FROM recipes ORDER BY id")
recipes = cur.fetchall()
updated_count = 0
total_lines_cleaned = 0
for r in recipes:
original = r['instructions'] or ""
cleaned = clean_instructions_text(original)
if cleaned != original:
cur.execute("UPDATE recipes SET instructions = %s WHERE id = %s", (cleaned, r['id']))
updated_count += 1
# count cleaned lines
for orig_line in original.split("\n"):
if STEP_REGEX.match(orig_line.strip()):
total_lines_cleaned += 1
conn.commit()
conn.close()
print(f"Successfully cleaned instructions across {updated_count} of {len(recipes)} recipes.")
print(f"Total step number prefixes removed: {total_lines_cleaned}.")
if __name__ == "__main__":
main()
+8
View File
@@ -33,6 +33,14 @@ def test_routes():
assert res.status_code == 200, f"Recipe 1 failed: {res.status_code}"
print("✓ Recipe detail (/cookbook/recipe/1) OK")
# Verify no double numbers in /cookbook
import re
res_cb = client.get("/cookbook")
pattern = re.compile(r'<div class="step-text">\s*((?:Schritt\s*\d+[:.]?|\d+[\.\)\:])\s*[^<]*)', re.IGNORECASE)
matches = pattern.findall(res_cb.text)
assert len(matches) == 0, f"Found {len(matches)} double numbers in /cookbook: {matches[:3]}"
print("✓ Zero double numbers in rendered /cookbook steps confirmed!")
print("\nAll route tests passed successfully!")
if __name__ == "__main__":