economist: fix parsing of <a> tags with multiple attributes

This commit is contained in:
Kovid Goyal 2025-08-05 13:44:12 +05:30
parent c4f2311ba7
commit e9c1216ded
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 16 additions and 6 deletions

View File

@ -44,7 +44,12 @@ def process_info_box(bx):
def parse_txt(ty):
typ = ty.get('type', '')
children = ty.get('children', [])
attr = ty.get('attributes', [{}])[0].get('value', '#')
href = '#'
attributes = ty.get('attributes') or ()
for a in attributes:
if a.get('name') == 'href':
href = a.get('value', href)
break
tag_map = {
'text': lambda: [ty.get('value', '')],
@ -57,12 +62,12 @@ def parse_txt(ty):
'italic': lambda: [f'<i>{"".join(parse_txt(c))}</i>' for c in children],
'linebreak': lambda: ['<br>'],
'external_link': lambda: [
f'<a href="{attr}">{"".join(parse_txt(children[0]))}</a>'
f'<a href="{href}">{"".join(parse_txt(children[0]))}</a>'
]
if children
else [],
'internal_link': lambda: [
f'<a href="{attr}">{"".join(parse_txt(children[0]))}</a>'
f'<a href="{href}">{"".join(parse_txt(children[0]))}</a>'
]
if children
else [],

View File

@ -44,7 +44,12 @@ def process_info_box(bx):
def parse_txt(ty):
typ = ty.get('type', '')
children = ty.get('children', [])
attr = ty.get('attributes', [{}])[0].get('value', '#')
href = '#'
attributes = ty.get('attributes') or ()
for a in attributes:
if a.get('name') == 'href':
href = a.get('value', href)
break
tag_map = {
'text': lambda: [ty.get('value', '')],
@ -57,12 +62,12 @@ def parse_txt(ty):
'italic': lambda: [f'<i>{"".join(parse_txt(c))}</i>' for c in children],
'linebreak': lambda: ['<br>'],
'external_link': lambda: [
f'<a href="{attr}">{"".join(parse_txt(children[0]))}</a>'
f'<a href="{href}">{"".join(parse_txt(children[0]))}</a>'
]
if children
else [],
'internal_link': lambda: [
f'<a href="{attr}">{"".join(parse_txt(children[0]))}</a>'
f'<a href="{href}">{"".join(parse_txt(children[0]))}</a>'
]
if children
else [],