Skip to content

Commit 723cffc

Browse files
authored
♻️ REFACTOR: docs example directive via sphinx-syntax-example (#1181)
1 parent 4ec4cf8 commit 723cffc

16 files changed

Lines changed: 192 additions & 163 deletions

.pre-commit-config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ repos:
3737
- sphinx~=8.2
3838
- markdown-it-py~=4.2
3939
- mdit-py-plugins~=0.6.0
40+
- sphinx-syntax-example~=0.2.0
4041
files: >
4142
(?x)^(
4243
myst_parser/.*py|

docs/_static/local.css

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,24 @@ h1:not(.rubric) {
99
text-decoration: underline;
1010
}
1111

12-
/** MyST examples */
13-
.myst-example {
12+
/** Syntax examples */
13+
.syntax-example {
1414
border: 1px solid var(--pst-color-border);
1515
border-left-color: var(--pst-color-info);
1616
border-left-width: .2em;
1717
box-shadow: rgba(0, 0, 0, 0.25) 0px 5px 15px;
1818
margin-bottom: 1rem;
1919
}
20-
.myst-example-source > div[class*="highlight-"]{
20+
/* The source pane *is* the highlight-<lang> div (not a wrapper around one),
21+
so zero its margin here to keep it flush inside the frame.
22+
local.css loads last, so this wins. */
23+
.syntax-example > .syntax-example-source {
2124
margin: 0;
2225
}
23-
.myst-example-render {
26+
.syntax-example-render {
2427
padding: 0.5rem;
2528
}
26-
.myst-example-render > :first-child {
29+
.syntax-example-render > :first-child {
2730
margin-top: 0.5rem;
2831
}
2932

docs/conf.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,19 +223,21 @@ def setup(app: Sphinx):
223223
DocutilsCliHelpDirective,
224224
MystAdmonitionDirective,
225225
MystConfigDirective,
226-
MystExampleDirective,
227226
MystLexer,
228227
MystToHTMLDirective,
229228
MystWarningsDirective,
230229
NumberSections,
231230
StripUnsupportedLatex,
231+
SyntaxExampleDocsDirective,
232232
)
233233

234234
app.add_directive("myst-config", MystConfigDirective)
235235
app.add_directive("docutils-cli-help", DocutilsCliHelpDirective)
236236
app.add_directive("doc-directive", DirectiveDoc)
237237
app.add_directive("myst-warnings", MystWarningsDirective)
238-
app.add_directive("myst-example", MystExampleDirective)
238+
# NOTE: if `sphinx_syntax_example` is ever added to `extensions` (e.g. to pick
239+
# up its stylesheet), drop this line too — it registers the same directive.
240+
app.add_directive("syntax-example", SyntaxExampleDocsDirective)
239241
app.add_directive("myst-admonitions", MystAdmonitionDirective)
240242
app.add_directive("myst-to-html", MystToHTMLDirective)
241243
app.add_post_transform(StripUnsupportedLatex)

docs/intro.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ extensions = [
147147

148148
Now, we can use the `design` directive to add a web-component to our Markdown file!
149149

150-
::::{myst-example}
150+
::::{syntax-example}
151151
:::{card} Card Title
152152
Header
153153
^^^
@@ -158,7 +158,7 @@ Footer
158158
::::
159159

160160

161-
::::::{myst-example}
161+
::::::{syntax-example}
162162

163163
::::{tab-set}
164164

@@ -196,7 +196,7 @@ Content 2
196196
% Now, add a **mermaid directive** to your Markdown file.
197197
% For example:
198198

199-
% :::{myst-example}
199+
% :::{syntax-example}
200200
% Here's a cool mermaid diagram!
201201
%
202202
% ```{mermaid}

docs/syntax/admonitions.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The [alert extension](syntax/alerts) provides GitHub-style alerts as a lightweig
1010
Admonitions are a special case of {{directive}} extensions.
1111
It is advised to use admonitions with the [colon_fence](#syntax/colon_fence) extension, which signify that the content of the block is also MyST Markdown.
1212

13-
::::{myst-example}
13+
::::{syntax-example}
1414

1515
:::{tip}
1616
Let's give readers a helpful hint!
@@ -30,7 +30,7 @@ These admonitions take no argument, but may be specified with options:
3030
:class: A space-separated list of CSS classes to add to the admonition, conforming to the [identifier normalization rules](https://docutils.sourceforge.io/docs/ref/rst/directives.html#identifier-normalization).
3131
:name: A reference target for the admonition (see [cross-referencing](#syntax/referencing)).
3232

33-
::::{myst-example}
33+
::::{syntax-example}
3434

3535
:::{tip}
3636
:class: myclass1 myclass2
@@ -44,7 +44,7 @@ Let's give readers a helpful hint!
4444

4545
Sphinx also adds a number of additional admonition types, for denoting changes to the documentation, or to the codebase:
4646

47-
::::{myst-example}
47+
::::{syntax-example}
4848

4949
:::{versionadded} 1.2.3
5050
Explanation of the new feature.
@@ -66,7 +66,7 @@ To provide a custom title for an admonition, use the `admonition` directive.
6666
If you also want to style the admonition as one of the core admonition types,
6767
you can use the `admonition` directive with the `class` option.
6868

69-
::::{myst-example}
69+
::::{syntax-example}
7070

7171
:::{admonition} My custom title with *Markdown*!
7272
:class: tip
@@ -80,7 +80,7 @@ This is a custom title for a tip admonition.
8080

8181
The [sphinx-togglebutton](https://sphinx-togglebutton.readthedocs.io) extension allows you to create collapsible admonitions, by adding a `dropdown` class to the admonition.
8282

83-
::::{myst-example}
83+
::::{syntax-example}
8484

8585
:::{note}
8686
:class: dropdown
@@ -97,7 +97,7 @@ without it taking up too much space on the page.
9797
Using the [colon_fence](#syntax/colon_fence) extension,
9898
content block can be wrapped in containers with a custom CSS class.
9999

100-
::::{myst-example}
100+
::::{syntax-example}
101101
:::bg-primary
102102
This is a container with a custom CSS class.
103103

@@ -108,7 +108,7 @@ This is a container with a custom CSS class.
108108
Using the [sphinx-design](https://github.com/executablebooks/sphinx-design) extension,
109109
it is also possible to create beautiful, screen-size responsive web-components.
110110

111-
::::{myst-example}
111+
::::{syntax-example}
112112
:::{card} Card Title
113113
Header
114114
^^^
@@ -119,7 +119,7 @@ Footer
119119
::::
120120

121121

122-
::::::{myst-example}
122+
::::::{syntax-example}
123123

124124
::::{tab-set}
125125

docs/syntax/code_and_apis.md

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Code blocks contain a language identifier, which is used to determine the language of the code.
77
This language is used to determine the syntax highlighting, using an available [pygments lexer](https://pygments.org/docs/lexers/).
88

9-
:::{myst-example}
9+
:::{syntax-example}
1010
```python
1111
from a import b
1212
c = "string"
@@ -27,7 +27,7 @@ you can do so by nesting them in backticks of a greater length.
2727
Markdown will treat the outer-most backticks as the edges of the "raw" block and everything inside will show up.
2828
For example:
2929

30-
:::{myst-example}
30+
:::{syntax-example}
3131
`` `hi` ``
3232

3333
````
@@ -43,7 +43,7 @@ hi
4343

4444
The [attrs_inline](#syntax/attributes/inline) extension can be used to apply syntax highlighting to inline code:
4545

46-
:::{myst-example}
46+
:::{syntax-example}
4747
Inline Python code `a = "b"`{l=python}
4848
:::
4949

@@ -59,7 +59,7 @@ myst_number_code_blocks = ["typescript"]
5959

6060
Will number all code blocks with the `typescript` lexer by default.
6161

62-
:::{myst-example}
62+
:::{syntax-example}
6363
```typescript
6464
type MyBool = true | false;
6565

@@ -73,7 +73,7 @@ interface User {
7373
To apply numbering and highlighting to a specific code block,
7474
the [attrs_block](#syntax/attributes/block) extension can be used:
7575

76-
:::{myst-example}
76+
:::{syntax-example}
7777
{lineno-start=1 emphasize-lines="2,3"}
7878
```python
7979
a = 1
@@ -87,7 +87,7 @@ c = 3
8787
With the `code-block` {{directive}},
8888
a caption can be added to a code blocks, as well as other options:
8989

90-
:::{myst-example}
90+
:::{syntax-example}
9191
```{code-block} python
9292
:caption: This is a caption
9393
:emphasize-lines: 2,3
@@ -136,7 +136,7 @@ The [Sphinx documentation](inv:sphinx#code-block)
136136

137137
Longer pieces of code can be included from files using the `literalinclude` {{directive}}:
138138

139-
:::{myst-example}
139+
:::{syntax-example}
140140
```{literalinclude} examples/example.py
141141
```
142142
:::
@@ -145,7 +145,7 @@ The file name is usually relative to the current file’s path. However, if it i
145145

146146
To select only a sub-section of the file, the `lines`, `pyobject` or `start-after` and `end-before` options can be used:
147147

148-
:::{myst-example}
148+
:::{syntax-example}
149149
```{literalinclude} examples/example.py
150150
:start-after: start example
151151
:end-before: end example
@@ -175,7 +175,7 @@ As opposed to `sphinx.ext.autodoc`, `sphinx-autodoc2` performs static (rather th
175175
Alternatively, the `autodoc2-object` directive can be used to generate documentation for a single object.
176176
To embed in a MyST document the MyST `render_plugin` should be specified, for example:
177177

178-
````{myst-example}
178+
````{syntax-example}
179179
```{autodoc2-object} myst_parser.sphinx_ext.main.setup_sphinx
180180
render_plugin = "myst"
181181
no_index = true
@@ -184,14 +184,14 @@ no_index = true
184184

185185
This can be referenced elsewhere in the document using the `:py:obj:` role, or a `#` link (see [cross-referencing](#syntax/referencing)).
186186

187-
````{myst-example}
187+
````{syntax-example}
188188
- {py:obj}`myst_parser.sphinx_ext.main.setup_sphinx`
189189
- [](#myst_parser.sphinx_ext.main.setup_sphinx)
190190
````
191191

192192
Additionally, summaries of multiple objects can be generated using the `autodoc2-summary` directive:
193193

194-
````{myst-example}
194+
````{syntax-example}
195195
```{autodoc2-summary}
196196
:renderer: myst
197197
@@ -215,7 +215,7 @@ autodoc2_docstring_parser_regexes = [
215215

216216
For example:
217217

218-
````{myst-example}
218+
````{syntax-example}
219219
```{autodoc2-object} myst_parser.setup
220220
render_plugin = "myst"
221221
no_index = true
@@ -231,7 +231,7 @@ docstring_parser_regexes = [
231231
[Sphinx extension `autodoc`](inv:sphinx#sphinx.ext.autodoc) also can generate documentation for Python objects.
232232
However, because it is hard-coded to generate RestructureText, the special [`eval-rst` directive](#syntax/directives/parsing) needs to be used:
233233

234-
````{myst-example}
234+
````{syntax-example}
235235
```{eval-rst}
236236
.. autofunction:: myst_parser.sphinx_ext.main.setup_sphinx
237237
:noindex:
@@ -240,7 +240,7 @@ However, because it is hard-coded to generate RestructureText, the special [`eva
240240

241241
Summaries can also be generated with [`autosummary`](inv:sphinx#sphinx.ext.autosummary):
242242

243-
````{myst-example}
243+
````{syntax-example}
244244
```{eval-rst}
245245
.. autosummary::
246246
:nosignatures:

docs/syntax/cross-referencing.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ primary HTML id, used by tocs, permalinks and `objects.inv`; the implicit
2323
heading id is kept as a secondary anchor, so existing fragment links keep working.
2424
```
2525

26-
::::{myst-example}
26+
::::{syntax-example}
2727

2828
(heading-target)=
2929
### Heading
@@ -47,7 +47,7 @@ This is a directive with a `name` option
4747
There are also other ways to create targets, specific to certain directives,
4848
such as [glossaries](#syntax/glossaries) create targets for terms, and [code APIs](#syntax/apis) create targets for objects:
4949

50-
::::{myst-example}
50+
::::{syntax-example}
5151
{.glossary}
5252
my other term
5353
: Definition of the term
@@ -79,7 +79,7 @@ By default the anchor "slugs" are created according to the [GitHub implementatio
7979

8080
For example, using `myst_heading_anchors = 2`:
8181

82-
::::{myst-example}
82+
::::{syntax-example}
8383
## A heading with slug
8484

8585
## A heading with slug
@@ -191,7 +191,7 @@ Otherwise, the link text will be the destination itself.
191191

192192
#### Autolinks
193193

194-
:::{myst-example}
194+
:::{syntax-example}
195195

196196
:External URL: <https://example.com>
197197
:Internal target reference: <project:#cross-references>
@@ -204,7 +204,7 @@ Otherwise, the link text will be the destination itself.
204204

205205
#### Inline links with implicit text
206206

207-
:::{myst-example}
207+
:::{syntax-example}
208208

209209
:External URL: [](https://example.com)
210210
:Internal target reference: [](#cross-references)
@@ -217,7 +217,7 @@ Otherwise, the link text will be the destination itself.
217217

218218
#### Inline links with explicit text
219219

220-
:::{myst-example}
220+
:::{syntax-example}
221221

222222
:External URL: [Explicit text](https://example.com)
223223
:Internal target reference: [Explicit text](#cross-references)
@@ -389,7 +389,7 @@ Sphinx offers numerous [roles for referencing](#usage/restructuredtext/roles) sp
389389

390390
These can also be used within MyST documents, although it is recommended to use the Markdown syntax where possible, which is more portable and native to MyST.
391391

392-
:::{myst-example}
392+
:::{syntax-example}
393393
Sphinx roles:
394394

395395
- {ref}`syntax/referencing`, {ref}`Explicit text <syntax/referencing>`

0 commit comments

Comments
 (0)