/
replace.go
54 lines (42 loc) · 1.1 KB
/
replace.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
package href
import (
"unicode"
"github.com/PuerkitoBio/goquery"
)
// Replace replaces all found HREFs with [index] symbol at end of node text.
// It returns slice of replaced HREFs.
func Replace(selection *goquery.Selection) (hrefs []string) {
replaceHrefsRecursively(selection, map[int]struct{}{}, &hrefs)
return hrefs
}
func replaceHrefsRecursively(selection *goquery.Selection, accum map[int]struct{}, hrefs *[]string) {
if _, processed := accum[selection.Index()]; processed {
return
}
if path, ok := selection.Attr("href"); ok {
accum[selection.Index()] = struct{}{}
*hrefs = append(*hrefs, path)
text, cut := cutRightSpaces(selection.Text())
text += superscript(len(*hrefs))
text += cut
selection.SetText(text)
}
selection.Find("*").Each(func(i int, s *goquery.Selection) {
replaceHrefsRecursively(s, accum, hrefs)
})
}
func cutRightSpaces(input string) (result, cut string) {
var (
r = []rune(input)
c []rune
)
for i := len(r) - 1; i >= 0; i-- {
if unicode.IsSpace(r[i]) {
c = append(c, r[i])
r = r[:i]
} else {
break
}
}
return string(r), string(c)
}