Method: rich.markup.escape
Calls: 21, Exceptions: 0, Paths: 1Back
Path 1: 21 calls (1.0)
'foo ' (3) 'bar' (3) 'foo' (2) 'foo[bar]' (1) 'foo\\[bar]' (1) '[5]' (1) '\\[5]' (1) '[@foo]' (1) '[@]' (1) '[nil, [nil]]' (1)
builtin_method (21)
'foo ' (3) 'bar' (3) 'foo' (2) 'foo\\[bar]' (1) 'foo\\\\\\[bar]' (1) '[5]' (1) '\\[5]' (1) '\\[@foo]' (1) '\\[@]' (1) '[nil, \\[nil]]' (1)
1def escape(
2 markup: str,
3 _escape: _EscapeSubMethod = re.compile(r"(\\*)(\[[a-z#/@][^[]*?])").sub,
4) -> str:
5 """Escapes text so that it won't be interpreted as markup.
6
7 Args:
8 markup (str): Content to be inserted in to markup.
9
10 Returns:
11 str: Markup with square brackets escaped.
12 """
13
14 def escape_backslashes(match: Match[str]) -> str:
15 """Called by re.sub replace matches."""
16 backslashes, text = match.groups()
17 return f"{backslashes}{backslashes}\\{text}"
18
19 markup = _escape(escape_backslashes, markup)
20 return markup