-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlegacy.go
More file actions
191 lines (180 loc) · 5.23 KB
/
Copy pathlegacy.go
File metadata and controls
191 lines (180 loc) · 5.23 KB
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
package readability
import (
"strings"
"unicode/utf8"
"github.com/PuerkitoBio/goquery"
xhtml "golang.org/x/net/html"
)
// Legacy helpers cover older table-based layouts that need normalization but
// should not shape the generic extraction path.
func legacyTableArticleSelection(doc *goquery.Document) *goquery.Selection {
main := bestLegacyTableCell(doc)
if main.Length() == 0 {
return &goquery.Selection{}
}
mainNode := main.Get(0)
sidebar := previousElementSibling(mainNode)
if mainNode == nil || sidebar == nil {
return &goquery.Selection{}
}
wrapper := &xhtml.Node{Type: xhtml.ElementNode, Data: "div"}
content := &xhtml.Node{Type: xhtml.ElementNode, Data: "div"}
wrapper.AppendChild(content)
for sibling := sidebar; sibling != nil; {
next := nextElementSibling(sibling)
appendNode(content, sibling)
sibling = next
}
return goquery.NewDocumentFromNode(wrapper).Selection
}
func bestLegacyTableCell(doc *goquery.Document) *goquery.Selection {
var best *goquery.Selection
bestScore := 0.0
doc.Find("td, th").Each(func(_ int, s *goquery.Selection) {
if !isLegacyMainTableCell(s) {
return
}
textLength := utf8.RuneCountInString(innerText(s))
score := float64(textLength) * (1 - linkDensity(s))
if score <= bestScore {
return
}
best = s
bestScore = score
})
if best == nil {
return &goquery.Selection{}
}
return best
}
func isLegacyMainTableCell(s *goquery.Selection) bool {
node := s.Get(0)
if node == nil || tagNameNode(node.Parent) != "tr" {
return false
}
if hasAncestorNodeTag(node.Parent, "td") {
return false
}
if previousElementSibling(node) == nil || nextElementSibling(node) == nil {
return false
}
if utf8.RuneCountInString(innerText(s)) < 300 {
return false
}
return linkDensity(s) < 0.65
}
func cleanLegacyTableCandidate(article *goquery.Selection, cfg parserConfig) {
replaceJavascriptLinks(article)
unwrapSingleCellTables(article)
unwrapSingleCellNestedTablesAsDiv(article)
normalizeLegacyFileURLs(article)
article.Find("div, td[colspan='4']").Each(func(_ int, s *goquery.Selection) {
wrapPhrasingContentInParagraphs(s.Get(0))
})
unwrapLegacySidebarParagraphs(article)
article.Find("*").Each(func(_ int, s *goquery.Selection) {
cleanPresentationAttributes(s.Get(0), cfg)
})
cleanPresentationAttributes(article.Get(0), cfg)
article.Find("p").Each(func(_ int, s *goquery.Selection) {
if normalizeSpace(s.Text()) == "" && s.Find("img, audio").Length() == 0 {
s.Remove()
}
})
}
func unwrapLegacySidebarParagraphs(article *goquery.Selection) {
content := article.ChildrenFiltered("div").First()
if content.Length() == 0 {
content = article.Find("div").First()
}
sidebar := content.ChildrenFiltered("td").Last()
if sidebar.Length() == 0 {
return
}
sidebar.Find("td > p").Each(func(_ int, s *goquery.Selection) {
node := s.Get(0)
if node == nil || node.Parent == nil || tagNameNode(node.Parent) != "td" {
return
}
for node.FirstChild != nil {
child := node.FirstChild
node.RemoveChild(child)
node.Parent.InsertBefore(child, node)
}
removeNode(node)
})
sidebar.Find("span > p").Each(func(_ int, s *goquery.Selection) {
node := s.Get(0)
if node == nil || node.Parent == nil || tagNameNode(firstElementChild(node)) != "u" {
return
}
for node.FirstChild != nil {
child := node.FirstChild
node.RemoveChild(child)
node.Parent.InsertBefore(child, node)
}
removeNode(node)
})
collapseLegacySidebarLinkSpacing(sidebar.Get(0))
}
func collapseLegacySidebarLinkSpacing(node *xhtml.Node) {
if node == nil {
return
}
for child := node.FirstChild; child != nil; child = child.NextSibling {
if child.Type == xhtml.TextNode && tagNameNode(nextElementSiblingLike(child)) == "a" {
child.Data = strings.TrimRight(child.Data, " \t\r\n")
}
collapseLegacySidebarLinkSpacing(child)
}
}
func nextElementSiblingLike(node *xhtml.Node) *xhtml.Node {
for sibling := node.NextSibling; sibling != nil; sibling = sibling.NextSibling {
if sibling.Type == xhtml.TextNode && strings.TrimSpace(sibling.Data) == "" {
continue
}
if sibling.Type == xhtml.ElementNode {
return sibling
}
return nil
}
return nil
}
func unwrapSingleCellNestedTablesAsDiv(root *goquery.Selection) {
root.Find("table").Each(func(_ int, s *goquery.Selection) {
table := s.Get(0)
if table == nil || table.Parent == nil {
return
}
rows := s.ChildrenFiltered("tbody").ChildrenFiltered("tr")
if rows.Length() == 0 {
rows = s.ChildrenFiltered("tr")
}
cells := rows.ChildrenFiltered("td, th")
if cells.Length() != 1 {
return
}
innerTable := cells.First().ChildrenFiltered("table").First()
if innerTable.Length() == 0 || cells.First().Children().Length() != 1 {
return
}
div := &xhtml.Node{Type: xhtml.ElementNode, Data: "div"}
appendNode(div, innerTable.Get(0))
replaceNode(table, div)
})
}
func normalizeLegacyFileURLs(root *goquery.Selection) {
root.Find("[src], [href]").Each(func(_ int, s *goquery.Selection) {
node := s.Get(0)
if node == nil {
return
}
for i := range node.Attr {
if node.Attr[i].Key != "src" && node.Attr[i].Key != "href" {
continue
}
node.Attr[i].Val = strings.ReplaceAll(node.Attr[i].Val, "file:///C%7C/", "file:///C:/")
node.Attr[i].Val = strings.ReplaceAll(node.Attr[i].Val, "file:///C|/", "file:///C:/")
}
})
}