Skip to content

Commit 0676b68

Browse files
committed
update
1 parent 429d7a8 commit 0676b68

5 files changed

Lines changed: 150 additions & 13 deletions

File tree

docs/_static/custom.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,11 @@ h3::before {
1414
counter-increment: subsubsection;
1515
content: counter(subsection) "." counter(subsubsection) ". ";
1616
}
17+
18+
/** No icon for admonitions with no-icon class */
19+
.admonition > .admonition-title, div.admonition.no-icon > .admonition-title::before {
20+
content: "";
21+
}
22+
.admonition > .admonition-title, div.admonition.no-icon > .admonition-title {
23+
padding-left: .6rem;
24+
}

docs/conf.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,13 @@
156156

157157
def setup(app: Sphinx):
158158
"""Add functions to the Sphinx setup."""
159-
from myst_parser._docs import DocutilsCliHelpDirective, MystConfigDirective
159+
from myst_parser._docs import (
160+
DirectiveDoc,
161+
DocutilsCliHelpDirective,
162+
MystConfigDirective,
163+
)
160164

161165
app.add_css_file("custom.css")
162166
app.add_directive("myst-config", MystConfigDirective)
163167
app.add_directive("docutils-cli-help", DocutilsCliHelpDirective)
168+
app.add_directive("doc-directive", DirectiveDoc)

docs/syntax/roles-and-directives.md

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
# Roles and Directives
44

5-
Roles and directives provide a way to extend the syntax of MyST,
5+
Roles and directives provide a way to extend the syntax of MyST in an unbound manner,
66
by interpreting a chuck of text as a specific type of markup, according to its name.
77

8-
Any [docutils role](https://docutils.sourceforge.io/docs/ref/rst/roles.html), [docutils directive](https://docutils.sourceforge.io/docs/ref/rst/directives.html), [sphinx role](https://www.sphinx-doc.org/en/master/usage/restructuredtext/roles.html), or [sphinx directive](https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html) can be used in MyST.
8+
Mostly all [docutils roles](https://docutils.sourceforge.io/docs/ref/rst/roles.html), [docutils directives](https://docutils.sourceforge.io/docs/ref/rst/directives.html), [sphinx roles](https://www.sphinx-doc.org/en/master/usage/restructuredtext/roles.html), or [sphinx directives](https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html) can be used in MyST.
99

1010
## Syntax
1111

@@ -334,7 +334,65 @@ How roles parse this content depends on the author that created the role.
334334

335335
## Common roles and directives
336336

337-
Under construction...
337+
:::{admonition} {material-regular}`engineering;1.5rem;sd-mr-1` Currently Under Construction
338+
:class: no-icon
339+
Check back for more...
340+
:::
341+
342+
### ToC Trees
343+
344+
```{doc-directive} contents
345+
Insert a table of contents tree of the documents headings.
346+
```
347+
348+
```{doc-directive} toctree
349+
Inserts a Sphinx "Table of Contents" tree, containing a list of (relative) child document paths.
350+
```
351+
352+
### Admonitions
353+
354+
```{doc-directive} admonition
355+
Create a generic "callout" box, containing the content.
356+
```
357+
358+
```{doc-directive} note
359+
Create a "callout" box, specific to notes, containing the content.
360+
```
361+
362+
Other admonitions (same structure as `note`): `attention`, `caution`, `danger`, `error`, `hint`, `important`, `tip`, `warning`.
363+
364+
Sphinx only: `deprecated`, `versionadded`, `versionchanged`.
365+
366+
### Images and Figures
367+
368+
```{doc-directive} image
369+
Insert an image, from a (relative) path or URL.
370+
```
371+
372+
```{doc-directive} figure
373+
Insert an image, from a (relative) path or URL,
374+
with a caption (first paragraph), and optional legend (subsequent content).
375+
```
376+
377+
```{doc-directive} table
378+
Insert a (MyST) table with a caption.
379+
```
380+
381+
### Tables
382+
383+
```{doc-directive} list-table
384+
Create a table from data in a uniform two-level bullet list.
385+
```
386+
387+
```{doc-directive} csv-table
388+
Create a table from CSV (comma-separated values) data.
389+
```
390+
391+
### Code
392+
393+
```{doc-directive} code-block
394+
Syntax highlight a block of code, according to the language.
395+
```
338396

339397
(syntax/roles/special)=
340398

docs/syntax/syntax.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
MyST is a strict superset of the [CommonMark syntax specification](https://spec.commonmark.org/).
88
It adds features focussed on scientific and technical documentation authoring, as detailed below.
99

10-
In addition, the roles and directives syntax provide inline/block-level extension points for plugins. This is detailed further in the [Roles and Directives](roles-directives) section.
10+
In addition, the roles and directives syntax provide inline/block-level extension points for plugins.
11+
This is detailed further in the [Roles and Directives](roles-directives) section.
1112

1213
:::{seealso}
1314
The [syntax token reference tables](syntax-tokens)
@@ -37,7 +38,6 @@ Ordered List | `1. item`
3738
Unordered List | `- item`
3839
Code Fence | opening ```` ```lang ```` to closing ```` ``` ````
3940

40-
4141
(syntax/frontmatter)=
4242

4343
## Front Matter

myst_parser/_docs.py

Lines changed: 73 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
"""Code to use internally, for documentation."""
22
from __future__ import annotations
33

4+
import io
45
from typing import Sequence, Union
56

67
from docutils import nodes
8+
from docutils.frontend import OptionParser
79
from docutils.parsers.rst import directives
10+
from sphinx.directives import other
11+
from sphinx.util import logging
812
from sphinx.util.docutils import SphinxDirective
913
from typing_extensions import get_args, get_origin
1014

1115
from .config.main import MdParserConfig
16+
from .parsers.docutils_ import Parser as DocutilsParser
17+
18+
logger = logging.getLogger(__name__)
1219

1320

1421
class _ConfigBase(SphinxDirective):
@@ -117,16 +124,75 @@ class DocutilsCliHelpDirective(SphinxDirective):
117124

118125
def run(self):
119126
"""Run the directive."""
120-
import io
121-
122-
from docutils import nodes
123-
from docutils.frontend import OptionParser
124-
125-
from myst_parser.parsers.docutils_ import Parser as DocutilsParser
126-
127127
stream = io.StringIO()
128128
OptionParser(
129129
components=(DocutilsParser,),
130130
usage="myst-docutils-<writer> [options] [<source> [<destination>]]",
131131
).print_help(stream)
132132
return [nodes.literal_block("", stream.getvalue())]
133+
134+
135+
class DirectiveDoc(SphinxDirective):
136+
"""Load and document a directive."""
137+
138+
required_arguments = 1 # name of the directive
139+
has_content = True
140+
141+
def run(self):
142+
"""Run the directive."""
143+
name = self.arguments[0]
144+
# load the directive class
145+
klass, _ = directives.directive(
146+
name, self.state.memo.language, self.state.document
147+
)
148+
if klass is None:
149+
logger.warning(f"Directive {name} not found.", line=self.lineno)
150+
return []
151+
content = " ".join(self.content)
152+
text = f"""\
153+
:Name: `{name}`
154+
:Description: {content}
155+
:Arguments: {klass.required_arguments} required, {klass.optional_arguments} optional
156+
:Content: {'yes' if klass.has_content else 'no'}
157+
:Options:
158+
"""
159+
if klass.option_spec:
160+
text += " name | type\n -----|------\n"
161+
for key, func in klass.option_spec.items():
162+
text += f" {key} | {convert_opt(name, func)}\n"
163+
node = nodes.Element()
164+
self.state.nested_parse(text.splitlines(), 0, node)
165+
return node.children
166+
167+
168+
def convert_opt(name, func):
169+
"""Convert an option function to a string."""
170+
if func is directives.flag:
171+
return "flag"
172+
if func is directives.unchanged:
173+
return "text"
174+
if func is directives.unchanged_required:
175+
return "text"
176+
if func is directives.class_option:
177+
return "space-delimited list"
178+
if func is directives.uri:
179+
return "URI"
180+
if func is directives.path:
181+
return "path"
182+
if func is int:
183+
return "integer"
184+
if func is directives.positive_int:
185+
return "integer (positive)"
186+
if func is directives.nonnegative_int:
187+
return "integer (non-negative)"
188+
if func is directives.positive_int_list:
189+
return "space/comma-delimited list of integers (positive)"
190+
if func is directives.percentage:
191+
return "percentage"
192+
if func is directives.length_or_unitless:
193+
return "length or unitless"
194+
if func is directives.length_or_percentage_or_unitless:
195+
return "length, percentage or unitless"
196+
if func is other.int_or_nothing:
197+
return "integer"
198+
return ""

0 commit comments

Comments
 (0)