Skip to content

Commit

Permalink
Fixed dbg, moved logging over to use dbg, made debugging optional
Browse files Browse the repository at this point in the history
  • Loading branch information
chetan51 committed Dec 20, 2010
1 parent 1e48479 commit f734aac
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions lib/readability.js
Expand Up @@ -2,7 +2,9 @@
/*global window: false, readConvertLinksToFootnotes: false, readStyle: false, readSize: false, readMargin: false, Typekit: false, ActiveXObject: false */

var dbg = (typeof console !== 'undefined') ? function(s) {
console.log("Readability: " + s);
if (readability.debugging) {
console.log("Readability: " + s);
}
} : function() {};

/*
Expand All @@ -17,6 +19,7 @@ var dbg = (typeof console !== 'undefined') ? function(s) {
**/
var readability = {
version: '1.7.1',
debugging: true,
emailSrc: 'http://lab.arc90.com/experiments/readability/email.php',
iframeLoads: 0,
convertLinksToFootnotes: false,
Expand Down Expand Up @@ -1930,7 +1933,7 @@ var jsdom = require('jsdom'),
mod_sprintf = require('./sprintf'),
sprintf = mod_sprintf.sprintf;

//LiveTagWalker(document.body, '*', function(n) {console.log(n.tagName + n.id + n.className);}),0;
//LiveTagWalker(document.body, '*', function(n) {dbg(n.tagName + n.id + n.className);}),0;

(function() {
var R = readability;
Expand Down Expand Up @@ -2045,7 +2048,7 @@ var MyProfiler = {
timed: function(func, name, options) {
options = options || {};
var z = this;
//console.log('begin ' + name);
//dbg('begin ' + name);
z.timed_level++;
name = name || func.name;
if (!z.stats[name]) z.stats[name] = 0;
Expand All @@ -2056,7 +2059,7 @@ var MyProfiler = {
st.time += ellapsed;
st.count++;
if (!options.silent)
console.log(new Array(z.timed_level).join(' ') + ellapsed / 1000 + ' seconds [' + name + '] ' + (options.moreInfo || ''));
dbg(new Array(z.timed_level).join(' ') + ellapsed / 1000 + ' seconds [' + name + '] ' + (options.moreInfo || ''));
z.timed_level--;
return ret;
},
Expand All @@ -2071,11 +2074,11 @@ var MyProfiler = {
},

report: function() {
console.log('Profiling summary ==========================');
dbg('Profiling summary ==========================');
var stats = this.stats;
for (var name in stats) {
var st = stats[name];
console.log(sprintf("%5d\t%7.3f\t%s", st.count, st.time / 1000, name));
dbg(sprintf("%5d\t%7.3f\t%s", st.count, st.time / 1000, name));
};
},

Expand Down Expand Up @@ -2114,8 +2117,6 @@ function timed() {

})();

dbg = function(){};

function start(w, cb) {
window = w;
document = w.document;
Expand All @@ -2133,7 +2134,7 @@ function start(w, cb) {

MyProfiler.report();

//console.log('[Readability] done');
//dbg('[Readability] done');
cb(document.body.innerHTML);
}

Expand All @@ -2144,7 +2145,7 @@ try {

exports.parse = function parse(theHtml, url, callback) {
var startTime = new Date().getTime();
//console.log(html);
//dbg(html);
var html = theHtml.replace(/<script[^>]*>([\s\S]*?)<\/script>/gi, '');
// Turn all double br's into p's. Advanced from prepDocument to here
// saves > 1 seconds for large pages.
Expand Down Expand Up @@ -2175,13 +2176,13 @@ exports.parse = function parse(theHtml, url, callback) {
var doc = createDocWithHTMLParser();

if (!doc.body) {
console.log('empty body');
dbg('empty body');

// HTMLParser is not forgiving enough for pages without closing head tag
// https://github.com/tautologistics/node-htmlparser/issues#issue/12
// Use HTML5 parser to fix
if (HTML5) {
console.log('retrying with HTML5.Parser');
dbg('retrying with HTML5.Parser');
doc = createDocWithHTML5();
if (doc.body) {
// recreate the doc with HTMLParser because HTML5 throws exception when running readability
Expand All @@ -2192,23 +2193,23 @@ exports.parse = function parse(theHtml, url, callback) {
}

if (!doc.body) {
console.log('doc.body is still null.');
dbg('doc.body is still null.');
return callback({title: '', content: '', error: true});
}

console.log('---DOM created');
dbg('---DOM created');

var win = doc.parentWindow;
win = win || doc.createWindow(); //for backward compatibility with jsdom <= 0.1.20

start(win, function(html) {
//console.log(html);
//dbg(html);
var time = new Date().getTime() - startTime;
callback({title: document.title, content: html, time: time / 1000, inputLength: theHtml.length});
});
} catch(e) {
//throw e;
console.log('Error', e.message, e.stack);
dbg('Error', e.message, e.stack);
callback({title: '', content: '', error: true});
}
};
Expand Down

0 comments on commit f734aac

Please sign in to comment.