-
Notifications
You must be signed in to change notification settings - Fork 8
/
htmlutil.go
128 lines (116 loc) · 3 KB
/
htmlutil.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
package htmlutil
import (
"html"
"regexp"
"strings"
"github.com/microcosm-cc/bluemonday"
)
// ChartColor1 is the color palette for Google Charts as collected by
// Craig Davis here: https://gist.github.com/there4/2579834
var ChartColor1 = [...]string{
"#3366CC",
"#DC3912",
"#FF9900",
"#109618",
"#990099",
"#3B3EAC",
"#0099C6",
"#DD4477",
"#66AA00",
"#B82E2E",
"#316395",
"#994499",
"#22AA99",
"#AAAA11",
"#6633CC",
"#E67300",
"#8B0707",
"#329262",
"#5574A6",
"#3B3EAC",
}
// Link is a struct to hold information for an HTML link.
type Link struct {
Href string
InnerHtml string
}
const (
Color2GreenHex = "#00FF2A"
Color2YellowHex = "#DFDD13"
Color2RedHex = "#FF0000"
RingCentralOrangeHex = "#FF8800"
RingCentralBlueHex = "#0073AE"
RingCentralGreyHex = "#585858"
)
var (
bluemondayStrictPolicy = bluemonday.StrictPolicy()
rxHtmlToTextNewLines *regexp.Regexp = regexp.MustCompile(`(?i:</?p>)`)
rxCarriageReturn *regexp.Regexp = regexp.MustCompile(`\r`)
rxLineFeed *regexp.Regexp = regexp.MustCompile(`\n`)
rxLineFeedMore *regexp.Regexp = regexp.MustCompile(`\n+`)
rxCarriageReturnLineFeed *regexp.Regexp = regexp.MustCompile(`\r\n`)
rxLineFeedMore2 *regexp.Regexp = regexp.MustCompile(`\n\n+`)
rxCarriageReturnLineFeedMore *regexp.Regexp = regexp.MustCompile(`[\r\n]+`)
rxEndingSpacesLineFeed *regexp.Regexp = regexp.MustCompile(`\s+\n`)
)
func StreamlineCRLFs(s string) string {
newLines := []string{}
extLines := strings.Split(s, "\n")
for _, line := range extLines {
newLines = append(newLines, strings.TrimSpace(line))
}
s2 := strings.Join(newLines, "\n")
s2 = rxLineFeedMore2.ReplaceAllString(
rxCarriageReturn.ReplaceAllString(
rxCarriageReturnLineFeed.ReplaceAllString(s2, "\n"),
"\n"),
"\n",
)
return s2
}
// HtmlToTextCondensed removes HTML tags, unescapes HTML entities,
// and removes extra whitespace including non-breaking spaces.
func HtmlToTextCondensed(s string) string {
return strings.Join(
strings.Fields(
html.UnescapeString(
bluemondayStrictPolicy.Sanitize(s),
),
),
" ",
)
}
// HtmlToText converts HTML to multi-line text.
func HtmlToText(s string) string {
return rxLineFeedMore2.ReplaceAllString(
strings.TrimSpace(
html.UnescapeString(
bluemondayStrictPolicy.Sanitize(
rxHtmlToTextNewLines.ReplaceAllString(s, "$1\n\n"),
),
),
),
"\n\n",
)
}
func SimplifyHtmlText(s string) string {
text := HtmlToText(s)
lines := strings.Split(text, "\n")
newlines := []string{}
for _, line := range lines {
line := strings.TrimSpace(line)
if len(line) > 0 {
newlines = append(newlines, "<p>"+line+"</p>")
}
}
return strings.Join(newlines, "")
}
func TextToHtml(s string) string {
return rxLineFeed.ReplaceAllString(StreamlineCRLFs(s), "<br/>")
}
func TextToHtmlBr2(s string) string {
return rxLineFeed.ReplaceAllString(
rxLineFeedMore.ReplaceAllString(StreamlineCRLFs(s), "\n"),
"<br/><br/>",
)
}