Skip to content

Commit 7692f87

Browse files
authored
✨ New: add .glossary for attrs_block (#719)
MyST follows Pandoc definition-lists, to denote lists of term -> definition. ```markdown Term 1 : Definition Term 2 : Definition ``` It should be easy for users to turn one of these lists into a glossary, where each term is uniquely referenceable across the entire project. This commit, allows this, via adding a `.glossary` class to the list: ```` {.glossary} Term 1 : Definition Term 2 : Definition ````
1 parent 7fe6411 commit 7692f87

File tree

6 files changed

+71
-2
lines changed

6 files changed

+71
-2
lines changed

myst_parser/mdit_to_docutils/base.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,7 @@ def render_dl(self, token: SyntaxTreeNode) -> None:
15181518
node = nodes.definition_list(classes=["simple", "myst"])
15191519
self.copy_attributes(token, node, ("class", "id"))
15201520
self.add_line_and_source_path(node, token)
1521+
make_terms = ("glossary" in node["classes"]) and (self.sphinx_env is not None)
15211522
with self.current_node_context(node, append=True):
15221523
item = None
15231524
for child in token.children or []:
@@ -1529,8 +1530,21 @@ def render_dl(self, token: SyntaxTreeNode) -> None:
15291530
child.children[0].content if child.children else ""
15301531
)
15311532
self.add_line_and_source_path(term, child)
1532-
with self.current_node_context(term, append=True):
1533+
with self.current_node_context(term):
15331534
self.render_children(child)
1535+
if make_terms:
1536+
from sphinx.domains.std import make_glossary_term
1537+
1538+
term = make_glossary_term(
1539+
self.sphinx_env, # type: ignore
1540+
term.children,
1541+
None, # type: ignore
1542+
term.source,
1543+
term.line,
1544+
node_id=None, # type: ignore
1545+
document=self.document,
1546+
)
1547+
self.current_node.append(term)
15341548
elif child.type == "dd":
15351549
if item is None:
15361550
error = self.reporter.error(

tests/test_renderers/fixtures/myst-config.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ a
341341
<string>:1: (WARNING/2) Invalid 'align' attribute value: 'other' [myst.attribute]
342342
.
343343

344-
[attr_block] --myst-enable-extensions=attrs_block
344+
[attrs_block] --myst-enable-extensions=attrs_block
345345
.
346346
{#myid1 .class1 .class2}
347347
{#myid2 .class3}

tests/test_sphinx/sourcedirs/extended_syntaxes/conf.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
"colon_fence",
1313
"linkify",
1414
"tasklist",
15+
"attrs_inline",
16+
"attrs_block",
1517
]
1618
myst_number_code_blocks = ["typescript"]
1719
myst_html_meta = {

tests/test_sphinx/sourcedirs/extended_syntaxes/index.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ Term 3
3939

4040
: other
4141

42+
{#myid1 .glossary}
43+
term
44+
: definition
45+
46+
other term
47+
: other definition
48+
49+
{term}`other term`
50+
4251
:::{figure-md} target
4352
:class: other
4453

tests/test_sphinx/test_sphinx_builds/test_extended_syntaxes.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,31 @@ <h1>
103103
</p>
104104
</dd>
105105
</dl>
106+
<dl class="simple myst glossary" id="myid1">
107+
<dt id="term-term">
108+
term
109+
</dt>
110+
<dd>
111+
<p>
112+
definition
113+
</p>
114+
</dd>
115+
<dt id="term-other-term">
116+
other term
117+
</dt>
118+
<dd>
119+
<p>
120+
other definition
121+
</p>
122+
</dd>
123+
</dl>
124+
<p>
125+
<a class="reference internal" href="#term-other-term">
126+
<span class="xref std std-term">
127+
other term
128+
</span>
129+
</a>
130+
</p>
106131
<figure class="other align-default" id="target">
107132
<img alt="fun-fish" src="_images/fun-fish.png"/>
108133
<figcaption>

tests/test_sphinx/test_sphinx_builds/test_extended_syntaxes.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,25 @@
6161
<definition>
6262
<paragraph>
6363
other
64+
<definition_list classes="simple myst glossary" ids="myid1" names="myid1">
65+
<definition_list_item>
66+
<term ids="term-term">
67+
term
68+
<index entries="('single',\ 'term',\ 'term-term',\ 'main',\ None)">
69+
<definition>
70+
<paragraph>
71+
definition
72+
<definition_list_item>
73+
<term ids="term-other-term">
74+
other term
75+
<index entries="('single',\ 'other\ term',\ 'term-other-term',\ 'main',\ None)">
76+
<definition>
77+
<paragraph>
78+
other definition
79+
<paragraph>
80+
<pending_xref refdoc="index" refdomain="std" refexplicit="False" reftarget="other term" reftype="term" refwarn="True">
81+
<inline classes="xref std std-term">
82+
other term
6483
<figure classes="other" ids="target" names="target">
6584
<image alt="fun-fish" candidates="{'*': 'fun-fish.png'}" uri="fun-fish.png">
6685
<caption>

0 commit comments

Comments
 (0)