Extract calculator teaser metadata
This commit is contained in:
43
migrate.py
43
migrate.py
@@ -16,24 +16,37 @@ def parse_calculators_list():
|
||||
lines = f.readlines()
|
||||
|
||||
in_table = False
|
||||
header_map = {}
|
||||
for line in lines:
|
||||
if line.startswith('## Backlog'):
|
||||
if in_table and line.startswith('## '):
|
||||
break
|
||||
if '| Calculator Name' in line:
|
||||
in_table = True
|
||||
headers = [p.strip() for p in line.strip().strip('|').split('|')]
|
||||
header_map = {header: idx for idx, header in enumerate(headers)}
|
||||
continue
|
||||
if in_table and line.startswith('| :---'):
|
||||
continue
|
||||
if in_table and line.startswith('|'):
|
||||
parts = [p.strip() for p in line.split('|')]
|
||||
if len(parts) >= 6:
|
||||
name = parts[1]
|
||||
slug = parts[4]
|
||||
factor_raw = parts[5]
|
||||
parts = [p.strip() for p in line.strip().strip('|').split('|')]
|
||||
name_idx = header_map.get('Calculator Name')
|
||||
slug_idx = header_map.get('Slug')
|
||||
factor_idx = header_map.get('Conversion Factor')
|
||||
if None not in (name_idx, slug_idx, factor_idx) and len(parts) > max(name_idx, slug_idx, factor_idx):
|
||||
name = parts[name_idx]
|
||||
slug = parts[slug_idx]
|
||||
factor_raw = parts[factor_idx]
|
||||
active_calcs.append((name, slug, factor_raw))
|
||||
|
||||
return active_calcs
|
||||
|
||||
def split_name_and_teaser(name):
|
||||
parts = re.split(r'\s[–—-]\s', name, maxsplit=1)
|
||||
if len(parts) == 2:
|
||||
return parts[0].strip(), parts[1].strip()
|
||||
return name.strip(), ''
|
||||
|
||||
|
||||
def split_conversion_name(name):
|
||||
parts = re.split(r'\s+to\s+', name, maxsplit=1, flags=re.IGNORECASE)
|
||||
if len(parts) == 2:
|
||||
@@ -67,17 +80,20 @@ def process():
|
||||
|
||||
calculators_ts_entries = []
|
||||
|
||||
for name, slug, factor_raw in active_rows:
|
||||
if name == 'Calculator Name' or not slug: continue
|
||||
|
||||
for raw_name, slug, factor_raw in active_rows:
|
||||
if raw_name == 'Calculator Name' or not slug:
|
||||
continue
|
||||
|
||||
display_name, teaser = split_name_and_teaser(raw_name)
|
||||
|
||||
# Name splitting
|
||||
parsed = split_conversion_name(name)
|
||||
parsed = split_conversion_name(display_name)
|
||||
if parsed:
|
||||
in1, in2 = parsed
|
||||
else:
|
||||
in1, in2 = "From", "To"
|
||||
|
||||
category = guess_category(name)
|
||||
category = guess_category(display_name)
|
||||
desc_html = external_descriptions.get(slug, "")
|
||||
|
||||
c_type = 'standard'
|
||||
@@ -135,10 +151,12 @@ def process():
|
||||
# Avoid escaping single quotes by using JSON or dict
|
||||
entry = {
|
||||
'slug': slug,
|
||||
'name': name,
|
||||
'name': display_name,
|
||||
'category': category,
|
||||
'type': c_type
|
||||
}
|
||||
if teaser:
|
||||
entry['teaser'] = teaser
|
||||
|
||||
# Determine labels
|
||||
labels = {'in1': in1, 'in2': in2}
|
||||
@@ -258,6 +276,7 @@ export interface CalculatorDef {
|
||||
toBase?: number;
|
||||
labels: { in1: string; in2: string; in3?: string };
|
||||
descriptionHTML?: string;
|
||||
teaser?: string;
|
||||
}
|
||||
|
||||
export const categories: Record<string, { label: string; icon: string }> = {
|
||||
|
||||
Reference in New Issue
Block a user