Skip to content

Commit 41a5061

Browse files
authored
📚 DOCS: Add myst-example directive (#705)
1 parent fc59f3b commit 41a5061

5 files changed

Lines changed: 74 additions & 78 deletions

File tree

docs/_static/local.css

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ h3:not(.tippy-header)::before {
1515
content: counter(subsection) "." counter(subsubsection) ". ";
1616
}
1717

18+
/** MyST examples */
19+
.myst-example {
20+
border: 1px solid var(--pst-color-border);
21+
border-left-color: var(--pst-color-info);
22+
border-left-width: .2em;
23+
box-shadow: rgba(0, 0, 0, 0.25) 0px 5px 15px;
24+
margin-bottom: 1rem;
25+
}
26+
.myst-example-source > div[class*="highlight-"]{
27+
margin: 0;
28+
}
29+
.myst-example-render {
30+
padding: 0.5rem;
31+
}
32+
1833
/** No icon for admonitions with no-icon class */
1934
.admonition > .admonition-title, div.admonition.no-icon > .admonition-title::before {
2035
content: "";

docs/conf.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"sphinx.ext.viewcode",
3434
"sphinx.ext.autodoc",
3535
"sphinx_design",
36+
"sphinx_copybutton",
3637
"sphinxext.rediraffe",
3738
"sphinxcontrib.mermaid",
3839
"sphinxext.opengraph",
@@ -212,13 +213,15 @@ def setup(app: Sphinx):
212213
DirectiveDoc,
213214
DocutilsCliHelpDirective,
214215
MystConfigDirective,
216+
MystExampleDirective,
215217
MystWarningsDirective,
216218
)
217219

218220
app.add_directive("myst-config", MystConfigDirective)
219221
app.add_directive("docutils-cli-help", DocutilsCliHelpDirective)
220222
app.add_directive("doc-directive", DirectiveDoc)
221223
app.add_directive("myst-warnings", MystWarningsDirective)
224+
app.add_directive("myst-example", MystExampleDirective)
222225
app.add_post_transform(StripUnsupportedLatex)
223226
app.connect("html-page-context", add_version_to_css)
224227

docs/syntax/syntax.md

Lines changed: 23 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -157,15 +157,11 @@ myst:
157157
You may add comments by putting the `%` character at the beginning of a line. This will
158158
prevent the line from being parsed into the output document.
159159

160-
For example, this code:
161-
162-
```md
163-
% my comment
164-
```
165-
166-
Is below, but it won't be parsed into the document.
160+
For example, this won't be parsed into the document:
167161

162+
:::{myst-example}
168163
% my comment
164+
:::
169165

170166
````{important}
171167
Since comments are a block-level entity, they will terminate the previous block.
@@ -197,16 +193,12 @@ like [jupyter notebooks](https://jupyter.org/),
197193
to indicate a new text cell. It will not show up in the rendered text,
198194
but is stored in the internal document structure for use by developers.
199195

200-
For example, this code:
196+
For example, this won't be parsed into the document:
201197

202-
```md
198+
```{myst-example}
203199
+++ some text
204200
```
205201

206-
Is below, but it won't be parsed into the document.
207-
208-
+++
209-
210202
(syntax/referencing)=
211203

212204
## Markdown Links and Referencing
@@ -529,24 +521,21 @@ markdown: `[](syntax.md)` will result in: [](syntax.md).
529521
Code blocks contain a language identifier, which is used to determine the language of the code.
530522
This language is used to determine the syntax highlighting, using an available [pygments lexer](https://pygments.org/docs/lexers/).
531523

532-
````markdown
533-
```python
534-
from a import b
535-
c = "string"
536-
```
537-
````
538524

525+
:::{myst-example}
539526
```python
540527
from a import b
541528
c = "string"
542529
```
530+
:::
543531

544532
You can create and register your own lexer, using the [`pygments.lexers` entry point](https://pygments.org/docs/plugins/#register-plugins),
545533
or within a sphinx extension, with the [`app.add_lexer` method](inv:sphinx#*.Sphinx.add_lexer).
546534

547535
Using the `myst_number_code_blocks` configuration option, you can also control whether code blocks are numbered by line.
548536
For example, using `myst_number_code_blocks = ["typescript"]`:
549537

538+
:::{myst-example}
550539
```typescript
551540
type MyBool = true | false;
552541

@@ -555,58 +544,45 @@ interface User {
555544
id: number;
556545
}
557546
```
547+
:::
558548

559549
### Show backticks inside raw markdown blocks
560550

561551
If you'd like to show backticks inside of your markdown, you can do so by nesting them
562552
in backticks of a greater length. Markdown will treat the outer-most backticks as the
563553
edges of the "raw" block and everything inside will show up. For example:
564554

565-
``` `` `hi` `` ``` will be rendered as: `` `hi` ``
566-
567-
and
568-
569-
`````
570-
````
571-
```
572-
hi
573-
```
574-
````
575-
`````
576-
577-
will be rendered as:
555+
:::{myst-example}
556+
`` `hi` ``
578557

579558
````
580559
```
581560
hi
582561
```
583562
````
563+
:::
584564

585565
## Tables
586566

587567
Tables can be written using the standard [Github Flavoured Markdown syntax](https://github.github.com/gfm/#tables-extension-):
588568

569+
:::{myst-example}
589570
```md
590571
| foo | bar |
591572
| --- | --- |
592573
| baz | bim |
593574
```
594-
595-
| foo | bar |
596-
| --- | --- |
597-
| baz | bim |
575+
:::
598576

599577
Cells in a column can be aligned using the `:` character:
600578

579+
:::{myst-example}
601580
```md
602581
| left | center | right |
603582
| :--- | :----: | ----: |
604583
| a | b | c |
605584
```
606-
607-
| left | center | right |
608-
| :--- | :----: | ----: |
609-
| a | b | c |
585+
:::
610586

611587
:::{note}
612588

@@ -632,11 +608,9 @@ MyST provides a few different syntaxes for including images in your documentatio
632608

633609
The standard Markdown syntax is:
634610

635-
```md
636-
![fishy](img/fun-fish.png)
637-
```
638-
611+
:::{myst-example}
639612
![fishy](img/fun-fish.png)
613+
:::
640614

641615
But you can also enable extended image syntaxes, to control attributes like width and captions.
642616
See the [extended image syntax guide](syntax/images).
@@ -653,24 +627,18 @@ Their labels **start with `^`** and can then be any alphanumeric string (no spac
653627
All footnote definitions are collected, and displayed at the bottom of the page (in the order they are referenced).
654628
Note that un-referenced footnote definitions will not be displayed.
655629

656-
```md
657-
- This is a manually-numbered footnote reference.[^3]
658-
- This is an auto-numbered footnote reference.[^myref]
659-
660-
[^myref]: This is an auto-numbered footnote definition.
661-
[^3]: This is a manually-numbered footnote definition.
662-
```
663-
630+
:::{myst-example}
664631
- This is a manually-numbered footnote reference.[^3]
665632
- This is an auto-numbered footnote reference.[^myref]
666633

667634
[^myref]: This is an auto-numbered footnote definition.
668635
[^3]: This is a manually-numbered footnote definition.
636+
:::
669637

670638
Any preceding text after a footnote definitions, which is
671639
indented by four or more spaces, will also be included in the footnote definition, and the text is rendered as MyST Markdown, e.g.
672640

673-
```md
641+
:::{myst-example}
674642
A longer footnote definition.[^mylongdef]
675643

676644
[^mylongdef]: This is the _**footnote definition**_.
@@ -683,37 +651,14 @@ A longer footnote definition.[^mylongdef]
683651
that are not separated by a blank line
684652

685653
This is not part of the footnote.
686-
```
687-
688-
A longer footnote definition.[^mylongdef]
689-
690-
[^mylongdef]: This is the _**footnote definition**_.
691-
692-
That continues for all indented lines
693-
694-
- even other block elements
695-
696-
Plus any subsequent unindented lines,
697-
that are not separated by a blank line
698-
699-
This is not part of the footnote.
654+
:::
700655

701656
````{important}
702657
Although footnote references can be used just fine within directives, e.g.[^myref],
703658
it is recommended that footnote definitions are not set within directives,
704659
unless they will only be referenced within that same directive:
705660
706-
```md
707-
[^other]
708-
709-
[^other]: A definition within a directive
710-
```
711-
712-
[^other]
713-
714-
[^other]: A definition within a directive
715-
716-
This is because, in the current implementation, they may not be available to reference in text above that particular directive.
661+
This is because, they may not be available to reference in text outside that particular directive.
717662
````
718663

719664
By default, a transition line (with a `footnotes` class) will be placed before any footnotes.

myst_parser/_docs.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,35 @@ def run(self):
224224
node = nodes.Element()
225225
self.state.nested_parse(text, 0, node)
226226
return node.children
227+
228+
229+
class MystExampleDirective(SphinxDirective):
230+
"""Directive to print an example."""
231+
232+
has_content = True
233+
option_spec = {
234+
"html": directives.flag,
235+
}
236+
237+
def run(self):
238+
"""Run the directive."""
239+
content_str = "\n".join(self.content)
240+
backticks = "```"
241+
while backticks in content_str:
242+
backticks += "`"
243+
content = f"""
244+
{backticks}``{{div}} myst-example
245+
246+
{backticks}`{{div}} myst-example-source
247+
{backticks}markdown
248+
{content_str}
249+
{backticks}
250+
{backticks}`
251+
{backticks}`{{div}} myst-example-render
252+
{content_str}
253+
{backticks}`
254+
{backticks}``
255+
"""
256+
node_ = nodes.Element()
257+
self.state.nested_parse(content.splitlines(), self.content_offset, node_)
258+
return node_.children

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ rtd = [
5757
# currently required to get sphinx v5
5858
"sphinx-book-theme==0.4.0rc1",
5959
"sphinx-design",
60+
"sphinx-copybutton",
6061
"sphinxext-rediraffe~=0.2.7",
6162
"sphinxcontrib.mermaid~=0.7.1",
6263
"sphinxext-opengraph~=0.7.5",

0 commit comments

Comments
 (0)