Skip to content

Commit b6542ab

Browse files
Allow to add content to directive (#158)
* Allow to add content to directive * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Minor change --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent f9e3ddc commit b6542ab

File tree

6 files changed

+53
-1
lines changed

6 files changed

+53
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
## Unreleased
6+
7+
- Allow to add content to directive.
8+
59
## 1.13.1
610

711
- Fix multiline handling of group descriptions.

roots/test-nested/conf.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from __future__ import annotations
2+
3+
import sys
4+
from pathlib import Path
5+
6+
sys.path.insert(0, str(Path(__file__).parent))
7+
extensions = ["sphinx_argparse_cli"]
8+
nitpicky = True

roots/test-nested/index.rst

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.. sphinx_argparse_cli::
2+
:module: parser
3+
:func: make_1
4+
5+
Some text inside first directive.
6+
7+
.. sphinx_argparse_cli::
8+
:module: parser
9+
:func: make_2
10+
11+
Some text inside second directive.
12+
13+
Some text after directives.

roots/test-nested/parser.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from __future__ import annotations
2+
3+
from argparse import ArgumentParser
4+
5+
6+
def make_1() -> ArgumentParser:
7+
return ArgumentParser(prog="basic-1")
8+
9+
10+
def make_2() -> ArgumentParser:
11+
return ArgumentParser(prog="basic-2")

src/sphinx_argparse_cli/_logic.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def make_id(key: str) -> str:
5757

5858
class SphinxArgparseCli(SphinxDirective):
5959
name = "sphinx_argparse_cli"
60-
has_content = False
60+
has_content = True
6161
option_spec: ClassVar[dict[str, Any]] = {
6262
"module": unchanged_required,
6363
"func": unchanged_required,
@@ -173,6 +173,9 @@ def run(self) -> list[Node]:
173173
if epilog := self._pre_format(self.options.get("epilog", self.parser.epilog)):
174174
home_section += epilog
175175

176+
if self.content:
177+
self.state.nested_parse(self.content, self.content_offset, home_section)
178+
176179
return [home_section]
177180

178181
def _pre_format(self, block: None | str) -> None | paragraph | literal_block:

tests/test_logic.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,3 +297,16 @@ def test_with_default(build_outcome: str) -> None:
297297
* **"x"** - arg (default: True)
298298
"""
299299
)
300+
301+
302+
@pytest.mark.sphinx(buildername="html", testroot="nested")
303+
def test_nested_content(build_outcome: str) -> None:
304+
assert '<section id="basic-1---CLI-interface">' in build_outcome
305+
assert "<h1>basic-1 - CLI interface" in build_outcome
306+
assert "<h2>basic-1 opt" in build_outcome
307+
assert "<p>Some text inside first directive.</p>" in build_outcome
308+
assert '<section id="basic-2---CLI-interface">' in build_outcome
309+
assert "<h2>basic-2 - CLI interface" in build_outcome
310+
assert "<h3>basic-2 opt" in build_outcome
311+
assert "<p>Some text inside second directive.</p>" in build_outcome
312+
assert "<p>Some text after directives.</p>" in build_outcome

0 commit comments

Comments
 (0)