Skip to content

Commit

Permalink
export some extra entity functions that don't need to be methods
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulSonOfLars committed Nov 23, 2024
1 parent 75a452b commit be71db5
Showing 1 changed file with 29 additions and 20 deletions.
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

0 comments on commit be71db5

Please sign in to comment.