-
Notifications
You must be signed in to change notification settings - Fork 6
/
item.go
131 lines (98 loc) · 2.35 KB
/
item.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package description
import (
"fmt"
"html"
)
// FormatItem is a single formatting item that contains a color and any optional formatting controls
type FormatItem struct {
Text string `json:"text"`
Color Color `json:"color"`
Obfuscated bool `json:"obfuscated"`
Bold bool `json:"bold"`
Strikethrough bool `json:"strikethrough"`
Underline bool `json:"underline"`
Italic bool `json:"italic"`
}
// Raw returns the Minecraft encoding of the formatting (§ + formatting codes/color + text)
func (i FormatItem) Raw() (result string) {
if i.Color != White {
result += i.Color.ToRaw()
}
if i.Obfuscated {
result += "\u00A7k"
}
if i.Bold {
result += "\u00A7l"
}
if i.Strikethrough {
result += "\u00A7m"
}
if i.Underline {
result += "\u00A7n"
}
if i.Italic {
result += "\u00A7o"
}
result += i.Text
return
}
// Clean returns the text only of the formatting item
func (i FormatItem) Clean() string {
return i.Text
}
// HTML returns the HTML representation of the format item.
// A <span> tag is used as well as the style and class attributes.
// Obfuscation is encoded as a class with the name 'minecraft-format-obfuscated'.
func (i FormatItem) HTML() (result string) {
classes := make([]string, 0)
styles := map[string]string{
"color": i.Color.ToHex(),
}
if i.Obfuscated {
classes = append(classes, "minecraft-format-obfuscated")
}
if i.Bold {
styles["font-weight"] = "bold"
}
if i.Strikethrough {
if _, ok := styles["text-decoration"]; ok {
styles["text-decoration"] += " "
}
styles["text-decoration"] += "line-through"
}
if i.Underline {
if _, ok := styles["text-decoration"]; ok {
styles["text-decoration"] += " "
}
styles["text-decoration"] += "underline"
}
if i.Italic {
styles["font-style"] = "italic"
}
result += "<span"
if len(classes) > 0 {
result += " class=\""
for _, v := range classes {
result += v
}
result += "\""
}
if len(styles) > 0 {
result += " style=\""
keys := make([]string, 0, len(styles))
for k := range styles {
keys = append(keys, k)
}
for i, l := 0, len(keys); i < l; i++ {
key := keys[i]
value := styles[key]
result += fmt.Sprintf("%s: %s;", key, value)
if i+1 != l {
result += " "
}
}
result += "\""
}
result += fmt.Sprintf(">%s</span>", html.EscapeString(i.Text))
return
}