From be71db55136adb6bc0effdcf38b15c365e67d17c Mon Sep 17 00:00:00 2001 From: Paul Larsen Date: Sat, 23 Nov 2024 19:15:57 +0000 Subject: [PATCH] export some extra entity functions that don't need to be methods --- entities.go | 49 +++++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/entities.go b/entities.go index fa9c20d8..9546b929 100644 --- a/entities.go +++ b/entities.go @@ -7,20 +7,20 @@ 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)) } @@ -28,25 +28,34 @@ func (m Message) ParseEntityTypes(accepted map[string]struct{}) (out []ParsedMes 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 {