Skip to content

Commit

Permalink
Update most of libraries. Get rid of Sync and use native promises.
Browse files Browse the repository at this point in the history
  • Loading branch information
kdzwinel committed Apr 6, 2018
1 parent b383f1b commit 1ba8bdb
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 168 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Editors
.idea
launch.json

# Logs
logs
Expand Down
6 changes: 4 additions & 2 deletions bin/cmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ var program = require('commander');
var mime = require('mime');
var marked = require('marked');
var clc = require('cli-color');
var Promise = require('promise');
var config = require('../settings.json');
var Proofreader = require('../lib/proofreader.js');
var SourceLoader = require('../lib/sourceloader.js');
Expand Down Expand Up @@ -51,7 +50,7 @@ if (config.dictionaries['custom']) {
}

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

if (mimeType === 'text/markdown') {
return marked(content);
Expand Down Expand Up @@ -115,6 +114,9 @@ sourceLoader
.then(function (result) {
printResults(source.path, result);
return result;
})
.catch(function (error) {
console.error('Proofreading failed', error);
});
}));
})
Expand Down
73 changes: 35 additions & 38 deletions lib/proofreader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
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');
Expand Down Expand Up @@ -39,7 +37,7 @@ Proofreader.prototype.addDictionary = function (dicFilePath, affFilePath) {
* @param {String} value
*/
Proofreader.prototype.setWhitelist = function (value) {
if(typeof value !== 'string') {
if (typeof value !== 'string') {
throw new Error('Whitelist must be a string.');
}
this._whitelist = value;
Expand All @@ -50,7 +48,7 @@ Proofreader.prototype.setWhitelist = function (value) {
* @param {String} value
*/
Proofreader.prototype.setBlacklist = function (value) {
if(typeof value !== 'string') {
if (typeof value !== 'string') {
throw new Error('Blacklist must be a string.');
}
this._blacklist = value;
Expand All @@ -62,7 +60,7 @@ Proofreader.prototype.setBlacklist = function (value) {
* @param {Object} settings
*/
Proofreader.prototype.setWriteGoodSettings = function (settings) {
if(settings !== undefined && typeof settings !== 'object') {
if (settings !== undefined && typeof settings !== 'object') {
throw new Error('Blacklist must be a string.');
}
this._writeGoodSettings = settings;
Expand All @@ -80,52 +78,51 @@ Proofreader.prototype.proofread = function (html) {
var blacklist = this._blacklist;
var writeGoodSettings = this._writeGoodSettings;

return new Promise(function(resolve, reject) {
Sync(function () {
var suggestions = [];
return new Promise(function (resolve, reject) {
var suggestions = [];
var promises = [];

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

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

//Only whitelisted elements are processed
$(whitelist).each(function () {
var text = $(this).text();
//remove linebreaks from text
text = text.replace(/(\r\n|\n|\r)+/gm, " ");

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

//replace with '
text = text.replace(//g, "'");
//replace multiple spaces with a single one
text = text.replace(/\s{2,}/g, ' ');

//replace multiple spaces with a single one
text = text.replace(/\s{2,}/g, ' ');
//trim text
text = text.trim();

//trim text
text = text.trim();
if (text.length) {
var writeGoodSuggestions = writeGood(text, writeGoodSettings);

if(text.length) {
var writeGoodSuggestions = writeGood(text, writeGoodSettings);
var spellcheckerSuggestions = dictionary ? spellcheck.sync(null, dictionary, text) : [];
var spellcheckResolve = null;
var promise = new Promise(function (r) { spellcheckResolve = r; });
promises.push(promise);

spellcheck(dictionary, text, function (error, spellcheckerSuggestions) {
suggestions.push({
text: text,
suggestions: {
writeGood: writeGoodSuggestions,
spelling: spellcheckerSuggestions
spelling: spellcheckerSuggestions || []
}
})
}
});

return suggestions;
}, function (err, result) {
if(err) {
reject(err);
} else {
resolve(result);
});
spellcheckResolve();
});
}
});

Promise.all(promises).then(function() {resolve(suggestions);})
});
};
};
3 changes: 1 addition & 2 deletions lib/sourceloader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
var fs = require('fs');
var request = require('request');
var Promise = require('promise');

module.exports = SourceLoader;

Expand Down Expand Up @@ -62,4 +61,4 @@ SourceLoader.prototype.add = function (source) {
*/
SourceLoader.prototype.load = function () {
return Promise.all((this._sources).map(getSource));
};
};
Loading

0 comments on commit 1ba8bdb

Please sign in to comment.