aboutsummaryrefslogtreecommitdiff
path: root/telegram/formatter/formatter.go
diff options
context:
space:
mode:
authorBohdan Horbeshko <bodqhrohro@gmail.com>2021-12-18 19:04:24 +0300
committerBohdan Horbeshko <bodqhrohro@gmail.com>2021-12-18 19:04:24 +0300
commitf2f20e680bb06ebda76f67f8fae9d7ed12498c63 (patch)
tree03eaeefb422396a6af55128b6b62230a9dd39b43 /telegram/formatter/formatter.go
parentad6e92e6d602c40e4bd44be785e00f563393192a (diff)
Fix %!(MISSING) and other formatting issues
Diffstat (limited to 'telegram/formatter/formatter.go')
-rw-r--r--telegram/formatter/formatter.go32
1 files changed, 27 insertions, 5 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,