useblocks / sphinx-needs

Adds needs/requirements to sphinx
https://sphinx-needs.readthedocs.io/en/latest/index.html
MIT License
213 stars 66 forks source link

Request: enable needuml to modify needs #1282

Open AlexanderLanin opened 2 months ago

AlexanderLanin commented 2 months ago

Abstract

For every element used in needuml/needarch we want to add a link to the need itself to every image it's used in. A generated "used_in_image" option. The current approach is to inject a custom uml-function (like e.g. ref), which will modify the need element when used.

However modifications to the need do not take effect, as they simply happen too late in the workflow.

Approach in Detail:

Problem in Detail:

Possible solution:

Not addressed here in this issue:

Code just for reference:

current_app: sphinx.application.Sphinx | None = None

def get_needs() -> dict[str, dict[str, Any]]:
    assert current_app
    assert current_app.env
    # old way of accessing needs:
    assert current_app.env.needs_all_needs
    assert isinstance(current_app.env.needs_all_needs, dict)
    return current_app.env.needs_all_needs

def uml_element(need: dict | str):
    if isinstance(need, str):
        needs = get_needs()
        need = needs[need]

    # FIXME: can we access current document through app? and potentially current need?
    current_document = need["docname"]

    # Modifying needs has no effect at this point in time !!
    needs[need_id]["used_in_image"] += current_document + ".html;"

    link = sphinx_needs.diagrams_common.calculate_link(
        current_app, need, current_document
    )
    title = need["title"]
    need_id = need["id"]

    return f'"[[{link} {title}]]" as {need_id}'

def setup(app: sphinx.application.Sphinx):
    global current_app  # noqa: PLW0603
    current_app = app
AlexanderLanin commented 2 months ago

Correction: maybe the change is simple after all while preserving current behavior.

Currently when elements have the :hide: option, they do appear in e.g. needtable and they can be referenced from needuml.