1 | 'use strict';
|
2 |
|
3 | const chalk = require('chalk');
|
4 |
|
5 | module.exports = {
|
6 | |
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | hasIgnoredText(text) {
|
13 | return text.search(/yaspeller\s+ignore/) !== -1;
|
14 | },
|
15 | |
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | lines(text) {
|
22 | return text
|
23 | .replace(/^.*?\/\/\s*yaspeller\s+ignore\s*$/mg, '')
|
24 | .replace(/^.*?<!--\s*yaspeller\s+ignore\s*-->.*?$/mg, '')
|
25 | .replace(/^.*?\/\*\s*yaspeller\s+ignore\s*\*\/.*?$/mg, '');
|
26 | },
|
27 | |
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | blocks(text) {
|
34 | return text
|
35 | .replace(/\/\*\s*yaspeller\s+ignore:start\s*\*\/[^]*?\/\*\s*yaspeller\s+ignore:end\s*\*\//g, '')
|
36 | .replace(/<!--\s*yaspeller\s+ignore:start\s*-->[^]*?<!--\s*yaspeller\s+ignore:end\s*-->/g, '')
|
37 | .replace(/\/\/\s*yaspeller\s+ignore:start[^]*?\/\/\s*yaspeller\s+ignore:end.*?(\r?\n|$)/g, '');
|
38 | },
|
39 | |
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | comments(text) {
|
46 | const comments = [
|
47 | ['<!--', '-->'],
|
48 | ['<!ENTITY', '>'],
|
49 | ['<!DOCTYPE', '>'],
|
50 | ['<\\?xml', '\\?>'],
|
51 | ['<!\\[CDATA\\[', '\\]\\]>']
|
52 | ];
|
53 |
|
54 | comments.forEach(function(tag) {
|
55 | const re = new RegExp(tag[0] + '[^]*?' + tag[1], 'gi');
|
56 | text = text.replace(re, ' ');
|
57 | });
|
58 |
|
59 | return text;
|
60 | },
|
61 | |
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 | tags(text, tags) {
|
69 | const bufTags = [];
|
70 | tags.forEach(function(tag) {
|
71 | bufTags.push(['<' + tag + '(\\s[^>]*?)?>', '</' + tag + '>']);
|
72 | }, this);
|
73 |
|
74 | bufTags.forEach(function(tag) {
|
75 | const re = new RegExp(tag[0] + '[^]*?' + tag[1], 'gi');
|
76 | text = text.replace(re, ' ');
|
77 | });
|
78 |
|
79 | return text;
|
80 | },
|
81 | |
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | prepareRegExpToIgnoreText(data) {
|
89 | const result = [];
|
90 |
|
91 | if (typeof data === 'string') {
|
92 | data = [data];
|
93 | }
|
94 |
|
95 | Array.isArray(data) && data.forEach(function(re) {
|
96 | try {
|
97 | if (typeof re === 'string') {
|
98 | result.push(new RegExp(re, 'g'));
|
99 | }
|
100 |
|
101 | if (Array.isArray(re)) {
|
102 | result.push(new RegExp(re[0], typeof re[1] === 'undefined' ? 'g' : re[1]));
|
103 | }
|
104 | } catch (e) {
|
105 | console.error(chalk.red('Error in RegExp "' + re.toString() + '": ' + e));
|
106 | }
|
107 | });
|
108 |
|
109 | return result;
|
110 | }
|
111 | };
|