aboutsummaryrefslogtreecommitdiff
path: root/telegram
diff options
context:
space:
mode:
authorBohdan Horbeshko <bodqhrohro@gmail.com>2022-03-11 20:01:38 +0300
committerBohdan Horbeshko <bodqhrohro@gmail.com>2022-03-11 20:01:38 +0300
commitcdbd960ff4337e2ee3bab877958c99ce6d63958d (patch)
treed6e925f9e66bcc064cce9c712b0e9fd3d139d333 /telegram
parentf6e62fe1fd208cba8214db70fe1ebbdc9bb41115 (diff)
Wrap inline code in single backticks
Diffstat (limited to 'telegram')
-rw-r--r--telegram/formatter/formatter.go14
-rw-r--r--telegram/formatter/formatter_test.go22
2 files changed, 30 insertions, 6 deletions
diff --git a/telegram/formatter/formatter.go b/telegram/formatter/formatter.go
index 5ea5cdb..8ab4281 100644
--- a/telegram/formatter/formatter.go
+++ b/telegram/formatter/formatter.go
@@ -20,7 +20,9 @@ type InsertionStack []*Insertion
var boldRunesMarkdown = []rune("**")
var boldRunesXEP0393 = []rune("*")
var italicRunes = []rune("_")
-var codeRunes = []rune("\n```\n")
+var codeRunes = []rune("`")
+var preRuneStart = []rune("```\n")
+var preRuneEnd = []rune("\n```")
// rebalance pumps all the values until the given offset to current stack (growing
// from start) from given stack (growing from end); should be called
@@ -131,8 +133,10 @@ func EntityToMarkdown(entity *client.TextEntity) (*Insertion, *Insertion) {
return markupBraces(entity, boldRunesMarkdown, boldRunesMarkdown)
case client.TypeTextEntityTypeItalic:
return markupBraces(entity, italicRunes, italicRunes)
- case client.TypeTextEntityTypeCode, client.TypeTextEntityTypePre:
+ case client.TypeTextEntityTypeCode:
return markupBraces(entity, codeRunes, codeRunes)
+ case client.TypeTextEntityTypePre:
+ return markupBraces(entity, preRuneStart, preRuneEnd)
case client.TypeTextEntityTypePreCode:
preCode, _ := entity.Type.(*client.TextEntityTypePreCode)
return markupBraces(entity, []rune("\n```"+preCode.Language+"\n"), codeRunes)
@@ -155,11 +159,13 @@ func EntityToXEP0393(entity *client.TextEntity) (*Insertion, *Insertion) {
return markupBraces(entity, boldRunesXEP0393, boldRunesXEP0393)
case client.TypeTextEntityTypeItalic:
return markupBraces(entity, italicRunes, italicRunes)
- case client.TypeTextEntityTypeCode, client.TypeTextEntityTypePre:
+ case client.TypeTextEntityTypeCode:
+ // inline code is non-standard
return markupBraces(entity, codeRunes, codeRunes)
+ case client.TypeTextEntityTypePre:
+ return markupBraces(entity, preRuneStart, preRuneEnd)
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)
diff --git a/telegram/formatter/formatter_test.go b/telegram/formatter/formatter_test.go
index 5ac6262..5e3cdf0 100644
--- a/telegram/formatter/formatter_test.go
+++ b/telegram/formatter/formatter_test.go
@@ -64,7 +64,7 @@ func TestFormattingAdjacentAndNested(t *testing.T) {
Type: &client.TextEntityTypeItalic{},
},
}, EntityToMarkdown)
- if markup != "\n```\n**👙**🐧\n```\n_🐖_" {
+ if markup != "```\n**👙**🐧\n```_🐖_" {
t.Errorf("Wrong adjacent&nested formatting: %v", markup)
}
}
@@ -265,7 +265,7 @@ func TestFormattingXEP0393AdjacentAndNested(t *testing.T) {
Type: &client.TextEntityTypeItalic{},
},
}, EntityToXEP0393)
- if markup != "\n```\n*👙*🐧\n```\n_🐖_" {
+ if markup != "```\n*👙*🐧\n```_🐖_" {
t.Errorf("Wrong adjacent&nested formatting: %v", markup)
}
}
@@ -348,3 +348,21 @@ func TestFormattingXEP0393Intersecting(t *testing.T) {
t.Errorf("Wrong intersecting formatting: %v", markup)
}
}
+
+func TestFormattingXEP0393InlineCode(t *testing.T) {
+ markup := Format("Is Gajim a thing?\n\necho 'Hello'\necho 'world'\n\nhruck(", []*client.TextEntity{
+ &client.TextEntity{
+ Offset: 3,
+ Length: 5,
+ Type: &client.TextEntityTypeCode{},
+ },
+ &client.TextEntity{
+ Offset: 19,
+ Length: 25,
+ Type: &client.TextEntityTypePre{},
+ },
+ }, EntityToXEP0393)
+ if markup != "Is `Gajim` a thing?\n\n```\necho 'Hello'\necho 'world'\n```\n\nhruck(" {
+ t.Errorf("Wrong intersecting formatting: %v", markup)
+ }
+}