forked from dstein64/highlight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
readabilitySAX.js.diff
354 lines (331 loc) · 11 KB
/
readabilitySAX.js.diff
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
--- readabilitySAX.js 2015-04-26 22:06:51.087880400 -0400
+++ highlight/src/lib/readabilitySAX.js 2015-04-26 21:50:12.779249300 -0400
@@ -38,9 +38,10 @@
elem;
for(var i=0; i < childNum; i++){
elem = childs[i];
- if(typeof elem === "string"){
- info.textLength += elem.trim()./*replace(re_whitespace, " ").*/length;
- if(re_commas.test(elem)) info.commas += elem.split(re_commas).length - 1;
+ if(elem.nodeType === Node.TEXT_NODE){
+ txt = elem.textContent;
+ info.textLength += txt.trim()./*replace(re_whitespace, " ").*/length;
+ if(re_commas.test(txt)) info.commas += txt.split(re_commas).length - 1;
}
else {
if(elem.name === "a"){
@@ -84,7 +85,7 @@
var nodes = this.children, ret = "";
for(var i = 0, j = nodes.length; i < j; i++){
- if(typeof nodes[i] === "string") ret += nodes[i];
+ if(nodes[i].nodeType === Node.TEXT_NODE) ret += nodes[i].textContent;
else ret += nodes[i].getOuterHTML();
}
return ret;
@@ -92,7 +93,8 @@
getFormattedText: function(){
var nodes = this.children, ret = "";
for(var i = 0, j = nodes.length; i < j; i++){
- if(typeof nodes[i] === "string") ret += nodes[i].replace(re_whitespace, " ");
+ if(nodes[i] && nodes[i].nodeType === Node.TEXT_NODE)
+ ret += nodes[i].textContent.replace(re_whitespace, " ");
else {
if(nodes[i].name === "p" || nodes[i].name in headerTags) ret += "\n";
ret += nodes[i].getFormattedText();
@@ -101,8 +103,32 @@
}
return ret;
},
+ getNodes: function(){
+ var nodes = this.children;
+ var ret = [];
+ for(var i = 0; i < nodes.length; i++){
+ if (nodes[i] && nodes[i].nodeType === Node.TEXT_NODE) {
+ if (nodes[i].textContent.length > 0)
+ ret.push(nodes[i]);
+ } else {
+ var more = nodes[i].getNodes();
+ for (var j = 0; j < more.length; j++) {
+ ret.push(more[j]);
+ }
+ }
+ }
+ return ret;
+ },
toString: function(){
- return this.children.join("");
+ text = [];
+ for (var i = 0; i < this.children.length; i++) {
+ var child = this.children[i];
+ if (child.nodeType === Node.TEXT_NODE)
+ text.push(child.textContent);
+ else
+ text.push(child.toString());
+ }
+ return text.join("");
},
getTopCandidate: function(){
var childs = this.children,
@@ -111,7 +137,7 @@
topCandidate, elem;
for(var i = 0, j = childs.length; i < j; i++){
- if(typeof childs[i] === "string") continue;
+ if(childs[i].nodeType === Node.TEXT_NODE) continue;
if(childs[i].isCandidate){
elem = childs[i];
//add points for the tags name
@@ -187,13 +213,18 @@
re_commas = /,[\s\,]*/g;
//3. the readability class
-var Readability = function(settings){
- //the root node
- this._currentElement = new Element("document");
+var Readability = function(doc, settings, skipLevel){
+ // the following get set in the getArticle method
+ this._currentElement = null;
this._topCandidate = null;
- this._origTitle = this._headerTitle = "";
- this._scannedLinks = {};
+ this._origTitle = null;
+ this._scannedLinks = null;
+ this._article = null;
+
+ // these get set now
+ this._doc = doc;
if(settings) this._processSettings(settings);
+ this.setSkipLevel(skipLevel);
};
Readability.prototype._settings = {
@@ -206,7 +237,8 @@
linksToSkip: {}, //pages that are already parsed
//pageURL: null, //URL of the page which is parsed
//type: "html", //default type of output
- resolvePaths: false
+ resolvePaths: false,
+ skipLevel: 3
};
Readability.prototype._convertLinks = function(path){
@@ -276,28 +308,28 @@
};
Readability.prototype._processSettings = function(settings){
- var Settings = this._settings;
- this._settings = {};
+ var Settings = this._settings;
+ this._settings = {};
- for(var i in Settings){
- if(typeof settings[i] !== "undefined"){
- this._settings[i] = settings[i];
- }
- else this._settings[i] = Settings[i];
- }
-
- var path;
- if(settings.pageURL){
- path = settings.pageURL.split(re_slashes);
- this._url = {
- protocol: path[0],
- domain: path[1],
- path: path.slice(2, -1),
- full: settings.pageURL.replace(re_closing,"")
- };
- this._baseURL = this._getBaseURL();
- }
- if(settings.type) this._settings.type = settings.type;
+ for(var i in Settings){
+ if(typeof settings[i] !== "undefined"){
+ this._settings[i] = settings[i];
+ }
+ else this._settings[i] = Settings[i];
+ }
+
+ var path;
+ if(settings.pageURL){
+ path = settings.pageURL.split(re_slashes);
+ this._url = {
+ protocol: path[0],
+ domain: path[1],
+ path: path.slice(2, -1),
+ full: settings.pageURL.replace(re_closing,"")
+ };
+ this._baseURL = this._getBaseURL();
+ }
+ if(settings.type) this._settings.type = settings.type;
};
Readability.prototype._scanLink = function(elem){
@@ -423,8 +455,8 @@
else elem.attributes[name] = value;
};
-Readability.prototype.ontext = function(text){
- this._currentElement.children.push(text);
+Readability.prototype.ontext = function(textNode){
+ this._currentElement.children.push(textNode);
};
Readability.prototype.onclosetag = function(tagName){
@@ -494,7 +526,7 @@
if(contentLength === 0){
if(elem.children.length === 0) return;
- if(elem.children.length === 1 && typeof elem.children[0] === "string") return;
+ if(elem.children.length === 1 && elem.children[0].nodeType === Node.TEXT_NODE) return;
}
if((elem.info.tagCount.li - 100) > p && tagName !== "ul" && tagName !== "ol") return;
if(contentLength < 25 && (!("img" in elem.info.tagCount) || elem.info.tagCount.img > 2) ) return;
@@ -555,7 +587,7 @@
siblingScoreThreshold = Math.max(10, candidate.totalScore * .2);
for(var i = 0; i < childNum; i++){
- if(typeof childs[i] === "string") continue;
+ if(childs[i].nodeType === Node.TEXT_NODE) continue;
if(childs[i] === candidate);
else if(candidate.elementData === childs[i].elementData){ //TODO: just the class name should be checked
@@ -594,7 +626,7 @@
}
while(elem.children.length === 1){
- if(typeof elem.children[0] === "object"){
+ if(typeof elem.children[0] === "object" && elem.children[0].nodeType !== Node.TEXT_NODE){
elem = elem.children[0];
} else break;
}
@@ -604,16 +636,16 @@
//skipLevel is a shortcut to allow more elements of the page
Readability.prototype.setSkipLevel = function(skipLevel){
- if(skipLevel === 0) return;
-
- //if the prototype is still used for settings, change that
- if(this._settings === Readability.prototype._settings){
- this._processSettings({});
- }
+ if(skipLevel === 0) return;
- if(skipLevel > 0) this._settings.stripUnlikelyCandidates = false;
- if(skipLevel > 1) this._settings.weightClasses = false;
- if(skipLevel > 2) this._settings.cleanConditionally = false;
+ //if the prototype is still used for settings, change that
+ if(this._settings === Readability.prototype._settings){
+ this._processSettings({});
+ }
+
+ if(skipLevel > 0) this._settings.stripUnlikelyCandidates = false;
+ if(skipLevel > 1) this._settings.weightClasses = false;
+ if(skipLevel > 2) this._settings.cleanConditionally = false;
};
Readability.prototype.getTitle = function(){
@@ -672,11 +704,16 @@
return node.getFormattedText().trim().replace(/\n+(?=\n{2})/g, "");
};
+Readability.prototype.getNodes = function(node){
+ if(!node) node = this._getCandidateNode();
+ return node.getNodes();
+};
+
Readability.prototype.getEvents = function(cbs){
(function process(node){
cbs.onopentag(node.name, node.attributes);
for(var i = 0, j = node.children.length; i < j; i++){
- if(typeof node.children[i] === "string"){
+ if(node.children[i].nodeType === Node.TEXT_NODE){
cbs.ontext(node.children[i]);
}
else process(node.children[i]);
@@ -685,22 +722,89 @@
})(this._getCandidateNode());
};
-Readability.prototype.getArticle = function(type){
- var elem = this._getCandidateNode();
+//The following method is from DOMasSAX.js
- var ret = {
- title: this._headerTitle || this.getTitle(),
- nextPage: this.getNextPage(),
- textLength: elem.info.textLength,
- score: this._topCandidate ? this._topCandidate.totalScore : 0
- };
+/*
+Explenation:
+ DOM port of E4XasSAX
+ use the document root to initialise it
+*/
+
+Readability.prototype.saxParser = function(elem){
+ var me = this;
+
+ //todo: support further events, options for trim & space normalisation
+
+ function parse(node){
+ var name = node.tagName.toLowerCase(),
+ attributeNodes = node.attributes;
+
+ me.onopentagname(name);
+
+ for(var i = 0, j = attributeNodes.length; i < j; i++){
+ me.onattribute(attributeNodes[i].name+'', attributeNodes[i].value);
+ }
+
+ var childs = node.childNodes,
+ num = childs.length, nodeType;
+
+ for(var i = 0; i < num; i++){
+ nodeType = childs[i].nodeType;
+ if(nodeType === Node.TEXT_NODE)
+ me.ontext(childs[i]);
+ else if(nodeType === Node.ELEMENT_NODE) parse(childs[i]);
+ /*else if(nodeType === Node.COMMENT_NODE)
+ if(callbacks.oncomment) callbacks.oncomment(childs[i].toString());
+ [...]
+ */
+ }
+ me.onclosetag(name);
+ }
+
+ parse(elem);
+};
+
+Readability.prototype.getArticle = function(useCache){
+ useCache = typeof useCache !== 'undefined' ? useCache : false;
+ if (!useCache || this._article === null) {
+ this._currentElement = new Element("document");
+ this._topCandidate = null;
+ this._origTitle = this._headerTitle = "";
+ this._scannedLinks = {};
+
+ this.saxParser(this._doc.documentElement);
+ var elem = this._getCandidateNode();
+ var me = this;
+
+ // cache
+ var text = null;
+ var nodes = null;
+ var html = null;
+
+ this._article = {
+ title: this._headerTitle || this.getTitle(),
+ nextPage: this.getNextPage(),
+ textLength: elem.info.textLength,
+ score: this._topCandidate ? this._topCandidate.totalScore : 0,
+ getText: function() {
+ if (text === null)
+ text = me.getText(elem);
+ return text;
+ },
+ getNodes: function() {
+ if (nodes === null)
+ nodes = me.getNodes(elem);
+ return nodes;
+ },
+ getHTML: function() {
+ if (html === null)
+ html = me.getHTML(elem);
+ return html;
+ }
+ };
+ }
- if(!type && this._settings.type) type = this._settings.type;
-
- if(type === "text") ret.text = this.getText(elem);
- else ret.html = this.getHTML(elem);
-
- return ret;
+ return this._article;
};
if(typeof module !== "undefined" && "exports" in module){
@@ -715,3 +819,5 @@
}
})(typeof window === "object" ? window : this);
+
+