From f2f20e680bb06ebda76f67f8fae9d7ed12498c63 Mon Sep 17 00:00:00 2001 From: Bohdan Horbeshko Date: Sat, 18 Dec 2021 11:04:24 -0500 Subject: Fix %!(MISSING) and other formatting issues --- telegram/formatter/formatter.go | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) (limited to 'telegram/formatter/formatter.go') 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, -- cgit v1.2.3