forked from requilence/integram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
richtext.go
104 lines (87 loc) · 2.62 KB
/
richtext.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package integram
import (
"fmt"
"strings"
)
// MarkdownRichText produce Markdown that can be sent to Telegram. Not recommended to use because of tricky escaping
// Use HTMLRichText instead
type MarkdownRichText struct{}
// HTMLRichText produce HTML that can be sent to Telegram
type HTMLRichText struct{}
// Pre generates <pre>text</pre>
func (hrt HTMLRichText) Pre(s string) string {
return "<pre>" + hrt.EncodeEntities(s) + "</pre>"
}
// Fixed generates <code>text</code>
func (hrt HTMLRichText) Fixed(s string) string {
return "<code>" + hrt.EncodeEntities(s) + "</code>"
}
// EncodeEntities encodes '<', '>'
func (hrt HTMLRichText) EncodeEntities(s string) string {
repalcer := strings.NewReplacer("<", "<", ">", ">")
return repalcer.Replace(s)
}
// URL generates <a href="URL>text</a>
func (hrt HTMLRichText) URL(text string, url string) string {
text = fmt.Sprintf("<a href=\"%s\">%s</a>", url, hrt.EncodeEntities(text))
return text
}
// Bold generates <b>text</b>
func (hrt HTMLRichText) Bold(text string) string {
if text == "" {
return ""
}
text = fmt.Sprintf("<b>%s</b>", hrt.EncodeEntities(text))
return text
}
// Italic generates <i>text</I>
func (hrt HTMLRichText) Italic(text string) string {
if text == "" {
return ""
}
text = fmt.Sprintf("<i>%s</i>", hrt.EncodeEntities(text))
return text
}
// Pre generates```text```
func (mrt MarkdownRichText) Pre(text string) string {
if text == "" {
return ""
}
repalcer := strings.NewReplacer("`", "‛")
return "```\n" + repalcer.Replace(text) + "\n```"
}
// Fixed generates`text`
func (mrt MarkdownRichText) Fixed(text string) string {
if text == "" {
return ""
}
repalcer := strings.NewReplacer("`", "‛")
return "`" + repalcer.Replace(text) + "`"
}
// Esc escapes '[', ']', '(', ')', "`", "_", "*" with \
func (mrt MarkdownRichText) Esc(s string) string {
repalcer := strings.NewReplacer("[", "\\[", "]", "\\]", "(", "\\(", ")", "\\)", "`", "\\`", "_", "\\_", "*", "\\*")
return repalcer.Replace(s)
}
// URL generates [text](URL)
func (mrt MarkdownRichText) URL(text string, url string) string {
repalcer := strings.NewReplacer("[", "⟦", "]", "⟧", "(", "❨", ")", "❩")
text = fmt.Sprintf("[%s](%s)", repalcer.Replace(text), url)
return text
}
// Bold generates *text*
func (mrt MarkdownRichText) Bold(text string) string {
if text == "" {
return ""
}
repalcer := strings.NewReplacer("*", "∗")
return "*" + repalcer.Replace(text) + "*"
}
// Italic generates _text_
func (mrt MarkdownRichText) Italic(text string) string {
if text == "" {
return ""
}
repalcer := strings.NewReplacer("_", "_")
return "_" + repalcer.Replace(text) + "_"
}