From 38c287067cb5836a2db475ab5ed1ddb5273f9bab Mon Sep 17 00:00:00 2001 From: Simon Leiner Date: Fri, 20 Oct 2023 10:13:16 +0200 Subject: [PATCH] Fix usage of reST syntax in prefix parameter of meta (#1046) The usage of the `prefix` parameter of the `meta` layout function (as per the docs) does not work. There are two problems with this: 1. The documentation for `meta` does not mention that backslashes need to be escaped and shows unescaped backslashes instead. The docs for `meta_all` do not make this mistake. 2. [Since version 0.16](https://docutils.sourceforge.io/HISTORY.html#release-0-16-2020-01-16), docutils inserts null-characters when parsing escaped backslashes, which in turn is not accepted by [`ast.parse`](https://docs.python.org/3/library/ast.html#ast.parse). The solution is described in the [release notes for docutils 0.16](https://docutils.sourceforge.io/HISTORY.html#release-0-16-2020-01-16): Using `node.astext()` instead of `str(node)` --- AUTHORS | 2 ++ sphinx_needs/layout.py | 8 ++++++-- tests/doc_test/doc_layout/conf.py | 7 +++++++ tests/doc_test/doc_layout/index.rst | 4 ++++ tests/test_layouts.py | 5 ++++- 5 files changed, 23 insertions(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index 821726ea2..b37fbbb7c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -37,3 +37,5 @@ Duodu Randy Christian Wappler Chris Sewell + +Simon Leiner diff --git a/sphinx_needs/layout.py b/sphinx_needs/layout.py index 887d2fea8..38b123e50 100644 --- a/sphinx_needs/layout.py +++ b/sphinx_needs/layout.py @@ -397,7 +397,7 @@ def _func_replace(self, section_nodes: List[nodes.Node]) -> List[nodes.Node]: node.replace(child, new_child) # type: ignore[attr-defined] return_nodes.append(node) else: - node_str = str(node) + node_str = node.astext() # func_elements = re.findall(r'<<([a-z_()]*)>>', node_str) node_line = nodes.inline() @@ -486,7 +486,11 @@ def meta(self, name: str, prefix: Optional[str] = None, show_empty: bool = False Returns the specific metadata of a need inside docutils nodes. Usage:: - <> + <> + + .. note:: + + You must escape all rst_content in your function strings! E.g. to get `**` one must use `\\\\*\\\\*`. :param name: name of the need item :param prefix: string as rst-code, will be added infront of the value output diff --git a/tests/doc_test/doc_layout/conf.py b/tests/doc_test/doc_layout/conf.py index 5bc569c33..adcc07d7f 100644 --- a/tests/doc_test/doc_layout/conf.py +++ b/tests/doc_test/doc_layout/conf.py @@ -30,6 +30,13 @@ "side": ['<>'], }, }, + "optional_author": { + "grid": "simple", + "layout": { + "head": ['**<>**'], + "meta": ['**status**: <>', r'<>'], + }, + }, "footer_grid": { "grid": "simple_footer", "layout": { diff --git a/tests/doc_test/doc_layout/index.rst b/tests/doc_test/doc_layout/index.rst index a34a4dac5..ce1cf38ea 100644 --- a/tests/doc_test/doc_layout/index.rst +++ b/tests/doc_test/doc_layout/index.rst @@ -13,5 +13,9 @@ TEST DOCUMENT .. spec:: title_example_layout :layout: example +.. spec:: title_layout_optional_author + :layout: optional_author + :author: some author + .. spec:: title_layout_footer_grid :layout: footer_grid diff --git a/tests/test_layouts.py b/tests/test_layouts.py index e20aca3f7..e886c2fbd 100644 --- a/tests/test_layouts.py +++ b/tests/test_layouts.py @@ -23,8 +23,11 @@ def test_doc_build_html(test_app): assert "title_example_layout" in html needs = extract_needs_from_html(html) - assert len(needs) == 5 + assert len(needs) == 6 + assert ( + 'author: some author' in html + ) assert '' in html # check simple_footer grid layout