Skip to content

comicbox.readinglists.text ¶

Text from outside, made safe once.

A reading list’s strings come from files and from the network. Control characters in them can make a CBL that XML cannot read back, or reach a terminal as escape sequences, so they are removed when a list is loaded, with a warning naming where.

clean_text(text) ¶

Remove control characters a list’s text may not hold.

Source code in comicbox/readinglists/text.py
22
23
24
def clean_text(text: str) -> str:
    """Remove control characters a list's text may not hold."""
    return _CONTROL_RE.sub("", text)

clean_tree(value, warnings, path='') ¶

Remove control characters from every string in a parsed list.

Each string that loses any is named in warnings by its path, as the load’s other warnings are: name, entries #3.series.name.

Source code in comicbox/readinglists/text.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def clean_tree(value: Any, warnings: list[str], path: str = "") -> Any:
    """
    Remove control characters from every string in a parsed list.

    Each string that loses any is named in ``warnings`` by its path, as the
    load's other warnings are: ``name``, ``entries #3.series.name``.
    """
    if isinstance(value, str):
        cleaned = clean_text(value)
        if cleaned != value:
            warnings.append(f"{path or 'text'}: control characters removed")
        return cleaned
    if isinstance(value, Mapping):
        return {
            key: clean_tree(item, warnings, _join(path, key))
            for key, item in value.items()
        }
    if isinstance(value, list | tuple):
        return [
            clean_tree(item, warnings, f"{path} #{position}")
            for position, item in enumerate(value, start=1)
        ]
    if isinstance(value, set | frozenset):
        return {clean_tree(item, warnings, path) for item in value}
    return value

printable(text) ¶

Make outside text safe for one terminal line.

Source code in comicbox/readinglists/text.py
27
28
29
def printable(text: str) -> str:
    """Make outside text safe for one terminal line."""
    return _LINE_BREAK_RE.sub(" ", clean_text(text))