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

Commit

Permalink
Ability to ignore the text using regular expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
hcodes committed Jul 30, 2016
1 parent 10dfec7 commit 27c7129
Show file tree
Hide file tree
Showing 21 changed files with 136 additions and 2 deletions.
1 change: 1 addition & 0 deletions .yaspellerrc
Expand Up @@ -30,6 +30,7 @@
"svg",
"Seleznev",
"sitemap",
"stdin",
"sublicense",
"yaspeller",
"yo"
Expand Down
8 changes: 8 additions & 0 deletions README.md
Expand Up @@ -93,6 +93,9 @@ Ignore HTML tags.<br/>
Default: `code,kbd,object,samp,script,style,var`<br/>
Option to formats `html` and` markdown`.

#### `--ignore-text <regexp>`
Removes the text from the scan using regular expressions.

#### `--ignore-capitalization`
Ignore the incorrect use of UPPERCASE / lowercase letters, for example, in the word `moscow`.

Expand Down Expand Up @@ -178,6 +181,10 @@ Yaspeller is configured using `.yaspellerrc` JSON file at the root of the projec
"some(w|W)ord[23]", // some(w|W)ord[23] = some(w|W)ord[23] and Some(w|W)ord[23]
"Some(w|W)ord" // Some(w|W)ord = Some(w|W)ord
],
"ignoreText": [
"<php\?[^]*?\?>", // Shortly
["<php\?[^]*?\?>", "g"] // Longly
],
"ignoreTags": ["code", "script"],
"ignoreUrls": true,
"findRepeatWords": true,
Expand All @@ -198,6 +205,7 @@ Yaspeller is configured using `.yaspellerrc` JSON file at the root of the projec
| `findRepeatWords` | `Boolean` | [`--find-repeat-words`](#--find-repeat-words) |
| `flagLatin` | `Boolean` | [`--flag-latin`](#--flag-latin) |
| `ignoreTags` | `Array` | [`--ignore-tags`](#--ignore-tags-tags) |
| `ignoreText` | `Array` | [`--ignore-text`](#--ignore-text-regexp) |
| `ignoreCapitalization` | `Boolean` | [`--ignore-capitalization`](#--ignore-capitalization) |
| `ignoreDigits` | `Boolean` | [`--ignore-digits`](#--ignore-digits) |
| `ignoreLatin` | `Boolean` | [`--ignore-latin`](#--ignore-latin) |
Expand Down
8 changes: 8 additions & 0 deletions README.ru.md
Expand Up @@ -88,6 +88,9 @@ JSON-褎邪泄谢 褋芯斜褋褌胁械薪薪芯谐芯 褋谢芯胁邪褉褟.
袩芯 褍屑芯谢褔邪薪懈褞: `code,kbd,object,samp,script,style,var`<br/>
袨锌褑懈褟 写谢褟 褎芯褉屑邪褌芯胁 `html``markdown`.

#### `--ignore-text <regexp>`
校写邪谢褟褌褜 褌械泻褋褌 懈蟹 锌褉芯胁械褉泻懈 褋 锌芯屑芯褖褜褞 褉械谐褍谢褟褉薪褘褏 胁褘褉邪卸械薪懈泄.

#### `--ignore-capitalization`
袠谐薪芯褉懈褉芯胁邪褌褜 薪械胁械褉薪芯械 褍锌芯褌褉械斜谢械薪懈械 袩袪袨袩袠小袧蝎啸/褋褌褉芯褔薪褘褏 斜褍泻胁, 薪邪锌褉懈屑械褉, 胁 褋谢芯胁械 `屑芯褋泻胁邪`.

Expand Down Expand Up @@ -174,6 +177,10 @@ JSON-褎邪泄谢 褋芯斜褋褌胁械薪薪芯谐芯 褋谢芯胁邪褉褟.
"Some(w|W)ord" // Some(w|W)ord = Some(w|W)ord
],
"ignoreTags": ["code", "script"],
"ignoreText": [
"<php\?[^]*?\?>", // 袣芯褉芯褌泻邪褟 蟹邪锌懈褋褜
["<php\?[^]*?\?>", "g"] // 袛谢懈薪薪邪褟 蟹邪锌懈褋褜
],
"ignoreUrls": true,
"findRepeatWords": true,
"maxRequests": 5
Expand All @@ -193,6 +200,7 @@ JSON-褎邪泄谢 褋芯斜褋褌胁械薪薪芯谐芯 褋谢芯胁邪褉褟.
| `findRepeatWords` | `Boolean` | [`--find-repeat-words`](#--find-repeat-words) |
| `flagLatin` | `Boolean` | [`--flag-latin`](#--flag-latin) |
| `ignoreTags` | `Array` | [`--ignore-tags`](#--ignore-tags-tags) |
| `ignoreText` | `Array` | [`--ignore-text`](#--ignore-text-regexp) |
| `ignoreCapitalization` | `Boolean` | [`--ignore-capitalization`](#--ignore-capitalization) |
| `ignoreDigits` | `Boolean` | [`--ignore-digits`](#--ignore-digits) |
| `ignoreLatin` | `Boolean` | [`--ignore-latin`](#--ignore-latin) |
Expand Down
13 changes: 12 additions & 1 deletion lib/cli.js
Expand Up @@ -9,6 +9,7 @@ var async = require('async'),
report = require('./report'),
tasks = require('./tasks'),
utils = require('./utils'),
ignore = require('./ignore'),
_ = require('lodash'),
isTTY = process.stdin.isTTY,
json,
Expand All @@ -32,10 +33,20 @@ var settings = {
options: json.options || {}
};

['checkYo', 'fileExtensions', 'format', 'ignoreTags', 'lang', 'maxRequests'].forEach(function(key) {
[
'checkYo',
'fileExtensions',
'format',
'ignoreTags',
'ignoreText',
'lang',
'maxRequests'
].forEach(function(key) {
settings[key] = program[key] || json[key];
});

settings.ignoreText = ignore.prepareRegExpToIgnoreText(settings.ignoreText);

programOptions.apiOptions.forEach(function(el) {
var key = el[0];
if(program[key]) {
Expand Down
2 changes: 2 additions & 0 deletions lib/debug.js
@@ -1,3 +1,5 @@
'use strict';

var chalk = require('chalk'),
_ = require('lodash'),
isDebug = false;
Expand Down
2 changes: 2 additions & 0 deletions lib/dictionary.js
@@ -1,3 +1,5 @@
'use strict';

var chalk = require('chalk'),
_ = require('lodash'),
debug = require('./debug'),
Expand Down
2 changes: 2 additions & 0 deletions lib/exit-codes.js
@@ -1,3 +1,5 @@
'use strict';

module.exports = {
HAS_TYPOS: 1,
ERROR_LOADING: 2,
Expand Down
2 changes: 2 additions & 0 deletions lib/format.js
@@ -1,3 +1,5 @@
'use strict';

module.exports = {
/**
* Is HTML?
Expand Down
34 changes: 34 additions & 0 deletions lib/ignore.js
@@ -1,3 +1,7 @@
'use strict';

var chalk = require('chalk');

// jshint maxlen: 256
module.exports = {
/**
Expand Down Expand Up @@ -74,5 +78,35 @@ module.exports = {
});

return text;
},
/**
* Prepares regular expressions to remove text.
*
* @param {Array|undefined} data
*
* @return {Array}
*/
prepareRegExpToIgnoreText: function(data) {
var result = [];

if(typeof data === 'string') {
data = [data];
}

Array.isArray(data) && data.forEach(function(re) {
try {
if(typeof re === 'string') {
result.push(new RegExp(re, 'g'));
}

if(Array.isArray(re)) {
result.push(new RegExp(re[0], typeof re[1] === 'undefined' ? 'g' : re[1]));
}
} catch(e) {
console.error(chalk.red('Error in RegExp "' + re.toString() + '": ' + e));
}
});

return result;
}
};
3 changes: 3 additions & 0 deletions lib/options.js
@@ -1,3 +1,5 @@
'use strict';

/*jshint maxlen:1000 */
var program = require('commander'),
_ = require('lodash');
Expand Down Expand Up @@ -27,6 +29,7 @@ module.exports = {
.option('--ignore-tags <tags>', 'ignore tags. Default: "' +
prefs.defaultIgnoreTags +
'"', splitByCommas, null)
.option('--ignore-text <regexp>', 'ignore text using RegExp')
.option('--max-requests <number>', 'max count of requests at a time. Default: 2', parseInt, 0)
.option('--only-errors', 'output only errors')
.option('--debug', 'debug mode')
Expand Down
2 changes: 2 additions & 0 deletions lib/report.js
@@ -1,3 +1,5 @@
'use strict';

var chalk = require('chalk'),
program = require('commander'),
pth = require('path'),
Expand Down
2 changes: 2 additions & 0 deletions lib/report/console.js
@@ -1,3 +1,5 @@
'use strict';

var chalk = require('chalk'),
program = require('commander'),
utils = require('../utils'),
Expand Down
2 changes: 2 additions & 0 deletions lib/report/error_dictionary.js
@@ -1,3 +1,5 @@
'use strict';

var fs = require('fs'),
chalk = require('chalk'),
_ = require('lodash');
Expand Down
2 changes: 2 additions & 0 deletions lib/report/example.js
@@ -1,3 +1,5 @@
'use strict';

module.exports = {
oneach: function(err, data) {
console.log('oneach: ', err, data);
Expand Down
2 changes: 2 additions & 0 deletions lib/report/html.js
@@ -1,3 +1,5 @@
'use strict';

/*jshint maxlen:1000 */
var fs = require('fs'),
pth = require('path'),
Expand Down
2 changes: 2 additions & 0 deletions lib/report/html/template.js
@@ -1,3 +1,5 @@
'use strict';

var App = {
init: function() {
this.events();
Expand Down
2 changes: 2 additions & 0 deletions lib/report/json.js
@@ -1,3 +1,5 @@
'use strict';

var fs = require('fs'),
chalk = require('chalk');

Expand Down
4 changes: 3 additions & 1 deletion lib/report/markdown.js
@@ -1,11 +1,13 @@
'use strict';

var fs = require('fs'),
pth = require('path'),
chalk = require('chalk'),
utils = require('../utils'),
yaspeller = require('../yaspeller'),
_ = require('lodash'),
buffer = [];

function prepareResource(resource) {
var relativeFile = resource;
if(!utils.isUrl(resource)) {
Expand Down
2 changes: 2 additions & 0 deletions lib/utils.js
@@ -1,3 +1,5 @@
'use strict';

var chalk = require('chalk'),
fs = require('fs'),
isutf8 = require('isutf8'),
Expand Down
7 changes: 7 additions & 0 deletions lib/yaspeller.js
@@ -1,3 +1,5 @@
'use strict';

/* jshint maxlen: 300 */
var async = require('async'),
entities = require('entities'),
Expand Down Expand Up @@ -42,6 +44,7 @@ function stripTags(html) {
* @param {Object} [settings]
* @param {string} [settings.format] Text format: plain or html.
* @param {string|Array} [settings.lang] Language: en, ru or uk.
* @param {Array<RegExp>} [settings.ignoreText]
* @param {Object} [settings.options]
*/
function checkText(text, callback, settings) {
Expand All @@ -51,6 +54,10 @@ function checkText(text, callback, settings) {

apiSettings.lang = Array.isArray(lang) ? lang.join(',') : lang;

apiSettings.ignoreText && apiSettings.ignoreText.forEach(function(re) {
text = text.replace(re, '');
});

if(ignore.hasIgnoredText(text)) {
text = ignore.lines(text);
text = ignore.blocks(text);
Expand Down
36 changes: 36 additions & 0 deletions test/test.ignore.js
@@ -1,5 +1,6 @@
// jshint maxlen:1024
var yaspeller = require('../lib/yaspeller'),
ignore = require('../lib/ignore'),
assert = require('chai').assert;

describe('Ignore text', function() {
Expand Down Expand Up @@ -56,4 +57,39 @@ describe('Ignore text', function() {
done();
}, {lang: 'ru'});
});

it('with regExp, long', function(done) {
var text = 'Moscaw1\n<ignore>Moscaw2</ignore>\n<ignore>Moscaw3</ignore>\nMoscaw4';
yaspeller.checkText(text, function(err, data) {
assert.equal(data.length, 2);
done();
}, {
lang: 'en',
ignoreText: ignore.prepareRegExpToIgnoreText([
['<ignore>[^]*?</ignore>', 'gi']
])
});
});

it('with regExp, short1', function(done) {
var text = 'Moscaw1\n<ignore>Moscaw2</ignore>\n<ignore>Moscaw3</ignore>\nMoscaw4';
yaspeller.checkText(text, function(err, data) {
assert.equal(data.length, 2);
done();
}, {
lang: 'en',
ignoreText: ignore.prepareRegExpToIgnoreText(['<ignore>[^]*?</ignore>'])
});
});

it('with regExp, short2', function(done) {
var text = 'Moscaw1\n<ignore>Moscaw2</ignore>\n<ignore>Moscaw3</ignore>\nMoscaw4';
yaspeller.checkText(text, function(err, data) {
assert.equal(data.length, 2);
done();
}, {
lang: 'en',
ignoreText: ignore.prepareRegExpToIgnoreText('<ignore>[^]*?</ignore>')
});
});
});

0 comments on commit 27c7129

Please sign in to comment.