diff options
-rw-r--r-- | rtemsqual/applconfig.py | 4 | ||||
-rw-r--r-- | rtemsqual/content.py | 46 | ||||
-rw-r--r-- | rtemsqual/glossary.py | 2 | ||||
-rw-r--r-- | rtemsqual/tests/test_content_sphinx.py | 48 |
4 files changed, 95 insertions, 5 deletions
diff --git a/rtemsqual/applconfig.py b/rtemsqual/applconfig.py index 2d1b947d..d0b5d773 100644 --- a/rtemsqual/applconfig.py +++ b/rtemsqual/applconfig.py @@ -191,14 +191,14 @@ def _generate_notes(content: SphinxContent, notes: Optional[str]) -> None: def _generate_file(group: Item, options: ItemMap, target: str) -> None: content = SphinxContent() content.register_license_and_copyrights_of_item(group) - content.add_header(group["appl-config-group-name"], level="=") + content.add_header(group["appl-config-group-name"], level=2) content.add(group["appl-config-group-description"]) for item in sorted(options.values(), key=lambda x: x.uid): name = item["appl-config-option-name"] content.register_license_and_copyrights_of_item(item) content.add_index_entries([name] + item["appl-config-option-index"]) content.add_label(name) - content.add_header(name, level="-") + content.add_header(name, level=3) content.add_definition_item("CONSTANT:", f"``{name}``") option_type = item["appl-config-option-type"] content.add_definition_item("OPTION TYPE:", _OPTION_TYPES[option_type]) diff --git a/rtemsqual/content.py b/rtemsqual/content.py index bdbaaeda..86104312 100644 --- a/rtemsqual/content.py +++ b/rtemsqual/content.py @@ -144,11 +144,21 @@ def _indent(lines: List[str], indent: str, return lines +def _to_camel_case(name: str) -> str: + return name[0].upper() + \ + re.sub(r"[^a-zA-Z0-9]+([a-zA-Z0-9])", + lambda match: match.group(1).upper(), + name[1:]) + + @contextmanager def _add_context(_content: "Content") -> Iterator[None]: yield +_HEADER_LEVELS = ["#", "*", "=", "-", "^", '\"'] + + class Content: """ This class builds content. """ @@ -298,10 +308,17 @@ class SphinxContent(Content): """ Adds a label. """ self.add(".. _" + label.strip() + ":") - def add_header(self, name, level="=") -> None: + def add_header(self, name, level=2) -> None: """ Adds a header. """ name = name.strip() - self.add([name, level * len(name)]) + self.add([name, _HEADER_LEVELS[level] * len(name)]) + + def add_header_with_ref(self, title, level) -> str: + """ Adds a header with reference. """ + section_ref = "Section" + _to_camel_case(title.strip()) + self.add_label(section_ref) + self.add_header(title, level) + return section_ref def add_index_entries(self, entries) -> None: """ Adds a list of index entries the content. """ @@ -318,6 +335,31 @@ class SphinxContent(Content): self.add(lines, _definition_item_context) + def push_directive(self, + name: str, + value: Optional[str] = None, + options: Optional[List[str]] = None) -> None: + """ Pushes a directive. """ + self.add(".. " + name.strip() + "::") + if value: + self.lines[-1] += " " + value + self.push_indent() + self.add(options) + + def pop_directive(self) -> None: + """ Pops a directive. """ + self.pop_indent() + + @contextmanager + def directive(self, + name: str, + value: Optional[str] = None, + options: Optional[List[str]] = None): + """ Opens a directive context. """ + self.push_directive(name, value, options) + yield + self.pop_directive() + def add_licence_and_copyrights(self) -> None: """ Adds a licence and copyright block according to the registered licenses diff --git a/rtemsqual/glossary.py b/rtemsqual/glossary.py index 52616477..a6f51788 100644 --- a/rtemsqual/glossary.py +++ b/rtemsqual/glossary.py @@ -50,7 +50,7 @@ def _gather_glossary_terms(item: Item, glossary_terms: ItemMap) -> None: def _generate_glossary_content(terms: ItemMap) -> SphinxContent: content = SphinxContent() - content.add_header("Glossary", level="*") + content.add_header("Glossary", level=1) content.add(".. glossary::") with content.indent(): content.add(":sorted:") diff --git a/rtemsqual/tests/test_content_sphinx.py b/rtemsqual/tests/test_content_sphinx.py index fa1823d2..997f298e 100644 --- a/rtemsqual/tests/test_content_sphinx.py +++ b/rtemsqual/tests/test_content_sphinx.py @@ -38,12 +38,60 @@ def test_add_label(): """ +def test_directive(): + sc = SphinxContent() + with sc.directive("x"): + sc.add("y") + assert str(sc) == """.. x:: + y +""" + with sc.directive("z", "xy", [":a:", ":b:"]): + sc.add("c") + assert str(sc) == """.. x:: + y + +.. z:: xy + :a: + :b: + + c +""" + + def test_add_header(): sc = SphinxContent() sc.add_header("x") assert str(sc) == """x = """ + sc.add_header("yz", 1) + assert str(sc) == """x += + +yz +** +""" + + +def test_add_header_with_ref(): + sc = SphinxContent() + sc.add_header_with_ref("x", 1) + assert str(sc) == """.. _SectionX: + +x +* +""" + sc.add_header_with_ref("yz w", 2) + assert str(sc) == """.. _SectionX: + +x +* + +.. _SectionYzW: + +yz w +==== +""" def test_append(): |