-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Main module separated from everything else - "show correct paragraphs" option removed - custom dictionary removed
- Loading branch information
Showing
5 changed files
with
190 additions
and
148 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,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); | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.
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,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(); | ||
} | ||
}); | ||
}); | ||
}; |
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
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,10 @@ | ||
{ | ||
"dictionaries": { | ||
"build-in": ["en_US"], | ||
"custom": [] | ||
}, | ||
"selectors": { | ||
"whitelist": "p, li, h1, h2, h3, h4, th, td, dl, figcaption", | ||
"blacklist": "pre, code" | ||
} | ||
} |