telegrinder 0.3.4__py3-none-any.whl → 0.4.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.

Potentially problematic release.


This version of telegrinder might be problematic. Click here for more details.

Files changed (192) hide show
  1. telegrinder/__init__.py +148 -149
  2. telegrinder/api/__init__.py +9 -8
  3. telegrinder/api/api.py +101 -93
  4. telegrinder/api/error.py +20 -16
  5. telegrinder/api/response.py +20 -20
  6. telegrinder/api/token.py +36 -36
  7. telegrinder/bot/__init__.py +72 -66
  8. telegrinder/bot/bot.py +83 -76
  9. telegrinder/bot/cute_types/__init__.py +19 -17
  10. telegrinder/bot/cute_types/base.py +184 -258
  11. telegrinder/bot/cute_types/callback_query.py +400 -385
  12. telegrinder/bot/cute_types/chat_join_request.py +62 -61
  13. telegrinder/bot/cute_types/chat_member_updated.py +157 -160
  14. telegrinder/bot/cute_types/inline_query.py +44 -43
  15. telegrinder/bot/cute_types/message.py +2590 -2637
  16. telegrinder/bot/cute_types/pre_checkout_query.py +42 -0
  17. telegrinder/bot/cute_types/update.py +112 -104
  18. telegrinder/bot/cute_types/utils.py +62 -95
  19. telegrinder/bot/dispatch/__init__.py +59 -55
  20. telegrinder/bot/dispatch/abc.py +76 -77
  21. telegrinder/bot/dispatch/context.py +96 -98
  22. telegrinder/bot/dispatch/dispatch.py +254 -202
  23. telegrinder/bot/dispatch/handler/__init__.py +13 -13
  24. telegrinder/bot/dispatch/handler/abc.py +23 -24
  25. telegrinder/bot/dispatch/handler/audio_reply.py +44 -44
  26. telegrinder/bot/dispatch/handler/base.py +57 -57
  27. telegrinder/bot/dispatch/handler/document_reply.py +44 -44
  28. telegrinder/bot/dispatch/handler/func.py +129 -135
  29. telegrinder/bot/dispatch/handler/media_group_reply.py +44 -43
  30. telegrinder/bot/dispatch/handler/message_reply.py +36 -36
  31. telegrinder/bot/dispatch/handler/photo_reply.py +44 -44
  32. telegrinder/bot/dispatch/handler/sticker_reply.py +37 -37
  33. telegrinder/bot/dispatch/handler/video_reply.py +44 -44
  34. telegrinder/bot/dispatch/middleware/__init__.py +3 -3
  35. telegrinder/bot/dispatch/middleware/abc.py +97 -22
  36. telegrinder/bot/dispatch/middleware/global_middleware.py +70 -0
  37. telegrinder/bot/dispatch/process.py +151 -157
  38. telegrinder/bot/dispatch/return_manager/__init__.py +15 -13
  39. telegrinder/bot/dispatch/return_manager/abc.py +104 -108
  40. telegrinder/bot/dispatch/return_manager/callback_query.py +20 -20
  41. telegrinder/bot/dispatch/return_manager/inline_query.py +15 -15
  42. telegrinder/bot/dispatch/return_manager/message.py +36 -36
  43. telegrinder/bot/dispatch/return_manager/pre_checkout_query.py +20 -0
  44. telegrinder/bot/dispatch/view/__init__.py +15 -13
  45. telegrinder/bot/dispatch/view/abc.py +45 -41
  46. telegrinder/bot/dispatch/view/base.py +231 -200
  47. telegrinder/bot/dispatch/view/box.py +140 -129
  48. telegrinder/bot/dispatch/view/callback_query.py +16 -17
  49. telegrinder/bot/dispatch/view/chat_join_request.py +11 -16
  50. telegrinder/bot/dispatch/view/chat_member.py +37 -39
  51. telegrinder/bot/dispatch/view/inline_query.py +16 -17
  52. telegrinder/bot/dispatch/view/message.py +43 -44
  53. telegrinder/bot/dispatch/view/pre_checkout_query.py +16 -0
  54. telegrinder/bot/dispatch/view/raw.py +116 -114
  55. telegrinder/bot/dispatch/waiter_machine/__init__.py +17 -17
  56. telegrinder/bot/dispatch/waiter_machine/actions.py +14 -13
  57. telegrinder/bot/dispatch/waiter_machine/hasher/__init__.py +8 -8
  58. telegrinder/bot/dispatch/waiter_machine/hasher/callback.py +55 -55
  59. telegrinder/bot/dispatch/waiter_machine/hasher/hasher.py +59 -57
  60. telegrinder/bot/dispatch/waiter_machine/hasher/message.py +51 -51
  61. telegrinder/bot/dispatch/waiter_machine/hasher/state.py +20 -19
  62. telegrinder/bot/dispatch/waiter_machine/machine.py +251 -172
  63. telegrinder/bot/dispatch/waiter_machine/middleware.py +94 -89
  64. telegrinder/bot/dispatch/waiter_machine/short_state.py +57 -68
  65. telegrinder/bot/polling/__init__.py +4 -4
  66. telegrinder/bot/polling/abc.py +25 -25
  67. telegrinder/bot/polling/polling.py +139 -131
  68. telegrinder/bot/rules/__init__.py +85 -62
  69. telegrinder/bot/rules/abc.py +213 -206
  70. telegrinder/bot/rules/callback_data.py +122 -163
  71. telegrinder/bot/rules/chat_join.py +45 -43
  72. telegrinder/bot/rules/command.py +126 -126
  73. telegrinder/bot/rules/enum_text.py +33 -36
  74. telegrinder/bot/rules/func.py +28 -26
  75. telegrinder/bot/rules/fuzzy.py +24 -24
  76. telegrinder/bot/rules/id.py +24 -0
  77. telegrinder/bot/rules/inline.py +58 -56
  78. telegrinder/bot/rules/integer.py +21 -20
  79. telegrinder/bot/rules/is_from.py +127 -127
  80. telegrinder/bot/rules/logic.py +18 -0
  81. telegrinder/bot/rules/markup.py +42 -43
  82. telegrinder/bot/rules/mention.py +14 -14
  83. telegrinder/bot/rules/message.py +15 -17
  84. telegrinder/bot/rules/message_entities.py +33 -35
  85. telegrinder/bot/rules/node.py +33 -27
  86. telegrinder/bot/rules/payload.py +81 -0
  87. telegrinder/bot/rules/payment_invoice.py +29 -0
  88. telegrinder/bot/rules/regex.py +36 -37
  89. telegrinder/bot/rules/rule_enum.py +72 -72
  90. telegrinder/bot/rules/start.py +42 -42
  91. telegrinder/bot/rules/state.py +35 -37
  92. telegrinder/bot/rules/text.py +38 -33
  93. telegrinder/bot/rules/update.py +15 -15
  94. telegrinder/bot/scenario/__init__.py +5 -5
  95. telegrinder/bot/scenario/abc.py +17 -19
  96. telegrinder/bot/scenario/checkbox.py +174 -176
  97. telegrinder/bot/scenario/choice.py +48 -51
  98. telegrinder/client/__init__.py +12 -4
  99. telegrinder/client/abc.py +100 -75
  100. telegrinder/client/aiohttp.py +134 -130
  101. telegrinder/client/form_data.py +31 -0
  102. telegrinder/client/sonic.py +212 -0
  103. telegrinder/model.py +208 -315
  104. telegrinder/modules.py +239 -237
  105. telegrinder/msgspec_json.py +14 -14
  106. telegrinder/msgspec_utils.py +478 -410
  107. telegrinder/node/__init__.py +86 -25
  108. telegrinder/node/attachment.py +163 -87
  109. telegrinder/node/base.py +288 -160
  110. telegrinder/node/callback_query.py +54 -53
  111. telegrinder/node/command.py +34 -33
  112. telegrinder/node/composer.py +163 -198
  113. telegrinder/node/container.py +33 -27
  114. telegrinder/node/either.py +82 -0
  115. telegrinder/node/event.py +54 -65
  116. telegrinder/node/file.py +51 -0
  117. telegrinder/node/me.py +15 -16
  118. telegrinder/node/payload.py +78 -0
  119. telegrinder/node/polymorphic.py +67 -48
  120. telegrinder/node/rule.py +72 -76
  121. telegrinder/node/scope.py +36 -38
  122. telegrinder/node/source.py +87 -71
  123. telegrinder/node/text.py +53 -41
  124. telegrinder/node/tools/__init__.py +3 -3
  125. telegrinder/node/tools/generator.py +36 -40
  126. telegrinder/py.typed +0 -0
  127. telegrinder/rules.py +1 -62
  128. telegrinder/tools/__init__.py +152 -93
  129. telegrinder/tools/adapter/__init__.py +19 -0
  130. telegrinder/tools/adapter/abc.py +49 -0
  131. telegrinder/tools/adapter/dataclass.py +56 -0
  132. telegrinder/{bot/rules → tools}/adapter/errors.py +5 -5
  133. telegrinder/{bot/rules → tools}/adapter/event.py +63 -65
  134. telegrinder/{bot/rules → tools}/adapter/node.py +46 -48
  135. telegrinder/{bot/rules → tools}/adapter/raw_event.py +27 -27
  136. telegrinder/{bot/rules → tools}/adapter/raw_update.py +30 -30
  137. telegrinder/tools/buttons.py +106 -80
  138. telegrinder/tools/callback_data_serilization/__init__.py +5 -0
  139. telegrinder/tools/callback_data_serilization/abc.py +51 -0
  140. telegrinder/tools/callback_data_serilization/json_ser.py +60 -0
  141. telegrinder/tools/callback_data_serilization/msgpack_ser.py +172 -0
  142. telegrinder/tools/error_handler/__init__.py +7 -7
  143. telegrinder/tools/error_handler/abc.py +30 -33
  144. telegrinder/tools/error_handler/error.py +9 -9
  145. telegrinder/tools/error_handler/error_handler.py +179 -193
  146. telegrinder/tools/formatting/__init__.py +83 -63
  147. telegrinder/tools/formatting/deep_links.py +541 -0
  148. telegrinder/tools/formatting/{html.py → html_formatter.py} +266 -294
  149. telegrinder/tools/formatting/spec_html_formats.py +71 -117
  150. telegrinder/tools/functional.py +8 -12
  151. telegrinder/tools/global_context/__init__.py +7 -7
  152. telegrinder/tools/global_context/abc.py +63 -63
  153. telegrinder/tools/global_context/global_context.py +387 -412
  154. telegrinder/tools/global_context/telegrinder_ctx.py +27 -27
  155. telegrinder/tools/i18n/__init__.py +7 -7
  156. telegrinder/tools/i18n/abc.py +30 -30
  157. telegrinder/tools/i18n/middleware/__init__.py +3 -3
  158. telegrinder/tools/i18n/middleware/abc.py +22 -25
  159. telegrinder/tools/i18n/simple.py +43 -43
  160. telegrinder/tools/input_file_directory.py +30 -0
  161. telegrinder/tools/keyboard.py +128 -128
  162. telegrinder/tools/lifespan.py +105 -0
  163. telegrinder/tools/limited_dict.py +32 -37
  164. telegrinder/tools/loop_wrapper/__init__.py +4 -4
  165. telegrinder/tools/loop_wrapper/abc.py +20 -15
  166. telegrinder/tools/loop_wrapper/loop_wrapper.py +169 -224
  167. telegrinder/tools/magic.py +307 -157
  168. telegrinder/tools/parse_mode.py +6 -6
  169. telegrinder/tools/state_storage/__init__.py +4 -4
  170. telegrinder/tools/state_storage/abc.py +31 -35
  171. telegrinder/tools/state_storage/memory.py +25 -25
  172. telegrinder/tools/strings.py +13 -0
  173. telegrinder/types/__init__.py +268 -260
  174. telegrinder/types/enums.py +711 -701
  175. telegrinder/types/input_file.py +51 -0
  176. telegrinder/types/methods.py +5055 -4633
  177. telegrinder/types/objects.py +7058 -6950
  178. telegrinder/verification_utils.py +30 -32
  179. {telegrinder-0.3.4.dist-info → telegrinder-0.4.0.dist-info}/LICENSE +22 -22
  180. telegrinder-0.4.0.dist-info/METADATA +144 -0
  181. telegrinder-0.4.0.dist-info/RECORD +182 -0
  182. {telegrinder-0.3.4.dist-info → telegrinder-0.4.0.dist-info}/WHEEL +1 -1
  183. telegrinder/bot/rules/adapter/__init__.py +0 -17
  184. telegrinder/bot/rules/adapter/abc.py +0 -31
  185. telegrinder/node/message.py +0 -14
  186. telegrinder/node/update.py +0 -15
  187. telegrinder/tools/formatting/links.py +0 -38
  188. telegrinder/tools/kb_set/__init__.py +0 -4
  189. telegrinder/tools/kb_set/base.py +0 -15
  190. telegrinder/tools/kb_set/yaml.py +0 -63
  191. telegrinder-0.3.4.dist-info/METADATA +0 -110
  192. telegrinder-0.3.4.dist-info/RECORD +0 -165
@@ -1,308 +1,280 @@
1
- import html
2
- import string
3
- import typing
4
- from contextlib import suppress
5
-
6
- from telegrinder.tools.parse_mode import ParseMode
7
- from telegrinder.types.enums import ProgrammingLanguage
8
-
9
- from .links import (
10
- get_channel_boost_link,
11
- get_invite_chat_link,
12
- get_mention_link,
13
- get_resolve_domain_link,
14
- get_start_bot_link,
15
- get_start_group_link,
16
- )
17
- from .spec_html_formats import SpecialFormat, is_spec_format
18
-
19
- TAG_FORMAT = "<{tag}{data}>{content}</{tag}>"
20
- QUOT_MARK = '"'
21
-
22
-
23
- class StringFormatterProto(typing.Protocol):
24
- def format_field(self, value: typing.Any, fmt: str) -> "HTMLFormatter": ...
25
-
26
- def format(self, __string: str, *args: object, **kwargs: object) -> "HTMLFormatter": ...
27
-
28
-
29
- class StringFormatter(string.Formatter):
30
- """String formatter, using substitutions from args and kwargs.
31
- The substitutions are identified by braces ('{' and '}') with
32
- specifiers: `bold`, `italic`, etc.
33
- """
34
-
35
- __formats__: typing.ClassVar[tuple[str, ...]] = (
36
- "blockquote",
37
- "bold",
38
- "code_inline",
39
- "italic",
40
- "spoiler",
41
- "strike",
42
- "underline",
43
- )
44
-
45
- def is_html_format(self, value: typing.Any, fmt: str) -> str:
46
- if not fmt:
47
- raise ValueError("Formats union should be: format+format.")
48
- if fmt not in self.__formats__:
49
- raise ValueError(
50
- "Unknown format {!r} for object of type {!r}.".format(
51
- fmt,
52
- type(value).__name__,
53
- )
54
- )
55
- return fmt
56
-
57
- def get_spec_formatter(self, value: SpecialFormat) -> typing.Callable[..., "TagFormat"]:
58
- return globals()[value.__formatter_name__]
59
-
60
- def check_formats(self, value: typing.Any, fmts: list[str]) -> "TagFormat":
61
- if is_spec_format(value):
62
- value = value.string
63
-
64
- current_format = globals()[fmts.pop(0)](
65
- str(value)
66
- if isinstance(value, TagFormat)
67
- else escape(FormatString(value) if not isinstance(value, str) else value)
68
- )
69
- for fmt in fmts:
70
- current_format = globals()[fmt](current_format)
71
-
72
- return (
73
- TagFormat(
74
- current_format,
75
- tag=value.tag,
76
- **value.data,
77
- )
78
- if isinstance(value, TagFormat)
79
- else current_format
80
- )
81
-
82
- def format_field(self, value: typing.Any, fmt: str) -> "HTMLFormatter":
83
- with suppress(ValueError):
84
- return HTMLFormatter(
85
- format(
86
- (
87
- value.formatting()
88
- if isinstance(value, TagFormat)
89
- else (
90
- self.get_spec_formatter(value)(**value.__dict__).formatting()
91
- if is_spec_format(value)
92
- else value
93
- )
94
- ),
95
- fmt,
96
- )
97
- )
98
- return self.format_raw_value(value, fmt)
99
-
100
- def format_raw_value(self, value: typing.Any, fmt: str) -> "HTMLFormatter":
101
- fmts = list(map(lambda fmt: self.is_html_format(value, fmt), fmt.split("+")))
102
- tag_format = self.check_formats(value, fmts)
103
-
104
- if is_spec_format(value):
105
- value.string = tag_format
106
- tag_format = self.get_spec_formatter(value)(**value.__dict__)
107
-
108
- return tag_format.formatting()
109
-
110
- def format(self, __string: str, *args: object, **kwargs: object) -> "HTMLFormatter":
111
- return HTMLFormatter(super().format(__string, *args, **kwargs))
112
-
113
-
114
- class FormatString(str):
115
- STRING_FORMATTER: StringFormatterProto = StringFormatter()
116
-
117
- def __new__(cls, string: str) -> typing.Self:
118
- if isinstance(string, TagFormat):
119
- return super().__new__(cls, string.formatting())
120
- return super().__new__(cls, string)
121
-
122
- def __add__(self, value: str) -> "HTMLFormatter":
123
- return HTMLFormatter(
124
- str.__add__(
125
- escape(self),
126
- value.formatting() if isinstance(value, TagFormat) else escape(value),
127
- )
128
- )
129
-
130
- def __radd__(self, value: str) -> "HTMLFormatter":
131
- """Return value+self."""
132
- return HTMLFormatter(FormatString.__add__(FormatString(value), self).as_str())
133
-
134
- def as_str(self) -> str:
135
- """Return self as a standart string."""
136
- return self.__str__()
137
-
138
- def format(self, *args: object, **kwargs: object) -> "HTMLFormatter":
139
- return self.STRING_FORMATTER.format(self, *args, **kwargs)
140
-
141
-
142
- class EscapedString(FormatString):
143
- @property
144
- def former_string(self) -> str:
145
- return html.unescape(self)
146
-
147
-
148
- class TagFormat(FormatString):
149
- tag: str
150
- data: dict[str, typing.Any]
151
-
152
- def __new__(
153
- cls,
154
- string: str,
155
- *,
156
- tag: str,
157
- **data: typing.Any,
158
- ) -> typing.Self:
159
- if isinstance(string, TagFormat):
160
- string = string.formatting()
161
- elif not isinstance(
162
- string,
163
- (
164
- EscapedString,
165
- HTMLFormatter,
166
- ),
167
- ):
168
- string = escape(string)
169
- obj = super().__new__(cls, string)
170
- obj.tag = tag
171
- obj.data = data
172
- return obj
173
-
174
- def get_tag_data(self) -> str:
175
- return "".join(f" {k}={v}" for k, v in self.data.items())
176
-
177
- def formatting(self) -> "HTMLFormatter":
178
- return HTMLFormatter(
179
- TAG_FORMAT.format(
180
- tag=self.tag,
181
- data=self.get_tag_data(),
182
- content=self,
183
- )
184
- )
185
-
186
-
187
- class HTMLFormatter(FormatString):
188
- """
189
- >>> HTMLFormatter(bold("Hello, World"))
190
- '<b>Hello, World</b>'
191
- HTMLFormatter("Hi, {name:italic}").format(name="Max")
192
- 'Hi, <i>Max</i>'
193
- """
194
-
195
- PARSE_MODE = ParseMode.HTML
196
-
197
-
198
- def escape(string: str) -> EscapedString:
199
- if isinstance(string, EscapedString | HTMLFormatter):
200
- return EscapedString(string)
201
- return EscapedString(html.escape(string, quote=False))
202
-
203
-
204
- def block_quote(string: str) -> TagFormat:
205
- return TagFormat(string, tag="blockquote")
206
-
207
-
208
- def bold(string: str) -> TagFormat:
209
- return TagFormat(string, tag="b")
210
-
211
-
212
- def channel_boost_link(channel_id: str | int, string: str | None = None):
213
- return link(get_channel_boost_link(channel_id), string)
214
-
215
-
216
- def code_inline(string: str) -> TagFormat:
217
- return TagFormat(string, tag="code")
218
-
219
-
220
- def italic(string: str) -> TagFormat:
221
- return TagFormat(string, tag="i")
222
-
223
-
224
- def link(href: str, string: str | None = None) -> TagFormat:
225
- return TagFormat(
226
- string or href,
227
- tag="a",
228
- href=QUOT_MARK + href + QUOT_MARK,
229
- )
230
-
231
-
232
- def pre_code(string: str, lang: str | ProgrammingLanguage | None = None) -> TagFormat:
233
- if lang is None:
234
- return TagFormat(string, tag="pre")
235
- lang = lang.value if isinstance(lang, ProgrammingLanguage) else lang
236
- return pre_code(TagFormat(string, tag="code", **{"class": f"language-{lang}"}))
237
-
238
-
239
- def spoiler(string: str) -> TagFormat:
240
- return TagFormat(string, tag="tg-spoiler")
241
-
242
-
243
- def start_bot_link(bot_id: str | int, data: str, string: str | None = None) -> TagFormat:
244
- return link(get_start_bot_link(bot_id, data), string)
245
-
246
-
247
- def start_group_link(bot_id: str | int, data: str, string: str | None = None) -> TagFormat:
248
- return link(get_start_group_link(bot_id, data), string)
249
-
250
-
251
- def strike(string: str) -> TagFormat:
252
- return TagFormat(string, tag="s")
253
-
254
-
255
- def mention(string: str, user_id: int) -> TagFormat:
256
- return link(get_mention_link(user_id), string)
257
-
258
-
259
- def tg_emoji(string: str, emoji_id: int) -> TagFormat:
260
- return TagFormat(string, tag="tg-emoji", **{"emoji-id": emoji_id})
261
-
262
-
263
- def invite_chat_link(invite_link: str, string: str | None = None) -> TagFormat:
264
- return link(
265
- get_invite_chat_link(invite_link),
266
- string or f"https://t.me/joinchat/{invite_link}",
267
- )
268
-
269
-
270
- def resolve_domain(username: str, string: str | None = None) -> TagFormat:
271
- return link(
272
- get_resolve_domain_link(username),
273
- string or f"t.me/{username}",
274
- )
275
-
276
-
277
- def underline(string: str) -> TagFormat:
278
- return TagFormat(string, tag="u")
279
-
280
-
281
- __all__ = (
1
+ from __future__ import annotations
2
+
3
+ import dataclasses
4
+ import html
5
+ import string
6
+ import typing
7
+ from contextlib import suppress
8
+
9
+ from telegrinder.tools.parse_mode import ParseMode
10
+ from telegrinder.types.enums import ProgrammingLanguage
11
+
12
+ from .deep_links import tg_mention_link
13
+ from .spec_html_formats import SpecialFormat, is_spec_format
14
+
15
+ type HTMLFormat = str | TagFormat
16
+
17
+ HTML_UNION_SPECIFIERS_SEPARATOR: typing.Final[str] = "+"
18
+ TAG_FORMAT: typing.Final[str] = "<{tag}{data}>{content}</{tag}>"
19
+ QUOT_MARK: typing.Final[str] = '"'
20
+
21
+
22
+ class StringFormatterProto(typing.Protocol):
23
+ def format_field(self, value: typing.Any, fmt: str) -> HTMLFormatter: ...
24
+
25
+ def format(self, __string: str, *args: object, **kwargs: object) -> HTMLFormatter: ...
26
+
27
+
28
+ class StringFormatter(string.Formatter):
29
+ """String formatter, using substitutions from args and kwargs.
30
+ The substitutions are identified by braces ('{' and '}') with
31
+ specifiers: `bold`, `italic`, `underline`, `strike` etc...
32
+ """
33
+
34
+ __formats__: typing.ClassVar[tuple[str, ...]] = (
35
+ "bold",
36
+ "code_inline",
37
+ "italic",
38
+ "spoiler",
39
+ "strike",
40
+ "underline",
41
+ )
42
+
43
+ def validate_html_format(self, value: typing.Any, fmt: str) -> HTMLFormat:
44
+ if not fmt:
45
+ raise ValueError("Formats union should be: format+format.")
46
+
47
+ if fmt not in self.__formats__:
48
+ raise ValueError(
49
+ "Unknown format {!r} for object of type {!r}.".format(
50
+ fmt,
51
+ type(value).__name__,
52
+ )
53
+ )
54
+
55
+ return fmt
56
+
57
+ def get_spec_formatter(self, value: SpecialFormat) -> typing.Callable[..., TagFormat]:
58
+ return globals()[value.__formatter_name__]
59
+
60
+ def make_tag_format(self, value: typing.Any, fmts: list[HTMLFormat]) -> TagFormat:
61
+ if is_spec_format(value):
62
+ value = value.string
63
+
64
+ current_format = globals()[fmts.pop(0)](
65
+ str(value)
66
+ if isinstance(value, TagFormat)
67
+ else escape(FormatString(value) if not isinstance(value, str) else value)
68
+ )
69
+ for fmt in fmts:
70
+ current_format = globals()[fmt](current_format)
71
+
72
+ return (
73
+ TagFormat(
74
+ current_format,
75
+ tag=value.tag,
76
+ **value.data,
77
+ )
78
+ if isinstance(value, TagFormat)
79
+ else current_format
80
+ )
81
+
82
+ def format_field(self, value: typing.Any, fmt: str) -> HTMLFormatter:
83
+ with suppress(ValueError):
84
+ return HTMLFormatter(
85
+ format(
86
+ (
87
+ value.formatting()
88
+ if isinstance(value, TagFormat)
89
+ else (
90
+ self.get_spec_formatter(value)(**dataclasses.asdict(value)).formatting()
91
+ if is_spec_format(value)
92
+ else value
93
+ )
94
+ ),
95
+ fmt,
96
+ )
97
+ )
98
+
99
+ fmts = list(
100
+ map(
101
+ lambda fmt: self.validate_html_format(value, fmt),
102
+ fmt.split(HTML_UNION_SPECIFIERS_SEPARATOR),
103
+ ),
104
+ )
105
+ tag_format = self.make_tag_format(value, fmts)
106
+
107
+ if is_spec_format(value):
108
+ value.string = tag_format
109
+ tag_format = self.get_spec_formatter(value)(**dataclasses.asdict(value))
110
+
111
+ return tag_format.formatting()
112
+
113
+ def format(self, __string: str, *args: object, **kwargs: object) -> HTMLFormatter:
114
+ return HTMLFormatter(super().format(__string, *args, **kwargs))
115
+
116
+
117
+ class FormatString(str):
118
+ STRING_FORMATTER: StringFormatterProto = StringFormatter()
119
+
120
+ def __new__(cls, string: str, /) -> typing.Self:
121
+ if isinstance(string, TagFormat):
122
+ return super().__new__(cls, string.formatting())
123
+ return super().__new__(cls, string)
124
+
125
+ def __add__(self, value: str) -> HTMLFormatter:
126
+ """Returns self+value."""
127
+ return HTMLFormatter(
128
+ str.__add__(
129
+ escape(self),
130
+ value.formatting() if isinstance(value, TagFormat) else escape(value),
131
+ )
132
+ )
133
+
134
+ def __radd__(self, value: str) -> HTMLFormatter:
135
+ """Returns value+self."""
136
+ return HTMLFormatter(FormatString.__add__(FormatString(value), self).as_str())
137
+
138
+ def as_str(self) -> str:
139
+ """Returns self as a standart string."""
140
+ return self.__str__()
141
+
142
+ def format(self, *args: object, **kwargs: object) -> "HTMLFormatter":
143
+ return self.STRING_FORMATTER.format(self, *args, **kwargs)
144
+
145
+
146
+ class EscapedString(FormatString):
147
+ @property
148
+ def former_string(self) -> str:
149
+ return html.unescape(self)
150
+
151
+
152
+ class TagFormat(FormatString):
153
+ tag: str
154
+ data: dict[str, typing.Any]
155
+
156
+ def __new__(
157
+ cls,
158
+ string: str,
159
+ /,
160
+ *,
161
+ tag: str,
162
+ **data: typing.Any | None,
163
+ ) -> typing.Self:
164
+ if isinstance(string, TagFormat):
165
+ string = string.formatting()
166
+ elif not isinstance(
167
+ string,
168
+ (
169
+ EscapedString,
170
+ HTMLFormatter,
171
+ ),
172
+ ):
173
+ string = escape(string)
174
+
175
+ obj = super().__new__(cls, string)
176
+ obj.tag = tag
177
+ obj.data = data
178
+ return obj
179
+
180
+ def get_tag_data(self) -> str:
181
+ return "".join(f" {k}={v}" if v is not None else f" {k}" for k, v in self.data.items())
182
+
183
+ def formatting(self) -> HTMLFormatter:
184
+ return HTMLFormatter(
185
+ TAG_FORMAT.format(
186
+ tag=self.tag,
187
+ data=self.get_tag_data(),
188
+ content=self,
189
+ )
190
+ )
191
+
192
+
193
+ class HTMLFormatter(FormatString):
194
+ """>>> HTMLFormatter(bold("Hello, World"))
195
+ >>> '<b>Hello, World</b>'
196
+ >>> HTMLFormatter("Hi, {name:italic}").format(name="Max")
197
+ >>> 'Hi, <i>Max</i>'
198
+ """
199
+
200
+ PARSE_MODE = ParseMode.HTML
201
+
202
+
203
+ def escape(string: str) -> EscapedString:
204
+ if isinstance(string, EscapedString | HTMLFormatter):
205
+ return EscapedString(string)
206
+ return EscapedString(html.escape(string, quote=False))
207
+
208
+
209
+ def block_quote(string: str, expandable: bool = False) -> TagFormat:
210
+ return TagFormat(
211
+ string,
212
+ tag="blockquote",
213
+ **{} if not expandable else {"expandable": None},
214
+ )
215
+
216
+
217
+ def bold(string: str) -> TagFormat:
218
+ return TagFormat(string, tag="b")
219
+
220
+
221
+ def code_inline(string: str) -> TagFormat:
222
+ return TagFormat(string, tag="code")
223
+
224
+
225
+ def italic(string: str) -> TagFormat:
226
+ return TagFormat(string, tag="i")
227
+
228
+
229
+ def link(href: str, string: str | None = None) -> TagFormat:
230
+ return TagFormat(
231
+ string or href,
232
+ tag="a",
233
+ href=QUOT_MARK + href + QUOT_MARK,
234
+ )
235
+
236
+
237
+ def pre_code(string: str, lang: str | ProgrammingLanguage | None = None) -> TagFormat:
238
+ if lang is None:
239
+ return TagFormat(string, tag="pre")
240
+ lang = lang.value if isinstance(lang, ProgrammingLanguage) else lang
241
+ return pre_code(TagFormat(string, tag="code", **{"class": f"language-{lang}"}))
242
+
243
+
244
+ def spoiler(string: str) -> TagFormat:
245
+ return TagFormat(string, tag="tg-spoiler")
246
+
247
+
248
+ def strike(string: str) -> TagFormat:
249
+ return TagFormat(string, tag="s")
250
+
251
+
252
+ def mention(string: str, user_id: int) -> TagFormat:
253
+ return link(tg_mention_link(user_id=user_id), string)
254
+
255
+
256
+ def tg_emoji(string: str, emoji_id: int) -> TagFormat:
257
+ return TagFormat(string, tag="tg-emoji", emoji_id=emoji_id)
258
+
259
+
260
+ def underline(string: str) -> TagFormat:
261
+ return TagFormat(string, tag="u")
262
+
263
+
264
+ __all__ = (
282
265
  "FormatString",
283
266
  "HTMLFormatter",
284
267
  "SpecialFormat",
285
268
  "block_quote",
286
269
  "bold",
287
- "channel_boost_link",
288
270
  "code_inline",
289
271
  "escape",
290
- "get_channel_boost_link",
291
- "get_invite_chat_link",
292
- "get_mention_link",
293
- "get_resolve_domain_link",
294
- "get_start_bot_link",
295
- "get_start_group_link",
296
- "invite_chat_link",
297
272
  "italic",
298
273
  "link",
299
274
  "mention",
300
275
  "pre_code",
301
- "resolve_domain",
302
276
  "spoiler",
303
- "start_bot_link",
304
- "start_group_link",
305
277
  "strike",
306
278
  "tg_emoji",
307
- "underline",
308
- )
279
+ "underline",
280
+ )