forked from go-shiori/go-readability
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
73 lines (59 loc) · 1.33 KB
/
utils.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
package readability
import (
"crypto/md5"
"fmt"
"os"
"strings"
"unicode/utf8"
"github.com/PuerkitoBio/goquery"
)
func createDocFromFile(path string) (*goquery.Document, error) {
// Open file
src, err := os.Open(path)
if err != nil {
return nil, err
}
defer src.Close()
// Create document
return goquery.NewDocumentFromReader(src)
}
func hashNode(node *goquery.Selection) string {
if node == nil {
return ""
}
html, _ := node.Html()
return fmt.Sprintf("%x", md5.Sum([]byte(html)))
}
func strLen(str string) int {
return utf8.RuneCountInString(str)
}
func findSeparator(str string, separators ...string) (int, string) {
words := strings.Fields(str)
for i, word := range words {
for _, separator := range separators {
if word == separator {
return i, separator
}
}
}
return -1, ""
}
func hasSeparator(str string, separators ...string) bool {
idx, _ := findSeparator(str, separators...)
return idx != -1
}
func removeSeparator(str string, separators ...string) string {
words := strings.Fields(str)
finalWords := []string{}
for _, word := range words {
for _, separator := range separators {
if word != separator {
finalWords = append(finalWords, word)
}
}
}
return strings.Join(finalWords, " ")
}
func normalizeText(str string) string {
return strings.Join(strings.Fields(str), " ")
}