Permalink
Browse files

Fully functional

  • Loading branch information...
1 parent 6ca202c commit 837e45214126dfd778e4a44231d2c7ee34963990 @rumpl committed Mar 20, 2013
Showing with 46 additions and 10 deletions.
  1. +3 −0 bin/md-read
  2. +43 −10 index.js
View
@@ -0,0 +1,3 @@
+#!/usr/bin/env node
+
+require('../');
View
@@ -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.