-
-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathidom_example.py
183 lines (144 loc) · 4.67 KB
/
idom_example.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
from __future__ import annotations
import re
from pathlib import Path
from typing import Any
from docutils.parsers.rst import directives
from docutils.statemachine import StringList
from sphinx.application import Sphinx
from sphinx.util.docutils import SphinxDirective
from sphinx_design.tabs import TabSetDirective
from docs.examples import (
SOURCE_DIR,
get_example_files_by_name,
get_normalized_example_name,
)
class WidgetExample(SphinxDirective):
has_content = False
required_arguments = 1
_next_id = 0
option_spec = {
"result-is-default-tab": directives.flag,
"activate-button": directives.flag,
}
def run(self):
example_name = get_normalized_example_name(
self.arguments[0],
# only used if example name starts with "/"
self.get_source_info()[0],
)
show_linenos = "linenos" in self.options
live_example_is_default_tab = "result-is-default-tab" in self.options
activate_result = "activate-button" not in self.options
ex_files = get_example_files_by_name(example_name)
if not ex_files:
src_file, line_num = self.get_source_info()
raise ValueError(
f"Missing example named {example_name!r} "
f"referenced by document {src_file}:{line_num}"
)
labeled_tab_items: list[tuple[str, Any]] = []
if len(ex_files) == 1:
labeled_tab_items.append(
(
"main.py",
_literal_include(
path=ex_files[0],
linenos=show_linenos,
),
)
)
else:
for path in sorted(
ex_files, key=lambda p: "" if p.name == "main.py" else p.name
):
labeled_tab_items.append(
(
path.name,
_literal_include(
path=path,
linenos=show_linenos,
),
)
)
result_tab_item = (
"▶️ result",
_interactive_widget(
name=example_name,
with_activate_button=not activate_result,
),
)
if live_example_is_default_tab:
labeled_tab_items.insert(0, result_tab_item)
else:
labeled_tab_items.append(result_tab_item)
return TabSetDirective(
"WidgetExample",
[],
{},
_make_tab_items(labeled_tab_items),
self.lineno - 1,
self.content_offset,
"",
self.state,
self.state_machine,
).run()
def _make_tab_items(labeled_content_tuples):
tab_items = ""
for label, content in labeled_content_tuples:
tab_items += _tab_item_template.format(
label=label,
content=content.replace("\n", "\n "),
)
return _string_to_nested_lines(tab_items)
def _literal_include(path: Path, linenos: bool):
try:
language = {
".py": "python",
".js": "javascript",
".json": "json",
}[path.suffix]
except KeyError:
raise ValueError(f"Unknown extension type {path.suffix!r}")
return _literal_include_template.format(
name=str(path.relative_to(SOURCE_DIR)),
language=language,
options=_join_options(_get_file_options(path)),
)
def _join_options(option_strings: list[str]) -> str:
return "\n ".join(option_strings)
OPTION_PATTERN = re.compile(r"#\s:[\w-]+:.*")
def _get_file_options(file: Path) -> list[str]:
options = []
for line in file.read_text().split("\n"):
if not line.strip():
continue
if not line.startswith("#"):
break
if not OPTION_PATTERN.match(line):
continue
option_string = line[1:].strip()
if option_string:
options.append(option_string)
return options
def _interactive_widget(name, with_activate_button):
return _interactive_widget_template.format(
name=name,
activate_button_opt=":activate-button:" if with_activate_button else "",
)
_tab_item_template = """
.. tab-item:: {label}
{content}
"""
_interactive_widget_template = """
.. idom-view:: {name}
{activate_button_opt}
"""
_literal_include_template = """
.. literalinclude:: /{name}
:language: {language}
{options}
"""
def _string_to_nested_lines(content):
return StringList(content.split("\n"))
def setup(app: Sphinx) -> None:
app.add_directive("idom", WidgetExample)