Skip to content

📚 DOCS: re-activate code cells #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/_static/custom.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.code-cell > .highlight > pre {
.cell_output > .output > .highlight > pre {
border-left-color: green;
border-left-width: medium;
border-left-style: solid;
Expand Down
15 changes: 1 addition & 14 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"myst_parser",
"sphinx_copybutton",
"sphinx_design",
"jupyter_sphinx",
]

# List of patterns, relative to source directory, that match files and
Expand Down Expand Up @@ -135,17 +136,3 @@ def setup(app):
"""Add functions to the Sphinx setup."""
if os.environ.get("SKIP_APIDOC", None) is None:
app.connect("builder-inited", run_apidoc)

from sphinx.directives.code import CodeBlock

class CodeCell(CodeBlock):
"""Custom code block directive."""

def run(self):
"""Run the directive."""
self.options["class"] = ["code-cell"]
return super().run()

# note, these could be run by myst-nb,
# but currently this causes a circular dependency issue
app.add_directive("code-cell", CodeCell)
66 changes: 33 additions & 33 deletions docs/using.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ then these are converted to other formats using 'renderers'.

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

```{code-cell} python
```{jupyter-execute}
from pprint import pprint
from markdown_it import MarkdownIt
```

```{code-cell} python
```{jupyter-execute}
md = MarkdownIt()
md.render("some *text*")
```

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

```{code-cell} python
```{jupyter-execute}
from markdown_it.presets import zero
zero.make()
```

```{code-cell} python
```{jupyter-execute}
md = MarkdownIt("zero")
md.options
```

You can also override specific options:

```{code-cell} python
```{jupyter-execute}
md = MarkdownIt("zero", {"maxNesting": 99})
md.options
```

```{code-cell} python
```{jupyter-execute}
pprint(md.get_active_rules())
```

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

```{code-cell} python
```{jupyter-execute}
pprint(md.get_all_rules())
```

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

```{code-cell} python
```{jupyter-execute}
md.render("- __*emphasise this*__")
```

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

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

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

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

```{code-cell} python
```{jupyter-execute}
md.renderInline("__*emphasise this*__")
```

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

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

```{code-cell} python
```{jupyter-execute}
md = MarkdownIt("commonmark", {"typographer": True})
md.enable(["replacements", "smartquotes"])
md.render("'single quotes' (c)")
Expand All @@ -151,7 +151,7 @@ md.render("'single quotes' (c)")
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]`).
This allows URI autolinks to be identified, without the need for enclosing in `<>` brackets:

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

```{code-cell} python
```{jupyter-execute}
from markdown_it import MarkdownIt
import mdit_py_plugins
from mdit_py_plugins.front_matter import front_matter_plugin
Expand All @@ -175,7 +175,7 @@ md = (
.use(footnote_plugin)
.enable('table')
)
text = ("""
text = ("""\
---
a: 1
---
Expand All @@ -188,7 +188,7 @@ A footnote [^1]

[^1]: some details
""")
md.render(text)
print(md.render(text))
```

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

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:

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

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

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

```{code-cell} python
```{jupyter-execute}
tokens[0]
```

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

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

```{code-cell} python
```{jupyter-execute}
tokens[1]
```

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

```{code-cell} python
```{jupyter-execute}
print(tokens[1].as_dict())
```

This dictionary can also be deserialized:

```{code-cell} python
```{jupyter-execute}
Token.from_dict(tokens[1].as_dict())
```

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

```{code-cell} python
```{jupyter-execute}
from markdown_it.tree import SyntaxTreeNode

md = MarkdownIt("commonmark")
Expand All @@ -274,11 +274,11 @@ print(node.pretty(indent=2, show_text=True))

You can then use methods to traverse the tree

```{code-cell} python
```{jupyter-execute}
node.children
```

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

You can inject render methods into the instantiated render class.

```{code-cell} python
```{jupyter-execute}
md = MarkdownIt("commonmark")

def render_em_open(self, tokens, idx, options, env):
Expand All @@ -319,7 +319,7 @@ Also `add_render_rule` method is specific to Python, rather than adding directly

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

```{code-cell} python
```{jupyter-execute}
from markdown_it.renderer import RendererHTML

class MyRenderer(RendererHTML):
Expand All @@ -332,7 +332,7 @@ md.render("*a*")

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

```{code-cell} python
```{jupyter-execute}
from markdown_it.renderer import RendererHTML

class MyRenderer1(RendererHTML):
Expand All @@ -358,7 +358,7 @@ print(md.render("*a*"))

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

```{code-cell} python
```{jupyter-execute}
import re
from markdown_it import MarkdownIt

Expand All @@ -384,7 +384,7 @@ print(md.render("![](https://www.vimeo.com/123)"))

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

```{code-cell} python
```{jupyter-execute}
from markdown_it import MarkdownIt

def render_blank_link(self, tokens, idx, options, env):
Expand All @@ -402,7 +402,7 @@ print(md.render("[a]\n\n[a]: b"))

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

```{code-cell} python
```python
from markdown_it import MarkdownIt
from mdformat.renderer import MDRenderer

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ rtd = [
"sphinx-copybutton",
"sphinx-design",
"sphinx_book_theme",
"jupyter_sphinx",
]
testing = [
"coverage",
Expand Down