Skip to content

Commit 8360c65

Browse files
committed
add docutils section
1 parent 374cb99 commit 8360c65

6 files changed

Lines changed: 114 additions & 8 deletions

File tree

docs/computation/execute.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ kernelspec:
44
name: python3
55
---
66

7+
(execute/intro)=
78
# Execute and cache
89

910
MyST-NB can automatically run and cache notebooks contained in your project using [jupyter-cache].

docs/conf.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,5 +277,31 @@ def run(self):
277277
self.state.nested_parse(text, 0, node)
278278
return node.children
279279

280+
class DocutilsCliHelpDirective(SphinxDirective):
281+
"""Directive to print the docutils CLI help."""
282+
283+
has_content = False
284+
required_arguments = 0
285+
optional_arguments = 0
286+
final_argument_whitespace = False
287+
option_spec = {}
288+
289+
def run(self):
290+
"""Run the directive."""
291+
import io
292+
293+
from docutils import nodes
294+
from docutils.frontend import OptionParser
295+
296+
from myst_nb.docutils_ import Parser as DocutilsParser
297+
298+
stream = io.StringIO()
299+
OptionParser(
300+
components=(DocutilsParser,),
301+
usage="mystnb-docutils-<writer> [options] [<source> [<destination>]]",
302+
).print_help(stream)
303+
return [nodes.literal_block("", stream.getvalue())]
304+
280305
app.add_directive("myst-config", MystConfigDirective)
281306
app.add_directive("mystnb-config", MystNbConfigDirective)
307+
app.add_directive("docutils-cli-help", DocutilsCliHelpDirective)

docs/docutils.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
(render/single-page)=
2+
# Single page builds
3+
4+
```{versionadded} 0.14.0
5+
```
6+
7+
Sphinx, and thus MyST-NB, is built on top of the [Docutils](https://docutils.sourceforge.io/docs/) package.
8+
MyST-NB offers a renderer, parser and CLI-interface for working directly with Docutils, independent of Sphinx, as described below.
9+
10+
:::{note}
11+
Since these tools are independent of Sphinx, this means they cannot parse any Sphinx or Sphinx extensions specific roles or directives.
12+
:::
13+
14+
On installing MyST-NB, the following CLI-commands are made available:
15+
16+
- `mystnb-docutils-html`: converts notebooks to HTML
17+
- `mystnb-docutils-html5`: converts notebooks to HTML5
18+
- `mystnb-docutils-latex`: converts notebooks to LaTeX
19+
- `mystnb-docutils-xml`: converts notebooks to docutils-native XML
20+
- `mystnb-docutils-pseudoxml`: converts notebooks to pseudo-XML (to visualise the AST structure)
21+
22+
Each command can be piped stdin or take a file path as an argument:
23+
24+
```console
25+
$ mystnb-docutils-html --help
26+
$ mystnb-docutils-html --nb-execution-mode="off" hello-world.ipynb
27+
$ mystnb-docutils-html --nb-read-as-md="yes" hello-world.md
28+
```
29+
30+
The commands are based on the [Docutils Front-End Tools](https://docutils.sourceforge.io/docs/user/tools.html), and so follow the same argument and options structure, included many of the MyST NB specific options detailed in the [](config/intro) section.
31+
32+
:::{dropdown} Shared Docutils CLI Options
33+
```{docutils-cli-help}
34+
```
35+
:::
36+
37+
The CLI commands can also utilise the [`docutils.conf` configuration file](https://docutils.sourceforge.io/docs/user/config.html) to configure the behaviour of the CLI commands. For example:
38+
39+
```ini
40+
# These entries affect all processing:
41+
[general]
42+
nb_execution_mode: off
43+
44+
# These entries affect specific HTML output:
45+
[html writers]
46+
embed-stylesheet: no
47+
48+
[html5 writer]
49+
stylesheet-dirs: path/to/html5_polyglot/
50+
stylesheet-path: minimal.css, responsive.css
51+
```
52+
53+
You can also use the {py:class}`myst_nb.docutils_.Parser` class programmatically with the [Docutils publisher API](https://docutils.sourceforge.io/docs/api/publisher.html):
54+
55+
```python
56+
from docutils.core import publish_string
57+
from nbformat import writes
58+
from nbformat.v4 import new_notebook
59+
from myst_nb.docutils_ import Parser
60+
61+
source = writes(new_notebook())
62+
output = publish_string(
63+
source=source,
64+
writer_name="html5",
65+
settings_overrides={
66+
"nb_execution_mode": "off",
67+
"embed_stylesheet": False,
68+
},
69+
parser=Parser(),
70+
)
71+
```
72+
73+
Finally, you can include MyST Markdown files within a RestructuredText file, using the [`include` directive](https://docutils.sourceforge.io/docs/ref/rst/directives.html#include):
74+
75+
```rst
76+
.. include:: include.ipynb
77+
:parser: myst_nb.docutils_
78+
```
79+
80+
```{important}
81+
The `parser` option requires `docutils>=0.17`
82+
```

docs/index.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ sd_hide_title: true
2727
```{rubric} Jupyter Notebook Publishing
2828
```
2929

30-
A Docutils and Sphinx extension for compiling Jupyter Notebooks into high quality documentation formats.
30+
A Sphinx and Docutils extension for compiling Jupyter Notebooks into high quality documentation formats.
3131

3232
```{button-ref} quickstart
3333
:ref-type: doc
@@ -58,14 +58,14 @@ Use MyST Markdown syntax to support technical authoring features such as cross-r
5858
:::
5959

6060
:::{grid-item-card} {material-regular}`published_with_changes;2em` Compute
61-
:link: computation/index
62-
:link-type: doc
61+
:link: execute/intro
62+
:link-type: ref
6363

6464
Generate dynamic outputs using Jupyter kernels, with configurable execution handling.\
6565
Cache execution outputs, for fast re-builds.
6666

6767
+++
68-
[Learn more »](computation/index)
68+
[Learn more »](execute/intro)
6969
:::
7070

7171
:::{grid-item-card} {material-regular}`preview;2em` Render
@@ -110,6 +110,7 @@ authoring/index
110110
computation/index
111111
render/index
112112
configuration
113+
docutils
113114
```
114115

115116
```{toctree}

docs/render/docutils.md

Lines changed: 0 additions & 3 deletions
This file was deleted.

docs/render/index.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ format_outputs
99
hiding
1010
glue
1111
interactive
12-
docutils
1312
```

0 commit comments

Comments
 (0)