simplify: remove redundant _has_description, unused param, merge loops

- Remove `_has_description` which duplicated `_extract_description` logic;
  use truthiness of the description string instead
- Remove unused `resources` parameter from `extract_entries`
- Merge two sequential loops in `parse_readme` into a single pass over
  children to find hr, Resources, and Contributing indices

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Vinta Chen 2026-03-18 17:41:13 +08:00
parent af3baab2ed
commit 266a6b6b6c
No known key found for this signature in database
GPG Key ID: B93DE4F003C33630
2 changed files with 8 additions and 27 deletions

View File

@ -157,7 +157,6 @@ def sort_entries(entries: list[dict]) -> list[dict]:
def extract_entries(
categories: list[dict],
resources: list[dict],
groups: list[dict],
) -> list[dict]:
"""Flatten categories into individual library entries for table display."""
@ -202,7 +201,7 @@ def build(repo_root: str) -> None:
total_entries = sum(c["entry_count"] for c in categories)
groups = group_categories(categories, resources)
entries = extract_entries(categories, resources, groups)
entries = extract_entries(categories, groups)
stars_data = load_stars(website / "data" / "github_stars.json")
for entry in entries:

View File

@ -123,20 +123,6 @@ def _extract_description(nodes: list[SyntaxTreeNode]) -> str:
return ""
def _has_description(nodes: list[SyntaxTreeNode]) -> bool:
"""Check if the first node is a description paragraph (_italic text_)."""
if not nodes:
return False
first = nodes[0]
if first.type != "paragraph":
return False
for child in first.children:
if child.type == "inline" and len(child.children) == 1:
if child.children[0].type == "em":
return True
return False
def _nodes_to_raw_markdown(nodes: list[SyntaxTreeNode], source_lines: list[str]) -> str:
"""Extract raw markdown text for AST nodes using source line mappings."""
if not nodes:
@ -349,7 +335,7 @@ def _group_by_h2(
if current_name is None:
return
desc = _extract_description(current_body)
content_nodes = current_body[1:] if _has_description(current_body) else current_body
content_nodes = current_body[1:] if desc else current_body
content = _nodes_to_raw_markdown(content_nodes, source_lines)
entries = _parse_section_entries(content_nodes)
entry_count = len(entries) + sum(len(e["also_see"]) for e in entries)
@ -391,25 +377,21 @@ def parse_readme(text: str) -> tuple[list[ParsedSection], list[ParsedSection]]:
source_lines = text.split("\n")
children = root.children
# Find thematic break (---)
# Find thematic break (---), # Resources, and # Contributing in one pass
hr_idx = None
for i, node in enumerate(children):
if node.type == "hr":
hr_idx = i
break
if hr_idx is None:
return [], []
# Find # Resources and # Contributing boundaries
resources_idx = None
contributing_idx = None
for i, node in enumerate(children):
if node.type == "heading" and node.tag == "h1":
if hr_idx is None and node.type == "hr":
hr_idx = i
elif node.type == "heading" and node.tag == "h1":
text_content = _heading_text(node)
if text_content == "Resources":
resources_idx = i
elif text_content == "Contributing":
contributing_idx = i
if hr_idx is None:
return [], []
# Slice into category and resource ranges
cat_end = resources_idx or contributing_idx or len(children)