Skip to content

Commit c6754a2

Browse files
authored
📚 DOCS: re-activate code cells (#269)
1 parent 798b9d0 commit c6754a2

File tree

4 files changed

+36
-48
lines changed

4 files changed

+36
-48
lines changed

docs/_static/custom.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.code-cell > .highlight > pre {
1+
.cell_output > .output > .highlight > pre {
22
border-left-color: green;
33
border-left-width: medium;
44
border-left-style: solid;

docs/conf.py

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"myst_parser",
3737
"sphinx_copybutton",
3838
"sphinx_design",
39+
"jupyter_sphinx",
3940
]
4041

4142
# List of patterns, relative to source directory, that match files and
@@ -135,17 +136,3 @@ def setup(app):
135136
"""Add functions to the Sphinx setup."""
136137
if os.environ.get("SKIP_APIDOC", None) is None:
137138
app.connect("builder-inited", run_apidoc)
138-
139-
from sphinx.directives.code import CodeBlock
140-
141-
class CodeCell(CodeBlock):
142-
"""Custom code block directive."""
143-
144-
def run(self):
145-
"""Run the directive."""
146-
self.options["class"] = ["code-cell"]
147-
return super().run()
148-
149-
# note, these could be run by myst-nb,
150-
# but currently this causes a circular dependency issue
151-
app.add_directive("code-cell", CodeCell)

docs/using.md

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ then these are converted to other formats using 'renderers'.
2727

2828
The simplest way to understand how text will be parsed is using:
2929

30-
```{code-cell} python
30+
```{jupyter-execute}
3131
from pprint import pprint
3232
from markdown_it import MarkdownIt
3333
```
3434

35-
```{code-cell} python
35+
```{jupyter-execute}
3636
md = MarkdownIt()
3737
md.render("some *text*")
3838
```
3939

40-
```{code-cell} python
40+
```{jupyter-execute}
4141
for token in md.parse("some *text*"):
4242
print(token)
4343
print()
@@ -59,48 +59,48 @@ You can define this configuration *via* directly supplying a dictionary or a pre
5959
Compared to `commonmark`, it enables the table, strikethrough and linkify components.
6060
**Important**, to use this configuration you must have `linkify-it-py` installed.
6161

62-
```{code-cell} python
62+
```{jupyter-execute}
6363
from markdown_it.presets import zero
6464
zero.make()
6565
```
6666

67-
```{code-cell} python
67+
```{jupyter-execute}
6868
md = MarkdownIt("zero")
6969
md.options
7070
```
7171

7272
You can also override specific options:
7373

74-
```{code-cell} python
74+
```{jupyter-execute}
7575
md = MarkdownIt("zero", {"maxNesting": 99})
7676
md.options
7777
```
7878

79-
```{code-cell} python
79+
```{jupyter-execute}
8080
pprint(md.get_active_rules())
8181
```
8282

8383
You can find all the parsing rules in the source code:
8484
`parser_core.py`, `parser_block.py`,
8585
`parser_inline.py`.
8686

87-
```{code-cell} python
87+
```{jupyter-execute}
8888
pprint(md.get_all_rules())
8989
```
9090

9191
Any of the parsing rules can be enabled/disabled, and these methods are "chainable":
9292

93-
```{code-cell} python
93+
```{jupyter-execute}
9494
md.render("- __*emphasise this*__")
9595
```
9696

97-
```{code-cell} python
97+
```{jupyter-execute}
9898
md.enable(["list", "emphasis"]).render("- __*emphasise this*__")
9999
```
100100

101101
You can temporarily modify rules with the `reset_rules` context manager.
102102

103-
```{code-cell} python
103+
```{jupyter-execute}
104104
with md.reset_rules():
105105
md.disable("emphasis")
106106
print(md.render("__*emphasise this*__"))
@@ -109,7 +109,7 @@ md.render("__*emphasise this*__")
109109

110110
Additionally `renderInline` runs the parser with all block syntax rules disabled.
111111

112-
```{code-cell} python
112+
```{jupyter-execute}
113113
md.renderInline("__*emphasise this*__")
114114
```
115115

@@ -140,7 +140,7 @@ The `smartquotes` and `replacements` components are intended to improve typograp
140140

141141
Both of these components require typography to be turned on, as well as the components enabled:
142142

143-
```{code-cell} python
143+
```{jupyter-execute}
144144
md = MarkdownIt("commonmark", {"typographer": True})
145145
md.enable(["replacements", "smartquotes"])
146146
md.render("'single quotes' (c)")
@@ -151,7 +151,7 @@ md.render("'single quotes' (c)")
151151
The `linkify` component requires that [linkify-it-py](https://github.com/tsutsu3/linkify-it-py) be installed (e.g. *via* `pip install markdown-it-py[linkify]`).
152152
This allows URI autolinks to be identified, without the need for enclosing in `<>` brackets:
153153

154-
```{code-cell} python
154+
```{jupyter-execute}
155155
md = MarkdownIt("commonmark", {"linkify": True})
156156
md.enable(["linkify"])
157157
md.render("github.com")
@@ -163,7 +163,7 @@ Plugins load collections of additional syntax rules and render methods into the
163163
A number of useful plugins are available in [`mdit_py_plugins`](https://github.com/executablebooks/mdit-py-plugins) (see [the plugin list](./plugins.md)),
164164
or you can create your own (following the [markdown-it design principles](./architecture.md)).
165165

166-
```{code-cell} python
166+
```{jupyter-execute}
167167
from markdown_it import MarkdownIt
168168
import mdit_py_plugins
169169
from mdit_py_plugins.front_matter import front_matter_plugin
@@ -175,7 +175,7 @@ md = (
175175
.use(footnote_plugin)
176176
.enable('table')
177177
)
178-
text = ("""
178+
text = ("""\
179179
---
180180
a: 1
181181
---
@@ -188,7 +188,7 @@ A footnote [^1]
188188
189189
[^1]: some details
190190
""")
191-
md.render(text)
191+
print(md.render(text))
192192
```
193193

194194
## The Token Stream
@@ -197,7 +197,7 @@ md.render(text)
197197

198198
Before rendering, the text is parsed to a flat token stream of block level syntax elements, with nesting defined by opening (1) and closing (-1) attributes:
199199

200-
```{code-cell} python
200+
```{jupyter-execute}
201201
md = MarkdownIt("commonmark")
202202
tokens = md.parse("""
203203
Here's some *text*
@@ -211,37 +211,37 @@ Here's some *text*
211211
Naturally all openings should eventually be closed,
212212
such that:
213213

214-
```{code-cell} python
214+
```{jupyter-execute}
215215
sum([t.nesting for t in tokens]) == 0
216216
```
217217

218218
All tokens are the same class, which can also be created outside the parser:
219219

220-
```{code-cell} python
220+
```{jupyter-execute}
221221
tokens[0]
222222
```
223223

224-
```{code-cell} python
224+
```{jupyter-execute}
225225
from markdown_it.token import Token
226226
token = Token("paragraph_open", "p", 1, block=True, map=[1, 2])
227227
token == tokens[0]
228228
```
229229

230230
The `'inline'` type token contain the inline tokens as children:
231231

232-
```{code-cell} python
232+
```{jupyter-execute}
233233
tokens[1]
234234
```
235235

236236
You can serialize a token (and its children) to a JSONable dictionary using:
237237

238-
```{code-cell} python
238+
```{jupyter-execute}
239239
print(tokens[1].as_dict())
240240
```
241241

242242
This dictionary can also be deserialized:
243243

244-
```{code-cell} python
244+
```{jupyter-execute}
245245
Token.from_dict(tokens[1].as_dict())
246246
```
247247

@@ -254,7 +254,7 @@ Token.from_dict(tokens[1].as_dict())
254254
In some use cases it may be useful to convert the token stream into a syntax tree,
255255
with opening/closing tokens collapsed into a single token that contains children.
256256

257-
```{code-cell} python
257+
```{jupyter-execute}
258258
from markdown_it.tree import SyntaxTreeNode
259259
260260
md = MarkdownIt("commonmark")
@@ -274,11 +274,11 @@ print(node.pretty(indent=2, show_text=True))
274274

275275
You can then use methods to traverse the tree
276276

277-
```{code-cell} python
277+
```{jupyter-execute}
278278
node.children
279279
```
280280

281-
```{code-cell} python
281+
```{jupyter-execute}
282282
print(node[0])
283283
node[0].next_sibling
284284
```
@@ -302,7 +302,7 @@ def function(renderer, tokens, idx, options, env):
302302

303303
You can inject render methods into the instantiated render class.
304304

305-
```{code-cell} python
305+
```{jupyter-execute}
306306
md = MarkdownIt("commonmark")
307307
308308
def render_em_open(self, tokens, idx, options, env):
@@ -319,7 +319,7 @@ Also `add_render_rule` method is specific to Python, rather than adding directly
319319

320320
You can also subclass a render and add the method there:
321321

322-
```{code-cell} python
322+
```{jupyter-execute}
323323
from markdown_it.renderer import RendererHTML
324324
325325
class MyRenderer(RendererHTML):
@@ -332,7 +332,7 @@ md.render("*a*")
332332

333333
Plugins can support multiple render types, using the `__output__` attribute (this is currently a Python only feature).
334334

335-
```{code-cell} python
335+
```{jupyter-execute}
336336
from markdown_it.renderer import RendererHTML
337337
338338
class MyRenderer1(RendererHTML):
@@ -358,7 +358,7 @@ print(md.render("*a*"))
358358

359359
Here's a more concrete example; let's replace images with vimeo links to player's iframe:
360360

361-
```{code-cell} python
361+
```{jupyter-execute}
362362
import re
363363
from markdown_it import MarkdownIt
364364
@@ -384,7 +384,7 @@ print(md.render("![](https://www.vimeo.com/123)"))
384384

385385
Here is another example, how to add `target="_blank"` to all links:
386386

387-
```{code-cell} python
387+
```{jupyter-execute}
388388
from markdown_it import MarkdownIt
389389
390390
def render_blank_link(self, tokens, idx, options, env):
@@ -402,7 +402,7 @@ print(md.render("[a]\n\n[a]: b"))
402402

403403
You can also render a token stream directly to markdown via the `MDRenderer` class from [`mdformat`](https://github.com/executablebooks/mdformat):
404404

405-
```{code-cell} python
405+
```python
406406
from markdown_it import MarkdownIt
407407
from mdformat.renderer import MDRenderer
408408

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ rtd = [
5252
"sphinx-copybutton",
5353
"sphinx-design",
5454
"sphinx_book_theme",
55+
"jupyter_sphinx",
5556
]
5657
testing = [
5758
"coverage",

0 commit comments

Comments
 (0)