-
Notifications
You must be signed in to change notification settings - Fork 120
/
entities.go
75 lines (60 loc) · 2.68 KB
/
entities.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package gotgbot
import "unicode/utf16"
type ParsedMessageEntity struct {
MessageEntity
Text string `json:"text"`
}
// ParseEntity parses a single MessageEntity into a ParsedMessageEntity.
func ParseEntity(text string, entity MessageEntity) ParsedMessageEntity {
return parseEntity(entity, utf16.Encode([]rune(text)))
}
// ParseEntities parses all MessageEntity items into a list of ParsedMessageEntity.
func ParseEntities(text string, entities []MessageEntity) (out []ParsedMessageEntity) {
return ParseEntityTypes(text, entities, nil)
}
// ParseEntityTypes parses a subset of MessageEntity items into a list of ParsedMessageEntity.
func ParseEntityTypes(text string, entities []MessageEntity, accepted map[string]struct{}) (out []ParsedMessageEntity) {
utf16Text := utf16.Encode([]rune(text))
for _, ent := range entities {
if _, ok := accepted[ent.Type]; ok || accepted == nil {
out = append(out, parseEntity(ent, utf16Text))
}
}
return out
}
// ParseEntities parses all message text entities into a list of ParsedMessageEntity.
func (m Message) ParseEntities() (out []ParsedMessageEntity) {
return m.ParseEntityTypes(nil)
}
// ParseCaptionEntities parses all message caption entities into a list of ParsedMessageEntity.
func (m Message) ParseCaptionEntities() (out []ParsedMessageEntity) {
return m.ParseCaptionEntityTypes(nil)
}
// ParseEntityTypes parses a subset of message text entities into a list of ParsedMessageEntity.
func (m Message) ParseEntityTypes(accepted map[string]struct{}) (out []ParsedMessageEntity) {
return ParseEntityTypes(m.Text, m.Entities, accepted)
}
// ParseCaptionEntityTypes parses a subset of message caption entities into a list of ParsedMessageEntity.
func (m Message) ParseCaptionEntityTypes(accepted map[string]struct{}) (out []ParsedMessageEntity) {
return ParseEntityTypes(m.Caption, m.CaptionEntities, accepted)
}
// ParseEntity parses a single message text entity to populate text contents, URL, and offsets in UTF8.
func (m Message) ParseEntity(entity MessageEntity) ParsedMessageEntity {
return ParseEntity(m.Text, entity)
}
// ParseCaptionEntity parses a single message caption entity to populate text contents, URL, and offsets in UTF8.
func (m Message) ParseCaptionEntity(entity MessageEntity) ParsedMessageEntity {
return ParseEntity(m.Caption, entity)
}
func parseEntity(entity MessageEntity, utf16Text []uint16) ParsedMessageEntity {
text := string(utf16.Decode(utf16Text[entity.Offset : entity.Offset+entity.Length]))
if entity.Type == "url" {
entity.Url = text
}
entity.Offset = int64(len(string(utf16.Decode(utf16Text[:entity.Offset]))))
entity.Length = int64(len(text))
return ParsedMessageEntity{
MessageEntity: entity,
Text: text,
}
}