-
Notifications
You must be signed in to change notification settings - Fork 6
/
item.go
143 lines (109 loc) · 2.66 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
132
133
134
135
136
137
138
139
140
141
142
143
package formatting
import (
"fmt"
"html"
"strings"
"github.com/mcstatus-io/mcutil/v3/formatting/colors"
"github.com/mcstatus-io/mcutil/v3/formatting/decorators"
)
// Item is a single formatting item that contains a color and any optional formatting controls
type Item struct {
Text string `json:"text"`
Color *colors.Color `json:"color,omitempty"`
Decorators []decorators.Decorator `json:"decorators"`
}
// Raw returns the Minecraft encoding of the formatting (§ + formatting codes/color + text)
func (i Item) Raw() (result string) {
if i.Color != nil {
result += i.Color.ToRaw()
}
for _, decorator := range i.Decorators {
result += decorator.ToRaw()
}
result += i.Text
return
}
// Clean returns the text only of the formatting item
func (i Item) 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 Item) HTML() string {
classes := make([]string, 0)
styles := map[string]string{}
if i.Color != nil {
styles["color"] = i.Color.ToHex()
}
for _, decorator := range i.Decorators {
switch decorator {
case decorators.Obfuscated:
{
classes = append(classes, "minecraft-format-obfuscated")
break
}
case decorators.Bold:
{
styles["font-weight"] = "bold"
break
}
case decorators.Strikethrough:
{
if _, ok := styles["text-decoration"]; ok {
styles["text-decoration"] += " "
}
styles["text-decoration"] += "line-through"
break
}
case decorators.Underlined:
{
if _, ok := styles["text-decoration"]; ok {
styles["text-decoration"] += " "
}
styles["text-decoration"] += "underline"
break
}
case decorators.Italic:
{
styles["font-style"] = "italic"
break
}
}
}
var (
rawClass string = ""
rawStyle string = ""
)
if len(classes) > 0 {
rawClass = fmt.Sprintf(" class=\"%s\"", strings.Join(classes, " "))
}
if len(styles) > 0 {
styleData := ""
for k, v := range styles {
styleData += fmt.Sprintf("%s: %s;", k, v)
}
rawStyle = fmt.Sprintf(" style=\"%s\"", styleData)
}
return fmt.Sprintf("<span%s%s>%s</span>", rawClass, rawStyle, html.EscapeString(i.Text))
}
func toRaw(tree []Item) (result string) {
for _, v := range tree {
result += v.Raw()
}
return
}
func toClean(tree []Item) (result string) {
for _, v := range tree {
result += v.Clean()
}
return
}
func toHTML(tree []Item) (result string) {
result = "<span>"
for _, v := range tree {
result += v.HTML()
}
result += "</span>"
return
}