Skip to content

Commit 81684ed

Browse files
authored
Merge pull request #145 from julwrites/staging
Reverting to use BotPlatform formatting
2 parents 23be324 + 3ba3c6b commit 81684ed

2 files changed

Lines changed: 17 additions & 31 deletions

File tree

pkg/app/passage.go

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"golang.org/x/net/html"
1313

1414
"github.com/julwrites/BotPlatform/pkg/def"
15+
"github.com/julwrites/BotPlatform/pkg/platform"
1516
"github.com/julwrites/ScriptureBot/pkg/utils"
1617
)
1718

@@ -36,24 +37,6 @@ func GetReference(doc *html.Node) string {
3637
return utils.GetTextNode(refNode).Data
3738
}
3839

39-
// Helper function to escape characters for Telegram MarkdownV2
40-
func escapeMarkdownV2(s string) string {
41-
// According to Telegram API docs for MarkdownV2, characters to escape are:
42-
// '_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!'
43-
// Note: '^' is not in this list. Let's assume it doesn't need escaping.
44-
// The logic should be to escape these characters *only* when they are not part of a formatting tag.
45-
// However, since we are processing raw text nodes, any special character should be escaped.
46-
var sb strings.Builder
47-
for _, r := range s {
48-
switch r {
49-
case '_', '*', '[', ']', '(', ')', '~', '`', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!', '\\':
50-
sb.WriteRune('\\')
51-
}
52-
sb.WriteRune(r)
53-
}
54-
return sb.String()
55-
}
56-
5740
// Helper functions for parsing
5841
func isFormattingTag(tag string) bool {
5942
return tag == "sup" || tag == "i" || tag == "b"
@@ -69,21 +52,20 @@ func wrapText(text, tag string) string {
6952
}
7053

7154
if tag == "sup" {
72-
// User-specified format for superscript
73-
return fmt.Sprintf("^%s^", strings.Trim(text, " "))
55+
return platform.TelegramSuperscript(strings.Trim(text, " "))
7456
}
7557
if tag == "i" {
76-
return fmt.Sprintf("_%s_", text)
58+
return platform.TelegramItalics(text)
7759
}
7860
if tag == "b" || isHeaderTag(tag) {
79-
return fmt.Sprintf("*%s*", text)
61+
return platform.TelegramBold(text)
8062
}
8163
return text
8264
}
8365

8466
func parseNode(node *html.Node) string {
8567
if node.Type == html.TextNode {
86-
return escapeMarkdownV2(node.Data)
68+
return node.Data
8769
}
8870

8971
if node.Type != html.ElementNode {

pkg/app/passage_test.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,31 +78,31 @@ func TestGetBiblePassage(t *testing.T) {
7878
func TestParsePassageFromHtml(t *testing.T) {
7979
t.Run("Valid HTML with superscript", func(t *testing.T) {
8080
html := `<p><span><sup>12 </sup>But to all who did receive him, who believed in his name, he gave the right to become children of God,</span></p>`
81-
expected := `^12^But to all who did receive him, who believed in his name, he gave the right to become children of God,`
81+
expected := `¹²But to all who did receive him, who believed in his name, he gave the right to become children of God,`
8282
if got := ParsePassageFromHtml(html); got != expected {
8383
t.Errorf("ParsePassageFromHtml() = %v, want %v", got, expected)
8484
}
8585
})
8686

8787
t.Run("HTML with italics", func(t *testing.T) {
8888
html := `<p><i>This is italic.</i></p>`
89-
expected := `_This is italic\._`
89+
expected := `_This is italic._`
9090
if got := ParsePassageFromHtml(html); got != expected {
9191
t.Errorf("ParsePassageFromHtml() = %v, want %v", got, expected)
9292
}
9393
})
9494

9595
t.Run("HTML with bold", func(t *testing.T) {
9696
html := `<p><b>This is bold.</b></p>`
97-
expected := `*This is bold\.*`
97+
expected := `*This is bold.*`
9898
if got := ParsePassageFromHtml(html); got != expected {
9999
t.Errorf("ParsePassageFromHtml() = %v, want %v", got, expected)
100100
}
101101
})
102102

103103
t.Run("HTML with line breaks", func(t *testing.T) {
104104
html := `<p>Line 1.<br>Line 2.</p>`
105-
expected := "Line 1\\.\nLine 2\\."
105+
expected := "Line 1.\nLine 2."
106106
if got := ParsePassageFromHtml(html); got != expected {
107107
t.Errorf("ParsePassageFromHtml() = %v, want %v", got, expected)
108108
}
@@ -118,31 +118,35 @@ func TestParsePassageFromHtml(t *testing.T) {
118118

119119
t.Run("Nested HTML tags", func(t *testing.T) {
120120
html := `<p><b>This is bold, <i>and this is italic.</i></b></p>`
121-
expected := `*This is bold, *_and this is italic\._`
121+
expected := `*This is bold, *_and this is italic._`
122122
if got := ParsePassageFromHtml(html); got != expected {
123123
t.Errorf("ParsePassageFromHtml() = %v, want %v", got, expected)
124124
}
125125
})
126126

127127
t.Run("MarkdownV2 escaping", func(t *testing.T) {
128+
// Note: We no longer escape explicitly in ParsePassageFromHtml as we rely on the platform
129+
// to handle it later (via PostTelegram).
130+
// However, returning raw characters like * might cause issues if not handled by platform.
131+
// For now, we expect them to be returned raw.
128132
html := `<p>This has special characters: *_. [hello](world)!</p>`
129-
expected := `This has special characters: \*\_\. \[hello\]\(world\)\!`
133+
expected := `This has special characters: *_. [hello](world)!`
130134
if got := ParsePassageFromHtml(html); got != expected {
131135
t.Errorf("ParsePassageFromHtml() = %v, want %v", got, expected)
132136
}
133137
})
134138

135139
t.Run("Backslash escaping", func(t *testing.T) {
136140
html := `added to you\.`
137-
expected := `added to you\\\.`
141+
expected := `added to you\.`
138142
if got := ParsePassageFromHtml(html); got != expected {
139143
t.Errorf("ParsePassageFromHtml() = %v, want %v", got, expected)
140144
}
141145
})
142146

143147
t.Run("Dot escaping", func(t *testing.T) {
144148
html := `heaven.`
145-
expected := `heaven\.`
149+
expected := `heaven.`
146150
if got := ParsePassageFromHtml(html); got != expected {
147151
t.Errorf("ParsePassageFromHtml() = %v, want %v", got, expected)
148152
}

0 commit comments

Comments
 (0)