summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--rtemsspec/sphinxcontent.py27
-rw-r--r--rtemsspec/tests/test_content_sphinx.py18
2 files changed, 43 insertions, 2 deletions
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]}`"
diff --git a/rtemsspec/tests/test_content_sphinx.py b/rtemsspec/tests/test_content_sphinx.py
index e45703a9..f9235089 100644
--- a/rtemsspec/tests/test_content_sphinx.py
+++ b/rtemsspec/tests/test_content_sphinx.py
@@ -1,7 +1,7 @@
# SPDX-License-Identifier: BSD-2-Clause
""" Unit tests for the rtemsspec.sphinxcontent module. """
-# Copyright (C) 2020 embedded brains GmbH (http://www.embedded-brains.de)
+# Copyright (C) 2020, 2021 embedded brains GmbH (http://www.embedded-brains.de)
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
@@ -270,6 +270,22 @@ def test_comment():
"""
+def test_simple_table():
+ content = SphinxContent()
+ content.add_simple_table([])
+ assert str(content) == ""
+ content.add_simple_table([["a", "b"], ["cc", "ddd"]])
+ assert str(content) == """.. table::
+ :class: longtable
+
+ == ===
+ a b
+ == ===
+ cc ddd
+ == ===
+"""
+
+
def test_substitute(tmpdir):
config = create_item_cache_config_and_copy_spec(tmpdir,
"spec-sphinx",