summaryrefslogtreecommitdiffstats
path: root/rtemsspec/sphinxcontent.py
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 /rtemsspec/sphinxcontent.py
parentcontent: Fix to_camel_case() (diff)
downloadrtems-central-bad73b6d29b03ea1a2ff344b25c602dc46ddaeeb.tar.bz2
sphinxcontent: Add add_grid_table()
Diffstat (limited to 'rtemsspec/sphinxcontent.py')
-rw-r--r--rtemsspec/sphinxcontent.py40
1 files changed, 40 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]}`"