mm_matchs 1.3.0 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -2
- package/index.js +6 -449
- package/lib/admin.js +17 -0
- package/lib/matchs.js +351 -0
- package/lib/util.js +75 -0
- package/package.json +10 -4
- package/tool/match/index.js +77 -5
- package/tool/match/tool.json +29 -8
- package/tool/match_admin/index.js +41 -0
- package/tool/match_admin/tool.json +11 -0
- package/.gitattributes +0 -5
- package/eslint.config.js +0 -106
- package/example/index.js +0 -53
- package/html.js +0 -250
- package/htmlformat.js +0 -396
- package/lang.js +0 -5
- package/lib/tools/index.js +0 -16
- package/lib/tools/match/index.js +0 -47
- package/test.js +0 -216
- package/test_html.js +0 -2679
package/eslint.config.js
DELETED
|
@@ -1,106 +0,0 @@
|
|
|
1
|
-
const mmEslint = require('mm_eslint');
|
|
2
|
-
const jsdoc = require('eslint-plugin-jsdoc');
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* 基于个人规则的ESLint配置
|
|
6
|
-
* 符合最新ESLint Flat Config格式
|
|
7
|
-
*/
|
|
8
|
-
module.exports = [
|
|
9
|
-
{
|
|
10
|
-
name: 'mos/base',
|
|
11
|
-
files: ['**/*.js'],
|
|
12
|
-
ignores: [
|
|
13
|
-
'**/node_modules/**',
|
|
14
|
-
'eslint.config.js',
|
|
15
|
-
'tests/**',
|
|
16
|
-
'regex.js',
|
|
17
|
-
'test.js'
|
|
18
|
-
],
|
|
19
|
-
languageOptions: {
|
|
20
|
-
ecmaVersion: 'latest',
|
|
21
|
-
sourceType: 'module'
|
|
22
|
-
},
|
|
23
|
-
plugins: {
|
|
24
|
-
mm_eslint: {
|
|
25
|
-
rules: mmEslint
|
|
26
|
-
},
|
|
27
|
-
jsdoc: jsdoc
|
|
28
|
-
},
|
|
29
|
-
rules: {
|
|
30
|
-
'mm_eslint/class-name': 'error', // 类名命名规则
|
|
31
|
-
'mm_eslint/class-instance-name': 'error', // 类实例名命名规则
|
|
32
|
-
'mm_eslint/function-name': 'error', // 函数(方法)名命名规则
|
|
33
|
-
'mm_eslint/object-name': 'error', // 对象名命名规则
|
|
34
|
-
'mm_eslint/param-name': 'error', // 参数名命名规则
|
|
35
|
-
'mm_eslint/variable-name': 'error', // 变量名命名规则
|
|
36
|
-
'mm_eslint/constant-name': 'error', // 常量名命名规则
|
|
37
|
-
|
|
38
|
-
// 代码风格规则
|
|
39
|
-
'max-len': ['error', {
|
|
40
|
-
code: 100,
|
|
41
|
-
ignoreComments: true,
|
|
42
|
-
ignoreUrls: true,
|
|
43
|
-
ignoreStrings: true,
|
|
44
|
-
ignoreTemplateLiterals: true
|
|
45
|
-
}],
|
|
46
|
-
'indent': ['error', 2, {
|
|
47
|
-
SwitchCase: 1,
|
|
48
|
-
VariableDeclarator: 1,
|
|
49
|
-
outerIIFEBody: 1,
|
|
50
|
-
MemberExpression: 1,
|
|
51
|
-
FunctionDeclaration: { parameters: 1, body: 1 },
|
|
52
|
-
FunctionExpression: { parameters: 1, body: 1 },
|
|
53
|
-
CallExpression: { arguments: 1 },
|
|
54
|
-
ArrayExpression: 1,
|
|
55
|
-
ObjectExpression: 1,
|
|
56
|
-
ImportDeclaration: 1
|
|
57
|
-
}],
|
|
58
|
-
'quotes': ['error', 'single', {
|
|
59
|
-
avoidEscape: true,
|
|
60
|
-
allowTemplateLiterals: true
|
|
61
|
-
}],
|
|
62
|
-
'no-tabs': 'error',
|
|
63
|
-
'semi': ['error', 'always'],
|
|
64
|
-
'comma-style': ['error', 'last'],
|
|
65
|
-
'comma-spacing': ['error', { before: false, after: true }],
|
|
66
|
-
|
|
67
|
-
// 错误处理规则
|
|
68
|
-
'no-unused-vars': ['error', {
|
|
69
|
-
args: 'none',
|
|
70
|
-
caughtErrors: 'all',
|
|
71
|
-
ignoreRestSiblings: true
|
|
72
|
-
}],
|
|
73
|
-
'no-unsafe-finally': 'warn',
|
|
74
|
-
'no-throw-literal': 'error',
|
|
75
|
-
|
|
76
|
-
// 最佳实践规则
|
|
77
|
-
'prefer-promise-reject-errors': 'error',
|
|
78
|
-
'no-param-reassign': ['error', {
|
|
79
|
-
props: true,
|
|
80
|
-
ignorePropertyModificationsFor: [
|
|
81
|
-
'acc', 'accumulator', 'e', '$', 'db', 'ctx', 'target', 'self',
|
|
82
|
-
'context', 'req', 'request', 'res', 'response', 'ret', 'msg', 'query', 'body', 'model', 'o', 'm', 'player', 'entity',
|
|
83
|
-
'$scope', 'staticContext'
|
|
84
|
-
]
|
|
85
|
-
}],
|
|
86
|
-
'prefer-object-spread': 'error',
|
|
87
|
-
'prefer-arrow-callback': 'error',
|
|
88
|
-
'max-lines-per-function': ['warn', {
|
|
89
|
-
max: 40,
|
|
90
|
-
skipBlankLines: true,
|
|
91
|
-
skipComments: true
|
|
92
|
-
}],
|
|
93
|
-
'complexity': ['warn', 10],
|
|
94
|
-
|
|
95
|
-
// JSDoc规则
|
|
96
|
-
'jsdoc/check-alignment': 'warn',
|
|
97
|
-
'jsdoc/check-indentation': 'warn',
|
|
98
|
-
'jsdoc/check-param-names': 'warn',
|
|
99
|
-
'jsdoc/check-syntax': 'warn',
|
|
100
|
-
'jsdoc/check-tag-names': 'warn',
|
|
101
|
-
'jsdoc/check-types': 'warn',
|
|
102
|
-
'jsdoc/no-undefined-types': 'warn',
|
|
103
|
-
'jsdoc/require-description': 'warn',
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
];
|
package/example/index.js
DELETED
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* mm_matchs 字符串匹配原型扩展模块示例
|
|
3
|
-
* 加载本模块后 String.prototype 增加 matchs 方法,
|
|
4
|
-
* 支持通配符(*)、占位符({手机号码}/{日期}/{邮箱} 等)与正则匹配
|
|
5
|
-
* 运行:node example/index.js
|
|
6
|
-
*/
|
|
7
|
-
require('../index.js');
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* 演示通配符匹配
|
|
11
|
-
*/
|
|
12
|
-
function demoWildcard() {
|
|
13
|
-
console.log('=== 通配符匹配 ===');
|
|
14
|
-
console.log('"我叫文武, 手机号码是15817188866".matchs("手机号码*") ->',
|
|
15
|
-
'我叫文武, 手机号码是15817188866'.matchs('手机号码*'));
|
|
16
|
-
console.log('"因为如果它是一只鸟,那么它就会飞".matchs("如果*鸟") ->',
|
|
17
|
-
'因为如果它是一只鸟,那么它就会飞'.matchs('如果*鸟'));
|
|
18
|
-
console.log('"因为如果它是一只鸟,那么它就会飞".matchs("如果*就*") ->',
|
|
19
|
-
'因为如果它是一只鸟,那么它就会飞'.matchs('如果*就*'));
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* 演示字典占位符匹配
|
|
24
|
-
*/
|
|
25
|
-
function demoDict() {
|
|
26
|
-
console.log('\n=== 字典占位符匹配 ===');
|
|
27
|
-
console.log('"我叫文武, 手机号码是15817188866".matchs("{手机号码}") ->',
|
|
28
|
-
'我叫文武, 手机号码是15817188866'.matchs('{手机号码}'));
|
|
29
|
-
console.log('"1991-04-01 12:15:36".matchs("{日期时间}") ->',
|
|
30
|
-
'1991-04-01 12:15:36'.matchs('{日期时间}'));
|
|
31
|
-
console.log('"573242395@qq.com".matchs("{邮箱}") ->',
|
|
32
|
-
'573242395@qq.com'.matchs('{邮箱}'));
|
|
33
|
-
console.log('"身份证号441423199104018012".matchs("{身份证号}") ->',
|
|
34
|
-
'身份证号441423199104018012'.matchs('{身份证号}'));
|
|
35
|
-
console.log('"广东省深圳市南山区粤海街道".matchs("{城市}") ->',
|
|
36
|
-
'广东省深圳市南山区粤海街道'.matchs('{城市}'));
|
|
37
|
-
console.log('"现在的时间是19点35分59秒".matchs("{时分秒}") ->',
|
|
38
|
-
'现在的时间是19点35分59秒'.matchs('{时分秒}'));
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* 演示正则匹配
|
|
43
|
-
*/
|
|
44
|
-
function demoRegex() {
|
|
45
|
-
console.log('\n=== 正则匹配 ===');
|
|
46
|
-
console.log('"今年28岁了".matchs("/[0-9]+/") ->', '今年28岁了'.matchs('/[0-9]+/'));
|
|
47
|
-
console.log('"手机号码是15817188866".matchs("/[0-9]+/g") ->',
|
|
48
|
-
'手机号码是15817188866'.matchs('/[0-9]+/g'));
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
demoWildcard();
|
|
52
|
-
demoDict();
|
|
53
|
-
demoRegex();
|
package/html.js
DELETED
|
@@ -1,250 +0,0 @@
|
|
|
1
|
-
const htmlformat = require('./htmlformat');
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* HTML转正文类
|
|
5
|
-
* 解析Html页面的文章正文内容,基于文本密度的HTML正文提取类
|
|
6
|
-
* @class
|
|
7
|
-
*/
|
|
8
|
-
class HtmlToArticle {
|
|
9
|
-
/**
|
|
10
|
-
* 构造函数
|
|
11
|
-
* @param {object} arg
|
|
12
|
-
*/
|
|
13
|
-
constructor(arg) {
|
|
14
|
-
// 导入正则表达式
|
|
15
|
-
this.regex = require('./regex');
|
|
16
|
-
|
|
17
|
-
// 正则表达式过滤:正则表达式,要替换成的文本
|
|
18
|
-
this.filters = [{
|
|
19
|
-
rx: this.regex.script,
|
|
20
|
-
value: ''
|
|
21
|
-
}, {
|
|
22
|
-
rx: this.regex.style,
|
|
23
|
-
value: ''
|
|
24
|
-
}, {
|
|
25
|
-
rx: this.regex.comment,
|
|
26
|
-
value: ''
|
|
27
|
-
}, {
|
|
28
|
-
rx: this.regex.link,
|
|
29
|
-
value: '</a>\n'
|
|
30
|
-
}];
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* 是否使用追加模式,默认为false
|
|
34
|
-
* 使用追加模式后,会将符合过滤条件的所有文本提取出来
|
|
35
|
-
*/
|
|
36
|
-
this.append_mode = false;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* 按行分析的深度,默认为6
|
|
40
|
-
*/
|
|
41
|
-
this.depth = 6;
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* 字符限定数,当分析的文本数量达到限定数则认为进入正文内容
|
|
45
|
-
* 默认180个字符数
|
|
46
|
-
*/
|
|
47
|
-
this.limit_count = 125;
|
|
48
|
-
|
|
49
|
-
// 确定文章正文头部时,向上查找,连续的空行到达this.headEmptyLines,则停止查找
|
|
50
|
-
this.head_empty_lines = 2;
|
|
51
|
-
|
|
52
|
-
// 用于确定文章结束的字符数
|
|
53
|
-
this.end_limit_char_count = 20;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* 从给定的Html原始文本中获取正文信息
|
|
59
|
-
* @param {string} html
|
|
60
|
-
* @returns {string} 返回正文
|
|
61
|
-
*/
|
|
62
|
-
HtmlToArticle.prototype.getArticle = function(html) {
|
|
63
|
-
var htm = html.trim();
|
|
64
|
-
// 如果换行符的数量小于10,则认为html为压缩后的html
|
|
65
|
-
// 由于处理算法是按照行进行处理,需要为html标签添加换行符,便于处理
|
|
66
|
-
var arr = htm.split('\n');
|
|
67
|
-
if (arr.length < 10) {
|
|
68
|
-
htm = htm.replace(/>/g, '>\n');
|
|
69
|
-
}
|
|
70
|
-
var tabsize = 1;
|
|
71
|
-
var tabchar = ' ';
|
|
72
|
-
var max_char = 80;
|
|
73
|
-
htm = htmlformat(htm, tabsize, tabchar, max_char);
|
|
74
|
-
// 获取html,body标签内容
|
|
75
|
-
var body = '';
|
|
76
|
-
|
|
77
|
-
if (htm.indexOf('<article') !== -1) {
|
|
78
|
-
var mh = htm.match(eval(this.regex.article));
|
|
79
|
-
if (mh) {
|
|
80
|
-
body = mh[0];
|
|
81
|
-
}
|
|
82
|
-
} else {
|
|
83
|
-
if (htm.indexOf('<main') !== -1) {
|
|
84
|
-
var mh = htm.match(eval(this.regex.main));
|
|
85
|
-
if (mh) {
|
|
86
|
-
body = mh[0];
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
var mh = htm.match(eval(this.regex.body));
|
|
91
|
-
if (mh) {
|
|
92
|
-
body = mh[0];
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
// 过滤样式,脚本等不相干标签
|
|
96
|
-
var lt = this.filters;
|
|
97
|
-
var len = lt.length;
|
|
98
|
-
for (var i = 0; i < len; i++) {
|
|
99
|
-
var o = lt[i];
|
|
100
|
-
var rx = eval(o.rx);
|
|
101
|
-
body = body.replace(rx, o.value);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
var {
|
|
106
|
-
content,
|
|
107
|
-
content_tag
|
|
108
|
-
} = this.getContent(body);
|
|
109
|
-
|
|
110
|
-
var article = {
|
|
111
|
-
title: this.getTitle(htm),
|
|
112
|
-
datetime: this.getPublishDate(body),
|
|
113
|
-
content: content,
|
|
114
|
-
html: htmlformat(content_tag, tabsize, tabchar, max_char)
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
return article;
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* 获取标题
|
|
123
|
-
* @param {string} html
|
|
124
|
-
* @returns {string} 返回标题
|
|
125
|
-
*/
|
|
126
|
-
HtmlToArticle.prototype.getTitle = function(html) {
|
|
127
|
-
var titleFilter = /<title>[\s\S]*?<\/title>/gi;
|
|
128
|
-
var h1Filter = /<h1.*?>.*?<\/h1>/gi;
|
|
129
|
-
var clearFilter = /<.*?>/g;
|
|
130
|
-
|
|
131
|
-
var title = '';
|
|
132
|
-
var mh = html.match(titleFilter);
|
|
133
|
-
if (mh) {
|
|
134
|
-
title = mh[0].replace(clearFilter, '');
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
if (!title) {
|
|
138
|
-
// 正文的标题一般在h1中,比title中的标题更干净
|
|
139
|
-
mh = html.match(h1Filter);
|
|
140
|
-
if (mh) {
|
|
141
|
-
title = mh[0].replace(clearFilter, '');
|
|
142
|
-
}
|
|
143
|
-
}
|
|
144
|
-
return title;
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
/**
|
|
148
|
-
* 获取文章发布日期
|
|
149
|
-
* @param {string} html
|
|
150
|
-
* @returns {string} 返回时间字符串
|
|
151
|
-
*/
|
|
152
|
-
HtmlToArticle.prototype.getPublishDate = function(html) {
|
|
153
|
-
var text = html.replace(eval(this.regex.clear_tag), '');
|
|
154
|
-
var rx = eval(this.regex.datetime);
|
|
155
|
-
var mh = text.match(rx);
|
|
156
|
-
var result = '1900-01-01';
|
|
157
|
-
if (mh) {
|
|
158
|
-
result = mh[0].replace('年', '-').replace('月', '-').replace('日', '-').replace('时', ':').replace('分', ':').replace('秒',
|
|
159
|
-
'').replace(/\//g, '-');
|
|
160
|
-
}
|
|
161
|
-
return result;
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* 从body标签文本中分析正文内容
|
|
166
|
-
* @param {string} body_text
|
|
167
|
-
* @param {string} content_tag 返回文本正文,不包含标签
|
|
168
|
-
*/
|
|
169
|
-
HtmlToArticle.prototype.getContent = function(body_text) {
|
|
170
|
-
// 保存干净的文本内容,不包含标签
|
|
171
|
-
var lines = [];
|
|
172
|
-
|
|
173
|
-
// 保存原始内容,按行存储
|
|
174
|
-
var org_lines = body_text.split('\n');
|
|
175
|
-
|
|
176
|
-
// 去除每行的空白字符,剔除标签
|
|
177
|
-
for (var i = 0; i < org_lines.length; i++) {
|
|
178
|
-
var line_info = org_lines[i];
|
|
179
|
-
var str = line_info.replace(eval(this.regex.line_break), '\r\n').replace(eval(this.regex.clear_tag), '').trim();
|
|
180
|
-
if (str) {
|
|
181
|
-
lines.push(str);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
var sb = '';
|
|
185
|
-
var org_sb = '';
|
|
186
|
-
|
|
187
|
-
// 记录上一次统计的字符数量
|
|
188
|
-
var pre_text_len = 0;
|
|
189
|
-
|
|
190
|
-
// 记录文章正文的起始位置
|
|
191
|
-
var start_pos = -1;
|
|
192
|
-
var d = this.depth;
|
|
193
|
-
for (var i = 0; i < lines.length - d; i++) {
|
|
194
|
-
var len = 0;
|
|
195
|
-
for (var j = 0; j < d; j++) {
|
|
196
|
-
len += lines[i + j].length;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
if (start_pos == -1) // 还没有找到文章起始位置,需要判断起始位置
|
|
200
|
-
{
|
|
201
|
-
if (pre_text_len > this.limit_count && len > 0) // 如果上次查找的文本数量超过了限定字数,且当前行数字符数不为0,则认为是开始位置
|
|
202
|
-
{
|
|
203
|
-
// 查找文章起始位置, 如果向上查找,发现2行连续的空行则认为是头部
|
|
204
|
-
var empty_count = 0;
|
|
205
|
-
for (var j = i - 1; j > 0; j--) {
|
|
206
|
-
if (!lines[j]) {
|
|
207
|
-
empty_count++;
|
|
208
|
-
} else {
|
|
209
|
-
empty_count = 0;
|
|
210
|
-
}
|
|
211
|
-
if (empty_count == this.head_empty_lines) {
|
|
212
|
-
start_pos = j + this.head_empty_lines;
|
|
213
|
-
break;
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
// 如果没有定位到文章头,则以当前查找位置作为文章头
|
|
217
|
-
if (start_pos == -1) {
|
|
218
|
-
start_pos = i;
|
|
219
|
-
}
|
|
220
|
-
// 填充发现的文章起始部分
|
|
221
|
-
for (var j = start_pos; j <= i; j++) {
|
|
222
|
-
sb += lines[j];
|
|
223
|
-
org_sb += org_lines[j];
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
} else {
|
|
227
|
-
//if (len == 0 && pre_text_len == 0) // 当前长度为0,且上一个长度也为0,则认为已经结束
|
|
228
|
-
if (len <= this.end_limit_char_count && pre_text_len < this.end_limit_char_count) // 当前长度为0,且上一个长度也为0,则认为已经结束
|
|
229
|
-
{
|
|
230
|
-
if (!this.end_limit_char_count) {
|
|
231
|
-
break;
|
|
232
|
-
}
|
|
233
|
-
start_pos = -1;
|
|
234
|
-
}
|
|
235
|
-
sb += lines[i];
|
|
236
|
-
org_sb += org_lines[i];
|
|
237
|
-
}
|
|
238
|
-
pre_text_len = len;
|
|
239
|
-
}
|
|
240
|
-
// 处理回车符,更好的将文本格式化输出
|
|
241
|
-
var content = sb;
|
|
242
|
-
// 输出带标签文本
|
|
243
|
-
var content_tag = org_sb;
|
|
244
|
-
return {
|
|
245
|
-
content,
|
|
246
|
-
content_tag
|
|
247
|
-
};
|
|
248
|
-
};
|
|
249
|
-
|
|
250
|
-
module.exports = HtmlToArticle;
|