python-trueconf-bot 1.2.3.dev1__py3-none-any.whl → 1.3.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,252 @@
1
+
2
+ from __future__ import annotations
3
+
4
+ """
5
+ Utilities for building formatted message text.
6
+
7
+ The module provides a small tree-based formatting API similar to aiogram's formatting
8
+ helpers. Formatting nodes can be composed and rendered either as HTML or Markdown.
9
+
10
+ Example:
11
+ ```python
12
+ from trueconf.utils.formatting import Bold, Link, Mention, Text
13
+
14
+ content = Text(
15
+ Bold("Important"),
16
+ " message for ",
17
+ Mention("John Doe", user_id="john_doe@video.example.com"),
18
+ "\n",
19
+ Link("Open website", url="https://trueconf.com"),
20
+ )
21
+
22
+ html = content.as_html()
23
+ markdown = content.as_markdown()
24
+ ```
25
+ """
26
+
27
+ from abc import ABC, abstractmethod
28
+
29
+ from .decorations import TextDecoration, html_decoration, markdown_decoration
30
+
31
+ class TextNode(ABC):
32
+ """
33
+ Base class for all formatting nodes.
34
+
35
+ Formatting nodes render themselves using a text decoration strategy, such as
36
+ HTML or Markdown. End users usually do not instantiate this class directly;
37
+ use :class:`Text`, :class:`Bold`, :class:`Italic`, :class:`Link`, and other
38
+ concrete nodes instead.
39
+ """
40
+
41
+ @abstractmethod
42
+ def render(self, decoration: TextDecoration) -> str:
43
+ """
44
+ Renders the node using the provided decoration strategy.
45
+
46
+ Args:
47
+ decoration (TextDecoration): Formatting strategy used to render the node.
48
+
49
+ Returns:
50
+ str: Rendered text.
51
+ """
52
+ ...
53
+
54
+
55
+ class Text(TextNode):
56
+ """
57
+ Container node for plain text and nested formatting nodes.
58
+
59
+ Args:
60
+ *body (str | TextNode): Plain strings or formatting nodes to concatenate.
61
+
62
+ Example:
63
+ ```python
64
+ content = Text("Hello, ", Bold("world"))
65
+ html = content.as_html()
66
+ ```
67
+ """
68
+ def __init__(self, *body: str | TextNode):
69
+ """
70
+ Initializes a text container.
71
+
72
+ Args:
73
+ *body (str | TextNode): Plain strings or formatting nodes.
74
+ """
75
+ self.body = body
76
+
77
+ def render(self, decoration: TextDecoration) -> str:
78
+ """
79
+ Renders the text container using the provided decoration strategy.
80
+
81
+ Args:
82
+ decoration (TextDecoration): Formatting strategy used for rendering.
83
+
84
+ Returns:
85
+ str: Rendered text.
86
+ """
87
+ parts: list[str] = []
88
+
89
+ for item in self.body:
90
+ if isinstance(item, TextNode):
91
+ parts.append(item.render(decoration))
92
+ else:
93
+ parts.append(decoration.escape(str(item)))
94
+
95
+ return "".join(parts)
96
+
97
+ def as_html(self) -> str:
98
+ """
99
+ Renders the text container as HTML.
100
+
101
+ Returns:
102
+ str: HTML representation of the formatted text.
103
+ """
104
+ return self.render(html_decoration)
105
+
106
+ def as_markdown(self) -> str:
107
+ """
108
+ Renders the text container as Markdown.
109
+
110
+ Returns:
111
+ str: Markdown representation of the formatted text.
112
+ """
113
+ return self.render(markdown_decoration)
114
+
115
+
116
+ class Bold(Text):
117
+ """Represents bold text."""
118
+ def render(self, decoration: TextDecoration) -> str:
119
+ """
120
+ Renders the nested content as bold text.
121
+
122
+ Args:
123
+ decoration (TextDecoration): Formatting strategy used for rendering.
124
+
125
+ Returns:
126
+ str: Rendered bold text.
127
+ """
128
+ return decoration.bold(super().render(decoration))
129
+
130
+
131
+ class Italic(Text):
132
+ """Represents italic text."""
133
+ def render(self, decoration: TextDecoration) -> str:
134
+ """
135
+ Renders the nested content as italic text.
136
+
137
+ Args:
138
+ decoration (TextDecoration): Formatting strategy used for rendering.
139
+
140
+ Returns:
141
+ str: Rendered italic text.
142
+ """
143
+ return decoration.italic(super().render(decoration))
144
+
145
+
146
+ class Underline(Text):
147
+ """Represents underlined text."""
148
+ def render(self, decoration: TextDecoration) -> str:
149
+ """
150
+ Renders the nested content as underlined text.
151
+
152
+ Args:
153
+ decoration (TextDecoration): Formatting strategy used for rendering.
154
+
155
+ Returns:
156
+ str: Rendered underlined text.
157
+ """
158
+ return decoration.underline(super().render(decoration))
159
+
160
+
161
+ class Strikethrough(Text):
162
+ """Represents strikethrough text."""
163
+ def render(self, decoration: TextDecoration) -> str:
164
+ """
165
+ Renders the nested content as strikethrough text.
166
+
167
+ Args:
168
+ decoration (TextDecoration): Formatting strategy used for rendering.
169
+
170
+ Returns:
171
+ str: Rendered strikethrough text.
172
+ """
173
+ return decoration.strikethrough(super().render(decoration))
174
+
175
+
176
+ class Link(Text):
177
+ """
178
+ Represents a hyperlink.
179
+
180
+ Args:
181
+ *body (str | TextNode): Link label as plain text or formatting nodes.
182
+ url (str): Link target URL.
183
+ """
184
+ def __init__(self, *body: str | TextNode, url: str):
185
+ """
186
+ Initializes a hyperlink node.
187
+
188
+ Args:
189
+ *body (str | TextNode): Link label as plain text or formatting nodes.
190
+ url (str): Link target URL.
191
+ """
192
+ super().__init__(*body)
193
+ self.url = url
194
+
195
+ def render(self, decoration: TextDecoration) -> str:
196
+ """
197
+ Renders the nested content as a hyperlink.
198
+
199
+ Args:
200
+ decoration (TextDecoration): Formatting strategy used for rendering.
201
+
202
+ Returns:
203
+ str: Rendered hyperlink.
204
+ """
205
+ return decoration.link(super().render(decoration), self.url)
206
+
207
+
208
+ class Mention(Text):
209
+ """
210
+ Represents a TrueConf user mention.
211
+
212
+ Args:
213
+ *body (str | TextNode): Mention label as plain text or formatting nodes.
214
+ user_id (str): TrueConf user ID, for example ``john_doe@video.example.com``.
215
+ """
216
+ def __init__(self, *body: str | TextNode, user_id: str):
217
+ """
218
+ Initializes a TrueConf mention node.
219
+
220
+ Args:
221
+ *body (str | TextNode): Mention label as plain text or formatting nodes.
222
+ user_id (str): TrueConf user ID.
223
+ """
224
+ super().__init__(*body)
225
+ self.user_id = user_id
226
+
227
+ def render(self, decoration: TextDecoration) -> str:
228
+ """
229
+ Renders the nested content as a TrueConf user mention.
230
+
231
+ Args:
232
+ decoration (TextDecoration): Formatting strategy used for rendering.
233
+
234
+ Returns:
235
+ str: Rendered mention.
236
+ """
237
+ return decoration.mention(super().render(decoration), self.user_id)
238
+
239
+
240
+ class AllMention(TextNode):
241
+ """Represents an ``@all`` mention."""
242
+ def render(self, decoration: TextDecoration) -> str:
243
+ """
244
+ Renders an ``@all`` mention.
245
+
246
+ Args:
247
+ decoration (TextDecoration): Formatting strategy used for rendering.
248
+
249
+ Returns:
250
+ str: Rendered ``@all`` mention.
251
+ """
252
+ return decoration.all_mention()