Skip to content
This repository has been archived by the owner on Apr 24, 2024. It is now read-only.

Commit

Permalink
Merge 1e5b338 into 3dd4a35
Browse files Browse the repository at this point in the history
  • Loading branch information
hcodes committed Mar 24, 2020
2 parents 3dd4a35 + 1e5b338 commit b060c54
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 22 deletions.
28 changes: 15 additions & 13 deletions lib/ignore.js
Expand Up @@ -51,10 +51,12 @@ module.exports = {
['<!\\[CDATA\\[', '\\]\\]>']
];

comments.forEach(function(tag) {
const re = new RegExp(tag[0] + '[^]*?' + tag[1], 'gi');
text = text.replace(re, ' ');
});
for (const tag of comments) {
text = text.replace(
new RegExp(tag[0] + '[^]*?' + tag[1], 'gi'),
' '
);
}

return text;
},
Expand All @@ -66,15 +68,15 @@ module.exports = {
* @returns {text}
*/
tags(text, tags) {
const bufTags = [];
tags.forEach(function(tag) {
bufTags.push(['<' + tag + '(\\s[^>]*?)?>', '</' + tag + '>']);
}, this);
for (const name of tags) {
const openingTags = '<' + name + '(\\s[^>]*?)?>';
const closingTags = '</' + name + '>';

bufTags.forEach(function(tag) {
const re = new RegExp(tag[0] + '[^]*?' + tag[1], 'gi');
text = text.replace(re, ' ');
});
text = text.replace(
new RegExp(openingTags + '[^]*?' + closingTags, 'gi'),
' … ' // For repeated words. Example: the `code` the.
);
}

return text;
},
Expand All @@ -92,7 +94,7 @@ module.exports = {
data = [data];
}

Array.isArray(data) && data.forEach(function(re) {
Array.isArray(data) && data.forEach(re => {
try {
if (typeof re === 'string') {
result.push(new RegExp(re, 'g'));
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion lib/report.js → lib/report/index.js
Expand Up @@ -17,7 +17,7 @@ const buffer = [];
module.exports = {
addReports(names) {
names.forEach(function(name) {
var moduleName = pth.extname(name) === '.js' ? name : './report/' + name;
const moduleName = pth.extname(name) === '.js' ? name : './report/' + name;
if (reportNames[moduleName]) {
return;
}
Expand Down
18 changes: 12 additions & 6 deletions lib/yaspeller.js
Expand Up @@ -3,7 +3,7 @@
const async = require('async');
const entities = require('entities');
const fs = require('fs');
const eyo = require('./eyo');
const eyo = require('./plugin/eyo');
const formatModule = require('./format');
const ignore = require('./ignore');
const isutf8 = require('isutf8');
Expand Down Expand Up @@ -40,6 +40,8 @@ function stripTags(html) {
function checkText(originalText, callback, settings) {
let text = originalText;

printDebug(`Original text: ${originalText}`);

const apiSettings = Object.assign({}, settings);
const format = formatModule.getFormat(text, apiSettings);
const lang = apiSettings.lang || 'en,ru';
Expand Down Expand Up @@ -70,6 +72,7 @@ function checkText(originalText, callback, settings) {
}

text = prepareText(text, format);
printDebug(`Prepared text for API: ${text}`);

const tasks = [];
const texts = splitText(text);
Expand All @@ -83,7 +86,7 @@ function checkText(originalText, callback, settings) {
apiFormat: apiSettings.format,
lang: apiSettings.lang,
options: apiSettings.options,
text: el.substring(0, 128)
text: el.substring(0, 128),
});

tasks.push(function(cb) {
Expand All @@ -100,6 +103,9 @@ function checkText(originalText, callback, settings) {
async.parallelLimit(tasks, getMaxRequest(apiSettings), function(err, data) {
const buf = mergeResults(data);

printDebug('Yandex.Speller API response:');
printDebug(JSON.stringify(buf, null, ' '));

if (!buf.err && apiSettings.checkYo) {
checkYo(text, buf.data);
}
Expand Down Expand Up @@ -189,13 +195,13 @@ function checkFile(file, callback, settings) {
settings = settings || {};
settings.extname = pth.extname(file);

printDebug('get: ' + file);
printDebug('Get file: ' + file);

if (fs.existsSync(file)) {
if (fs.statSync(file).isFile()) {
const buf = fs.readFileSync(file);
if (isutf8(buf)) {
printDebug('post text -> Yandex.Speller API: ' + file);
printDebug('Post text Yandex.Speller API: ' + file);

const startTime = Date.now();
checkText(buf.toString(), function(err, data, originalText) {
Expand Down Expand Up @@ -227,7 +233,7 @@ function checkUrl(url, callback, settings) {
settings = settings || {};
settings.extname = pth.extname(url);

printDebug('get: ' + url);
printDebug('Get url: ' + url);

fetch(url)
.then(response => {
Expand Down Expand Up @@ -265,7 +271,7 @@ function checkSitemap(url, commonCallback, settings, callback) {

const results = [];

printDebug('get: ' + url);
printDebug('Get sitemap: ' + url);

fetch(url)
.then(res => {
Expand Down
17 changes: 15 additions & 2 deletions test/test.api.js
Expand Up @@ -18,7 +18,20 @@ describe('API', function() {
assert.equal(err, false);
assert.equal(data.data.length, 2);
done();
}, {lang: 'ru', format: 'plain'});
}, {lang: 'ru', format: 'plain', options: { findRepeatWords: true }});
});

it('checkFile markdown', function(done) {
yaspeller.checkFile('./test/texts/repeat_words.md', function(err, data) {
debug.setDebug(false);
assert.equal(err, false);
assert.equal(data.data.length, 0);
done();
}, {
lang: 'ru',
ignoreTags: ['code'],
options: { findRepeatWords: true }
});
});

it('checkFile with window 1251', function(done) {
Expand Down Expand Up @@ -111,7 +124,7 @@ describe('API', function() {
assert.equal(err, false);
assert.equal(data.length, 2);
done();
}, {lang: 'ru', format: 'plain'});
}, {lang: 'ru', format: 'plain', options: { findRepeatWords: true }});
});

it('checkText > 10000 bytes', function(done) {
Expand Down
1 change: 1 addition & 0 deletions test/texts/repeat_words.md
@@ -0,0 +1 @@
the `code` the

0 comments on commit b060c54

Please sign in to comment.