MCPcopy
hub / github.com/vinta/awesome-python / _parse_grouped_sections

Function _parse_grouped_sections

website/readme_parser.py:308–360  ·  view source on GitHub ↗

Parse nodes into groups of categories using bold markers as group boundaries. Bold-only paragraphs (**Group Name**) delimit groups. H3 headings under each bold marker become categories within that group. Categories appearing before any bold marker go into an "Other" group.

(
    nodes: list[SyntaxTreeNode],
)

Source from the content-addressed store, hash-verified

306
307
308def _parse_grouped_sections(
309 nodes: list[SyntaxTreeNode],
310) -> list[ParsedGroup]:
311 """Parse nodes into groups of categories using bold markers as group boundaries.
312
313 Bold-only paragraphs (**Group Name**) delimit groups. H3 headings under each
314 bold marker become categories within that group. Categories appearing before
315 any bold marker go into an "Other" group.
316 """
317 groups: list[ParsedGroup] = []
318 current_group_name: str | None = None
319 current_group_cats: list[ParsedSection] = []
320 current_cat_name: str | None = None
321 current_cat_body: list[SyntaxTreeNode] = []
322
323 def flush_cat() -> None:
324 nonlocal current_cat_name
325 if current_cat_name is None:
326 return
327 current_group_cats.append(_build_section(current_cat_name, current_cat_body))
328 current_cat_name = None
329
330 def flush_group() -> None:
331 nonlocal current_group_name, current_group_cats
332 if current_group_cats:
333 name = current_group_name or "Other"
334 groups.append(
335 ParsedGroup(
336 name=name,
337 slug=slugify(name),
338 categories=list(current_group_cats),
339 )
340 )
341 current_group_name = None
342 current_group_cats = []
343
344 for node in nodes:
345 bold_name = _is_bold_marker(node)
346 if bold_name is not None:
347 flush_cat()
348 flush_group()
349 current_group_name = bold_name
350 current_cat_body = []
351 elif node.type == "heading" and node.tag in ("h2", "h3"):
352 flush_cat()
353 current_cat_name = _heading_text(node)
354 current_cat_body = []
355 elif current_cat_name is not None:
356 current_cat_body.append(node)
357
358 flush_cat()
359 flush_group()
360 return groups
361
362
363_SPONSOR_SEP_RE = re.compile(r"^\s*[:\-\u2013\u2014]\s*")

Callers 1

parse_readmeFunction · 0.85

Calls 4

_is_bold_markerFunction · 0.85
flush_catFunction · 0.85
flush_groupFunction · 0.85
_heading_textFunction · 0.85

Tested by

no test coverage detected