Skip to content
Permalink
Browse files

Initial commit

  • Loading branch information...
arrix committed Nov 6, 2010
0 parents commit 5482d05731fc8b4e2f5630d3a799a1f08092e3ce
Showing with 7,010 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +20 −0 LICENSE.txt
  3. +2,005 −0 lib/readability.js
  4. +174 −0 misc/readability-feedflock.js
  5. +20 −0 misc/readability-module.js
  6. +1,907 −0 misc/readability-my.js
  7. +1,836 −0 misc/readability-ori.js
  8. +25 −0 notes.txt
  9. +48 −0 package.json
  10. +14 −0 test.txt
  11. +16 −0 test/clean-file.js
  12. +138 −0 test/clean-proxy.js
  13. +19 −0 test/jsdom-bug.js
  14. +787 −0 test/nytime.html
@@ -0,0 +1 @@
.DS_Store
@@ -0,0 +1,20 @@
Copyright (c) 2010 Arrix Zhou

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Large diffs are not rendered by default.

@@ -0,0 +1,174 @@
(function() {
//require.paths.unshift('./vendor');
var sys = require('sys');
var jsdom = require('jsdom');
//var htmlparser = require('./htmlparser');
//var level = jsdom.defaultLevel;
// var doc = new (level.Document)();
// doc.createWindow = function() {
// window = jsdom.windowAugmentation(level, { document: doc, parser: htmlparser })
// delete window.document.createWindow
// return window
// };
// var document = doc.createWindow().document;

var document;
var Client = {
parse: function(content, callback) {
document = jsdom.jsdom(content).createWindow().document;
//document.innerHTML = content;
//console.log(document.body);
if (!document.body) {
callback({content:'',title:''});
return;
}

// Replace all doubled-up <BR> tags with <P> tags, and remove fonts.
var pattern = new RegExp ("<br/?>[ \r\n\s]*<br/?>", "g");
document.body.innerHTML = document.body.innerHTML.replace(pattern, "</p><p>").replace(/<\/?font[^>]*>/g, '');

var allParagraphs = document.getElementsByTagName("p");
var contentDiv = null;
var topDivParas =[];

var articleContent = document.createElement("DIV");
var articleTitle = document.title

if (articleTitle)
articleTitle = articleTitle.replace(/^\s+|\s+$/g, '');

// Study all the paragraphs and find the chunk that has the best score.
// A score is determined by things like: Number of <p>'s, commas, special classes, etc.
for (var j=0; j < allParagraphs.length; j++) {
var parentNode = allParagraphs[j].parentNode;

if(typeof(parentNode) != 'undefined') {
// Initialize readability data
if(typeof parentNode.readability == 'undefined')
{
parentNode.readability = {"contentScore": 0};

// Look for a special classname
if(parentNode.className.match(/(comment|meta|footer|footnote)/))
parentNode.readability.contentScore -= 50;
else if(parentNode.className.match(/((^|\\s)(post|hentry|entry[-]?(content|text|body)?|article[-]?(content|text|body)?)(\\s|$))/))
parentNode.readability.contentScore += 25;

// Look for a special ID
if(parentNode.id.match(/(comment|meta|footer|footnote)/))
parentNode.readability.contentScore -= 50;
else if(parentNode.id.match(/^(post|hentry|entry[-]?(content|text|body)?|article[-]?(content|text|body)?)$/))
parentNode.readability.contentScore += 25;
}

// Add a point for the paragraph found
if(this.getInnerText(allParagraphs[j]).length > 10)
parentNode.readability.contentScore++;

// Add points for any commas within this paragraph
parentNode.readability.contentScore += this.getCharCount(allParagraphs[j]);

topDivParas.push({ 'node': parentNode, 'score': parentNode.readability.contentScore });
}
}

for (var i=0; i < topDivParas.length; i++) {
var score = topDivParas[i].score;
if (contentDiv == null || score > contentDiv.score) {
contentDiv = { 'node': topDivParas[i].node, 'score': score }
}
}

if (contentDiv == null)
return callback({ content: '', title: '' });

var topDiv = contentDiv.node

this.cleanStyles(topDiv); // Removes all style attributes
topDiv = this.killDivs(topDiv); // Goes in and removes DIV's that have more non <p> stuff than <p> stuff
topDiv = this.killBreaks(topDiv); // Removes any consecutive <br />'s into just one <br />

// Cleans out junk from the topDiv just in case:
topDiv = this.clean(topDiv, "form");
topDiv = this.clean(topDiv, "object");
topDiv = this.clean(topDiv, "table", 250);
topDiv = this.clean(topDiv, "h1");
topDiv = this.clean(topDiv, "h2");
topDiv = this.clean(topDiv, "iframe");

articleContent.appendChild(topDiv);

return callback({ content: articleContent.innerHTML, title: articleTitle });
},
getInnerText: function(e) {
return e.textContent;
},
getCharCount: function( e,s ) {
s = s || ",";
return this.getInnerText(e).split(s).length;
},
cleanStyles: function( e ) {
e = e || document;
var cur = e.firstChild;

// If we had a bad node, there's not much we can do.
if(!e)
return;

// Remove any root styles, if we're able.
if(typeof e.removeAttribute == 'function')
e.removeAttribute('style');

// Go until there are no more child nodes
while ( cur != null ) {
if ( cur.nodeType == 1 ) {
// Remove style attribute(s) :
cur.removeAttribute("style");
this.cleanStyles( cur );
}
cur = cur.nextSibling;
}
},
killDivs: function ( e ) {
var divsList = e.getElementsByTagName( "div" );
var curDivLength = divsList.length;

// Gather counts for other typical elements embedded within.
// Traverse backwards so we can remove nodes at the same time without effecting the traversal.
for (var i=curDivLength-1; i >= 0; i--) {
var p = divsList[i].getElementsByTagName("p").length;
var img = divsList[i].getElementsByTagName("img").length;
var li = divsList[i].getElementsByTagName("li").length;
var a = divsList[i].getElementsByTagName("a").length;
var embed = divsList[i].getElementsByTagName("embed").length;

// If the number of commas is less than 10 (bad sign) ...
if ( this.getCharCount(divsList[i]) < 10) {
// And the number of non-paragraph elements is more than paragraphs
// or other ominous signs :
if ( img > p || li > p || a > p || p == 0 || embed > 0) {
divsList[i].parentNode.removeChild(divsList[i]);
}
}
}
return e;
},
killBreaks: function ( e ) {
e.innerHTML = e.innerHTML.replace(/(<br\s*\/?>(\s|&nbsp;?)*){1,}/g,'<br />');
return e;
},
clean: function(e, tags, minWords) {
var targetList = e.getElementsByTagName( tags );
minWords = minWords || 1000000;

for (var y=0; y < targetList.length; y++) {
// If the text content isn't laden with words, remove the child:
if (this.getCharCount(targetList[y], " ") < minWords) {
targetList[y].parentNode.removeChild(targetList[y]);
}
}
return e;
}
};
exports.Client = Client;
})();
@@ -0,0 +1,20 @@
exports.parse = parse;
var jsdom = require('jsdom');
var rdom = require('./readability-my2.js');
var util = require('util');

function parse(html, url, callback) {
//util.debug(html);
var doc = jsdom.jsdom(html, null, {url: url});
util.log('---DOM created');
var win = doc.parentWindow;
if (!doc.body) {
console.log('empty body');
return callback({title: '', content: ''});
}

rdom.start(win, function(html) {
//console.log(html);
callback({title: document.title, content: html});
});
}

0 comments on commit 5482d05

Please sign in to comment.
You can’t perform that action at this time.