Skip to content

Commit

Permalink
✨ Add ndf role
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjsewell committed Sep 16, 2024
1 parent d25bd33 commit a3c642c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions sphinx_needs/needs.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ def setup(app: Sphinx) -> dict[str, Any]:
)

app.add_role("need_func", NeedFuncRole())
app.add_role("ndf", NeedFuncRole(no_braces=True))

########################################################################
# EVENTS
Expand Down
22 changes: 20 additions & 2 deletions sphinx_needs/roles/need_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,21 @@
class NeedFuncRole(SphinxRole):
"""Role for creating ``NeedFunc`` node."""

def __init__(self, no_braces: bool = False) -> None:
"""Initialize the role.
:param no_braces: If True, the function should not be wrapped ``[[]]``.
"""
self.no_braces = no_braces
super().__init__()

def run(self) -> tuple[list[nodes.Node], list[nodes.system_message]]:
add_doc(self.env, self.env.docname)
node = NeedFunc(
self.rawtext, nodes.literal(self.rawtext, self.text), **self.options
self.rawtext,
nodes.literal(self.rawtext, self.text),
no_braces=self.no_braces,
**self.options,
)
self.set_source_info(node)
return [node], []
Expand All @@ -31,7 +42,14 @@ def run(self) -> tuple[list[nodes.Node], list[nodes.system_message]]:
class NeedFunc(nodes.Inline, nodes.Element):
def get_text(self, env: BuildEnvironment, need: NeedsInfoType | None) -> nodes.Text:
"""Execute function and return result."""
from sphinx_needs.functions.functions import check_and_get_content
from sphinx_needs.functions.functions import check_and_get_content, execute_func

if self.get("no_braces"):
func_return = execute_func(env.app, need, self.astext(), self)
if isinstance(func_return, list):
func_return = ", ".join(str(el) for el in func_return)

Check warning on line 50 in sphinx_needs/roles/need_func.py

View check run for this annotation

Codecov / codecov/patch

sphinx_needs/roles/need_func.py#L50

Added line #L50 was not covered by tests

return nodes.Text("" if func_return is None else str(func_return))

result = check_and_get_content(self.astext(), need, env, self)
return nodes.Text(str(result))
Expand Down
6 changes: 5 additions & 1 deletion tests/doc_test/doc_dynamic_functions/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ DYNAMIC FUNCTIONS

This is also id :need_func:`[[copy("id")]]`

This is the best id :ndf:`copy("id")`

.. spec:: TEST_2
:id: TEST_2
:tags: my_tag; [[copy("tags", "SP_TOO_001")]]
Expand Down Expand Up @@ -37,4 +39,6 @@ DYNAMIC FUNCTIONS

nested id also :need_func:`[[copy("id")]]`

This should warn since it has no associated need: :need_func:`[[copy("id")]]`
nested id best :ndf:`copy("id")`

These should warn since they have no associated need: :need_func:`[[copy("id")]]`, :ndf:`copy("id")`
5 changes: 4 additions & 1 deletion tests/test_dynamic_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ def test_doc_dynamic_functions(test_app):
app._warning.getvalue().replace(str(app.srcdir) + os.sep, "srcdir/")
).splitlines()
assert warnings == [
"srcdir/index.rst:40: WARNING: Error while executing function 'copy': Need not found [needs.dynamic_function]"
"srcdir/index.rst:44: WARNING: Error while executing function 'copy': Need not found [needs.dynamic_function]",
"srcdir/index.rst:44: WARNING: Error while executing function 'copy': Need not found [needs.dynamic_function]",
]

html = Path(app.outdir, "index.html").read_text()
assert "This is id SP_TOO_001" in html
assert "This is also id SP_TOO_001" in html
assert "This is the best id SP_TOO_001" in html

assert (
sum(1 for _ in re.finditer('<span class="needs_data">test2</span>', html)) == 2
Expand Down Expand Up @@ -67,6 +69,7 @@ def test_doc_dynamic_functions(test_app):

assert "nested id TEST_6" in html
assert "nested id also TEST_6" in html
assert "nested id best TEST_6" in html


@pytest.mark.parametrize(
Expand Down

0 comments on commit a3c642c

Please sign in to comment.