-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Djordje Lukic
committed
Mar 20, 2013
1 parent
6ca202c
commit 837e452
Showing
2 changed files
with
46 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#!/usr/bin/env node | ||
|
||
require('../'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ''); | ||
} |