Escapes text so that it won't be interpreted as markup. Args: markup (str): Content to be inserted in to markup. Returns: str: Markup with square brackets escaped.
(
markup: str,
_escape: _EscapeSubMethod = re.compile(r"(\\*)(\[[a-z#/@][^[]*?])").sub,
)
| 46 | |
| 47 | |
| 48 | def escape( |
| 49 | markup: str, |
| 50 | _escape: _EscapeSubMethod = re.compile(r"(\\*)(\[[a-z#/@][^[]*?])").sub, |
| 51 | ) -> str: |
| 52 | """Escapes text so that it won't be interpreted as markup. |
| 53 | |
| 54 | Args: |
| 55 | markup (str): Content to be inserted in to markup. |
| 56 | |
| 57 | Returns: |
| 58 | str: Markup with square brackets escaped. |
| 59 | """ |
| 60 | |
| 61 | def escape_backslashes(match: Match[str]) -> str: |
| 62 | """Called by re.sub replace matches.""" |
| 63 | backslashes, text = match.groups() |
| 64 | return f"{backslashes}{backslashes}\\{text}" |
| 65 | |
| 66 | markup = _escape(escape_backslashes, markup) |
| 67 | if markup.endswith("\\") and not markup.endswith("\\\\"): |
| 68 | return markup + "\\" |
| 69 | |
| 70 | return markup |
| 71 | |
| 72 | |
| 73 | def _parse(markup: str) -> Iterable[Tuple[int, Optional[str], Optional[Tag]]]: |
no outgoing calls