Coverage for src/pyTRLCConverter/markdown/document.py: 91%

11 statements  

« prev     ^ index     » next       coverage.py v7.14.3, created at 2026-06-26 12:41 +0000

1"""Markdown document container. 

2 Collects Markdown block elements and renders them into a single string. 

3 Consecutive blocks are separated by a single blank line, the first block 

4 has no leading blank line and the document ends with a single newline. 

5 

6 Author: Andreas Merkle (andreas.merkle@newtec.de) 

7""" 

8 

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/>. 

24 

25# Imports ********************************************************************** 

26from typing import List 

27from pyTRLCConverter.markdown.element import MarkdownElement 

28 

29# Variables ******************************************************************** 

30 

31# Classes ********************************************************************** 

32 

33 

34class MarkdownDocument: 

35 """In-memory Markdown document built from block elements. 

36 """ 

37 

38 def __init__(self) -> None: 

39 """Initialize an empty Markdown document. 

40 """ 

41 self._elements: List[MarkdownElement] = [] 

42 

43 def add(self, element: MarkdownElement) -> None: 

44 # lobster-trace: SwRequirements.sw_req_markdown 

45 """Append a block element to the document. 

46 

47 Args: 

48 element (MarkdownElement): Block element to append. 

49 """ 

50 self._elements.append(element) 

51 

52 def is_empty(self) -> bool: 

53 # lobster-trace: SwRequirements.sw_req_markdown 

54 """Return whether the document has no elements yet. 

55 

56 Returns: 

57 bool: True if no element has been added, otherwise False. 

58 """ 

59 return len(self._elements) == 0 

60 

61 def render(self) -> str: 

62 # lobster-trace: SwRequirements.sw_req_markdown 

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. 

66 

67 Returns: 

68 str: Rendered Markdown document 

69 """ 

70 return "\n".join(element.render() for element in self._elements) 

71 

72# Functions ******************************************************************** 

73 

74# Main *************************************************************************