telegram_entities 0.1.0
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.
- checksums.yaml +7 -0
- data/.standard.yml +3 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +132 -0
- data/LICENSE.txt +21 -0
- data/README.md +313 -0
- data/Rakefile +10 -0
- data/lib/telegram_entities/entities.rb +462 -0
- data/lib/telegram_entities/entity_tools.rb +121 -0
- data/lib/telegram_entities/tdlib_converter.rb +88 -0
- data/lib/telegram_entities/version.rb +5 -0
- data/lib/telegram_entities.rb +33 -0
- data/sig/telegram_entities.rbs +4 -0
- metadata +71 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 59bea042c497fc78acc233e6226d4e74fedd0350b98f5c04d532352531106f9f
|
|
4
|
+
data.tar.gz: f01c66585462f4f3e49f32739907a8777b9e2fbeefa056b95ce5c5f7ab20e086
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 7fa8daab2f8252ce3792be80892beea8e6eab0c2af9ffcf0d5f8978fc0eb7c0d2af64344f2a943fcd56575ef1cf75ba706ca40e657117c3388a974fadc4d2389
|
|
7
|
+
data.tar.gz: 26a866427a665ae4198a0c863dbe6a96dd5db423d84a186bdfbe3708e5295cfa798b29d5ecb177719a0a7100a6bb0fa2f781adf0e1936e2e7de6439e5dd3dc6e
|
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our
|
|
6
|
+
community a harassment-free experience for everyone, regardless of age, body
|
|
7
|
+
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
|
8
|
+
identity and expression, level of experience, education, socio-economic status,
|
|
9
|
+
nationality, personal appearance, race, caste, color, religion, or sexual
|
|
10
|
+
identity and orientation.
|
|
11
|
+
|
|
12
|
+
We pledge to act and interact in ways that contribute to an open, welcoming,
|
|
13
|
+
diverse, inclusive, and healthy community.
|
|
14
|
+
|
|
15
|
+
## Our Standards
|
|
16
|
+
|
|
17
|
+
Examples of behavior that contributes to a positive environment for our
|
|
18
|
+
community include:
|
|
19
|
+
|
|
20
|
+
* Demonstrating empathy and kindness toward other people
|
|
21
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
|
22
|
+
* Giving and gracefully accepting constructive feedback
|
|
23
|
+
* Accepting responsibility and apologizing to those affected by our mistakes,
|
|
24
|
+
and learning from the experience
|
|
25
|
+
* Focusing on what is best not just for us as individuals, but for the overall
|
|
26
|
+
community
|
|
27
|
+
|
|
28
|
+
Examples of unacceptable behavior include:
|
|
29
|
+
|
|
30
|
+
* The use of sexualized language or imagery, and sexual attention or advances of
|
|
31
|
+
any kind
|
|
32
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
|
33
|
+
* Public or private harassment
|
|
34
|
+
* Publishing others' private information, such as a physical or email address,
|
|
35
|
+
without their explicit permission
|
|
36
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
37
|
+
professional setting
|
|
38
|
+
|
|
39
|
+
## Enforcement Responsibilities
|
|
40
|
+
|
|
41
|
+
Community leaders are responsible for clarifying and enforcing our standards of
|
|
42
|
+
acceptable behavior and will take appropriate and fair corrective action in
|
|
43
|
+
response to any behavior that they deem inappropriate, threatening, offensive,
|
|
44
|
+
or harmful.
|
|
45
|
+
|
|
46
|
+
Community leaders have the right and responsibility to remove, edit, or reject
|
|
47
|
+
comments, commits, code, wiki edits, issues, and other contributions that are
|
|
48
|
+
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
|
49
|
+
decisions when appropriate.
|
|
50
|
+
|
|
51
|
+
## Scope
|
|
52
|
+
|
|
53
|
+
This Code of Conduct applies within all community spaces, and also applies when
|
|
54
|
+
an individual is officially representing the community in public spaces.
|
|
55
|
+
Examples of representing our community include using an official email address,
|
|
56
|
+
posting via an official social media account, or acting as an appointed
|
|
57
|
+
representative at an online or offline event.
|
|
58
|
+
|
|
59
|
+
## Enforcement
|
|
60
|
+
|
|
61
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
62
|
+
reported to the community leaders responsible for enforcement at
|
|
63
|
+
[INSERT CONTACT METHOD].
|
|
64
|
+
All complaints will be reviewed and investigated promptly and fairly.
|
|
65
|
+
|
|
66
|
+
All community leaders are obligated to respect the privacy and security of the
|
|
67
|
+
reporter of any incident.
|
|
68
|
+
|
|
69
|
+
## Enforcement Guidelines
|
|
70
|
+
|
|
71
|
+
Community leaders will follow these Community Impact Guidelines in determining
|
|
72
|
+
the consequences for any action they deem in violation of this Code of Conduct:
|
|
73
|
+
|
|
74
|
+
### 1. Correction
|
|
75
|
+
|
|
76
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed
|
|
77
|
+
unprofessional or unwelcome in the community.
|
|
78
|
+
|
|
79
|
+
**Consequence**: A private, written warning from community leaders, providing
|
|
80
|
+
clarity around the nature of the violation and an explanation of why the
|
|
81
|
+
behavior was inappropriate. A public apology may be requested.
|
|
82
|
+
|
|
83
|
+
### 2. Warning
|
|
84
|
+
|
|
85
|
+
**Community Impact**: A violation through a single incident or series of
|
|
86
|
+
actions.
|
|
87
|
+
|
|
88
|
+
**Consequence**: A warning with consequences for continued behavior. No
|
|
89
|
+
interaction with the people involved, including unsolicited interaction with
|
|
90
|
+
those enforcing the Code of Conduct, for a specified period of time. This
|
|
91
|
+
includes avoiding interactions in community spaces as well as external channels
|
|
92
|
+
like social media. Violating these terms may lead to a temporary or permanent
|
|
93
|
+
ban.
|
|
94
|
+
|
|
95
|
+
### 3. Temporary Ban
|
|
96
|
+
|
|
97
|
+
**Community Impact**: A serious violation of community standards, including
|
|
98
|
+
sustained inappropriate behavior.
|
|
99
|
+
|
|
100
|
+
**Consequence**: A temporary ban from any sort of interaction or public
|
|
101
|
+
communication with the community for a specified period of time. No public or
|
|
102
|
+
private interaction with the people involved, including unsolicited interaction
|
|
103
|
+
with those enforcing the Code of Conduct, is allowed during this period.
|
|
104
|
+
Violating these terms may lead to a permanent ban.
|
|
105
|
+
|
|
106
|
+
### 4. Permanent Ban
|
|
107
|
+
|
|
108
|
+
**Community Impact**: Demonstrating a pattern of violation of community
|
|
109
|
+
standards, including sustained inappropriate behavior, harassment of an
|
|
110
|
+
individual, or aggression toward or disparagement of classes of individuals.
|
|
111
|
+
|
|
112
|
+
**Consequence**: A permanent ban from any sort of public interaction within the
|
|
113
|
+
community.
|
|
114
|
+
|
|
115
|
+
## Attribution
|
|
116
|
+
|
|
117
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
|
118
|
+
version 2.1, available at
|
|
119
|
+
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
|
|
120
|
+
|
|
121
|
+
Community Impact Guidelines were inspired by
|
|
122
|
+
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
|
|
123
|
+
|
|
124
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
|
125
|
+
[https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
|
|
126
|
+
[https://www.contributor-covenant.org/translations][translations].
|
|
127
|
+
|
|
128
|
+
[homepage]: https://www.contributor-covenant.org
|
|
129
|
+
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
|
|
130
|
+
[Mozilla CoC]: https://github.com/mozilla/diversity
|
|
131
|
+
[FAQ]: https://www.contributor-covenant.org/faq
|
|
132
|
+
[translations]: https://www.contributor-covenant.org/translations
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Kirill Bobykin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
# TelegramEntities
|
|
2
|
+
|
|
3
|
+
[](https://www.ruby-lang.org/)
|
|
4
|
+
[](LICENSE.txt)
|
|
5
|
+
[](https://rubygems.org/gems/telegram_entities)
|
|
6
|
+
|
|
7
|
+
Ruby gem for converting Telegram message entities between HTML and Markdown formats. Supports all Telegram MessageEntity types with UTF-16 offset/length handling.
|
|
8
|
+
|
|
9
|
+
📚 **Official Telegram Documentation:**
|
|
10
|
+
- [MessageEntity Types](https://core.telegram.org/type/MessageEntity) - Complete schema of all entity types
|
|
11
|
+
- [Styled Text with Message Entities](https://core.telegram.org/api/entities) - How Telegram styles text using entities
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
Install the gem and add to the application's Gemfile by executing:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
bundle add telegram_entities
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
gem install telegram_entities
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Quick Start
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
require 'telegram_entities'
|
|
31
|
+
|
|
32
|
+
# Convert Markdown to Telegram entities
|
|
33
|
+
entities = TelegramEntities.from_markdown('*bold* _italic_ `code`')
|
|
34
|
+
puts entities.message
|
|
35
|
+
# => "bold italic code"
|
|
36
|
+
|
|
37
|
+
puts entities.entities
|
|
38
|
+
# => [
|
|
39
|
+
# {"type"=>"bold", "offset"=>0, "length"=>4},
|
|
40
|
+
# {"type"=>"italic", "offset"=>5, "length"=>6},
|
|
41
|
+
# {"type"=>"code", "offset"=>12, "length"=>4}
|
|
42
|
+
# ]
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
### Converting from Markdown to Entities
|
|
48
|
+
|
|
49
|
+
Parse Markdown text and extract Telegram entities:
|
|
50
|
+
|
|
51
|
+
```ruby
|
|
52
|
+
require 'telegram_entities'
|
|
53
|
+
|
|
54
|
+
text = '*Hello* _world_! Visit https://example.com'
|
|
55
|
+
entities = TelegramEntities.from_markdown(text)
|
|
56
|
+
|
|
57
|
+
puts entities.message
|
|
58
|
+
# => "Hello world! Visit https://example.com"
|
|
59
|
+
|
|
60
|
+
puts entities.entities.inspect
|
|
61
|
+
# => [
|
|
62
|
+
# {"type"=>"bold", "offset"=>0, "length"=>5},
|
|
63
|
+
# {"type"=>"italic", "offset"=>6, "length"=>5},
|
|
64
|
+
# {"type"=>"url", "offset"=>18, "length"=>19}
|
|
65
|
+
# ]
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### Converting from HTML to Entities
|
|
69
|
+
|
|
70
|
+
Parse HTML and extract Telegram entities:
|
|
71
|
+
|
|
72
|
+
```ruby
|
|
73
|
+
html = '<b>bold</b> <i>italic</i> <a href="https://example.com">link</a>'
|
|
74
|
+
entities = TelegramEntities.from_html(html)
|
|
75
|
+
|
|
76
|
+
puts entities.message
|
|
77
|
+
# => "bold italic link"
|
|
78
|
+
|
|
79
|
+
puts entities.entities
|
|
80
|
+
# => [
|
|
81
|
+
# {"type"=>"bold", "offset"=>0, "length"=>4},
|
|
82
|
+
# {"type"=>"italic", "offset"=>5, "length"=>6},
|
|
83
|
+
# {"type"=>"text_url", "offset"=>12, "length"=>4, "url"=>"https://example.com"}
|
|
84
|
+
# ]
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Converting from TDLib formattedText to Entities
|
|
88
|
+
|
|
89
|
+
Convert TDLib formattedText (from [tdlib-ruby](https://github.com/southbridgeio/tdlib-ruby)) to TelegramEntities:
|
|
90
|
+
|
|
91
|
+
```ruby
|
|
92
|
+
require 'telegram_entities'
|
|
93
|
+
|
|
94
|
+
# Get formattedText from TDLib
|
|
95
|
+
puts tdlib_formatted_text.inspect
|
|
96
|
+
# => #<TD::Types::FormattedText text="" entities=[]>
|
|
97
|
+
|
|
98
|
+
# Convert to TelegramEntities
|
|
99
|
+
entities = TelegramEntities.from_tdlib_formatted_text(tdlib_formatted_text.to_h)
|
|
100
|
+
|
|
101
|
+
puts entities.message
|
|
102
|
+
# => "text content"
|
|
103
|
+
|
|
104
|
+
puts entities.entities
|
|
105
|
+
# => [
|
|
106
|
+
# {"type"=>"bold", "offset"=>0, "length"=>4},
|
|
107
|
+
# {"type"=>"text_url", "offset"=>10, "length"=>5, "url"=>"https://example.com"}
|
|
108
|
+
# ]
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
The method supports all TDLib text entity types and automatically converts them to TelegramEntities format.
|
|
112
|
+
|
|
113
|
+
### Converting Entities to HTML
|
|
114
|
+
|
|
115
|
+
Convert Telegram entities back to HTML:
|
|
116
|
+
|
|
117
|
+
```ruby
|
|
118
|
+
# Create entities manually
|
|
119
|
+
entities = TelegramEntities.new('Hello world', [
|
|
120
|
+
{'type' => 'bold', 'offset' => 0, 'length' => 5},
|
|
121
|
+
{'type' => 'italic', 'offset' => 6, 'length' => 5}
|
|
122
|
+
])
|
|
123
|
+
|
|
124
|
+
html = entities.to_html
|
|
125
|
+
puts html
|
|
126
|
+
# => "<strong>Hello</strong> <em>world</em>"
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
### Telegram-Specific Tags
|
|
130
|
+
|
|
131
|
+
When sending HTML to Telegram Bot API, use `allow_telegram_tags: true` to get Telegram-compatible tags. This is especially important for special entity types like spoilers, custom emojis, and cashtags:
|
|
132
|
+
|
|
133
|
+
```ruby
|
|
134
|
+
# Create entities with Telegram-specific types
|
|
135
|
+
# Message: "$BTC 🚀 secret"
|
|
136
|
+
entities = TelegramEntities.new('$BTC 🚀 secret', [
|
|
137
|
+
{'type' => 'cashtag', 'offset' => 0, 'length' => 4}, # $BTC
|
|
138
|
+
{'type' => 'custom_emoji', 'offset' => 5, 'length' => 2, 'custom_emoji_id' => 12345}, # 🚀
|
|
139
|
+
{'type' => 'spoiler', 'offset' => 7, 'length' => 6} # secret
|
|
140
|
+
])
|
|
141
|
+
|
|
142
|
+
# Standard HTML (for web display)
|
|
143
|
+
html_standard = entities.to_html
|
|
144
|
+
puts html_standard
|
|
145
|
+
# => "<span class=\"tg-cashtag\">$BTC</span> 🚀 <span class=\"tg-spoiler\">secret</span>"
|
|
146
|
+
|
|
147
|
+
# Telegram-specific tags (for Telegram Bot API)
|
|
148
|
+
html_telegram = entities.to_html(allow_telegram_tags: true)
|
|
149
|
+
puts html_telegram
|
|
150
|
+
# => "<tg-cashtag>$BTC</tg-cashtag> <tg-emoji emoji-id=\"12345\">🚀</tg-emoji> <tg-spoiler>secret</tg-spoiler>"
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
**Key differences for Telegram-specific entities:**
|
|
154
|
+
|
|
155
|
+
| Entity Type | Standard HTML | Telegram Tags |
|
|
156
|
+
|-------------|---------------|---------------|
|
|
157
|
+
| **Spoiler** | `<span class="tg-spoiler">text</span>` | `<tg-spoiler>text</tg-spoiler>` |
|
|
158
|
+
| **Custom Emoji** | `🚀` (plain text, no tags) | `<tg-emoji emoji-id="12345">🚀</tg-emoji>` |
|
|
159
|
+
| **Cashtag** | `<span class="tg-cashtag">$BTC</span>` | `<tg-cashtag>$BTC</tg-cashtag>` |
|
|
160
|
+
| **Hashtag** | `<span class="tg-hashtag">#tag</span>` | `<tg-hashtag>#tag</tg-hashtag>` |
|
|
161
|
+
| **Bot Command** | `<span class="tg-bot-command">/start</span>` | `<tg-bot-command>/start</tg-bot-command>` |
|
|
162
|
+
|
|
163
|
+
**When to use each mode:**
|
|
164
|
+
- **Standard HTML** (`allow_telegram_tags: false`): For displaying in web browsers or general HTML rendering
|
|
165
|
+
- **Telegram Tags** (`allow_telegram_tags: true`): For sending messages via Telegram Bot API using `parse_mode: 'HTML'`
|
|
166
|
+
|
|
167
|
+
### Real-World Example
|
|
168
|
+
|
|
169
|
+
```ruby
|
|
170
|
+
require 'telegram_entities'
|
|
171
|
+
|
|
172
|
+
# User sends a message with Markdown
|
|
173
|
+
user_input = '*Important*: Check out https://github.com/qelphybox/telegram_entities_rb'
|
|
174
|
+
|
|
175
|
+
# Convert to Telegram entities for Bot API
|
|
176
|
+
entities = TelegramEntities.from_markdown(user_input)
|
|
177
|
+
|
|
178
|
+
# Send to Telegram Bot API
|
|
179
|
+
# bot.send_message(
|
|
180
|
+
# chat_id: chat_id,
|
|
181
|
+
# text: entities.message,
|
|
182
|
+
# entities: entities.entities
|
|
183
|
+
# )
|
|
184
|
+
|
|
185
|
+
# Later, convert received entities back to HTML for display
|
|
186
|
+
html_entities = TelegramEntities.new(entities.message, entities.entities)
|
|
187
|
+
html_output = html_entities.to_html
|
|
188
|
+
# => "<strong>Important</strong>: Check out <a href=\"https://github.com/qelphybox/telegram_entities_rb\">https://github.com/qelphybox/telegram_entities_rb</a>"
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
## Supported Entity Types
|
|
192
|
+
|
|
193
|
+
The gem supports **all** Telegram MessageEntity types. Here's a complete reference:
|
|
194
|
+
|
|
195
|
+
> 📖 For the complete list of entity types and their specifications, see the [official Telegram documentation](https://core.telegram.org/type/MessageEntity).
|
|
196
|
+
|
|
197
|
+
### 📝 Text Formatting
|
|
198
|
+
|
|
199
|
+
| Type | Markdown | HTML | Description |
|
|
200
|
+
|------|----------|------|-------------|
|
|
201
|
+
| `bold` | `*text*` or `**text**` | `<b>text</b>` | Bold text |
|
|
202
|
+
| `italic` | `_text_` or `*text*` | `<i>text</i>` | Italic text |
|
|
203
|
+
| `underline` | `__text__` | `<u>text</u>` | Underlined text |
|
|
204
|
+
| `strike` | `~~text~~` | `<s>text</s>` | Strikethrough text |
|
|
205
|
+
| `code` | `` `code` `` | `<code>code</code>` | Inline code |
|
|
206
|
+
| `pre` | ` ```code``` ` | `<pre>code</pre>` | Code block (with optional `language`) |
|
|
207
|
+
| `spoiler` | `||text||` | `<tg-spoiler>text</tg-spoiler>` | Spoiler text |
|
|
208
|
+
|
|
209
|
+
### 🔗 Links and References
|
|
210
|
+
|
|
211
|
+
| Type | Example | HTML Output | Notes |
|
|
212
|
+
|------|---------|-------------|-------|
|
|
213
|
+
| `mention` | `@username` | `<a href="https://t.me/username">@username</a>` | Username mention |
|
|
214
|
+
| `mention_name` | User by ID | `<a href="tg://user?id=123">Name</a>` | Requires `user.id` field |
|
|
215
|
+
| `hashtag` | `#hashtag` | `#hashtag` | Hashtag |
|
|
216
|
+
| `cashtag` | `$USD` | `$USD` | Cashtag |
|
|
217
|
+
| `bot_command` | `/start` | `/start` | Bot command |
|
|
218
|
+
| `url` | `https://example.com` | `<a href="https://example.com">https://example.com</a>` | Auto-detected URL |
|
|
219
|
+
| `email` | `user@example.com` | `<a href="mailto:user@example.com">user@example.com</a>` | Auto-detected email |
|
|
220
|
+
| `phone` | `+1234567890` | `<a href="tel:+1234567890">+1234567890</a>` | Auto-detected phone |
|
|
221
|
+
| `text_url` | Custom link | `<a href="url">text</a>` | Requires `url` field |
|
|
222
|
+
|
|
223
|
+
### 🎨 Media and Special
|
|
224
|
+
|
|
225
|
+
| Type | Description | Required Fields |
|
|
226
|
+
|------|-------------|----------------|
|
|
227
|
+
| `custom_emoji` | Custom emoji | `custom_emoji_id` |
|
|
228
|
+
| `media_timestamp` | Media timestamp | `media_timestamp` (integer) |
|
|
229
|
+
| `bank_card` | Bank card number | - |
|
|
230
|
+
|
|
231
|
+
### 💬 Block Quotes
|
|
232
|
+
|
|
233
|
+
| Type | HTML | Description |
|
|
234
|
+
|------|------|-------------|
|
|
235
|
+
| `blockquote` | `<blockquote>text</blockquote>` | Block quote |
|
|
236
|
+
| `expandable_blockquote` | `<blockquote expandable>text</blockquote>` | Expandable block quote |
|
|
237
|
+
|
|
238
|
+
### Entity Structure
|
|
239
|
+
|
|
240
|
+
Each entity is a hash with the following structure:
|
|
241
|
+
|
|
242
|
+
```ruby
|
|
243
|
+
{
|
|
244
|
+
'type' => 'bold', # Entity type (required)
|
|
245
|
+
'offset' => 0, # UTF-16 offset (required)
|
|
246
|
+
'length' => 4, # UTF-16 length (required)
|
|
247
|
+
'url' => '...', # For text_url type
|
|
248
|
+
'user' => {'id' => 123}, # For mention_name type
|
|
249
|
+
'custom_emoji_id' => 12345, # For custom_emoji type
|
|
250
|
+
'media_timestamp' => 30, # For media_timestamp type
|
|
251
|
+
'language' => 'ruby' # For pre type
|
|
252
|
+
}
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
### Example with Complex Entities
|
|
256
|
+
|
|
257
|
+
```ruby
|
|
258
|
+
require 'telegram_entities'
|
|
259
|
+
|
|
260
|
+
# Complex message with multiple entity types
|
|
261
|
+
markdown = <<~TEXT
|
|
262
|
+
*Bold* and _italic_ text with `code`.
|
|
263
|
+
|
|
264
|
+
Visit https://example.com or email user@example.com
|
|
265
|
+
|
|
266
|
+
||Spoiler text||
|
|
267
|
+
TEXT
|
|
268
|
+
|
|
269
|
+
entities = TelegramEntities.from_markdown(markdown)
|
|
270
|
+
|
|
271
|
+
puts "Message: #{entities.message}"
|
|
272
|
+
puts "\nEntities (#{entities.entities.length}):"
|
|
273
|
+
entities.entities.each do |entity|
|
|
274
|
+
puts " - #{entity['type']}: offset=#{entity['offset']}, length=#{entity['length']}"
|
|
275
|
+
end
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
**Output:**
|
|
279
|
+
```
|
|
280
|
+
Message: Bold and italic text with code.
|
|
281
|
+
|
|
282
|
+
Visit https://example.com or email user@example.com
|
|
283
|
+
|
|
284
|
+
Spoiler text
|
|
285
|
+
|
|
286
|
+
Entities (5):
|
|
287
|
+
- bold: offset=0, length=4
|
|
288
|
+
- italic: offset=9, length=6
|
|
289
|
+
- code: offset=22, length=4
|
|
290
|
+
- url: offset=35, length=19
|
|
291
|
+
- email: offset=59, length=19
|
|
292
|
+
- spoiler: offset=82, length=11
|
|
293
|
+
```
|
|
294
|
+
|
|
295
|
+
**⚠️ Important Note:** All offsets and lengths are in **UTF-16 code units**, not bytes or characters. The gem handles UTF-16 encoding automatically, so you don't need to worry about it!
|
|
296
|
+
|
|
297
|
+
## Development
|
|
298
|
+
|
|
299
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
300
|
+
|
|
301
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
302
|
+
|
|
303
|
+
## Contributing
|
|
304
|
+
|
|
305
|
+
Bug reports and pull requests are welcome on GitHub at [https://github.com/qelphybox/telegram_entities_rb](https://github.com/qelphybox/telegram_entities_rb). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/qelphybox/telegram_entities_rb/blob/master/CODE_OF_CONDUCT.md).
|
|
306
|
+
|
|
307
|
+
## License
|
|
308
|
+
|
|
309
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
|
310
|
+
|
|
311
|
+
## Code of Conduct
|
|
312
|
+
|
|
313
|
+
Everyone interacting in the TelegramEntities project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/qelphybox/telegram_entities_rb/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "nokogiri"
|
|
4
|
+
|
|
5
|
+
module TelegramEntities
|
|
6
|
+
# Class that represents a message + set of Telegram entities.
|
|
7
|
+
class Entities
|
|
8
|
+
attr_accessor :message, :entities
|
|
9
|
+
|
|
10
|
+
# Creates an Entities container using a message and a list of entities.
|
|
11
|
+
#
|
|
12
|
+
# @param message [String] Converted message
|
|
13
|
+
# @param entities [Array<Hash>] Converted entities
|
|
14
|
+
def initialize(message, entities = [])
|
|
15
|
+
@message = message
|
|
16
|
+
@entities = entities
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Manually convert markdown to a message and a set of entities.
|
|
20
|
+
#
|
|
21
|
+
# @param markdown [String] Markdown text
|
|
22
|
+
# @return [Entities] Object containing message and entities
|
|
23
|
+
def self.from_markdown(markdown)
|
|
24
|
+
markdown = markdown.tr("\r\n", "\n").strip
|
|
25
|
+
message = ""
|
|
26
|
+
message_len = 0
|
|
27
|
+
entities = []
|
|
28
|
+
offset = 0
|
|
29
|
+
stack = []
|
|
30
|
+
|
|
31
|
+
while offset < markdown.length
|
|
32
|
+
# Find next special character
|
|
33
|
+
special_chars = "*_~`[]|!\\"
|
|
34
|
+
len = 0
|
|
35
|
+
while offset + len < markdown.length && !special_chars.include?(markdown[offset + len])
|
|
36
|
+
len += 1
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
piece = markdown[offset, len]
|
|
40
|
+
offset += len
|
|
41
|
+
|
|
42
|
+
if offset >= markdown.length
|
|
43
|
+
message += piece
|
|
44
|
+
break
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
char = markdown[offset]
|
|
48
|
+
offset += 1
|
|
49
|
+
next_char = markdown[offset]
|
|
50
|
+
|
|
51
|
+
# Handle escape
|
|
52
|
+
if char == "\\"
|
|
53
|
+
message += piece + (next_char || "")
|
|
54
|
+
message_len += EntityTools.mb_strlen(piece) + 1
|
|
55
|
+
offset += 1 if next_char
|
|
56
|
+
next
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Handle double characters
|
|
60
|
+
if char == "_" && next_char == "_"
|
|
61
|
+
offset += 1
|
|
62
|
+
char = "__"
|
|
63
|
+
elsif char == "|"
|
|
64
|
+
if next_char == "|"
|
|
65
|
+
offset += 1
|
|
66
|
+
char = "||"
|
|
67
|
+
else
|
|
68
|
+
message += piece + char
|
|
69
|
+
message_len += EntityTools.mb_strlen(piece) + 1
|
|
70
|
+
next
|
|
71
|
+
end
|
|
72
|
+
elsif char == "!"
|
|
73
|
+
if next_char == "["
|
|
74
|
+
offset += 1
|
|
75
|
+
char = "]("
|
|
76
|
+
else
|
|
77
|
+
message += piece + char
|
|
78
|
+
message_len += EntityTools.mb_strlen(piece) + 1
|
|
79
|
+
next
|
|
80
|
+
end
|
|
81
|
+
elsif char == "["
|
|
82
|
+
char = "]("
|
|
83
|
+
elsif char == "]"
|
|
84
|
+
if stack.empty? || stack.last[0] != "]("
|
|
85
|
+
message += piece + char
|
|
86
|
+
message_len += EntityTools.mb_strlen(piece) + 1
|
|
87
|
+
next
|
|
88
|
+
end
|
|
89
|
+
if next_char != "("
|
|
90
|
+
stack.pop
|
|
91
|
+
message += "[" + piece + char
|
|
92
|
+
message_len += EntityTools.mb_strlen(piece) + 2
|
|
93
|
+
next
|
|
94
|
+
end
|
|
95
|
+
offset += 1
|
|
96
|
+
char = "]("
|
|
97
|
+
elsif char == "`"
|
|
98
|
+
message += piece
|
|
99
|
+
message_len += EntityTools.mb_strlen(piece)
|
|
100
|
+
|
|
101
|
+
token = "`"
|
|
102
|
+
language = nil
|
|
103
|
+
if next_char == "`" && markdown[offset + 1] == "`"
|
|
104
|
+
token = "```"
|
|
105
|
+
offset += 2
|
|
106
|
+
lang_len = 0
|
|
107
|
+
while offset + lang_len < markdown.length && !["\n", " "].include?(markdown[offset + lang_len])
|
|
108
|
+
lang_len += 1
|
|
109
|
+
end
|
|
110
|
+
language = markdown[offset, lang_len] if lang_len > 0
|
|
111
|
+
offset += lang_len
|
|
112
|
+
offset += 1 if markdown[offset] == "\n"
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
piece = ""
|
|
116
|
+
pos_close = offset
|
|
117
|
+
found = false
|
|
118
|
+
while pos_close < markdown.length
|
|
119
|
+
pos_close = markdown.index(token, pos_close)
|
|
120
|
+
unless pos_close
|
|
121
|
+
found = false
|
|
122
|
+
break
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
if pos_close > 0 && markdown[pos_close - 1] == "\\"
|
|
126
|
+
piece += markdown[offset, pos_close - offset - 1] + token
|
|
127
|
+
pos_close += token.length
|
|
128
|
+
offset = pos_close
|
|
129
|
+
next
|
|
130
|
+
end
|
|
131
|
+
found = true
|
|
132
|
+
break
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
raise "Unclosed #{token} opened @ pos #{offset}!" unless found
|
|
136
|
+
|
|
137
|
+
piece += markdown[offset, pos_close - offset]
|
|
138
|
+
|
|
139
|
+
start = message_len
|
|
140
|
+
message += piece
|
|
141
|
+
piece_len = EntityTools.mb_strlen(piece)
|
|
142
|
+
message_len += piece_len
|
|
143
|
+
|
|
144
|
+
# Trim trailing whitespace from piece
|
|
145
|
+
piece_bytes = piece.bytes
|
|
146
|
+
piece_len_bytes = piece_bytes.length
|
|
147
|
+
(piece_len_bytes - 1).downto(0) do |x|
|
|
148
|
+
char = piece_bytes[x]
|
|
149
|
+
break unless [32, 13, 10].include?(char) # space, \r, \n
|
|
150
|
+
piece_len -= 1
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
if piece_len > 0
|
|
154
|
+
entity = {
|
|
155
|
+
"type" => ((token == "```") ? "pre" : "code"),
|
|
156
|
+
"offset" => start,
|
|
157
|
+
"length" => piece_len
|
|
158
|
+
}
|
|
159
|
+
entity["language"] = language if language
|
|
160
|
+
entities << entity
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
offset = pos_close + token.length
|
|
164
|
+
next
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# Handle closing tag
|
|
168
|
+
if !stack.empty? && stack.last[0] == char
|
|
169
|
+
_, start = stack.pop
|
|
170
|
+
|
|
171
|
+
if char == "]("
|
|
172
|
+
pos_close = offset
|
|
173
|
+
link = ""
|
|
174
|
+
while pos_close < markdown.length
|
|
175
|
+
pos_close = markdown.index(")", pos_close)
|
|
176
|
+
break unless pos_close
|
|
177
|
+
|
|
178
|
+
if pos_close > 0 && markdown[pos_close - 1] == "\\"
|
|
179
|
+
link += markdown[offset, pos_close - offset - 1]
|
|
180
|
+
offset = pos_close + 1
|
|
181
|
+
next
|
|
182
|
+
end
|
|
183
|
+
link += markdown[offset, pos_close - offset]
|
|
184
|
+
break
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
raise "Unclosed ) opened @ pos #{offset}!" unless pos_close
|
|
188
|
+
|
|
189
|
+
entity = handle_link(link)
|
|
190
|
+
offset = pos_close + 1
|
|
191
|
+
else
|
|
192
|
+
entity = case char
|
|
193
|
+
when "*" then {"type" => "bold"}
|
|
194
|
+
when "_" then {"type" => "italic"}
|
|
195
|
+
when "__" then {"type" => "underline"}
|
|
196
|
+
when "`" then {"type" => "code"}
|
|
197
|
+
when "~" then {"type" => "strike"}
|
|
198
|
+
when "||" then {"type" => "spoiler"}
|
|
199
|
+
else raise "Unknown char #{char} @ pos #{offset}!"
|
|
200
|
+
end
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
message += piece
|
|
204
|
+
message_len += EntityTools.mb_strlen(piece)
|
|
205
|
+
|
|
206
|
+
length_real = message_len - start
|
|
207
|
+
# Trim trailing whitespace from message
|
|
208
|
+
message_bytes = message.bytes
|
|
209
|
+
(message_bytes.length - 1).downto(0) do |x|
|
|
210
|
+
char = message_bytes[x]
|
|
211
|
+
break unless [32, 13, 10].include?(char) # space, \r, \n
|
|
212
|
+
length_real -= 1
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
if length_real > 0
|
|
216
|
+
entity["offset"] = start
|
|
217
|
+
entity["length"] = length_real
|
|
218
|
+
entities << entity
|
|
219
|
+
end
|
|
220
|
+
else
|
|
221
|
+
message += piece
|
|
222
|
+
message_len += EntityTools.mb_strlen(piece)
|
|
223
|
+
stack << [char, message_len]
|
|
224
|
+
end
|
|
225
|
+
end
|
|
226
|
+
|
|
227
|
+
raise "Found unclosed markdown elements #{stack.map(&:first).join(", ")}" unless stack.empty?
|
|
228
|
+
|
|
229
|
+
new(message.strip, entities)
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
# Manually convert HTML to a message and a set of entities.
|
|
233
|
+
#
|
|
234
|
+
# @param html [String] HTML text
|
|
235
|
+
# @return [Entities] Object containing message and entities
|
|
236
|
+
def self.from_html(html)
|
|
237
|
+
html = html.gsub(/<br(\s*)?\/?>/i, "\n")
|
|
238
|
+
# Use HTML parser to properly handle boolean attributes like 'expandable'
|
|
239
|
+
doc = Nokogiri::HTML::DocumentFragment.parse("<body>#{html.strip}</body>")
|
|
240
|
+
message = +""
|
|
241
|
+
entities = []
|
|
242
|
+
body = doc.at_css("body")
|
|
243
|
+
parse_node(body, 0, message, entities)
|
|
244
|
+
new(message.strip, entities)
|
|
245
|
+
end
|
|
246
|
+
|
|
247
|
+
# Convert a message and a set of entities to HTML.
|
|
248
|
+
#
|
|
249
|
+
# @param allow_telegram_tags [Boolean] Whether to allow telegram-specific tags
|
|
250
|
+
# @return [String] HTML string
|
|
251
|
+
def to_html(allow_telegram_tags = false)
|
|
252
|
+
insertions = {}
|
|
253
|
+
@entities.each do |entity|
|
|
254
|
+
offset = entity["offset"]
|
|
255
|
+
length = entity["length"]
|
|
256
|
+
insertions[offset] ||= ""
|
|
257
|
+
|
|
258
|
+
insertions[offset] += case entity["type"]
|
|
259
|
+
when "bold" then "<strong>"
|
|
260
|
+
when "italic" then "<i>"
|
|
261
|
+
when "code" then "<code>"
|
|
262
|
+
when "pre"
|
|
263
|
+
if entity["language"] && !entity["language"].empty?
|
|
264
|
+
"<pre language=\"#{EntityTools.html_escape(entity["language"])}\">"
|
|
265
|
+
else
|
|
266
|
+
"<pre>"
|
|
267
|
+
end
|
|
268
|
+
when "text_url" then "<a href=\"#{EntityTools.html_escape(entity["url"])}\">"
|
|
269
|
+
when "strike" then "<s>"
|
|
270
|
+
when "underline" then "<u>"
|
|
271
|
+
when "blockquote" then "<blockquote>"
|
|
272
|
+
when "url"
|
|
273
|
+
url = EntityTools.html_escape(EntityTools.mb_substr(@message, offset, length))
|
|
274
|
+
"<a href=\"#{url}\">"
|
|
275
|
+
when "email"
|
|
276
|
+
email = EntityTools.html_escape(EntityTools.mb_substr(@message, offset, length))
|
|
277
|
+
"<a href=\"mailto:#{email}\">"
|
|
278
|
+
when "phone"
|
|
279
|
+
phone = EntityTools.html_escape(EntityTools.mb_substr(@message, offset, length))
|
|
280
|
+
"<a href=\"phone:#{phone}\">"
|
|
281
|
+
when "mention"
|
|
282
|
+
mention = EntityTools.html_escape(EntityTools.mb_substr(@message, offset + 1, length - 1))
|
|
283
|
+
"<a href=\"https://t.me/#{mention}\">"
|
|
284
|
+
when "spoiler"
|
|
285
|
+
allow_telegram_tags ? "<tg-spoiler>" : '<span class="tg-spoiler">'
|
|
286
|
+
when "custom_emoji"
|
|
287
|
+
allow_telegram_tags ? "<tg-emoji emoji-id=\"#{entity["custom_emoji_id"]}\">" : ""
|
|
288
|
+
when "mention_name"
|
|
289
|
+
allow_telegram_tags ? "<a href=\"tg://user?id=#{entity["user"]["id"]}\">" : ""
|
|
290
|
+
when "hashtag"
|
|
291
|
+
allow_telegram_tags ? "<tg-hashtag>" : '<span class="tg-hashtag">'
|
|
292
|
+
when "cashtag"
|
|
293
|
+
allow_telegram_tags ? "<tg-cashtag>" : '<span class="tg-cashtag">'
|
|
294
|
+
when "bot_command"
|
|
295
|
+
allow_telegram_tags ? "<tg-bot-command>" : '<span class="tg-bot-command">'
|
|
296
|
+
when "media_timestamp"
|
|
297
|
+
media_timestamp = entity["media_timestamp"]
|
|
298
|
+
if allow_telegram_tags && media_timestamp
|
|
299
|
+
"<tg-media-timestamp timestamp=\"#{EntityTools.html_escape(media_timestamp.to_s)}\">"
|
|
300
|
+
else
|
|
301
|
+
'<span class="tg-media-timestamp">'
|
|
302
|
+
end
|
|
303
|
+
when "bank_card"
|
|
304
|
+
allow_telegram_tags ? "<tg-bank-card-number>" : '<span class="tg-bank-card-number">'
|
|
305
|
+
when "expandable_blockquote"
|
|
306
|
+
allow_telegram_tags ? "<blockquote expandable>" : '<blockquote class="expandable">'
|
|
307
|
+
else ""
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
end_offset = offset + length
|
|
311
|
+
insertions[end_offset] ||= ""
|
|
312
|
+
insertions[end_offset] = case entity["type"]
|
|
313
|
+
when "bold" then "</strong>"
|
|
314
|
+
when "italic" then "</i>"
|
|
315
|
+
when "code" then "</code>"
|
|
316
|
+
when "pre" then "</pre>"
|
|
317
|
+
when "text_url", "url", "email", "mention", "phone" then "</a>"
|
|
318
|
+
when "strike" then "</s>"
|
|
319
|
+
when "underline" then "</u>"
|
|
320
|
+
when "blockquote" then "</blockquote>"
|
|
321
|
+
when "spoiler" then allow_telegram_tags ? "</tg-spoiler>" : "</span>"
|
|
322
|
+
when "custom_emoji" then allow_telegram_tags ? "</tg-emoji>" : ""
|
|
323
|
+
when "mention_name" then allow_telegram_tags ? "</a>" : ""
|
|
324
|
+
when "hashtag" then allow_telegram_tags ? "</tg-hashtag>" : "</span>"
|
|
325
|
+
when "cashtag" then allow_telegram_tags ? "</tg-cashtag>" : "</span>"
|
|
326
|
+
when "bot_command" then allow_telegram_tags ? "</tg-bot-command>" : "</span>"
|
|
327
|
+
when "media_timestamp" then allow_telegram_tags ? "</tg-media-timestamp>" : "</span>"
|
|
328
|
+
when "bank_card" then allow_telegram_tags ? "</tg-bank-card-number>" : "</span>"
|
|
329
|
+
when "expandable_blockquote" then "</blockquote>"
|
|
330
|
+
else ""
|
|
331
|
+
end + insertions[end_offset]
|
|
332
|
+
end
|
|
333
|
+
|
|
334
|
+
insertions = insertions.sort.to_h
|
|
335
|
+
final = ""
|
|
336
|
+
pos = 0
|
|
337
|
+
insertions.each do |ins_offset, insertion|
|
|
338
|
+
final += EntityTools.html_escape(EntityTools.mb_substr(@message, pos, ins_offset - pos))
|
|
339
|
+
final += insertion
|
|
340
|
+
pos = ins_offset
|
|
341
|
+
end
|
|
342
|
+
final += EntityTools.html_escape(EntityTools.mb_substr(@message, pos))
|
|
343
|
+
final.gsub("\n", "<br>")
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
# Parse HTML node recursively
|
|
347
|
+
def self.parse_node(node, offset, message, entities)
|
|
348
|
+
if node.text?
|
|
349
|
+
text = node.text
|
|
350
|
+
message << text
|
|
351
|
+
return EntityTools.mb_strlen(text)
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
if node.name == "br"
|
|
355
|
+
message << "\n"
|
|
356
|
+
return 1
|
|
357
|
+
end
|
|
358
|
+
|
|
359
|
+
entity = case node.name
|
|
360
|
+
when "s", "strike", "del" then {"type" => "strike"}
|
|
361
|
+
when "u" then {"type" => "underline"}
|
|
362
|
+
when "b", "strong" then {"type" => "bold"}
|
|
363
|
+
when "i", "em" then {"type" => "italic"}
|
|
364
|
+
when "code" then {"type" => "code"}
|
|
365
|
+
when "spoiler", "tg-spoiler" then {"type" => "spoiler"}
|
|
366
|
+
when "pre"
|
|
367
|
+
if node["language"]
|
|
368
|
+
{"type" => "pre", "language" => node["language"]}
|
|
369
|
+
else
|
|
370
|
+
{"type" => "pre"}
|
|
371
|
+
end
|
|
372
|
+
when "span"
|
|
373
|
+
case node["class"]
|
|
374
|
+
when "tg-spoiler"
|
|
375
|
+
{"type" => "spoiler"}
|
|
376
|
+
when "tg-hashtag"
|
|
377
|
+
{"type" => "hashtag"}
|
|
378
|
+
when "tg-cashtag"
|
|
379
|
+
{"type" => "cashtag"}
|
|
380
|
+
when "tg-bot-command"
|
|
381
|
+
{"type" => "bot_command"}
|
|
382
|
+
when "tg-media-timestamp"
|
|
383
|
+
media_timestamp = node["timestamp"] || node["data-timestamp"]
|
|
384
|
+
if media_timestamp
|
|
385
|
+
{"type" => "media_timestamp", "media_timestamp" => media_timestamp.to_i}
|
|
386
|
+
else
|
|
387
|
+
{"type" => "media_timestamp"}
|
|
388
|
+
end
|
|
389
|
+
when "tg-bank-card-number"
|
|
390
|
+
{"type" => "bank_card"}
|
|
391
|
+
end
|
|
392
|
+
when "tg-emoji"
|
|
393
|
+
{"type" => "custom_emoji", "custom_emoji_id" => node["emoji-id"].to_i}
|
|
394
|
+
when "emoji"
|
|
395
|
+
{"type" => "custom_emoji", "custom_emoji_id" => node["id"].to_i}
|
|
396
|
+
when "tg-hashtag"
|
|
397
|
+
{"type" => "hashtag"}
|
|
398
|
+
when "tg-cashtag"
|
|
399
|
+
{"type" => "cashtag"}
|
|
400
|
+
when "tg-bot-command"
|
|
401
|
+
{"type" => "bot_command"}
|
|
402
|
+
when "tg-media-timestamp"
|
|
403
|
+
media_timestamp = node["timestamp"] || node["data-timestamp"]
|
|
404
|
+
if media_timestamp
|
|
405
|
+
{"type" => "media_timestamp", "media_timestamp" => media_timestamp.to_i}
|
|
406
|
+
else
|
|
407
|
+
{"type" => "media_timestamp"}
|
|
408
|
+
end
|
|
409
|
+
when "tg-bank-card-number"
|
|
410
|
+
{"type" => "bank_card"}
|
|
411
|
+
when "blockquote"
|
|
412
|
+
# Check for expandable attribute or class
|
|
413
|
+
if !node["expandable"].nil? || node["class"] == "expandable"
|
|
414
|
+
{"type" => "expandable_blockquote"}
|
|
415
|
+
else
|
|
416
|
+
{"type" => "blockquote"}
|
|
417
|
+
end
|
|
418
|
+
when "a"
|
|
419
|
+
handle_link(node["href"] || "")
|
|
420
|
+
end
|
|
421
|
+
|
|
422
|
+
length = 0
|
|
423
|
+
node.children.each do |child|
|
|
424
|
+
length += parse_node(child, offset + length, message, entities)
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
if entity
|
|
428
|
+
length_real = length
|
|
429
|
+
# Trim trailing whitespace from message
|
|
430
|
+
message_bytes = message.bytes
|
|
431
|
+
(message_bytes.length - 1).downto(0) do |x|
|
|
432
|
+
char = message_bytes[x]
|
|
433
|
+
break unless [32, 13, 10].include?(char) # space, \r, \n
|
|
434
|
+
length_real -= 1
|
|
435
|
+
end
|
|
436
|
+
|
|
437
|
+
if length_real > 0
|
|
438
|
+
entity["offset"] = offset
|
|
439
|
+
entity["length"] = length_real
|
|
440
|
+
entities << entity
|
|
441
|
+
end
|
|
442
|
+
end
|
|
443
|
+
|
|
444
|
+
length
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
# Handle link href
|
|
448
|
+
def self.handle_link(href)
|
|
449
|
+
if (match = href.match(/^mention:(.+)/)) || (match = href.match(/^tg:\/\/user\?id=(.+)/))
|
|
450
|
+
user_id = match[1].to_i
|
|
451
|
+
{"type" => "mention_name", "user" => {"id" => user_id}}
|
|
452
|
+
elsif (match = href.match(/^emoji:(\d+)$/)) || (match = href.match(/^tg:\/\/emoji\?id=(.+)/))
|
|
453
|
+
emoji_id = match[1].to_i
|
|
454
|
+
{"type" => "custom_emoji", "custom_emoji_id" => emoji_id}
|
|
455
|
+
else
|
|
456
|
+
{"type" => "text_url", "url" => href}
|
|
457
|
+
end
|
|
458
|
+
end
|
|
459
|
+
|
|
460
|
+
private_class_method :parse_node, :handle_link
|
|
461
|
+
end
|
|
462
|
+
end
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "cgi"
|
|
4
|
+
|
|
5
|
+
module TelegramEntities
|
|
6
|
+
# Telegram UTF-16 styled text entity tools.
|
|
7
|
+
module EntityTools
|
|
8
|
+
# Get length of string in UTF-16 code points.
|
|
9
|
+
#
|
|
10
|
+
# @param text [String] Text
|
|
11
|
+
# @return [Integer] Length in UTF-16 code units
|
|
12
|
+
def self.mb_strlen(text)
|
|
13
|
+
# Convert to UTF-16 and count code units
|
|
14
|
+
# Each character in UTF-16 is 2 bytes, but surrogate pairs (for emojis) take 4 bytes (2 code units)
|
|
15
|
+
utf16 = text.encode("UTF-16BE")
|
|
16
|
+
utf16.bytesize / 2
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Telegram UTF-16 multibyte substring.
|
|
20
|
+
#
|
|
21
|
+
# @param text [String] Text to substring
|
|
22
|
+
# @param offset [Integer] Offset in UTF-16 code units
|
|
23
|
+
# @param length [Integer, nil] Length in UTF-16 code units
|
|
24
|
+
# @return [String] Substring
|
|
25
|
+
def self.mb_substr(text, offset, length = nil)
|
|
26
|
+
utf16 = text.encode("UTF-16BE")
|
|
27
|
+
byte_offset = offset * 2
|
|
28
|
+
byte_length = length ? length * 2 : nil
|
|
29
|
+
substring = if byte_length
|
|
30
|
+
utf16.byteslice(byte_offset, byte_length)
|
|
31
|
+
else
|
|
32
|
+
utf16.byteslice(byte_offset..-1)
|
|
33
|
+
end
|
|
34
|
+
substring&.force_encoding("UTF-16BE")&.encode("UTF-8") || ""
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Telegram UTF-16 multibyte split.
|
|
38
|
+
#
|
|
39
|
+
# @param text [String] Text
|
|
40
|
+
# @param length [Integer] Length in UTF-16 code units
|
|
41
|
+
# @return [Array<String>] Array of strings
|
|
42
|
+
def self.mb_str_split(text, length)
|
|
43
|
+
utf16 = text.encode("UTF-16BE")
|
|
44
|
+
byte_length = length * 2
|
|
45
|
+
result = []
|
|
46
|
+
offset = 0
|
|
47
|
+
while offset < utf16.bytesize
|
|
48
|
+
chunk = utf16.byteslice(offset, byte_length)
|
|
49
|
+
break if chunk.nil?
|
|
50
|
+
chunk.force_encoding("UTF-16BE")
|
|
51
|
+
result << chunk.encode("UTF-8")
|
|
52
|
+
offset += byte_length
|
|
53
|
+
end
|
|
54
|
+
result
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Telegram UTF-16 multibyte subreplace.
|
|
58
|
+
#
|
|
59
|
+
# @param string [String] Text
|
|
60
|
+
# @param replace [String] Replacement
|
|
61
|
+
# @param offset [Integer] Offset in UTF-16 code units
|
|
62
|
+
# @param length [Integer, nil] Length in UTF-16 code units
|
|
63
|
+
# @return [String] Result string
|
|
64
|
+
def self.mb_substr_replace(string, replace, offset, length = nil)
|
|
65
|
+
utf16_string = string.encode("UTF-16BE")
|
|
66
|
+
utf16_replace = replace.encode("UTF-16BE")
|
|
67
|
+
byte_offset = offset * 2
|
|
68
|
+
byte_length = length ? length * 2 : nil
|
|
69
|
+
|
|
70
|
+
result = if byte_length
|
|
71
|
+
utf16_string.byteslice(0, byte_offset) +
|
|
72
|
+
utf16_replace +
|
|
73
|
+
utf16_string.byteslice(byte_offset + byte_length..-1)
|
|
74
|
+
else
|
|
75
|
+
utf16_string.byteslice(0, byte_offset) + utf16_replace
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
result.force_encoding("UTF-16BE").encode("UTF-8")
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Escape string for this library's HTML entity converter.
|
|
82
|
+
#
|
|
83
|
+
# @param what [String] String to escape
|
|
84
|
+
# @return [String] Escaped string
|
|
85
|
+
def self.html_escape(what)
|
|
86
|
+
CGI.escapeHTML(what)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Escape string for markdown.
|
|
90
|
+
#
|
|
91
|
+
# @param what [String] String to escape
|
|
92
|
+
# @return [String] Escaped string
|
|
93
|
+
def self.markdown_escape(what)
|
|
94
|
+
what.gsub(/[\\_*\[\]()~`>#+\-=|{}.!]/) { |char| "\\#{char}" }
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Escape string for markdown codeblock.
|
|
98
|
+
#
|
|
99
|
+
# @param what [String] String to escape
|
|
100
|
+
# @return [String] Escaped string
|
|
101
|
+
def self.markdown_codeblock_escape(what)
|
|
102
|
+
what.gsub("```") { "\\```" }
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Escape string for markdown code section.
|
|
106
|
+
#
|
|
107
|
+
# @param what [String] String to escape
|
|
108
|
+
# @return [String] Escaped string
|
|
109
|
+
def self.markdown_code_escape(what)
|
|
110
|
+
what.gsub("`") { "\\`" }
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# Escape string for URL.
|
|
114
|
+
#
|
|
115
|
+
# @param what [String] String to escape
|
|
116
|
+
# @return [String] Escaped string
|
|
117
|
+
def self.markdown_url_escape(what)
|
|
118
|
+
what.gsub(")", '\\)')
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module TelegramEntities
|
|
4
|
+
# Converter for TDLib formattedText to TelegramEntities format
|
|
5
|
+
module TdlibConverter
|
|
6
|
+
# Mapping from TDLib entity types to TelegramEntities types
|
|
7
|
+
TYPE_MAPPING = {
|
|
8
|
+
"textEntityTypeMention" => "mention",
|
|
9
|
+
"textEntityTypeHashtag" => "hashtag",
|
|
10
|
+
"textEntityTypeCashtag" => "cashtag",
|
|
11
|
+
"textEntityTypeBotCommand" => "bot_command",
|
|
12
|
+
"textEntityTypeUrl" => "url",
|
|
13
|
+
"textEntityTypeEmailAddress" => "email",
|
|
14
|
+
"textEntityTypePhoneNumber" => "phone",
|
|
15
|
+
"textEntityTypeBankCardNumber" => "bank_card",
|
|
16
|
+
"textEntityTypeBold" => "bold",
|
|
17
|
+
"textEntityTypeItalic" => "italic",
|
|
18
|
+
"textEntityTypeUnderline" => "underline",
|
|
19
|
+
"textEntityTypeStrikethrough" => "strike",
|
|
20
|
+
"textEntityTypeSpoiler" => "spoiler",
|
|
21
|
+
"textEntityTypeCode" => "code",
|
|
22
|
+
"textEntityTypePre" => "pre",
|
|
23
|
+
"textEntityTypePreCode" => "pre",
|
|
24
|
+
"textEntityTypeBlockQuote" => "blockquote",
|
|
25
|
+
"textEntityTypeExpandableBlockQuote" => "expandable_blockquote",
|
|
26
|
+
"textEntityTypeTextUrl" => "text_url",
|
|
27
|
+
"textEntityTypeMentionName" => "mention_name",
|
|
28
|
+
"textEntityTypeCustomEmoji" => "custom_emoji",
|
|
29
|
+
"textEntityTypeMediaTimestamp" => "media_timestamp"
|
|
30
|
+
}.freeze
|
|
31
|
+
|
|
32
|
+
# Convert TDLib formattedText data to TelegramEntities format
|
|
33
|
+
#
|
|
34
|
+
# @param data [Hash] TDLib formattedText data with keys: "text", "@type", "entities"
|
|
35
|
+
# @return [Array<String, Array<Hash>>] Array containing text and converted entities
|
|
36
|
+
def self.convert_tdlib_data(data)
|
|
37
|
+
text = data["text"] || ""
|
|
38
|
+
tdlib_entities = data["entities"] || []
|
|
39
|
+
|
|
40
|
+
converted_entities = tdlib_entities.map do |entity|
|
|
41
|
+
convert_entity(entity)
|
|
42
|
+
end.compact
|
|
43
|
+
|
|
44
|
+
[text, converted_entities]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Convert a single TDLib entity to TelegramEntities format
|
|
48
|
+
#
|
|
49
|
+
# @param entity [Hash] TDLib entity with keys: "type", "offset", "length"
|
|
50
|
+
# @return [Hash, nil] Converted entity or nil if type is not supported
|
|
51
|
+
def self.convert_entity(entity)
|
|
52
|
+
entity_type = entity["type"]
|
|
53
|
+
return nil unless entity_type.is_a?(Hash)
|
|
54
|
+
|
|
55
|
+
tdlib_type = entity_type["@type"]
|
|
56
|
+
telegram_type = TYPE_MAPPING[tdlib_type]
|
|
57
|
+
return nil unless telegram_type
|
|
58
|
+
|
|
59
|
+
converted = {
|
|
60
|
+
"type" => telegram_type,
|
|
61
|
+
"offset" => entity["offset"] || 0,
|
|
62
|
+
"length" => entity["length"] || 0
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
# Handle special fields based on entity type
|
|
66
|
+
case tdlib_type
|
|
67
|
+
when "textEntityTypeTextUrl"
|
|
68
|
+
converted["url"] = entity_type["url"] if entity_type["url"]
|
|
69
|
+
when "textEntityTypeMentionName"
|
|
70
|
+
if entity_type["user_id"]
|
|
71
|
+
converted["user"] = {"id" => entity_type["user_id"]}
|
|
72
|
+
end
|
|
73
|
+
when "textEntityTypeCustomEmoji"
|
|
74
|
+
converted["custom_emoji_id"] = entity_type["custom_emoji_id"] if entity_type["custom_emoji_id"]
|
|
75
|
+
when "textEntityTypePreCode"
|
|
76
|
+
converted["language"] = entity_type["language"] if entity_type["language"]
|
|
77
|
+
when "textEntityTypePre"
|
|
78
|
+
converted["language"] = entity_type["language"] if entity_type["language"]
|
|
79
|
+
when "textEntityTypeMediaTimestamp"
|
|
80
|
+
converted["media_timestamp"] = entity_type["media_timestamp"] if entity_type["media_timestamp"]
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
converted
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
private_class_method :convert_entity
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "telegram_entities/version"
|
|
4
|
+
require_relative "telegram_entities/entity_tools"
|
|
5
|
+
require_relative "telegram_entities/entities"
|
|
6
|
+
require_relative "telegram_entities/tdlib_converter"
|
|
7
|
+
|
|
8
|
+
module TelegramEntities
|
|
9
|
+
class Error < StandardError; end
|
|
10
|
+
|
|
11
|
+
# Alias for TelegramEntities::Entities
|
|
12
|
+
def self.new(*args, **kwargs)
|
|
13
|
+
Entities.new(*args, **kwargs)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Delegate class methods to Entities
|
|
17
|
+
def self.from_markdown(*args, **kwargs)
|
|
18
|
+
Entities.from_markdown(*args, **kwargs)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def self.from_html(*args, **kwargs)
|
|
22
|
+
Entities.from_html(*args, **kwargs)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# Convert TDLib formattedText to TelegramEntities
|
|
26
|
+
#
|
|
27
|
+
# @param data [Hash] TDLib formattedText data with keys: "text", "@type", "entities"
|
|
28
|
+
# @return [Entities] Object containing message and entities
|
|
29
|
+
def self.from_tdlib_formatted_text(data)
|
|
30
|
+
text, entities = TdlibConverter.convert_tdlib_data(data)
|
|
31
|
+
Entities.new(text, entities)
|
|
32
|
+
end
|
|
33
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: telegram_entities
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Kirill Bobykin
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: nokogiri
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - "~>"
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '1.0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - "~>"
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '1.0'
|
|
26
|
+
description: Ruby gem for converting Telegram message entities between HTML and Markdown
|
|
27
|
+
formats. Supports all Telegram MessageEntity types with UTF-16 offset/length handling.
|
|
28
|
+
email:
|
|
29
|
+
- qelphybox@gmail.com
|
|
30
|
+
executables: []
|
|
31
|
+
extensions: []
|
|
32
|
+
extra_rdoc_files: []
|
|
33
|
+
files:
|
|
34
|
+
- ".standard.yml"
|
|
35
|
+
- CHANGELOG.md
|
|
36
|
+
- CODE_OF_CONDUCT.md
|
|
37
|
+
- LICENSE.txt
|
|
38
|
+
- README.md
|
|
39
|
+
- Rakefile
|
|
40
|
+
- lib/telegram_entities.rb
|
|
41
|
+
- lib/telegram_entities/entities.rb
|
|
42
|
+
- lib/telegram_entities/entity_tools.rb
|
|
43
|
+
- lib/telegram_entities/tdlib_converter.rb
|
|
44
|
+
- lib/telegram_entities/version.rb
|
|
45
|
+
- sig/telegram_entities.rbs
|
|
46
|
+
homepage: https://github.com/qelphybox/telegram_entities_rb
|
|
47
|
+
licenses:
|
|
48
|
+
- MIT
|
|
49
|
+
metadata:
|
|
50
|
+
homepage_uri: https://github.com/qelphybox/telegram_entities_rb
|
|
51
|
+
source_code_uri: https://github.com/qelphybox/telegram_entities_rb
|
|
52
|
+
changelog_uri: https://github.com/qelphybox/telegram_entities_rb/blob/master/CHANGELOG.md
|
|
53
|
+
rdoc_options: []
|
|
54
|
+
require_paths:
|
|
55
|
+
- lib
|
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
57
|
+
requirements:
|
|
58
|
+
- - ">="
|
|
59
|
+
- !ruby/object:Gem::Version
|
|
60
|
+
version: 3.1.0
|
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
|
+
requirements:
|
|
63
|
+
- - ">="
|
|
64
|
+
- !ruby/object:Gem::Version
|
|
65
|
+
version: '0'
|
|
66
|
+
requirements: []
|
|
67
|
+
rubygems_version: 3.6.9
|
|
68
|
+
specification_version: 4
|
|
69
|
+
summary: Ruby gem for converting Telegram message entities between HTML and Markdown
|
|
70
|
+
formats
|
|
71
|
+
test_files: []
|