Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export extra entity functions that don't need to be methods #207

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 29 additions & 20 deletions entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,55 @@ type ParsedMessageEntity struct {
Text string `json:"text"`
}

// ParseEntities calls Message.ParseEntity on all message text entities.
func (m Message) ParseEntities() (out []ParsedMessageEntity) {
return m.ParseEntityTypes(nil)
// ParseEntity parses a single MessageEntity into a ParsedMessageEntity.
func ParseEntity(text string, entity MessageEntity) ParsedMessageEntity {
return parseEntity(entity, utf16.Encode([]rune(text)))
}

// ParseCaptionEntities calls Message.ParseEntity on all message caption entities.
func (m Message) ParseCaptionEntities() (out []ParsedMessageEntity) {
return m.ParseCaptionEntityTypes(nil)
// ParseEntities parses all MessageEntity items into a list of ParsedMessageEntity.
func ParseEntities(text string, entities []MessageEntity) (out []ParsedMessageEntity) {
return ParseEntityTypes(text, entities, nil)
}

// ParseEntityTypes calls Message.ParseEntity on a subset of message text entities.
func (m Message) ParseEntityTypes(accepted map[string]struct{}) (out []ParsedMessageEntity) {
utf16Text := utf16.Encode([]rune(m.Text))
for _, ent := range m.Entities {
// 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
}

// ParseCaptionEntityTypes calls Message.ParseEntity on a subset of message caption entities.
// 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) {
utf16Caption := utf16.Encode([]rune(m.Caption))
for _, ent := range m.CaptionEntities {
if _, ok := accepted[ent.Type]; ok || accepted == nil {
out = append(out, parseEntity(ent, utf16Caption))
}
}
return out
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(entity, utf16.Encode([]rune(m.Text)))
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(entity, utf16.Encode([]rune(m.Caption)))
return ParseEntity(m.Caption, entity)
}

func parseEntity(entity MessageEntity, utf16Text []uint16) ParsedMessageEntity {
Expand Down