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,
)
| 431 | |
| 432 | |
| 433 | def 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 |
no test coverage detected