diff --git a/src/cli.js b/src/cli.js index fc05a00..a8b0bce 100755 --- a/src/cli.js +++ b/src/cli.js @@ -15,16 +15,21 @@ if(argv.h){ return; } -var callback = function(err, article){ - if(err) +var callback = function(err, article, meta){ + if(err){ console.error(err); - process.stdout.write(article.html); + process.exit(-1); + } + if(!article.content){ + process.exit(-2); + } + process.stdout.write(article.content); } if(typeof argv.url === 'string'){ read(argv.url, callback); } else { - var html; + var html = ""; process.stdin.on("data", function(chunk){ html += chunk; }); diff --git a/src/readability.js b/src/readability.js index 4096250..63cf001 100644 --- a/src/readability.js +++ b/src/readability.js @@ -27,7 +27,7 @@ function Readability(window, options) { }; this.__defineGetter__('content', function() { - return this.getContent(true); + return this.getContent(this, true); }); this.__defineGetter__('title', function() { return this.getTitle(true); @@ -48,7 +48,7 @@ Readability.prototype.close = function() { this._document = null; }; -Readability.prototype.getContent = function(notDeprecated) { +Readability.prototype.getContent = function(Readability, notDeprecated) { if (!notDeprecated) { console.warn('The method `getContent()` is deprecated, using `content` property instead.'); } @@ -65,6 +65,13 @@ Readability.prototype.getContent = function(notDeprecated) { } } + // Add a h1 tag with the title when the articleContent doesn't contain a h1. + if(articleContent.getElementsByTagName("h1").length === 0){ + var h1 = this._document.createElement("h1"); + h1.innerHTML = Readability.title; + articleContent.insertBefore(h1, articleContent.childNodes[0]); + } + return this.cache['article-content'] = articleContent.innerHTML; };