Skip to content

Commit

Permalink
Fully functional
Browse files Browse the repository at this point in the history
  • Loading branch information
Djordje Lukic committed Mar 20, 2013
1 parent 6ca202c commit 837e452
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
3 changes: 3 additions & 0 deletions bin/md-read
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('../');
53 changes: 43 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,50 @@
var fs = require('fs');
var readability = require('node-readability');
var path = require('path');
var md = require('html-md');
var readability = require('node-readability');
var options;

try {
options = require(path.join(process.env.HOME, '.md-read.json'));
} catch (e) {
console.warn('.md-read.json file not found, creatin one with directory: ', process.cwd())
options = { directory: process.cwd()};
fs.writeFile(path.join(process.env.HOME, '.md-read.json'), JSON.stringify(options));
}

if (process.argv.length < 3) {
console.error('Please give a URL');
} else {
for (var i = 2; i < process.argv.length; i++) {
read(process.argv[i]);
}
}

function read(url) {
readability.read(url, function (error, article) {
if (error) {
console.error(error);
} else {
writeArticle(article);
}
});
}

readability.read(process.argv[2], function(err, article) {
var title = article.getTitle();
var fileTitle = title.replace(/\ /g, '_') + '.md';
function writeArticle(article) {
var title = cleanName(article.getTitle());
var fileTitle = title.replace(/ /g, '_') + '.md';
var content = md(article.getContent());
fs.writeFile(fileTitle, content, function (error) {
if(err) {
console.log(err);

fs.writeFile(path.join(options.directory, fileTitle), content, function (error) {
if (error) {
console.log(error);
} else {
console.log("The file " + fileTitle + " was saved!");
console.log("The file " + fileTitle + " was saved!");
}
});
});
}

function cleanName(name) {
name = name.replace(/\s+/gi, '-');
return name.replace(/[^a-zA-Z0-9\-]/gi, '');
}

0 comments on commit 837e452

Please sign in to comment.