[rtems-central commit] sphinxcontent: Add add_grid_table()

Sebastian Huber sebh at rtems.org
Tue May 9 13:45:26 UTC 2023


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

Author:    Sebastian Huber <sebastian.huber at embedded-brains.de>
Date:      Fri May  5 14:41:18 2023 +0200

sphinxcontent: Add add_grid_table()

---

 rtemsspec/sphinxcontent.py             | 40 ++++++++++++++++++++++++++++++++++
 rtemsspec/tests/test_content_sphinx.py | 36 ++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+)

diff --git a/rtemsspec/sphinxcontent.py b/rtemsspec/sphinxcontent.py
index 199ee874..281761ed 100644
--- a/rtemsspec/sphinxcontent.py
+++ b/rtemsspec/sphinxcontent.py
@@ -59,6 +59,16 @@ def _simple_row(row: Iterable[str], maxi: Iterable[int]) -> str:
     return line.rstrip()
 
 
+def _grid_sep(maxi: Iterable[int], sep: str) -> str:
+    return f"+{sep}" + f"{sep}+{sep}".join(f"{sep * width}"
+                                           for width in maxi) + f"{sep}+"
+
+
+def _grid_row(row: Iterable[str], maxi: Iterable[int]) -> str:
+    line = " | ".join(f"{cell:{width}}" for cell, width in zip(row, maxi))
+    return f"| {line} |"
+
+
 class SphinxContent(Content):
     """ This class builds Sphinx content. """
 
@@ -199,6 +209,36 @@ class SphinxContent(Content):
         with self.directive("table", options=[":class: longtable"]):
             self.add(lines)
 
+    def add_grid_table(self, rows: Sequence[Iterable[str]],
+                       widths: List[int]) -> None:
+        """ Adds a grid table. """
+        if not rows:
+            return
+        maxi = tuple(map(len, rows[0]))
+        for row in rows:
+            row_lengths = tuple(map(len, row))
+            maxi = tuple(map(max, zip(maxi, row_lengths)))
+        begin_end = _grid_sep(maxi, "-")
+        lines = [begin_end, _grid_row(rows[0], maxi), _grid_sep(maxi, "=")]
+        for index, row in enumerate(rows[1:]):
+            if index > 0:
+                sep = ""
+                for cell, width in zip(row, maxi):
+                    if cell:
+                        sep += f"+{'-' * (width + 2)}"
+                    else:
+                        sep += f"+{' ' * (width + 2)}"
+                lines.append(sep + "+")
+            lines.append(_grid_row(row, maxi))
+        lines.append(begin_end)
+        with self.directive(
+                "table",
+                options=[
+                    ":class: longtable",
+                    f":widths: {','.join(str(width) for width in widths)}"
+                ]):
+            self.add(lines)
+
 
 def _get_ref_term(ctx: ItemGetValueContext) -> Any:
     return f":term:`{ctx.value[ctx.key]}`"
diff --git a/rtemsspec/tests/test_content_sphinx.py b/rtemsspec/tests/test_content_sphinx.py
index 35e7282a..2176b332 100644
--- a/rtemsspec/tests/test_content_sphinx.py
+++ b/rtemsspec/tests/test_content_sphinx.py
@@ -286,6 +286,42 @@ def test_simple_table():
 """
 
 
+def test_grid_table():
+    content = SphinxContent()
+    content.add_grid_table([], [])
+    assert str(content) == ""
+    content.add_grid_table([["a", "b"], ["cc", "ddd"]], [50, 50])
+    content.add_grid_table(
+        [["1", "2", "3"], ["aa", "bbb", "cccc"], ["ddd", "", "e"],
+         ["ff", "g", "h"], ["", "i", "j"]], [30, 30, 40])
+    assert str(content) == """.. table::
+    :class: longtable
+    :widths: 50,50
+
+    +----+-----+
+    | a  | b   |
+    +====+=====+
+    | cc | ddd |
+    +----+-----+
+
+.. table::
+    :class: longtable
+    :widths: 30,30,40
+
+    +-----+-----+------+
+    | 1   | 2   | 3    |
+    +=====+=====+======+
+    | aa  | bbb | cccc |
+    +-----+     +------+
+    | ddd |     | e    |
+    +-----+-----+------+
+    | ff  | g   | h    |
+    +     +-----+------+
+    |     | i   | j    |
+    +-----+-----+------+
+"""
+
+
 def test_substitute(tmpdir):
     config = create_item_cache_config_and_copy_spec(tmpdir,
                                                     "spec-sphinx",



More information about the vc mailing list