b28-cli 1.6.7 → 1.6.8

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.
@@ -20,7 +20,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
20
20
  * 词条提取、翻译基类
21
21
  */
22
22
  var Extract = function () {
23
- function Extract(option) {
23
+ function Extract(option, words) {
24
24
  (0, _classCallCheck3.default)(this, Extract);
25
25
 
26
26
  this.option = Object.assign({}, {
@@ -29,45 +29,43 @@ var Extract = function () {
29
29
  ignoreCode: /<!--\s*hide|-->/g,
30
30
  // 将对应的词条全部修改为'/**<%%>**/window.MS'
31
31
  // templateExp: /(\=|\+|\-|\*|\/|\s|\(|\[|\{)\s*<%.*?%>/g
32
- templateExp: /<%([^\n]*?)%>/g,
32
+ templateExp: /<%\s*multilang\(([^\n]*?)\);*\s*%>/g ///<%([^\n]*?)%>/g,只能存在一个捕获组
33
33
  // 自定义不提词条规则,可以是正则也可以是function
34
- customRules: []
34
+ // customRules: []
35
35
  }, option);
36
- this.init();
36
+ this.init(words);
37
37
  }
38
38
 
39
- Extract.prototype.init = function init() {
40
- var _this = this;
41
-
39
+ Extract.prototype.init = function init(words) {
40
+ this.words = words;
42
41
  // 记录当前的文件路径
43
42
  this.curFilePath = "";
44
- // 提取的词条,去除了重复项,当为翻译模式时间,只存储未被翻译的词条
45
- this.words = [];
46
43
  // 是否正在处理文件
47
44
  this.isWorking = false;
48
45
  // 待处理文件列表
49
46
  this.handleList = [];
50
47
  this.ignoreRE = _config.IGNORE_REGEXP.slice(0);
51
48
  this.ignoreFuns = _config.IGNORE_FUNCTIONS.slice(0);
52
-
53
- var customRules = this.option.customRules;
54
- if (Array.isArray(customRules)) {
55
- customRules.forEach(function (item) {
56
- if (typeof item === "function") {
57
- _this.ignoreFuns.push(item);
58
- } else {
59
- _this.ignoreRE.push(item);
60
- }
61
- });
62
- } else if (typeof customRules === "function") {
63
- this.ignoreFuns.push(customRules);
64
- } else if (customRules) {
65
- this.ignoreRE.push((0, _index.string2Regexp)(customRules));
66
- }
49
+ this.suspectLine = [];
50
+
51
+ // let customRules = this.option.customRules;
52
+ // if (Array.isArray(customRules)) {
53
+ // customRules.forEach(item => {
54
+ // if (typeof item === "function") {
55
+ // this.ignoreFuns.push(item);
56
+ // } else {
57
+ // this.ignoreRE.push(item);
58
+ // }
59
+ // });
60
+ // } else if (typeof customRules === "function") {
61
+ // this.ignoreFuns.push(customRules);
62
+ // } else if (customRules) {
63
+ // this.ignoreRE.push(string2Regexp(customRules));
64
+ // }
67
65
  };
68
66
 
69
67
  Extract.prototype.handleFile = function handleFile(filePath) {
70
- var _this2 = this;
68
+ var _this = this;
71
69
 
72
70
  // log(`开始提取文件-${filePath}`);
73
71
  this.isWorking = true;
@@ -75,17 +73,31 @@ var Extract = function () {
75
73
  return (0, _index.loadFile)(filePath).then(function (data) {
76
74
  // 写入文件
77
75
  (0, _index.log)("\u6DFB\u52A0\u7FFB\u8BD1\u51FD\u6570-" + filePath);
78
- return _this2.transNode(data);
76
+ return _this.transNode(data);
79
77
  }).then(function (AST) {
80
- return _this2.scanNode(AST);
78
+ return _this.scanNode(AST);
81
79
  }).then(function (fileData) {
82
- (0, _index.writeTextFile)(_path2.default.resolve(_this2.option.baseWritePath, _path2.default.relative(_this2.option.baseReadPath, _this2.curFilePath)), fileData);
83
- _this2.complete();
84
- return _this2.startTrans();
80
+ (0, _index.writeTextFile)(_path2.default.resolve(_this.option.baseWritePath, _path2.default.relative(_this.option.baseReadPath, _this.curFilePath)), fileData);
81
+ _this.complete();
82
+ // 记录可能需要人工二次审核的词条
83
+ var matchs = fileData.match(/(alert|confirm)\(.*?\)/gi);
84
+ if (matchs) {
85
+ matchs = matchs.filter(function (item) {
86
+ return !(/(alert|confirm)\((.).*?\2\)/gi.test(item) || /_\(.*?\)/g.test(item) || !/\s/g.test(item));
87
+ });
88
+ if (matchs.length > 0) {
89
+ var _suspectLine;
90
+
91
+ (_suspectLine = _this.suspectLine).push.apply(_suspectLine, ["#--#--#--#--#--# " + _this.curFilePath + " #--#--#--#--#--#"].concat(matchs));
92
+ }
93
+ }
94
+
95
+ return _this.startTrans();
85
96
  }).catch(function (error) {
86
- _this2.copyFile(filePath);
87
- (0, _index.log)("\u6587\u4EF6[" + filePath + "]\u5904\u7406\u51FA\u9519- " + error.message, _index.LOG_TYPE.ERROR);
88
- return _this2.startTrans();
97
+ _this.copyFile(filePath);
98
+ (0, _index.log)("\u6587\u4EF6[" + filePath + "]\u5904\u7406\u51FA\u9519", _index.LOG_TYPE.ERROR);
99
+ (0, _index.log)(error, _index.LOG_TYPE.ERROR);
100
+ return _this.startTrans();
89
101
  });
90
102
  };
91
103
 
@@ -98,92 +110,25 @@ var Extract = function () {
98
110
  return Promise.resolve(data);
99
111
  };
100
112
 
101
- Extract.prototype.setAttr = function setAttr(attr, value) {
102
- if (Object.prototype.toString.call(attr) === "[object Object]") {
103
- for (var key in attr) {
104
- this.setSingleAttr(key, attr[key]);
105
- }
106
- } else {
107
- this.setSingleAttr(attr, value);
108
- }
109
- };
110
-
111
- Extract.prototype.setSingleAttr = function setSingleAttr(attr, value) {
112
- this.option[attr] = value;
113
- };
114
-
115
113
  Extract.prototype.startTrans = function startTrans() {
116
114
  // 当一个文件执行完成,立即执行下一个指令
117
115
  if (this.handleList.length > 0) {
118
116
  return this.handleFile(this.handleList.shift());
119
117
  }
120
- return Promise.resolve("done");
118
+ return Promise.resolve(this.suspectLine);
121
119
  };
122
120
 
123
121
  Extract.prototype.addTask = function addTask(filePath) {
124
122
  this.handleList.push(filePath);
125
123
  };
126
124
 
127
- Extract.prototype.addWord = function addWord(word) {
128
- if (!this.words.includes(word)) {
129
- this.words.push(word);
130
- }
131
- };
132
-
133
- Extract.prototype.addWords = function addWords(words) {
134
- var _this3 = this;
135
-
136
- words.forEach(function (word) {
137
- _this3.addWord(word);
138
- });
139
- };
140
-
141
- Extract.prototype.getWord = function getWord(val) {
142
- if (!val || /^\s*$/.test(val)) {
143
- return "";
144
- }
145
-
146
- // 经过处理的字符串
147
- if (/\{%s\}/i.test(val)) {
148
- return val;
149
- }
150
-
151
- var skip = this.ignoreRE.some(function (item) {
152
- return item.test(val);
153
- });
154
- if (skip) {
155
- return "";
156
- }
157
-
158
- for (var i = 0, l = this.ignoreFuns.length; i < l; i++) {
159
- var fun = this.ignoreFuns[i],
160
- str = val.replace(/(^\s+)|(\s+$)/g, "");
161
-
162
- if (typeof fun === "function") {
163
- if (skip = fun(str)) {
164
- break;
165
- }
166
- }
167
- }
168
- if (skip) {
169
- return "";
170
- }
171
-
172
- var addValue = "";
173
-
174
- //中英文都提取
175
- if (/[a-z]/i.test(val) || /[\u4e00-\u9fa5]/.test(val)) {
176
- addValue = val;
177
- }
178
-
179
- return addValue;
125
+ Extract.prototype.transWord = function transWord(val) {
126
+ return this.words[val];
180
127
  };
181
128
 
182
129
  Extract.prototype.complete = function complete() {
183
130
  this.isWorking = false;
184
131
  this.option.onComplete && this.option.onComplete(this.curFilePath, this.words);
185
- // 重置提取的词条
186
- this.words = [];
187
132
  };
188
133
 
189
134
  return Extract;
@@ -35,20 +35,23 @@ var JSDOM = jsdom.JSDOM;
35
35
  var ExtractHTML = function (_Extract) {
36
36
  (0, _inherits3.default)(ExtractHTML, _Extract);
37
37
 
38
- function ExtractHTML(option) {
38
+ function ExtractHTML(option, words) {
39
39
  (0, _classCallCheck3.default)(this, ExtractHTML);
40
40
 
41
- var _this = (0, _possibleConstructorReturn3.default)(this, _Extract.call(this, option));
41
+ var _this = (0, _possibleConstructorReturn3.default)(this, _Extract.call(this, option, words));
42
42
 
43
43
  _this.extractJS = new _extract_js_ori2.default({
44
44
  ignoreCode: _this.option.ignoreCode,
45
45
  templateExp: _this.option.templateExp
46
- });
46
+ }, words);
47
47
  _this.jsHandleList = [];
48
48
  return _this;
49
49
  }
50
50
 
51
51
  ExtractHTML.prototype.transNode = function transNode(html) {
52
+ var _this2 = this;
53
+
54
+ html = html.replace(/\s*<!--\s*<(\/?)script>\s*-->/ig, '');
52
55
  this.oldHtml = html;
53
56
  this.scripts = [];
54
57
  this.getHeaderTag(html);
@@ -59,7 +62,7 @@ var ExtractHTML = function (_Extract) {
59
62
  var dom = new JSDOM(html, {
60
63
  virtualConsole: virtualConsole
61
64
  });
62
- var document = dom.window.document;
65
+ var document = _this2.document = dom.window.document;
63
66
  resolve(document);
64
67
  } catch (err) {
65
68
  reject(err);
@@ -76,7 +79,7 @@ var ExtractHTML = function (_Extract) {
76
79
 
77
80
 
78
81
  ExtractHTML.prototype.scanNode = function scanNode(document) {
79
- var _this2 = this;
82
+ var _this3 = this;
80
83
 
81
84
  // 遍历各节点
82
85
  this.listNode(document.documentElement);
@@ -84,26 +87,46 @@ var ExtractHTML = function (_Extract) {
84
87
  return this.nextJsTask().then(function () {
85
88
  // ͨ通过正则替换,为了规避jsdom对html中的特殊字符串进行编码
86
89
  var outHtml = document.documentElement.innerHTML;
87
- var match = outHtml.match(/<script\b[^>]*>[\s\S]*?<\/script>/g);
88
- outHtml = _this2.oldHtml.replace(/<script\b[^>]*>[\s\S]*?<\/script>/g, function () {
89
- return match.shift();
90
- });
90
+ return _this3.handleHtml(outHtml);
91
+ });
92
+ };
93
+
94
+ ExtractHTML.prototype.handleHtml = function handleHtml(htmlCode) {
95
+ var _this4 = this;
91
96
 
92
- return outHtml;
97
+ var oldHtml = this.oldHtml;
98
+ var match = htmlCode.match(/<script\b[^>]*>[\s\S]*?<\/script>/gi);
99
+ htmlCode = oldHtml.replace(/<script\b[^>]*>[\s\S]*?<\/script>/gi, function () {
100
+ return match.shift();
101
+ });
102
+
103
+ var templateExp = this.option.templateExp;
104
+
105
+ return htmlCode.replace(templateExp, function (match, p1, index) {
106
+ var reg = (0, _index.escapeRegExp)(match);
107
+ var tempReg = templateExp.toString();
108
+ reg += templateExp.source;
109
+ reg = new RegExp(reg, tempReg.slice(tempReg.lastIndexOf("/") + 1));
110
+ var word = _this4.transWord(p1);
111
+ if (reg.test(htmlCode) && word && !/\s/.test(htmlCode[index + match.length])) {
112
+ return "word ";
113
+ }
114
+ return word || match;
93
115
  });
94
116
  };
95
117
 
96
118
  ExtractHTML.prototype.handleJsTask = function handleJsTask(child) {
97
- var _this3 = this;
119
+ var _this5 = this;
98
120
 
99
- return this.extractJS.transNode(child.nodeValue, true).then(function (AST) {
100
- return _this3.extractJS.scanNode(AST);
121
+ return this.extractJS.transNode(child.innerHTML, true).then(function (AST) {
122
+ return _this5.extractJS.scanNode(AST);
101
123
  }).then(function (fileData) {
102
124
  // 写入文件
103
- child.nodeValue = fileData;
104
- return _this3.nextJsTask();
105
- }).catch(function () {
106
- return _this3.nextJsTask();
125
+ child.innerHTML = fileData;
126
+ return _this5.nextJsTask();
127
+ }).catch(function (e) {
128
+ (0, _index.log)(e, _index.LOG_TYPE.ERROR);
129
+ return _this5.nextJsTask();
107
130
  });
108
131
  };
109
132
 
@@ -126,25 +149,24 @@ var ExtractHTML = function (_Extract) {
126
149
 
127
150
  var firstChild = element.firstChild,
128
151
  nextSibling = element.nextSibling,
129
- nodeType = element.nodeType,
130
152
  nodeName = element.nodeName.toLowerCase();
131
- // 处理html节点
132
- // nodeType: 1-元素 2-属性 3-文本内容 8-代表注释
133
- if (nodeType === 1 && nodeName == "script") {
134
- if (firstChild && firstChild.nodeValue && (0, _index.trim)(firstChild.nodeValue)) {
135
- this.addJsTask(firstChild);
136
- }
137
- } else {
138
- // 处理子节点
139
- if (firstChild) {
140
- this.listNode(firstChild);
141
- }
153
+
154
+ if (nodeName == "script") {
155
+ this.addJsTask(element);
156
+ nextSibling && this.listNode(nextSibling);
157
+ return;
142
158
  }
143
159
 
144
- // 处理兄弟节点
145
- if (nextSibling) {
146
- this.listNode(nextSibling);
160
+ // noscript内的文本不处理
161
+ if (nodeName === "noscript" || nodeName == "style") {
162
+ nextSibling && this.listNode(nextSibling);
163
+ return;
147
164
  }
165
+ // 处理子节点
166
+ firstChild && this.listNode(firstChild);
167
+
168
+ // 处理兄弟节点
169
+ nextSibling && this.listNode(nextSibling);
148
170
  };
149
171
 
150
172
  return ExtractHTML;