[rtems-central commit] sphinxcontent: Add SphinxContent.label_scope()

Sebastian Huber sebh at rtems.org
Tue Nov 21 13:35:40 UTC 2023


Module:    rtems-central
Branch:    master
Commit:    07aaa327e54123c2c9852a650fd9db598c3dc11a
Changeset: http://git.rtems.org/rtems-central/commit/?id=07aaa327e54123c2c9852a650fd9db598c3dc11a

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Tue Nov 21 11:13:15 2023 +0100

sphinxcontent: Add SphinxContent.label_scope()

---

 rtemsspec/sphinxcontent.py             |  7 +++++++
 rtemsspec/tests/test_content_sphinx.py | 12 ++++++++++++
 2 files changed, 19 insertions(+)

diff --git a/rtemsspec/sphinxcontent.py b/rtemsspec/sphinxcontent.py
index c47ea68e..12497ccc 100644
--- a/rtemsspec/sphinxcontent.py
+++ b/rtemsspec/sphinxcontent.py
@@ -113,6 +113,13 @@ class SphinxContent(Content):
         """ Pops the top from the label stack. """
         self._label_stack.pop()
 
+    @contextmanager
+    def label_scope(self, label: str) -> Iterator[None]:
+        """ Opens a label scope context. """
+        self.push_label(label)
+        yield
+        self.pop_label()
+
     def add_label(self, label: str) -> None:
         """ Adds a label. """
         self.add(".. _" + label.strip() + ":")
diff --git a/rtemsspec/tests/test_content_sphinx.py b/rtemsspec/tests/test_content_sphinx.py
index 00be7a5f..e6bbd4bf 100644
--- a/rtemsspec/tests/test_content_sphinx.py
+++ b/rtemsspec/tests/test_content_sphinx.py
@@ -40,6 +40,18 @@ def test_add_label():
 """
 
 
+def test_label_scope():
+    content = SphinxContent()
+    with content.label_scope("x"):
+        with content.section("y"):
+            pass
+    assert str(content) == """.. _xY:
+
+y
+=
+"""
+
+
 def test_directive():
     content = SphinxContent()
     with content.directive("x"):



More information about the vc mailing list