summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Huber <sebastian.huber@embedded-brains.de>2023-05-05 14:41:18 +0200
committerSebastian Huber <sebastian.huber@embedded-brains.de>2023-05-08 14:34:56 +0200
commitbad73b6d29b03ea1a2ff344b25c602dc46ddaeeb (patch)
tree5d28e5bf6610757d3e185396d683c9e11a1670e4
parentcontent: Fix to_camel_case() (diff)
downloadrtems-central-bad73b6d29b03ea1a2ff344b25c602dc46ddaeeb.tar.bz2
sphinxcontent: Add add_grid_table()
-rw-r--r--rtemsspec/sphinxcontent.py40
-rw-r--r--rtemsspec/tests/test_content_sphinx.py36
2 files changed, 76 insertions, 0 deletions
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",