mirror of
https://github.com/vinta/awesome-python.git
synced 2026-05-09 22:53:49 +08:00
refactor(readme_parser): fuse _parse_sponsor_item into single pass
Eliminate the redundant _find_link_deep precheck by merging the two walks over inline.children into one loop that simultaneously locates the link and records its top-level index. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
+11
-16
@@ -360,22 +360,17 @@ def _find_link_deep(node: SyntaxTreeNode) -> SyntaxTreeNode | None:
|
|||||||
|
|
||||||
def _parse_sponsor_item(inline: SyntaxTreeNode) -> ParsedSponsor | None:
|
def _parse_sponsor_item(inline: SyntaxTreeNode) -> ParsedSponsor | None:
|
||||||
"""Parse `**[name](url)**: description` (or `[name](url) - description`)."""
|
"""Parse `**[name](url)**: description` (or `[name](url) - description`)."""
|
||||||
link = _find_link_deep(inline)
|
for split_idx, child in enumerate(inline.children):
|
||||||
if link is None:
|
link = child if child.type == "link" else _find_link_deep(child)
|
||||||
return None
|
if link is None:
|
||||||
name = render_inline_text(link.children)
|
continue
|
||||||
url = _href(link)
|
desc_html = render_inline_html(inline.children[split_idx + 1 :])
|
||||||
|
return ParsedSponsor(
|
||||||
split_idx = None
|
name=render_inline_text(link.children),
|
||||||
for i, child in enumerate(inline.children):
|
url=_href(link),
|
||||||
if child is link or _find_link_deep(child) is link:
|
description=_SPONSOR_SEP_RE.sub("", desc_html),
|
||||||
split_idx = i
|
)
|
||||||
break
|
return None
|
||||||
if split_idx is None:
|
|
||||||
return None
|
|
||||||
desc_html = render_inline_html(inline.children[split_idx + 1 :])
|
|
||||||
desc_html = _SPONSOR_SEP_RE.sub("", desc_html)
|
|
||||||
return ParsedSponsor(name=name, url=url, description=desc_html)
|
|
||||||
|
|
||||||
|
|
||||||
def parse_sponsors(text: str) -> list[ParsedSponsor]:
|
def parse_sponsors(text: str) -> list[ParsedSponsor]:
|
||||||
|
|||||||
Reference in New Issue
Block a user