/
top_image_marker.go
71 lines (57 loc) · 1.58 KB
/
top_image_marker.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
package processor
import (
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/urandom/readeef/log"
"github.com/urandom/readeef/parser"
"golang.org/x/net/html"
)
type TopImageMarker struct {
log log.Log
}
func NewTopImageMarker(l log.Log) TopImageMarker {
return TopImageMarker{log: l}
}
func (p TopImageMarker) ProcessFeed(f parser.Feed) parser.Feed {
p.log.Infof("Locating suitable top images in articles of '%s'\n", f.Title)
for i := range f.Articles {
if d, err := goquery.NewDocumentFromReader(strings.NewReader(f.Articles[i].Description)); err == nil {
if markTopImage(d) {
if content, err := d.Html(); err == nil {
// net/http tries to provide valid html, adding html, head and body tags
content = content[strings.Index(content, "<body>")+6 : strings.LastIndex(content, "</body>")]
f.Articles[i].Description = content
}
}
}
}
return f
}
func markTopImage(d *goquery.Document) bool {
changed := false
totalTextCount := len(d.Text())
img := d.Find("img")
if img.Length() == 0 {
return changed
}
if totalTextCount == 0 {
img.AddClass("top-image")
changed = true
} else {
afterTextCount := len(img.NextAll().Text())
// Add any text-node siblings of the image
for n := img.Get(0).NextSibling; n != nil; n = n.NextSibling {
if n.Type == html.TextNode {
afterTextCount += len(n.Data)
}
}
img.Parents().Each(func(i int, s *goquery.Selection) {
afterTextCount += len(s.NextAll().Text())
})
if totalTextCount-afterTextCount <= afterTextCount {
img.AddClass("top-image")
changed = true
}
}
return changed
}