Skip to content

Commit

Permalink
- Settings file introduced
Browse files Browse the repository at this point in the history
- Main module separated from everything else
- "show correct paragraphs" option removed
- custom dictionary removed
  • Loading branch information
kdzwinel committed Jan 11, 2015
1 parent 495be84 commit 9354af5
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 148 deletions.
70 changes: 70 additions & 0 deletions bin/cmd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env node

//Helpers
var fs = require('fs');
var request = require('request');
var program = require('commander');
var mime = require('mime');
var marked = require('marked');
var Promise = require('promise');
var config = require('../settings.json');
var Proofreader = require('../lib/proofreader.js');

program
.option('-u, --url [url]', 'URL to website that should be proofread.')
.option('-f, --file [path]', 'Path to HTML file that should be proofread.')
.option('-l, --file-list [path]', 'Path to a list of files that should be proofread.')
.option('-c, --config-file', 'Path to a configuration file')
.parse(process.argv);

if(program.configFile) {
config = require(program.configFile);
}

var proofreader = new Proofreader(config);

function toHTML(path, content) {
var mimeType = mime.lookup(path);

if(mimeType === 'text/x-markdown') {
return marked(content);
}

return content;
}

var resultPromise = null;

if(program.url) {
request({uri: program.url}, function (err, response, body) {
if (err) {
throw err;
}

resultPromise = proofreader.proofread(toHTML(program.url, body));
});
} else if(program.file) {
var content = fs.readFileSync(program.file).toString();

resultPromise = proofreader.proofread(toHTML(program.file, content));
} else if(program.fileList) {
var listOfFiles = fs.readFileSync(program.fileList).toString().split("\n");
var promises = [];

listOfFiles.forEach(function(filePath) {
if(filePath) {
var content = fs.readFileSync(filePath).toString();
var promise = proofreader.proofread(toHTML(filePath, content));

promises.push(promise);
}
});

resultPromise = Promise.all(promises);
}

if(resultPromise) {
resultPromise.then(null, function() {
process.exit(1);
})
}
146 changes: 0 additions & 146 deletions bin/proofreader.js.sh

This file was deleted.

108 changes: 108 additions & 0 deletions lib/proofreader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
var path = require('path');
var clc = require('cli-color');
var Sync = require('sync');
var cheerio = require('cheerio');
var Promise = require('promise');
var fs = require('fs');
var writeGood = require('write-good');
var nodehun = require('nodehun');
var spellcheck = require('nodehun-sentences');

module.exports = Proofreader;

function Proofreader(settings) {
if(!settings) {
throw new Error('Settings object missing.');
} else if(!settings.dictionaries['build-in'] || !settings.dictionaries['build-in'].length) {
throw new Error('At least one build-in dictionary has to be set.');
} else if(!settings.selectors || !settings.selectors.whitelist) {
throw new Error('Whitelist has to be set.');
}

this._settings = settings;
this._setupDictionaries(settings.dictionaries);
}

Proofreader.prototype._setupDictionaries = function(dictionaries) {
var mainDictName = dictionaries['build-in'][0];
dictionaries['build-in'].shift();

//main dictionary
var dict = new nodehun(
fs.readFileSync(path.join(__dirname, '../dictionaries/' + mainDictName + '.aff')),
fs.readFileSync(path.join(__dirname, '../dictionaries/' + mainDictName + '.dic'))
);

//other build-in dictionaries
dictionaries['build-in'].forEach(function(dictName) {
dict.addDictionary(path.join(__dirname, '../dictionaries/' + dictName + '.dic'));
});

//other custom dictionaries
if(dictionaries['custom']) {
dictionaries['custom'].forEach(function(dictPath) {
dict.addDictionary(fs.readFileSync(dictPath));
});
}

this._dictionary = dict;
};

Proofreader.prototype.proofread = function(html) {
var $ = cheerio.load(html);
var dictionary = this._dictionary;
var whitelist = this._settings.selectors.whitelist;
var blacklist = this._settings.selectors.blacklist;

return new Promise(function(resolve, reject) {
Sync(function () {
var suggestionsCount = 0;

//Blacklisted elements are removed before text is processed
if(blacklist) {
$(blacklist).remove();
}

//Only whitelisted elements are processed
$(whitelist).each(function () {
var text = $(this).text();

//remove linebreaks from text
text = text.replace(/(\r\n|\n|\r)+/gm," ");

//replace ’ with '
text = text.replace(//g, "'");

if(text.trim().length) {
var writeGoodSuggestions = writeGood(text);
var spellingSuggestions = spellcheck.sync(null, dictionary, text);

//Printing output
if(writeGoodSuggestions.length || spellingSuggestions.length) {
console.log(clc.red(text));

writeGoodSuggestions.forEach(function(item) {
console.log(clc.blue.bold(' - ' + item.reason));
});

spellingSuggestions.forEach(function(item) {
console.log(clc.magenta.bold(' - "' + item.word + '" -> ' + item.suggestions));
});

console.log();
}

suggestionsCount += writeGoodSuggestions.length + spellingSuggestions.length;
}
});

return suggestionsCount;
}, function (err, result) {
if(err || result !== 0) {
reject();
} else {
resolve();
}
});
});
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "proofreader",
"description": "Simple text proofreader based on 'write-good' (hemingway-app-like suggestions) and 'nodehun' (spelling).",
"version": "0.4.0",
"version": "0.5.0",
"repository": {
"type": "git",
"url": "git://github.com/kdzwinel/Proofreader.git"
Expand All @@ -16,7 +16,7 @@
}
],
"bin": {
"proofreader": "./bin/proofreader.js.sh"
"proofreader": "./bin/cmd.js"
},
"dependencies": {
"cheerio": "^0.18.0",
Expand Down
10 changes: 10 additions & 0 deletions settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"dictionaries": {
"build-in": ["en_US"],
"custom": []
},
"selectors": {
"whitelist": "p, li, h1, h2, h3, h4, th, td, dl, figcaption",
"blacklist": "pre, code"
}
}

0 comments on commit 9354af5

Please sign in to comment.