-
Notifications
You must be signed in to change notification settings - Fork 52
/
escape.py
298 lines (224 loc) · 10.7 KB
/
escape.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
"""Markdown escaper."""
# pyright: reportAssignmentType=false
from __future__ import annotations
import re
import string
from collections.abc import Callable, Iterable, Mapping
from functools import wraps
from typing import (
Any,
Concatenate,
ParamSpec,
Self,
SupportsIndex,
TypeVar,
cast,
no_type_check,
)
__all__ = ['MarkdownString', 'markdown_escape']
_P = ParamSpec('_P')
_ListOrDict = TypeVar('_ListOrDict', list, dict)
#: Based on the ASCII punctuation list in the CommonMark spec at
#: https://spec.commonmark.org/0.30/#backslash-escapes
markdown_escape_re = re.compile(r"""([\[\\\]{|}\(\)`~!@#$%^&*=+;:'"<>/,.?_-])""")
#: Unescape regex has a `\` prefix and the same characters
markdown_unescape_re = re.compile(r"""\\([\[\\\]{|}\(\)`~!@#$%^&*=+;:'"<>/,.?_-])""")
class _MarkdownEscapeFormatter(string.Formatter):
"""Support class for :meth:`MarkdownString.format`."""
__slots__ = ('escape',)
def __init__(self, escape: Callable[[Any], MarkdownString]) -> None:
self.escape = escape
super().__init__()
def format_field(self, value: Any, format_spec: str) -> str:
if hasattr(value, '__markdown_format__'):
rv = value.__markdown_format__(format_spec)
elif hasattr(value, '__markdown__'):
if format_spec:
raise ValueError(
f"Format specifier {format_spec} given, but {type(value)} does not"
" define __markdown_format__. A class that defines __markdown__"
" must define __markdown_format__ to work with format specifiers."
)
rv = value.__markdown__()
else:
# We need to make sure the format spec is str here as
# otherwise the wrong callback methods are invoked.
rv = string.Formatter.format_field(self, value, str(format_spec))
return str(self.escape(rv))
class _MarkdownEscapeHelper:
"""Helper for :meth:`MarkdownString.__mod__`."""
__slots__ = ('obj', 'escape')
def __init__(self, obj: Any, escape: Callable[[Any], MarkdownString]) -> None:
self.obj = obj
self.escape = escape
def __getitem__(self, item: Any) -> Self:
return self.__class__(self.obj[item], self.escape)
def __str__(self) -> str:
return str(self.escape(self.obj))
def __repr__(self) -> str:
return str(self.escape(repr(self.obj)))
def __int__(self) -> int:
return int(self.obj)
def __float__(self) -> float:
return float(self.obj)
def _escape_argspec(
obj: _ListOrDict,
iterable: Iterable[tuple[Any, object]],
escape: Callable[[Any], MarkdownString],
) -> _ListOrDict:
"""Escape all arguments."""
for key, value in iterable:
if isinstance(value, str) or hasattr(value, '__markdown__'):
obj[key] = escape(value)
return obj
@no_type_check # For TypeGuard, since this is used within class definition below
def _simple_escaping_wrapper(
func: Callable[Concatenate[Any, _P], str],
) -> Callable[Concatenate[Any, _P], MarkdownString]:
@wraps(func)
def wrapped(self: Any, *args: _P.args, **kwargs: _P.kwargs) -> MarkdownString:
_escape_argspec(cast(list, args), enumerate(args), self.escape)
_escape_argspec(kwargs, kwargs.items(), self.escape)
return self.__class__(func(self, *args, **kwargs))
return wrapped
class MarkdownString(str):
"""Markdown string, implements a __markdown__ method."""
__slots__ = ()
def __new__(
cls, base: Any = '', encoding: str | None = None, errors: str = 'strict'
) -> Self:
if hasattr(base, '__markdown__'):
base = base.__markdown__()
if encoding is None:
return super().__new__(cls, base)
return super().__new__(cls, base, encoding, errors)
def __markdown__(self) -> Self:
"""Return a markdown embed-compatible string."""
return self
def __markdown_format__(self, format_spec: str) -> Self:
if format_spec:
# MarkdownString cannot support format_spec because any manipulation may
# remove an escape char, causing downstream damage with unwanted formatting
raise ValueError("Unsupported format specification for MarkdownString.")
return self
def unescape(self) -> str:
"""Unescape the string."""
return markdown_unescape_re.sub(r'\1', str(self))
@classmethod
def escape(cls, text: str, silent: bool = True) -> Self:
"""Escape a string, for internal use only. Use :func:`markdown_escape`."""
if silent and text is None:
return cls('') # type: ignore[unreachable]
if callable(dunder_markdown := getattr(text, '__markdown__', None)):
return cls(dunder_markdown())
return cls(markdown_escape_re.sub(r'\\\1', text))
# These additional methods are borrowed from the implementation in MarkupSafe
def __add__(self, other: Any) -> Self:
if isinstance(other, str) or hasattr(other, '__markdown__'):
return self.__class__(super().__add__(self.escape(other)))
return NotImplemented
def __radd__(self, other: Any) -> Self:
if isinstance(other, str) or hasattr(other, '__markdown__'):
return self.escape(other).__add__(self)
return NotImplemented
def __mul__(self, num: SupportsIndex) -> Self:
if isinstance(num, int):
return self.__class__(super().__mul__(num))
return NotImplemented
__rmul__ = __mul__
def __mod__(self, arg: Any) -> Self:
"""Apply legacy `str % arg(s)` formatting."""
if isinstance(arg, tuple):
# a tuple of arguments, each wrapped
arg = tuple(_MarkdownEscapeHelper(x, self.escape) for x in arg)
elif hasattr(type(arg), '__getitem__') and not isinstance(arg, str):
# a mapping of arguments, wrapped
arg = _MarkdownEscapeHelper(arg, self.escape)
else:
# a single argument, wrapped with the helper and a tuple
arg = (_MarkdownEscapeHelper(arg, self.escape),)
return self.__class__(super().__mod__(arg))
def __repr__(self) -> str:
return f'{self.__class__.__name__}({super().__repr__()})'
def join(self, iterable: Iterable[str]) -> Self:
return self.__class__(super().join(map(self.escape, iterable)))
join.__doc__ = str.join.__doc__
def split( # type: ignore[override]
self, sep: str | None = None, maxsplit: SupportsIndex = -1
) -> list[Self]:
return [self.__class__(v) for v in super().split(sep, maxsplit)]
split.__doc__ = str.split.__doc__
def rsplit( # type: ignore[override]
self, sep: str | None = None, maxsplit: SupportsIndex = -1
) -> list[Self]:
return [self.__class__(v) for v in super().rsplit(sep, maxsplit)]
rsplit.__doc__ = str.rsplit.__doc__
def splitlines( # type: ignore[override]
self, keepends: bool = False
) -> list[Self]:
return [self.__class__(v) for v in super().splitlines(keepends)]
splitlines.__doc__ = str.splitlines.__doc__
__getitem__ = _simple_escaping_wrapper(str.__getitem__)
capitalize = _simple_escaping_wrapper(str.capitalize) # type: ignore[assignment]
title = _simple_escaping_wrapper(str.title) # type: ignore[assignment]
lower = _simple_escaping_wrapper(str.lower) # type: ignore[assignment]
upper = _simple_escaping_wrapper(str.upper) # type: ignore[assignment]
replace = _simple_escaping_wrapper(str.replace)
ljust = _simple_escaping_wrapper(str.ljust)
rjust = _simple_escaping_wrapper(str.rjust)
lstrip = _simple_escaping_wrapper(str.lstrip)
rstrip = _simple_escaping_wrapper(str.rstrip)
center = _simple_escaping_wrapper(str.center)
strip = _simple_escaping_wrapper(str.strip)
translate = _simple_escaping_wrapper(str.translate)
expandtabs = _simple_escaping_wrapper(str.expandtabs) # type: ignore[assignment]
swapcase = _simple_escaping_wrapper(str.swapcase) # type: ignore[assignment]
zfill = _simple_escaping_wrapper(str.zfill)
casefold = _simple_escaping_wrapper(str.casefold) # type: ignore[assignment]
removeprefix = _simple_escaping_wrapper(str.removeprefix)
removesuffix = _simple_escaping_wrapper(str.removesuffix)
def partition(self, sep: str) -> tuple[Self, Self, Self]:
left, sep, right = super().partition(self.escape(sep))
cls = self.__class__
return cls(left), cls(sep), cls(right)
partition.__doc__ = str.partition.__doc__
def rpartition(self, sep: str) -> tuple[Self, Self, Self]:
left, sep, right = super().rpartition(self.escape(sep))
cls = self.__class__
return cls(left), cls(sep), cls(right)
rpartition.__doc__ = str.rpartition.__doc__
def format(self, *args: Any, **kwargs: Any) -> Self:
formatter = _MarkdownEscapeFormatter(self.escape)
return self.__class__(formatter.vformat(self, args, kwargs))
format.__doc__ = str.format.__doc__
# pylint: disable=redefined-builtin
def format_map(
self,
map: Mapping[str, Any], # type: ignore[override] # noqa: A002
) -> Self:
formatter = _MarkdownEscapeFormatter(self.escape)
return self.__class__(formatter.vformat(self, (), map))
format_map.__doc__ = str.format_map.__doc__
def markdown_escape(text: str) -> MarkdownString:
"""
Escape all Markdown formatting characters and strip whitespace at ends.
As per the CommonMark spec, all ASCII punctuation can be escaped with a backslash
and compliant parsers will then render the punctuation mark as a literal character.
However, escaping any other character will cause the backslash to be rendered. This
escaper therefore targets only ASCII punctuation characters listed in the spec.
Edge whitespace is significant in Markdown:
* Four spaces at the start will initiate a code block
* Two spaces at the end will cause a line-break in non-GFM Markdown
The space and tab characters cannot be escaped, and replacing spaces with is
not suitable because non-breaking spaces affect HTML rendering, specifically the
CSS ``white-space: normal`` sequence collapsing behaviour. Since there is no way to
predict adjacent whitespace when this escaped variable is placed in a Markdown
document, it is safest to strip all edge whitespace.
..note::
This function strips edge whitespace and calls :meth:`MarkdownString.escape`,
and should be preferred over calling :meth:`MarkdownString.escape` directly.
That classmethod is internal to :class:`MarkdownString`.
:returns: Escaped text as an instance of :class:`MarkdownString`, to avoid
double-escaping
"""
return MarkdownString.escape(text.strip())