-
Notifications
You must be signed in to change notification settings - Fork 0
/
selector.go
41 lines (34 loc) · 961 Bytes
/
selector.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
package selector
import (
"strings"
"github.com/PuerkitoBio/goquery"
"ebooker/data"
)
func Select(selectables []*data.Chapter) error {
for _, s := range selectables {
var content []string
for _, c := range s.Content() {
selected, err := selectContent(c, s.Selector())
if err != nil {
return err
}
content = append(content, selected)
}
s.SetContent(content)
}
return nil
}
// selectContent assumes content is valid HTML markup, and applies selector to return a portion of the content.
// It also scrubs any links from the returned content.
func selectContent(content string, selector string) (string, error) {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(content))
if err != nil {
return "", err
}
result := doc.Find(selector)
// Remove any link tags, replace with the textual content.
result.Find("a").Each(func(i int, s *goquery.Selection) {
s.ReplaceWithHtml(s.Text())
})
return result.Html()
}