Coverage for src/pyTRLCConverter/markdown/text.py: 100%
22 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"""Markdown text helpers.
2 Provides Markdown escaping and inline Markdown string builders.
4 Author: Andreas Merkle (andreas.merkle@newtec.de)
5"""
7# pyTRLCConverter - A tool to convert TRLC files to specific formats.
8# Copyright (c) 2024 - 2026 NewTec GmbH
9#
10# This file is part of pyTRLCConverter program.
11#
12# The pyTRLCConverter program is free software: you can redistribute it and/or modify it under
13# the terms of the GNU General Public License as published by the Free Software Foundation,
14# either version 3 of the License, or (at your option) any later version.
15#
16# The pyTRLCConverter program is distributed in the hope that it will be useful, but
17# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License along with pyTRLCConverter.
21# If not, see <https://www.gnu.org/licenses/>.
23# Imports **********************************************************************
25# Variables ********************************************************************
27# Classes **********************************************************************
30class MarkdownText:
31 """Utility class providing Markdown text escaping and inline element builders.
32 """
34 @staticmethod
35 def escape(text: str) -> str:
36 # lobster-trace: SwRequirements.sw_req_markdown_escape
37 """
38 Escapes the text to be used in a Markdown document.
40 Args:
41 text (str): Text to escape
43 Returns:
44 str: Escaped text
45 """
46 characters = ["\\", "`", "*", "_", "{", "}", "[", "]", "<", ">", "(", ")", "#", "+", "-", ".", "!", "|"]
48 for character in characters:
49 text = text.replace(character, "\\" + character)
51 return text
53 @staticmethod
54 def lf2soft_return(text: str) -> str:
55 # lobster-trace: SwRequirements.sw_req_markdown_soft_return
56 """
57 A single LF will be converted to backslash + LF.
58 Use it for paragraphs, but not for headings or tables.
60 Args:
61 text (str): Text
63 Returns:
64 str: Handled text
65 """
66 return text.replace("\n", "\\\n")
68 @staticmethod
69 def link(text: str, url: str, escape: bool = True) -> str:
70 # lobster-trace: SwRequirements.sw_req_markdown_link
71 """
72 Create a Markdown link.
73 The text will be automatically escaped for Markdown if necessary.
74 There will be no newline appended at the end.
76 Args:
77 text (str): Link text
78 url (str): Link URL
79 escape (bool): Escapes text (default: True).
81 Returns:
82 str: Markdown link
83 """
84 text_raw = text
86 if escape is True:
87 text_raw = MarkdownText.escape(text)
89 return f"[{text_raw}]({url})"
91 @staticmethod
92 def colored_text(text: str, color: str, escape: bool = True) -> str:
93 # lobster-trace: SwRequirements.sw_req_markdown_text_color
94 """
95 Create colored text in Markdown.
96 The text will be automatically escaped for Markdown if necessary.
97 There will be no newline appended at the end.
99 Args:
100 text (str): Text
101 color (str): HTML color
102 escape (bool): Escapes text (default: True).
104 Returns:
105 str: Colored text
106 """
107 text_raw = text
109 if escape is True:
110 text_raw = MarkdownText.escape(text)
112 return f"<span style=\"{color}\">{text_raw}</span>"
114# Functions ********************************************************************
116# Main *************************************************************************