forked from agonopol/readability
/
document.go
246 lines (213 loc) · 6.17 KB
/
document.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package readability
import (
"fmt"
"github.com/moovweb/gokogiri"
"github.com/moovweb/gokogiri/html"
"github.com/moovweb/gokogiri/xml"
"io/ioutil"
"math"
"net/http"
"regexp"
"strings"
)
type Document struct {
doc *html.HtmlDocument
}
func ParseURL(url string) (*Document, error) {
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return Parse(body)
}
func Parse(content []byte) (*Document, error) {
content = regexes["replaceFontsRe"].ReplaceAll(regexes["replaceBrsRe"].ReplaceAll(content, []byte(`</p><p>`)), []byte(`<\1span>`))
doc, err := gokogiri.ParseHtml(content)
if err != nil {
return nil, err
}
comments, err := doc.Search("//comment()")
if err != nil {
return nil, err
}
for _, comment := range comments {
comment.Remove()
}
return &Document{doc}, nil
}
func (this *Document) Content() (string, error) {
if err := this.prepare(); err != nil {
return "", err
}
candidates, err := getCandidates(this.doc, 25)
if err != nil {
return "", err
}
article := this.getArticle(candidates)
return article.Content(), nil
}
func (this *Document) prepare() error {
scripts, err := this.doc.Search("//script")
if err != nil {
return err
}
for _, script := range scripts {
script.Remove()
}
styles, err := this.doc.Search("//style")
if err != nil {
return err
}
for _, style := range styles {
style.Remove()
}
this.removeUnlikelyCandidates()
this.misusedDivsIntoParagraphs()
return nil
}
func (this *Document) Free() {
this.doc.Free()
}
func (this *Document) walkElements(node xml.Node, f func(xml.Node) error) error {
f(node)
for child := node.FirstChild(); child != nil; child = child.NextSibling() {
err := this.walkElements(child, f)
if err != nil {
return err
}
}
return nil
}
func (this *Document) removeUnlikelyCandidates() error {
this.walkElements(this.doc, func(node xml.Node) error {
attributes := node.Attributes()
info := ""
if class, found := attributes["class"]; found {
info = class.Value()
}
if id, found := attributes["id"]; found {
info = fmt.Sprintf("%s%s", info, id.Value())
}
if regexes["unlikelyCandidatesRe"].Match([]byte(info)) && regexes["okMaybeItsACandidateRe"].Match([]byte(info)) {
name := strings.ToLower(node.Name())
if name != "body" && name != "html" {
node.Remove()
}
}
return nil
})
return nil
}
func (this *Document) misusedDivsIntoParagraphs() error {
this.walkElements(this.doc, func(node xml.Node) error {
if strings.ToLower(node.Name()) == "div" {
if regexes["divToPElementsRe"].Match([]byte(node.InnerHtml())) {
node.SetName("p")
}
}
return nil
})
return nil
}
func extend(doc *xml.ElementNode, node xml.Node) {
dup := node.Duplicate(1).(*xml.ElementNode)
doc.AddChild(dup)
if strings.ToLower(dup.Name()) != "p" || strings.ToLower(dup.Name()) != "div" {
dup.SetName("div")
}
}
func (this *Document) getArticle(candidates map[string]*Candidate) xml.Node {
best := bestCandidate(candidates)
threshold := math.Max(10.0, best.score*0.2)
doc := this.doc.CreateElementNode("div")
for node := best.node.Parent().FirstChild(); node != nil && node.IsValid(); node = node.NextSibling() {
if node.String() == best.node.String() {
extend(doc, node)
} else if candidate, found := candidates[node.String()]; found && candidate.score >= threshold {
extend(doc, node)
} else if strings.ToLower(node.Name()) == "p" {
lDensity := linkDensity(node)
length := len(node.Content())
if lDensity < 0.25 && length > 80 {
extend(doc, node)
} else if length < 80 && lDensity == 0.0 {
match, err := regexp.Match(`\.( |$)`, []byte(node.Content()))
if err != nil {
panic(err)
}
if match {
extend(doc, node)
}
}
}
}
return doc
}
func (this *Document) sanitize(candidates map[string]*Candidate, article xml.Node) string {
return ""
}
// def sanitize(node, candidates, options = {})
// node.css("h1, h2, h3, h4, h5, h6").each do |header|
// header.remove if class_weight(header) < 0 || get_link_density(header) > 0.33
// end
//
// node.css("form, object, iframe, embed").each do |elem|
// elem.remove
// end
//
// if @options[:remove_empty_nodes]
// # remove <p> tags that have no text content - this will also remove p tags that contain only images.
// node.css("p").each do |elem|
// elem.remove if elem.content.strip.empty?
// end
// end
//
// # Conditionally clean <table>s, <ul>s, and <div>s
// clean_conditionally(node, candidates, "table, ul, div")
//
// # We'll sanitize all elements using a whitelist
// base_whitelist = @options[:tags] || %w[div p]
// # We'll add whitespace instead of block elements,
// # so a<br>b will have a nice space between them
// base_replace_with_whitespace = %w[br hr h1 h2 h3 h4 h5 h6 dl dd ol li ul address blockquote center]
//
// # Use a hash for speed (don't want to make a million calls to include?)
// whitelist = Hash.new
// base_whitelist.each {|tag| whitelist[tag] = true }
// replace_with_whitespace = Hash.new
// base_replace_with_whitespace.each { |tag| replace_with_whitespace[tag] = true }
//
// ([node] + node.css("*")).each do |el|
// # If element is in whitelist, delete all its attributes
// if whitelist[el.node_name]
// el.attributes.each { |a, x| el.delete(a) unless @options[:attributes] && @options[:attributes].include?(a.to_s) }
//
// # Otherwise, replace the element with its contents
// else
// # If element is root, replace the node as a text node
// if el.parent.nil?
// node = Nokogiri::XML::Text.new(el.text, el.document)
// break
// else
// if replace_with_whitespace[el.node_name]
// el.swap(Nokogiri::XML::Text.new(' ' << el.text << ' ', el.document))
// else
// el.swap(Nokogiri::XML::Text.new(el.text, el.document))
// end
// end
// end
//
// end
//
// s = Nokogiri::XML::Node::SaveOptions
// save_opts = s::NO_DECLARATION | s::NO_EMPTY_TAGS | s::AS_XHTML
// html = node.serialize(:save_with => save_opts)
//
// # Get rid of duplicate whitespace
// return html.gsub(/[\r\n\f]+/, "\n" )
// end