This commit is contained in:
Codex
2026-03-07 09:08:27 +00:00
parent 97c372d942
commit 824c270f95
2 changed files with 63 additions and 54 deletions

View File

@@ -1,7 +1,10 @@
import json
import re
from pathlib import Path
CALCLIST = '/home/ben/Seafile/Storage/Docs/howdoyouconvert/calculators_list.md'
BASE_DIR = Path(__file__).resolve().parent
CALCLIST = BASE_DIR / 'calculators_list.md'
OUTPUT_FILE = BASE_DIR / 'hdyc-svelte/src/lib/data/calculators.ts'
def load_external_descriptions():
# Placeholder for future enrichment sources.
@@ -31,6 +34,12 @@ def parse_calculators_list():
return active_calcs
def split_conversion_name(name):
parts = re.split(r'\s+to\s+', name, maxsplit=1, flags=re.IGNORECASE)
if len(parts) == 2:
return parts[0].strip(), parts[1].strip()
return None
def guess_category(name):
name_l = name.lower()
if any(x in name_l for x in ['meter', 'inch', 'feet', 'yard', 'mile', 'cable', 'fathom', 'rod', 'chain', 'nautical', 'league']): return 'length'
@@ -62,9 +71,9 @@ def process():
if name == 'Calculator Name' or not slug: continue
# Name splitting
parts = name.split(' to ')
if len(parts) == 2:
in1, in2 = parts[0].strip(), parts[1].strip()
parsed = split_conversion_name(name)
if parsed:
in1, in2 = parsed
else:
in1, in2 = "From", "To"
@@ -219,9 +228,9 @@ def process():
for e in calculators_ts_entries:
# Check if inverse exists. We hide the one with the smaller factor (usually < 1) or hide alphabetical later one.
# But a better heuristic: reverse of split(' to ')
parts = e['name'].split(' to ')
if len(parts) == 2:
rev_name = f"{parts[1]} to {parts[0]}"
parsed = split_conversion_name(e['name'])
if parsed:
rev_name = f"{parsed[1]} to {parsed[0]}"
rev_slug = rev_name.lower().replace(' ', '-')
if rev_slug in existing_slugs and e['slug'] != rev_slug:
# hide one of them. We'll hide the one where factor < 1, or if both 1, arbitrarily
@@ -314,7 +323,7 @@ export function searchCalculators(query: string): CalculatorDef[] {
);
}
"""
with open('/home/ben/Seafile/Storage/Docs/howdoyouconvert/hdyc-svelte/src/lib/data/calculators.ts', 'w') as f:
with open(OUTPUT_FILE, 'w', encoding='utf-8') as f:
f.write(out)
print(f"Generated {len(calculators_ts_entries)} calculators into calculators.ts")