Coverage for fpdf2_textindex / utils.py: 100.00%
18 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-24 15:45 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-04-24 15:45 +0000
1"""Utils."""
3# ruff: noqa: D103
5from collections.abc import Iterable
6import re
8from fpdf2_textindex import constants as const
11def escape_square_brackets(text: str) -> str:
12 return text.replace("[", "\\[").replace("]", "\\]")
15def insert_at_match(
16 text: str,
17 match: re.Match[str],
18 insert: str,
19 offset: int = 0,
20) -> str:
21 return (
22 text[: match.start() + offset] + insert + text[match.end() + offset :]
23 )
26def join_label_path(label_path: Iterable[str]) -> str:
27 return f" {const.PATH_DELIMITER:s} ".join(f'"{la:s}"' for la in label_path)
30def md_link(link_text: str | None, link: str) -> str:
31 link_text = escape_square_brackets(link_text or "")
32 return f"[{link_text:s}]({link:s})"
35def remove_match_from_str(
36 text: str,
37 match: re.Match[str],
38 offset: int = 0,
39) -> str:
40 return text[: match.start() + offset] + text[match.end() + offset :]
43def remove_quotes(text: str) -> str:
44 return text.strip(" '\"")
47def split_label_path(label_path_str: str) -> list[str]:
48 # Split label path and remove quotes from path elements
49 return [
50 remove_quotes(la) for la in label_path_str.split(const.PATH_DELIMITER)
51 ]