MCPcopy Create free account
hub / github.com/fastapi/fastapi / extract_html_links

Function extract_html_links

scripts/doc_parsing_utils.py:357–403  ·  view source on GitHub ↗

Extract all HTML links from the given lines. Return list of HtmlLinkInfo, where each dict contains: - `line_no` - line number (1-based) - `full_tag` - full HTML link tag - `attributes` - list of HTMLLinkAttribute (name, quote, value) - `text` - link text

(lines: list[str])

Source from the content-addressed store, hash-verified

355
356
357def extract_html_links(lines: list[str]) -> list[HtmlLinkInfo]:
358 """
359 Extract all HTML links from the given lines.
360
361 Return list of HtmlLinkInfo, where each dict contains:
362 - `line_no` - line number (1-based)
363 - `full_tag` - full HTML link tag
364 - `attributes` - list of HTMLLinkAttribute (name, quote, value)
365 - `text` - link text
366 """
367
368 links = []
369 for line_no, line in enumerate(lines, start=1):
370 for html_link in HTML_LINK_RE.finditer(line):
371 link_str = html_link.group(0)
372
373 link_text_match = HTML_LINK_TEXT_RE.match(link_str)
374 assert link_text_match is not None
375 link_text = link_text_match.group(2)
376 assert isinstance(link_text, str)
377
378 link_open_tag_match = HTML_LINK_OPEN_TAG_RE.match(link_str)
379 assert link_open_tag_match is not None
380 link_open_tag = link_open_tag_match.group(1)
381 assert isinstance(link_open_tag, str)
382
383 attributes: list[HTMLLinkAttribute] = []
384 for attr_name, attr_quote, attr_value in re.findall(
385 HTML_ATTR_RE, link_open_tag
386 ):
387 assert isinstance(attr_name, str)
388 assert isinstance(attr_quote, str)
389 assert isinstance(attr_value, str)
390 attributes.append(
391 HTMLLinkAttribute(
392 name=attr_name, quote=attr_quote, value=attr_value
393 )
394 )
395 links.append(
396 HtmlLinkInfo(
397 line_no=line_no,
398 full_tag=link_str,
399 attributes=attributes,
400 text=link_text,
401 )
402 )
403 return links
404
405
406def _construct_html_link(

Callers 1

check_translationFunction · 0.85

Calls 2

HTMLLinkAttributeClass · 0.85
HtmlLinkInfoClass · 0.85

Tested by

no test coverage detected