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

Function replace_html_links

scripts/doc_parsing_utils.py:433–468  ·  view source on GitHub ↗

Replace HTML links in the given text with the links from the original document. Adjust URLs for the given language code. Fail if the number of links does not match the original.

(
    text: list[str],
    links: list[HtmlLinkInfo],
    original_links: list[HtmlLinkInfo],
    lang_code: str,
)

Source from the content-addressed store, hash-verified

431
432
433def replace_html_links(
434 text: list[str],
435 links: list[HtmlLinkInfo],
436 original_links: list[HtmlLinkInfo],
437 lang_code: str,
438) -> list[str]:
439 """
440 Replace HTML links in the given text with the links from the original document.
441
442 Adjust URLs for the given language code.
443 Fail if the number of links does not match the original.
444 """
445
446 if len(links) != len(original_links):
447 raise ValueError(
448 "Number of HTML links does not match the number in the "
449 "original document "
450 f"({len(links)} vs {len(original_links)})"
451 )
452
453 modified_text = text.copy()
454 for link_index, link in enumerate(links):
455 original_link_info = original_links[link_index]
456
457 # Replace in the document text
458 replacement_link = _construct_html_link(
459 link_text=link["text"],
460 attributes=original_link_info["attributes"],
461 lang_code=lang_code,
462 )
463 line_no = link["line_no"] - 1
464 modified_text[line_no] = modified_text[line_no].replace(
465 link["full_tag"], replacement_link, 1
466 )
467
468 return modified_text
469
470
471# Multiline code blocks

Callers 1

check_translationFunction · 0.85

Calls 1

_construct_html_linkFunction · 0.85

Tested by

no test coverage detected