diff options
author | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2021-12-18 19:04:24 +0300 |
---|---|---|
committer | Bohdan Horbeshko <bodqhrohro@gmail.com> | 2021-12-18 19:04:24 +0300 |
commit | f2f20e680bb06ebda76f67f8fae9d7ed12498c63 (patch) | |
tree | 03eaeefb422396a6af55128b6b62230a9dd39b43 /telegram | |
parent | ad6e92e6d602c40e4bd44be785e00f563393192a (diff) |
Fix %!(MISSING) and other formatting issues
Diffstat (limited to 'telegram')
-rw-r--r-- | telegram/formatter/formatter.go | 32 | ||||
-rw-r--r-- | telegram/handlers.go | 2 | ||||
-rw-r--r-- | telegram/utils.go | 2 |
3 files changed, 29 insertions, 7 deletions
diff --git a/telegram/formatter/formatter.go b/telegram/formatter/formatter.go index 4b26f83..2946a55 100644 --- a/telegram/formatter/formatter.go +++ b/telegram/formatter/formatter.go @@ -17,10 +17,10 @@ type Insertion struct { // from the start or from the end type InsertionStack []*Insertion -var boldRunes = []rune("**") +var boldRunesMarkdown = []rune("**") +var boldRunesXEP0393 = []rune("*") var italicRunes = []rune("_") var codeRunes = []rune("\n```\n") -var urlRuneL = []rune("[") // rebalance pumps all the values at given offset to current stack (growing // from start) from given stack (growing from end); should be called @@ -80,7 +80,7 @@ func markupBraces(entity *client.TextEntity, lbrace, rbrace []rune) (*Insertion, func EntityToMarkdown(entity *client.TextEntity) (*Insertion, *Insertion) { switch entity.Type.TextEntityTypeType() { case client.TypeTextEntityTypeBold: - return markupBraces(entity, boldRunes, boldRunes) + return markupBraces(entity, boldRunesMarkdown, boldRunesMarkdown) case client.TypeTextEntityTypeItalic: return markupBraces(entity, italicRunes, italicRunes) case client.TypeTextEntityTypeCode, client.TypeTextEntityTypePre: @@ -90,13 +90,35 @@ func EntityToMarkdown(entity *client.TextEntity) (*Insertion, *Insertion) { return markupBraces(entity, []rune("\n```"+preCode.Language+"\n"), codeRunes) case client.TypeTextEntityTypeTextUrl: textURL, _ := entity.Type.(*client.TextEntityTypeTextUrl) - return markupBraces(entity, urlRuneL, []rune("]("+textURL.Url+")")) + return markupBraces(entity, []rune("["), []rune("]("+textURL.Url+")")) } return nil, nil } -// Format traverses an already sorted list of entities and wraps the text in Markdown +// EntityToXEP0393 generates the wrapping XEP-0393 tags +func EntityToXEP0393(entity *client.TextEntity) (*Insertion, *Insertion) { + switch entity.Type.TextEntityTypeType() { + case client.TypeTextEntityTypeBold: + return markupBraces(entity, boldRunesXEP0393, boldRunesXEP0393) + case client.TypeTextEntityTypeItalic: + return markupBraces(entity, italicRunes, italicRunes) + case client.TypeTextEntityTypeCode, client.TypeTextEntityTypePre: + return markupBraces(entity, codeRunes, codeRunes) + case client.TypeTextEntityTypePreCode: + preCode, _ := entity.Type.(*client.TextEntityTypePreCode) + // TODO: inline code support (non-standard too) + return markupBraces(entity, []rune("\n```"+preCode.Language+"\n"), codeRunes) + case client.TypeTextEntityTypeTextUrl: + textURL, _ := entity.Type.(*client.TextEntityTypeTextUrl) + // non-standard, Pidgin-specific + return markupBraces(entity, []rune{}, []rune(" <"+textURL.Url+">")) + } + + return nil, nil +} + +// Format traverses an already sorted list of entities and wraps the text in a markup func Format( sourceText string, entities []*client.TextEntity, diff --git a/telegram/handlers.go b/telegram/handlers.go index f8e6751..a897291 100644 --- a/telegram/handlers.go +++ b/telegram/handlers.go @@ -237,7 +237,7 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) { // message content updated func (c *Client) updateMessageContent(update *client.UpdateMessageContent) { - markupFunction := formatter.EntityToMarkdown + markupFunction := formatter.EntityToXEP0393 if update.NewContent.MessageContentType() == client.TypeMessageText { textContent := update.NewContent.(*client.MessageText) text := fmt.Sprintf("✎ %v | %s", update.MessageID, formatter.Format( diff --git a/telegram/utils.go b/telegram/utils.go index 476ae43..387ddea 100644 --- a/telegram/utils.go +++ b/telegram/utils.go @@ -292,7 +292,7 @@ func (c *Client) messageToText(message *client.Message) string { return "<BIG EMOJI>" } - markupFunction := formatter.EntityToMarkdown + markupFunction := formatter.EntityToXEP0393 switch message.Content.MessageContentType() { case client.TypeMessageSticker: sticker, _ := message.Content.(*client.MessageSticker) |