-
Notifications
You must be signed in to change notification settings - Fork 12
/
util.go
116 lines (95 loc) · 3.19 KB
/
util.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
package fulltext
import (
"bytes"
"html/template"
re "regexp"
"strings"
)
var wordizeRe *re.Regexp
func init() {
wordizeRe = re.MustCompile("[\\s,.;:!?[\\]()'\"]+")
}
type WordSplitter func(string) []string
// Split a string up into words
func Wordize(t string) []string {
return wordizeRe.Split(t, -1)
}
type WordCleaner func(string) string
// Make word appropriate for indexing
func IndexizeWord(w string) string {
return strings.TrimSpace(strings.ToLower(w))
}
type StopWordChecker func(string) bool
// This function copied from here: https://github.com/kennygrant/sanitize/blob/master/sanitize.go
// License is: https://github.com/kennygrant/sanitize/blob/master/License-BSD.txt
// Strip html tags, replace common entities, and escape <>&;'" in the result.
// Note the returned text may contain entities as it is escaped by HTMLEscapeString,
// and most entities are not translated.
func HTMLStripTags(s string) (output string) {
output = ""
// Shortcut strings with no tags in them
if !strings.ContainsAny(s, "<>") {
output = s
} else {
// First remove line breaks etc as these have no meaning outside html tags (except pre)
// this means pre sections will lose formatting... but will result in less uninentional paras.
s = strings.Replace(s, "\n", "", -1)
// Then replace line breaks with newlines, to preserve that formatting
s = strings.Replace(s, "</p>", "\n", -1)
s = strings.Replace(s, "<br>", "\n", -1)
s = strings.Replace(s, "</br>", "\n", -1)
// Walk through the string removing all tags
b := bytes.NewBufferString("")
inTag := false
for _, r := range s {
switch r {
case '<':
inTag = true
case '>':
inTag = false
default:
if !inTag {
b.WriteRune(r)
}
}
}
output = b.String()
}
// In case we have missed any tags above, escape the text - removes <, >, &, ' and ".
output = template.HTMLEscapeString(output)
// Remove a few common harmless entities, to arrive at something more like plain text
// This relies on having removed *all* tags above
output = strings.Replace(output, " ", " ", -1)
output = strings.Replace(output, """, "\"", -1)
output = strings.Replace(output, "'", "'", -1)
output = strings.Replace(output, """, "\"", -1)
output = strings.Replace(output, "'", "'", -1)
// NB spaces here are significant - we only allow & not part of entity
output = strings.Replace(output, "& ", "& ", -1)
output = strings.Replace(output, "&amp; ", "& ", -1)
return output
}
var titlere *re.Regexp
var descre *re.Regexp
func init() {
titlere = re.MustCompile("(?i)<title>([^<]+)</title>")
descre = re.MustCompile("(?i)<meta\\s+name\\s*=\\s*[\"']description[\"']\\s+content\\s*=\\s*[\"'](.*?)[\"']\\s*/?>")
}
// Helper to extract an HTML title from the title tag
func HTMLExtractTitle(html string) string {
tret := titlere.FindSubmatch([]byte(html))
title := ""
if len(tret) > 1 {
title = strings.TrimSpace(string(tret[1]))
}
return title
}
// Helper to extract an HTML description from the meta[name=description] tag
func HTMLExtractDescription(html string) string {
tret := descre.FindSubmatch([]byte(html))
desc := ""
if len(tret) > 1 {
desc = strings.TrimSpace(string(tret[1]))
}
return desc
}