Skip to content

Commit c0a3a2f

Browse files
committed
Centralise nested directive checking
1 parent e770fad commit c0a3a2f

File tree

7 files changed

+26
-33
lines changed

7 files changed

+26
-33
lines changed

docs/_static/custom.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
/* required for nice formatting with furo */
2+
.need_container {
3+
overflow-y: scroll;
4+
}
25
table.need {
36
background-color: var(--color-code-background);
47
}

docs/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ This package is intended to bring API documentation for Rust crates to Sphinx:
1919
- Integrates with the Sphinx cross-referencing system and
2020
:external:doc:`intersphinx <usage/extensions/intersphinx>` indexing.
2121

22-
- Supports writing docstrings for any valid Sphinx parser (reStructuredText, MyST markdown, ...)
22+
- Supports writing docstrings for any valid Sphinx parser
23+
(reStructuredText, `MyST markdown <https://myst-parser.readthedocs.io>`__, ...)
2324

2425
Installation
2526
------------

python/sphinx_rust/directives/_core.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,24 @@ def rust_domain(self) -> RustDomain:
4141
def cache_path(self) -> str:
4242
return str(self.env.rust_cache_path) # type: ignore[attr-defined]
4343

44+
def is_nested(self, warn: bool = True) -> bool:
45+
"""Check if the directive is nested inside another directive.
46+
47+
If we are going to generate section nodes, then this is not allowed,
48+
since it would break the documentation structure.
49+
"""
50+
if warn and not self.state_machine.match_titles:
51+
# we are going to generate section nodes, and they will not work
52+
# if e.g. the directive is called from inside a directive
53+
LOGGER.warning(
54+
f"{self.name!r} directive can only be used at the root of the document",
55+
type="rust",
56+
subtype="root",
57+
)
58+
return self.state_machine.match_titles # type: ignore[no-any-return]
59+
4460
def create_section(self, title: str) -> nodes.section:
61+
"""Create a new section node."""
4562
section = nodes.section()
4663
self.set_source_info(section)
4764
section += nodes.title(text=title)

python/sphinx_rust/directives/crate.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,7 @@ class RustCrateAutoDirective(RustAutoDirective):
2626
"""Directive to auto-document a Rust crate."""
2727

2828
def run(self) -> list[nodes.Node]:
29-
if not self.state_machine.match_titles:
30-
# we are going to generate section nodes, and they will not work
31-
# if e.g. the directive is called from inside a directive
32-
LOGGER.warning(
33-
f"{self.name!r} directive can only be used at the root of the document",
34-
type="rust",
35-
subtype="root",
36-
)
29+
if self.is_nested():
3730
return []
3831

3932
qualifier = self.arguments[0]

python/sphinx_rust/directives/enum.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,7 @@ class RustEnumAutoDirective(RustAutoDirective):
2525
"""Directive to auto-document a Rust enum."""
2626

2727
def run(self) -> list[nodes.Node]:
28-
if not self.state_machine.match_titles:
29-
# we are going to generate section nodes, and they will not work
30-
# if e.g. the directive is called from inside a directive
31-
LOGGER.warning(
32-
f"{self.name!r} directive can only be used at the root of the document",
33-
type="rust",
34-
subtype="root",
35-
)
28+
if self.is_nested():
3629
return []
3730

3831
qualifier = self.arguments[0]

python/sphinx_rust/directives/module.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,7 @@ class RustModuleAutoDirective(RustAutoDirective):
2727
"""Directive to auto-document a Rust module."""
2828

2929
def run(self) -> list[nodes.Node]:
30-
if not self.state_machine.match_titles:
31-
# we are going to generate section nodes, and they will not work
32-
# if e.g. the directive is called from inside a directive
33-
LOGGER.warning(
34-
f"{self.name!r} directive can only be used at the root of the document",
35-
type="rust",
36-
subtype="root",
37-
)
30+
if self.is_nested():
3831
return []
3932

4033
qualifier = self.arguments[0]

python/sphinx_rust/directives/struct.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,7 @@ class RustStructAutoDirective(RustAutoDirective):
2626
"""Directive to auto-document a Rust struct."""
2727

2828
def run(self) -> list[nodes.Node]:
29-
if not self.state_machine.match_titles:
30-
# we are going to generate section nodes, and they will not work
31-
# if e.g. the directive is called from inside a directive
32-
LOGGER.warning(
33-
f"{self.name!r} directive can only be used at the root of the document",
34-
type="rust",
35-
subtype="root",
36-
)
29+
if self.is_nested():
3730
return []
3831

3932
qualifier = self.arguments[0]

0 commit comments

Comments
 (0)