Coverage for src/pyTRLCConverter/rst/document.py: 91%
11 statements
« prev ^ index » next coverage.py v7.14.3, created at 2026-06-26 12:41 +0000
« prev ^ index » next coverage.py v7.14.3, created at 2026-06-26 12:41 +0000
1"""reStructuredText document container.
2 Collects reStructuredText block elements and renders them into a single
3 string. Consecutive blocks are separated by a single blank line, the first
4 block has no leading blank line and the document ends with a single newline.
6 Author: Gabryel Reyes (gabryel.reyes@newtec.de)
7"""
9# pyTRLCConverter - A tool to convert TRLC files to specific formats.
10# Copyright (c) 2024 - 2026 NewTec GmbH
11#
12# This file is part of pyTRLCConverter program.
13#
14# The pyTRLCConverter program is free software: you can redistribute it and/or modify it under
15# the terms of the GNU General Public License as published by the Free Software Foundation,
16# either version 3 of the License, or (at your option) any later version.
17#
18# The pyTRLCConverter program is distributed in the hope that it will be useful, but
19# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
21#
22# You should have received a copy of the GNU General Public License along with pyTRLCConverter.
23# If not, see <https://www.gnu.org/licenses/>.
25# Imports **********************************************************************
26from typing import List
27from pyTRLCConverter.rst.element import RstElement
29# Variables ********************************************************************
31# Classes **********************************************************************
34class RstDocument:
35 """In-memory reStructuredText document built from block elements.
36 """
38 def __init__(self) -> None:
39 """Initialize an empty reStructuredText document.
40 """
41 self._elements: List[RstElement] = []
43 def add(self, element: RstElement) -> None:
44 # lobster-trace: SwRequirements.sw_req_rst
45 """Append a block element to the document.
47 Args:
48 element (RstElement): Block element to append.
49 """
50 self._elements.append(element)
52 def is_empty(self) -> bool:
53 # lobster-trace: SwRequirements.sw_req_rst
54 """Return whether the document has no elements yet.
56 Returns:
57 bool: True if no element has been added, otherwise False.
58 """
59 return len(self._elements) == 0
61 def render(self) -> str:
62 # lobster-trace: SwRequirements.sw_req_rst
63 """Render the whole document.
64 Blocks are separated by a single blank line, the first block has no
65 leading blank line and the document ends with a single newline.
67 Returns:
68 str: Rendered reStructuredText document
69 """
70 return "\n".join(element.render() for element in self._elements)
72# Functions ********************************************************************
74# Main *************************************************************************