Skip to content

Commit

Permalink
Added option argument to readability.parse. Turn off debugging and pr…
Browse files Browse the repository at this point in the history
…ofiling by default.
  • Loading branch information
arrix committed Feb 12, 2011
1 parent 2f4b871 commit 719da2e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
56 changes: 49 additions & 7 deletions lib/readability.js
Original file line number Diff line number Diff line change
Expand Up @@ -1928,11 +1928,33 @@ var LiveTagWalker = function(root, tag, func) {
};

//==============================================================================
var Utils = {
extend: function(/* dst, src1, src2, ... */) {
var args = [].slice.call(arguments);
var dst = args.shift();

for (var i = 0, l = args.length, src; src = args[i], i < l; i++) {
for (var k in src) {
dst[k] = src[k];
}
}
return dst;
}
};
var jsdom = require('jsdom'),
assert = require('assert'),
mod_sprintf = require('./sprintf'),
sprintf = mod_sprintf.sprintf;

// var util;
// try {
// util = require('util');
// } catch(e) {
// util = {
// debug: function(a) { console.log(a); },
// log: function(a) { console.log(a); }
// }
// }
//LiveTagWalker(document.body, '*', function(n) {dbg(n.tagName + n.id + n.className);}),0;

(function() {
Expand Down Expand Up @@ -2045,7 +2067,9 @@ var jsdom = require('jsdom'),
var MyProfiler = {
stats: {},
timed_level: 0,
enabled: false,
timed: function(func, name, options) {
if (!MyProfiler.enabled) return func();
options = options || {};
var z = this;
//dbg('begin ' + name);
Expand Down Expand Up @@ -2154,7 +2178,7 @@ function removeClassNames(e) {
}
}

function start(w, cb) {
function start(w, options, cb) {
window = w;
document = w.document;

Expand All @@ -2165,14 +2189,19 @@ function start(w, cb) {
w.scrollTo = function(){};

readability.reset();
MyProfiler.reset();
readability.debugging = options.debug;

MyProfiler.enabled = options.profile;
if (options.profile) {
MyProfiler.reset();
}

readability.init();

MyProfiler.report();
if (options.profile) MyProfiler.report();

removeReadabilityArtifacts();
removeClassNames();
if (options.removeReadabilityArtifacts) removeReadabilityArtifacts();
if (options.removeClassNames) removeClassNames();

//dbg('[Readability] done');
cb(document.body.innerHTML);
Expand All @@ -2183,7 +2212,20 @@ try {
HTML5 = require('html5');
} catch(e) {}

exports.parse = function parse(theHtml, url, callback) {
exports.parse = function parse(theHtml, url, options, callback) {
// backward compatibility: readability.parse(html, url, callback)
if (typeof options == 'function') {
callback = options;
options = {};
}
var defaultOptions = {
profile: false,
debug: false,
removeReadabilityArtifacts: true,
removeClassNames: true
};
options = Utils.extend({}, defaultOptions, options);

var startTime = new Date().getTime();
//dbg(html);
var html = theHtml.replace(/<script[^>]*>([\s\S]*?)<\/script>/gi, '');
Expand Down Expand Up @@ -2242,7 +2284,7 @@ exports.parse = function parse(theHtml, url, callback) {
var win = doc.parentWindow;
win = win || doc.createWindow(); //for backward compatibility with jsdom <= 0.1.20

start(win, function(html) {
start(win, options, function(html) {
//dbg(html);
var time = new Date().getTime() - startTime;
callback({title: document.title, content: html, time: time / 1000, inputLength: theHtml.length});
Expand Down
2 changes: 1 addition & 1 deletion test/clean-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var readability = require('../lib/readability.js'),

function cleanFile(path, url, cb) {
var content = fs.readFileSync(path, 'utf-8');
readability.parse(content, url, cb);
readability.parse(content, url, {removeReadabilityArtifacts: false, removeClassNames: false, debug: true, profile: 1}, cb);
}
if (1) {
cleanFile(__dirname + '/weird-pages/w3c-css-no-closing-head.html', '', function(info) {
Expand Down
2 changes: 1 addition & 1 deletion test/clean-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ http.createServer(function(req, res) {
// });
// clean.clean();

readability2.parse(result.body, '', function(info) {
readability2.parse(result.body, '', {removeReadabilityArtifacts: false, removeClassNames: false, debug: true, profile: true}, function(info) {
res.write(CleanReading.prototype.wrapContent(info.title, info.content));
res.end();
});
Expand Down

0 comments on commit 719da2e

Please sign in to comment.