From 11ee8371d588b3bf4430fe80d131deaddb4e3699 Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Mon, 8 Mar 2021 14:26:51 +0100 Subject: sphinxcontent: Add add_simple_table() --- rtemsspec/sphinxcontent.py | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'rtemsspec/sphinxcontent.py') diff --git a/rtemsspec/sphinxcontent.py b/rtemsspec/sphinxcontent.py index e7881cf3..2d214021 100644 --- a/rtemsspec/sphinxcontent.py +++ b/rtemsspec/sphinxcontent.py @@ -25,7 +25,7 @@ # POSSIBILITY OF SUCH DAMAGE. from contextlib import contextmanager -from typing import Any, Iterable, Iterator, List, Optional, Union +from typing import Any, Iterable, Iterator, List, Optional, Sequence, Union from rtemsspec.content import Content, make_lines, to_camel_case from rtemsspec.items import Item, ItemGetValueContext, ItemMapper @@ -49,6 +49,16 @@ def get_label(name: str) -> str: return to_camel_case(name.strip()) +def _simple_sep(maxi: Iterable[int]) -> str: + return " ".join(f"{'=' * val}" for val in maxi) + + +def _simple_row(row: Iterable[str], maxi: Iterable[int]) -> str: + line = " ".join("{0:{width}}".format(cell, width=val) + for cell, val in zip(row, maxi)) + return line.rstrip() + + class SphinxContent(Content): """ This class builds Sphinx content. """ def __init__(self, section_level: int = 2): @@ -168,6 +178,21 @@ class SphinxContent(Content): """ Opens a comment block. """ self.push_indent(".. ", "..") + def add_simple_table(self, rows: Sequence[Iterable[str]]) -> None: + """ Adds a simple 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))) + sep = _simple_sep(maxi) + lines = [sep, _simple_row(rows[0], maxi), sep] + lines.extend(_simple_row(row, maxi) for row in rows[1:]) + lines.append(sep) + with self.directive("table", options=[":class: longtable"]): + self.add(lines) + def _get_ref_term(ctx: ItemGetValueContext) -> Any: return f":term:`{ctx.value[ctx.key]}`" -- cgit v1.2.3