importpython-markdown 0.1.0__py3-none-any.whl → 0.1.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- importpython_markdown/__init__.py +69 -16
- {importpython_markdown-0.1.0.dist-info → importpython_markdown-0.1.2.dist-info}/METADATA +6 -4
- importpython_markdown-0.1.2.dist-info/RECORD +7 -0
- importpython_markdown-0.1.0.dist-info/RECORD +0 -7
- {importpython_markdown-0.1.0.dist-info → importpython_markdown-0.1.2.dist-info}/WHEEL +0 -0
- {importpython_markdown-0.1.0.dist-info → importpython_markdown-0.1.2.dist-info}/entry_points.txt +0 -0
- {importpython_markdown-0.1.0.dist-info → importpython_markdown-0.1.2.dist-info}/licenses/LICENSE +0 -0
- {importpython_markdown-0.1.0.dist-info → importpython_markdown-0.1.2.dist-info}/top_level.txt +0 -0
|
@@ -14,8 +14,11 @@ time in the element's own vocabulary, so an authoring mistake is
|
|
|
14
14
|
a loud build failure rather than a silent page.
|
|
15
15
|
|
|
16
16
|
The emitted markup follows the element's contract exactly: each
|
|
17
|
-
part is a
|
|
18
|
-
order the author wrote it.
|
|
17
|
+
part is a ``<pre>`` holding one ``<code>``, the pre a direct
|
|
18
|
+
child of the element, in the order the author wrote it. The
|
|
19
|
+
element reads a ``<pre><code>`` pair and a bare ``<pre>``
|
|
20
|
+
identically; the inner ``<code>`` is for the surrounding theme,
|
|
21
|
+
whose block styling and copy button expect it.
|
|
19
22
|
"""
|
|
20
23
|
|
|
21
24
|
import html
|
|
@@ -49,8 +52,9 @@ ATTRIBUTE = re.compile(r'([\w-]+)(?:="([^"]*)")?')
|
|
|
49
52
|
# fail the build, not be silently ignored in every reader's
|
|
50
53
|
# browser.
|
|
51
54
|
TYPES = ("py", "mpy")
|
|
52
|
-
FLAGS = ("terminal", "debug")
|
|
53
|
-
VALUED = ("title", "run-label", "stop-label", "theme", "class",
|
|
55
|
+
FLAGS = ("terminal", "debug", "send-embedder-url")
|
|
56
|
+
VALUED = ("title", "run-label", "stop-label", "theme", "class",
|
|
57
|
+
"id", "terminal-rows", "terminal-cols")
|
|
54
58
|
|
|
55
59
|
|
|
56
60
|
class ExampleError(ValueError):
|
|
@@ -106,6 +110,23 @@ def parse_gate(header, line):
|
|
|
106
110
|
raise ExampleError(
|
|
107
111
|
line, f'cannot parse attributes at "{rest[span:]}"'
|
|
108
112
|
)
|
|
113
|
+
# The terminal's explicit size: each dimension needs the
|
|
114
|
+
# terminal it sizes, and only a positive whole number will do
|
|
115
|
+
# - the same rules the element applies, failing the build
|
|
116
|
+
# here instead of a reader's browser there.
|
|
117
|
+
for name in ("terminal-rows", "terminal-cols"):
|
|
118
|
+
if name in attributes:
|
|
119
|
+
if "terminal" not in attributes:
|
|
120
|
+
raise ExampleError(
|
|
121
|
+
line, f"{name} requires the terminal flag"
|
|
122
|
+
)
|
|
123
|
+
value = attributes[name]
|
|
124
|
+
if not (value.isdigit() and int(value) >= 1):
|
|
125
|
+
raise ExampleError(
|
|
126
|
+
line,
|
|
127
|
+
f'{name} is "{value}"; use a positive whole '
|
|
128
|
+
"number",
|
|
129
|
+
)
|
|
109
130
|
return attributes
|
|
110
131
|
|
|
111
132
|
|
|
@@ -173,7 +194,7 @@ def split_example(lines, first_line):
|
|
|
173
194
|
return pieces
|
|
174
195
|
|
|
175
196
|
|
|
176
|
-
def validate(pieces, line):
|
|
197
|
+
def validate(pieces, attributes, line):
|
|
177
198
|
"""
|
|
178
199
|
The element's own rules, applied at build time: exactly one
|
|
179
200
|
program, no duplicate filenames, at most one config (as valid
|
|
@@ -201,12 +222,37 @@ def validate(pieces, line):
|
|
|
201
222
|
)
|
|
202
223
|
for piece in [p for p in pieces if p["part"] == "config"]:
|
|
203
224
|
try:
|
|
204
|
-
json.loads("\n".join(piece["text"]))
|
|
225
|
+
settings = json.loads("\n".join(piece["text"]))
|
|
205
226
|
except json.JSONDecodeError as error:
|
|
206
227
|
raise ExampleError(
|
|
207
228
|
piece["line"],
|
|
208
229
|
f"the config block is not valid JSON: {error}",
|
|
209
230
|
) from None
|
|
231
|
+
# send-embedder-url writes importpython.py, so while the
|
|
232
|
+
# flag is on the name is reserved - here in the author's
|
|
233
|
+
# own config files too, matching the element's rule.
|
|
234
|
+
if "send-embedder-url" in attributes:
|
|
235
|
+
names = (settings.get("files") or {}).values()
|
|
236
|
+
if any(
|
|
237
|
+
str(name).removeprefix("./") == "importpython.py"
|
|
238
|
+
for name in names
|
|
239
|
+
):
|
|
240
|
+
raise ExampleError(
|
|
241
|
+
piece["line"],
|
|
242
|
+
"the send-embedder-url flag reserves the "
|
|
243
|
+
'file "importpython.py"',
|
|
244
|
+
)
|
|
245
|
+
if "send-embedder-url" in attributes:
|
|
246
|
+
for piece in pieces:
|
|
247
|
+
if (
|
|
248
|
+
piece["part"] == "file"
|
|
249
|
+
and piece["title"] == "importpython.py"
|
|
250
|
+
):
|
|
251
|
+
raise ExampleError(
|
|
252
|
+
piece["line"],
|
|
253
|
+
"the send-embedder-url flag reserves the "
|
|
254
|
+
'file "importpython.py"',
|
|
255
|
+
)
|
|
210
256
|
|
|
211
257
|
|
|
212
258
|
class ImportPythonPreprocessor(Preprocessor):
|
|
@@ -301,7 +347,7 @@ class ImportPythonPreprocessor(Preprocessor):
|
|
|
301
347
|
"""
|
|
302
348
|
attributes = parse_gate(header, first_line)
|
|
303
349
|
pieces = split_example(lines, first_line + 1)
|
|
304
|
-
validate(pieces, first_line)
|
|
350
|
+
validate(pieces, attributes, first_line)
|
|
305
351
|
parts = ["<import-python"]
|
|
306
352
|
for name, value in attributes.items():
|
|
307
353
|
if value is None:
|
|
@@ -331,22 +377,29 @@ class ImportPythonPreprocessor(Preprocessor):
|
|
|
331
377
|
)
|
|
332
378
|
return inner.convert("\n".join(piece["text"])) + "\n"
|
|
333
379
|
text, lit = self.paint("\n".join(piece["text"]), piece)
|
|
380
|
+
# One code element inside every pre, highlighted or not:
|
|
381
|
+
# MkDocs-family themes hang block styling and the copy
|
|
382
|
+
# button on pre > code, and the element reads both shapes
|
|
383
|
+
# identically. No newline before the text - a pre swallows
|
|
384
|
+
# one after its own tag, a code does not - so textContent
|
|
385
|
+
# stays byte-identical to the bare-pre form.
|
|
386
|
+
text = f"<code>{text}\n</code>"
|
|
334
387
|
if piece["part"] == "file":
|
|
335
388
|
title = html.escape(piece["title"])
|
|
336
|
-
return f'<pre{lit} title="{title}"
|
|
389
|
+
return f'<pre{lit} title="{title}">{text}</pre>\n'
|
|
337
390
|
if piece["part"] in ("config", "body"):
|
|
338
391
|
role = piece["part"]
|
|
339
|
-
return f'<pre{lit} data-role="{role}"
|
|
340
|
-
return f"<pre{lit}
|
|
392
|
+
return f'<pre{lit} data-role="{role}">{text}</pre>\n'
|
|
393
|
+
return f"<pre{lit}>{text}</pre>\n"
|
|
341
394
|
|
|
342
395
|
def paint(self, code, piece):
|
|
343
396
|
"""
|
|
344
397
|
Highlight a part's code as Pygments spans with no wrapper
|
|
345
|
-
of any kind
|
|
346
|
-
|
|
347
|
-
the coloured path adds is what lets a
|
|
348
|
-
Zensical site's existing Pygments stylesheet
|
|
349
|
-
blocks, palettes and all, with no CSS of ours.
|
|
398
|
+
of any kind - render_piece adds the one code element both
|
|
399
|
+
paths share, and the pre stays the direct child. The
|
|
400
|
+
class="highlight" the coloured path adds is what lets a
|
|
401
|
+
Material or Zensical site's existing Pygments stylesheet
|
|
402
|
+
colour these blocks, palettes and all, with no CSS of ours.
|
|
350
403
|
|
|
351
404
|
The language comes from the fence, or a file's name, with
|
|
352
405
|
json and html assumed for config and body; an unknown or
|
|
@@ -385,7 +438,7 @@ class ImportPythonExtension(Extension):
|
|
|
385
438
|
def __init__(self, **kwargs):
|
|
386
439
|
self.config = {
|
|
387
440
|
"script_url": [
|
|
388
|
-
"https://importpython.org/
|
|
441
|
+
"https://importpython.org/v/test/embed.js",
|
|
389
442
|
"Where embed.js is served from; point this at "
|
|
390
443
|
"your own copy to use one.",
|
|
391
444
|
],
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: importpython-markdown
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.2
|
|
4
4
|
Summary: Runnable Python examples in Markdown, via importpython.org.
|
|
5
5
|
Author-email: "Nicholas H.Tollervey" <ntoll@ntoll.org>
|
|
6
6
|
License: MIT
|
|
@@ -43,9 +43,11 @@ reader presses Run. Plain fences are the program; a fence with
|
|
|
43
43
|
`title="name.py"` is a file delivered beside it; `config` and
|
|
44
44
|
`body` fences carry BFP settings and page markup (escaped for
|
|
45
45
|
you). The `py` and `mpy` types choose Pyodide or MicroPython,
|
|
46
|
-
and `terminal`, `debug`, `
|
|
47
|
-
`stop-label="..."`, `theme="..."`,
|
|
48
|
-
|
|
46
|
+
and `terminal`, `debug`, `send-embedder-url`, `title="..."`,
|
|
47
|
+
`run-label="..."`, `stop-label="..."`, `theme="..."`,
|
|
48
|
+
`terminal-rows="..."`, `terminal-cols="..."`, `class` and `id`
|
|
49
|
+
pass through to the element - each documented at
|
|
50
|
+
[importpython.org](https://importpython.org/docs/writing-markdown/).
|
|
49
51
|
|
|
50
52
|
Install and enable:
|
|
51
53
|
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
importpython_markdown/__init__.py,sha256=C9tEDiPB7CCeW76X0D-X2Rv3lCnPcwYDhH9fVNUnKjI,18453
|
|
2
|
+
importpython_markdown-0.1.2.dist-info/licenses/LICENSE,sha256=ksfiPPZ3bBB4_KgmDo6-NLda44B_-onAZ3JLVxYSIVg,1077
|
|
3
|
+
importpython_markdown-0.1.2.dist-info/METADATA,sha256=Pp5BqSH9hVCCrlFih5572ePkMcQedzY7q6JrJ61rH_s,2335
|
|
4
|
+
importpython_markdown-0.1.2.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
5
|
+
importpython_markdown-0.1.2.dist-info/entry_points.txt,sha256=0Fo68SFJa3rmdmuysJyRTCu35rve3xGdegmhOutPs18,81
|
|
6
|
+
importpython_markdown-0.1.2.dist-info/top_level.txt,sha256=gZT7U42MZK8PK1UxWpt0IGHGv8ynHvdYlxn-OjZRQ-0,22
|
|
7
|
+
importpython_markdown-0.1.2.dist-info/RECORD,,
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
importpython_markdown/__init__.py,sha256=hT7LSHnfozBbaNInQD6vsTC5dO4EcKRV9KY2zLBjyw8,15955
|
|
2
|
-
importpython_markdown-0.1.0.dist-info/licenses/LICENSE,sha256=ksfiPPZ3bBB4_KgmDo6-NLda44B_-onAZ3JLVxYSIVg,1077
|
|
3
|
-
importpython_markdown-0.1.0.dist-info/METADATA,sha256=uzuRuKzAGStQHOnMMAKbp6G4iayeKl8SzOJLDpqt8Rk,2179
|
|
4
|
-
importpython_markdown-0.1.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
5
|
-
importpython_markdown-0.1.0.dist-info/entry_points.txt,sha256=0Fo68SFJa3rmdmuysJyRTCu35rve3xGdegmhOutPs18,81
|
|
6
|
-
importpython_markdown-0.1.0.dist-info/top_level.txt,sha256=gZT7U42MZK8PK1UxWpt0IGHGv8ynHvdYlxn-OjZRQ-0,22
|
|
7
|
-
importpython_markdown-0.1.0.dist-info/RECORD,,
|
|
File without changes
|
{importpython_markdown-0.1.0.dist-info → importpython_markdown-0.1.2.dist-info}/entry_points.txt
RENAMED
|
File without changes
|
{importpython_markdown-0.1.0.dist-info → importpython_markdown-0.1.2.dist-info}/licenses/LICENSE
RENAMED
|
File without changes
|
{importpython_markdown-0.1.0.dist-info → importpython_markdown-0.1.2.dist-info}/top_level.txt
RENAMED
|
File without changes
|