-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathhtml.go
106 lines (88 loc) · 2.09 KB
/
html.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
package main
import (
"bytes"
"fmt"
"sort"
"strings"
)
type HtmlCodes map[string]string
func (c HtmlCodes) String() string {
var cc []string
for k, _ := range c {
if k == "" {
continue
}
cc = append(cc, k)
}
sort.Strings(cc)
var s []string
for _, ss := range cc {
s = append(s, Htmlize(ss, ss))
}
return strings.Join(s, ", ")
}
var htmlCodes = HtmlCodes{
"": "",
"reset": `</span>`,
"bold": `<span class="bold">`,
"faint": `<span class="faint">`,
"standout": `<span class="standout">`,
"underline": `<span class="underline">`,
"blink": `<span class="blink">`,
"overline": `<span class="overline">`,
}
func init() {
darkHtmls := []string{
"black",
"darkred",
"darkgreen",
"brown",
"darkblue",
"purple",
"teal",
"lightgray",
}
lightHtmls := []string{
"darkgray",
"red",
"green",
"yellow",
"blue",
"fuchsia",
"turquoise",
"white",
}
for i, x := 0, 30; i < len(darkHtmls); i, x = i+1, x+1 {
htmlCodes[darkHtmls[i]] = fmt.Sprintf(`<span class="%s">`, darkHtmls[i])
htmlCodes[lightHtmls[i]] = fmt.Sprintf(`<span class="%s">`, lightHtmls[i])
}
htmlCodes["darkteal"] = htmlCodes["turquoise"]
htmlCodes["darkyellow"] = htmlCodes["brown"]
htmlCodes["fuscia"] = htmlCodes["fuchsia"]
htmlCodes["white"] = htmlCodes["bold"]
}
func Htmlize(attr, text string) string {
if attr == "" {
return text
}
result := new(bytes.Buffer)
if strings.HasPrefix(attr, "+") && strings.HasSuffix(attr, "+") {
result.WriteString(htmlCodes["blink"])
attr = strings.TrimPrefix(attr, "+")
attr = strings.TrimSuffix(attr, "+")
}
if strings.HasPrefix(attr, "*") && strings.HasSuffix(attr, "*") {
result.WriteString(htmlCodes["bold"])
attr = strings.TrimPrefix(attr, "*")
attr = strings.TrimSuffix(attr, "*")
}
if strings.HasPrefix(attr, "_") && strings.HasSuffix(attr, "_") {
result.WriteString(htmlCodes["underline"])
attr = strings.TrimPrefix(attr, "_")
attr = strings.TrimSuffix(attr, "_")
}
result.WriteString(htmlCodes[attr])
result.WriteString(text)
result.WriteString(htmlCodes["reset"])
return result.String()
}