Skip to content

Commit

Permalink
Add support for global tags to list2need
Browse files Browse the repository at this point in the history
  • Loading branch information
OFFICE\cs authored and chrisjsewell committed Oct 4, 2024
1 parent 3534131 commit eb29af0
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 14 deletions.
20 changes: 20 additions & 0 deletions docs/directives/list2need.rst
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,26 @@ The amount of given link-types must be the amount of used levels minus 1.
* (NEED-D)Recalculate hash and compare


tags
~~~~

``tags`` sets tags globally to all items in the list.

.. code-block:: rst
.. list2need::
:types: req, spec
:tags: A, B
* (NEED-A)Login user
* (NEED-B)Provide login screen
* (NEED-C)Create password hash
* (NEED-D)Recalculate hash and compare
The tags ``A`` and ``B`` are attached to all ``NEED-A``, ``NEED-B``, ``NEED-C`` and ``NEED-D``.


List examples
-------------

Expand Down
15 changes: 15 additions & 0 deletions sphinx_needs/directives/list2need.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def presentation(argument: str) -> Any:
"delimiter": directives.unchanged,
"presentation": directives.unchanged,
"links-down": directives.unchanged,
"tags": directives.unchanged,
}

def run(self) -> Sequence[nodes.Node]:
Expand Down Expand Up @@ -106,6 +107,10 @@ def run(self) -> Sequence[nodes.Node]:
f"Unknown link configured: {down_link_raw}. "
f"Allowed are {', '.join(link_types)}"
)

# Retrieve tags defined at list level
tags = self.options.get("tags", "")

list_needs = []
# Storing the data in a sorted list
for content_line in content_raw.split("\n"):
Expand Down Expand Up @@ -190,6 +195,16 @@ def run(self) -> Sequence[nodes.Node]:
list_need["title"] = OPTION_AREA_REGEX.sub("", list_need["title"])
list_need["content"] = OPTION_AREA_REGEX.sub("", list_need["content"])

# Add tags defined at list level (if exists) to the ones potentially defined in the content
if tags is not None:
if "options" not in list_need:
list_need["options"] = {}
current_tags = list_need["options"].get("tags", "")
if current_tags:
list_need["options"]["tags"] = current_tags + "," + tags
else:
list_need["options"]["tags"] = tags

template = Template(NEED_TEMPLATE, autoescape=True)

data = list_need
Expand Down
19 changes: 19 additions & 0 deletions tests/doc_test/doc_list2need/global_tags.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
LIST2NEED GLOBAL TAGS
=====================


.. list2need::
:types: story, spec, test
:presentation: nested
:delimiter: .
:tags: global_tag1, global_tag2, global_tag3

* (NEED-W)Need example on level 1 ((status="open"))
* (NEED-X)Need example on level 1 ((status="done", tags="tag1, tag2, tag3"))
* (NEED-Y)Link example ((links="NEED-W, NEED-Y"))
* (NEED-Z)New line example.
With some content in the next line.
((status="in progress", links="NEED-3"))



3 changes: 2 additions & 1 deletion tests/doc_test/doc_list2need/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ Foo Bar.
.. toctree::

options
links_down
links_down
global_tags
3 changes: 3 additions & 0 deletions tests/doc_test/doc_needextend/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ Need extend
:links: extend_test_003


.. needextend:: id == extend_test_003
:links: extend_test_004

.. needextend:: extend_test_003
:links: extend_test_004

Expand Down
13 changes: 0 additions & 13 deletions tests/test_list2need.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,6 @@
def test_doc_list2need_html(test_app):
app = test_app
app.build()
index_html = Path(app.outdir, "index.html").read_text()
assert "NEED-002" in index_html
assert "Sub-Need on level 3" in index_html
assert (
'<a class="reference internal" href="#test"><span class="std std-ref">Test chapter</span></a>'
in index_html
)

# Check parent-child linking (nested)
assert (
'<span class="parent_needs"><span><a class="reference internal" href="#NEED-002" title="NEED-003">NEED-002</a></span></span>'
in index_html
)

options_html = Path(app.outdir, "options.html").read_text()

Expand Down

0 comments on commit eb29af0

Please sign in to comment.