-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
doc_examples.py
332 lines (262 loc) · 8.49 KB
/
doc_examples.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
"""Test examples in docs.
Note: heavily inspired by https://github.com/koaning/mktestdocs
What I've written here is kind of a mess.
I'm working on rewriting it and PR'ing it upstream:
https://github.com/maxb2/mktestdocs/tree/feat/superfences
For now, it works.
"""
# NOTE: using "from __future__ import annotations" completely breaks
# this file for some reason.
import os
import re
from collections import OrderedDict
from pathlib import Path
from subprocess import run
from tempfile import TemporaryDirectory
from textwrap import dedent
from typing import Any, Dict, List, NamedTuple, Optional, Tuple
CLASS_RE = re.compile(
dedent(
r"""
[ \t]*
\.
(?P<class>[a-zA-Z][a-zA-Z0-9_\-]*)
[ \t]*
"""
),
re.DOTALL | re.VERBOSE,
)
ID_RE = re.compile(
dedent(
r"""
[ \t]*
\#
(?P<id>[a-zA-Z][a-zA-Z0-9_\-]*)
[ \t]*
"""
),
re.DOTALL | re.VERBOSE,
)
KEY_VAL_RE = re.compile(
dedent(
r"""
[ \t]*
(?P<key>\b[a-zA-Z][a-zA-Z0-9_]*)
(?:
=
(?P<quot>"|')
(?P<value>.*?)
(?P=quot)
)?
[ \t]*
"""
),
re.DOTALL | re.VERBOSE,
)
# NOTE: this is modified from
# `markdown.extensions.fenced_code.FencedBlockPreprocessor.FENCED_BLOCK_RE`
# to include options from `pymdownx/superfences.RE_OPTIONS`
FENCED_BLOCK_RE = re.compile(
dedent(
r"""
(?P<raw>
(?P<fence>^(?:~{3,}|`{3,}))[ ]* # opening fence
(
(\{(?P<attrs>[^\}\n]*)\})| # (optional {attrs} or
(\.?(?P<lang>[\w#.+-]*)[ ]*)? # optional (.)lang
(?P<options> # optional "options"
(?: # key-value pairs
(?:
\b[a-zA-Z][a-zA-Z0-9_]*
(?:
=
(?P<quot>"|')
.*?
(?P=quot)
)?
[ \t]*
) |
)*
)
)
\n # newline (end of opening fence)
(?P<code>.*?)(?<=\n) # the code block
(?P=fence)[ ]*$ # closing fence
)
"""
),
re.MULTILINE | re.DOTALL | re.VERBOSE,
)
class Attr(NamedTuple):
"""Attribute in a fence."""
value: str
type_: str
# @dataclass
class Fence(NamedTuple):
"""Markdown Fence."""
fence: str = ""
lang: Optional[str] = None
attrs: "Optional[OrderedDict[str, Attr]]" = None
options: Optional[Dict[str, Any]] = None
contents: str = ""
raw: Optional[str] = None
def options_from_str(raw: str) -> Dict[str, Any]:
"""Markdown fence options dict from string.
Args:
raw (str): string of options
Returns:
Dict[str, Any]: dict of options
"""
options = {}
while raw:
match = KEY_VAL_RE.match(raw)
if match is None:
break
options[match.groupdict()["key"]] = match.groupdict()["value"]
raw = raw[match.span()[1] :]
return options
def attrs_from_str(raw: str) -> "OrderedDict[str, Attr]":
"""Markdown fence attributes from string.
Args:
raw (str): string of attributes
Returns:
Dict[str, Any]: dict of attrs
"""
attrs = OrderedDict()
while raw:
if match := CLASS_RE.match(raw):
attrs[match.groupdict()["class"]] = Attr(value=None, type_="class")
elif match := ID_RE.match(raw):
attrs[match.groupdict()["id"]] = Attr(value=None, type_="id")
elif match := KEY_VAL_RE.match(raw):
attrs[match.groupdict()["key"]] = Attr(
value=match.groupdict()["value"], type_="keyval"
)
else:
break
raw = raw[match.span()[1] :]
return attrs
def from_re_groups(groups: Tuple[str]) -> "Fence":
"""Make Fence from regex groups.
Notes:
This is tightly coupled to `FENCED_BLOCK_RE`.
Args:
groups (Tuple[str]): regex match groups
Returns:
Fence: markdown fence
"""
attrs = Fence.attrs_from_str(groups[4])
try:
lang_attr = list(attrs.items())[0]
_lang = lang_attr[0] if lang_attr[1].type_ == "class" else None
except IndexError:
_lang = None
lang = groups[6] or _lang
return Fence(
fence=groups[1],
lang=lang,
attrs=attrs,
options=Fence.options_from_str(groups[7]),
contents=groups[9],
raw=groups[0],
)
def from_str(raw: str) -> "Fence":
"""Fence from markdown string.
Args:
raw (str): markdown string
Raises:
Exception: couldn't find a markdown fence
Returns:
Fence: markdown fence
"""
return Fence.from_re_groups(FENCED_BLOCK_RE.match(raw).groups())
class WorkingDirectory:
"""Sets the cwd within the context."""
def __init__(self, path: Path) -> None:
self.path = path
self.origin = Path().absolute()
def __enter__(self):
"""Enter context."""
os.chdir(self.path)
def __exit__(self, exc_type, exc_value, exc_traceback):
"""Exit context."""
os.chdir(self.origin)
_executors = {}
def register_executor(lang, executor):
"""Add a new executor for markdown code blocks.
lang should be the tag used after the opening ```
executor should be a callable that takes one argument:
the code block found
"""
_executors[lang] = executor
def grab_fences(source: str) -> List[Fence]:
"""Grab fences in markdown.
Args:
source (str): markdown string
Returns:
List[Fence]: list of fences in markdown
"""
return [Fence.from_re_groups(groups) for groups in FENCED_BLOCK_RE.findall(source)]
def exec_file_fence(fence: Fence, **kwargs):
"""Executor that writes out file.
Args:
fence (Fence): markdown fence
**kwargs: not used
"""
fname = fence.options.get("title", None) or fence.attrs.get("title", [None])[0]
with open(fname, "w") as f:
f.write(fence.contents)
register_executor("yaml", exec_file_fence)
register_executor("yml", exec_file_fence)
register_executor("toml", exec_file_fence)
def exec_python_fence(fence: Fence, globals_: Optional[Dict] = None):
"""Python fence executor.
Args:
fence (Fence): markdown fence
globals_ (Dict, optional): python globals to pass to exec. Defaults to {}.
"""
if fence.options.get("title", False) or fence.attrs.get("title", False):
exec_file_fence(fence)
try:
if globals_ is None:
globals_ = {}
exec(fence.contents, globals_)
except Exception:
print(fence.contents) # noqa: T201
raise
register_executor("python", exec_python_fence)
register_executor("py", exec_python_fence)
def exec_bash_fence(fence: Fence, **kwargs):
"""Bash fence executor.
Args:
fence (Fence): markdown fence
**kwargs: not used
"""
_cmds = fence.contents.split("$ ")
commands: List[Dict] = []
for _cmd in _cmds:
if not _cmd:
continue
lines = _cmd.splitlines()
commands.append({"input": lines[0], "output": "\n".join(lines[1:])})
for command in commands:
result = run(command["input"], shell=True, check=True, capture_output=True)
assert (
result.stdout.decode()
.strip()
.replace("\r", "") # NOTE: fixing windows line ends
== command["output"].strip()
)
register_executor("bash", exec_bash_fence)
def check_typer_md_file(fpath: Path):
"""Check a markdown file with typer apps defined in it.
Args:
fpath (Path): path to markdown file
"""
with open(fpath, "r") as f:
source = f.read()
fences = grab_fences(source)
globals_ = {"__MODULE__": "__main__"}
with TemporaryDirectory() as td, WorkingDirectory(td):
for fence in fences:
_executors[fence.lang](fence, globals_=globals_)