-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathextract.js
More file actions
418 lines (363 loc) · 14.8 KB
/
extract.js
File metadata and controls
418 lines (363 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
'use strict';
var cheerio = require('cheerio');
var Po = require('pofile');
var espree = require('espree');
var search = require('binary-search');
var _ = require('lodash');
var escapeRegex = /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g;
var noContext = '$$noContext';
function mkAttrRegex(startDelim, endDelim, attributes) {
var start = startDelim.replace(escapeRegex, '\\$&');
var end = endDelim.replace(escapeRegex, '\\$&');
if (start === '' && end === '') {
start = '^';
} else {
// match optional :: (Angular 1.3's bind once syntax) without capturing
start += '(?:\\s*\\:\\:\\s*)?';
}
if (typeof attributes === 'undefined') {
attributes = ['translate'];
}
return new RegExp(start + '\\s*(\'|"|"|')(.*?)\\1\\s*\\|\\s*(' + attributes.join('|') + ')\\s*(' + end + '|\\|)', 'g');
}
function localeCompare(a, b) {
return a.localeCompare(b);
}
function comments2String(comments) {
return comments.join(', ');
}
function walkJs(node, fn, parentComment) {
fn(node, parentComment);
for (var key in node) {
var obj = node[key];
if (node && node.leadingComments) {
parentComment = node;
}
if (typeof obj === 'object') {
walkJs(obj, fn, parentComment);
}
}
}
function getJSExpression(node) {
var res = '';
if (node.type === 'Literal') {
res = node.value;
}
if (node.type === 'BinaryExpression' && node.operator === '+') {
res += getJSExpression(node.left);
res += getJSExpression(node.right);
}
return res;
}
var Extractor = (function () {
function Extractor(options) {
this.options = _.extend({
startDelim: '{{',
endDelim: '}}',
markerName: 'gettext',
markerNames: [],
moduleName: 'gettextCatalog',
moduleMethodString: 'getString',
moduleMethodPlural: 'getPlural',
attribute: 'translate',
attributes: [],
lineNumbers: true,
extensions: {
htm: 'html',
html: 'html',
php: 'html',
phtml: 'html',
tml: 'html',
ejs: 'html',
erb: 'html',
js: 'js',
tag: 'html',
jsp: 'html'
},
postProcess: function (po) {}
}, options);
this.options.markerNames.unshift(this.options.markerName);
this.options.attributes.unshift(this.options.attribute);
this.strings = {};
this.attrRegex = mkAttrRegex(this.options.startDelim, this.options.endDelim, this.options.attributes);
this.noDelimRegex = mkAttrRegex('', '', this.options.attributes);
}
Extractor.isValidStrategy = function (strategy) {
return strategy === 'html' || strategy === 'js';
};
Extractor.mkAttrRegex = mkAttrRegex;
Extractor.prototype.addString = function (reference, string, plural, extractedComment, context) {
// maintain backwards compatibility
if (_.isString(reference)) {
reference = { file: reference };
}
string = string.trim();
if (string.length === 0) {
return;
}
if (!context) {
context = noContext;
}
if (!this.strings[string]) {
this.strings[string] = {};
}
if (!this.strings[string][context]) {
this.strings[string][context] = new Po.Item();
}
var item = this.strings[string][context];
item.msgid = string;
var refString = reference.file;
if (this.options.lineNumbers && reference.location && reference.location.start) {
var line = reference.location.start.line;
if (line || line === 0) {
refString += ':' + reference.location.start.line;
}
}
var refIndex = search(item.references, refString, localeCompare);
if (refIndex < 0) { // don't add duplicate references
// when not found, binary-search returns -(index_where_it_should_be + 1)
item.references.splice(Math.abs(refIndex + 1), 0, refString);
}
if (context !== noContext) {
item.msgctxt = context;
}
if (plural && plural !== '') {
if (item.msgid_plural && item.msgid_plural !== plural) {
throw new Error('Incompatible plural definitions for ' + string + ': ' + item.msgid_plural + ' / ' + plural + ' (in: ' + (item.references.join(', ')) + ')');
}
item.msgid_plural = plural;
item.msgstr = ['', ''];
}
if (extractedComment) {
var commentIndex = search(item.extractedComments, extractedComment, localeCompare);
if (commentIndex < 0) { // don't add duplicate comments
item.extractedComments.splice(Math.abs(commentIndex + 1), 0, extractedComment);
}
}
};
Extractor.prototype.extractJs = function (filename, src, lineNumber) {
// used for line number of JS in HTML <script> tags
lineNumber = lineNumber || 0;
var self = this;
var syntax;
try {
syntax = espree.parse(src, {
tolerant: true,
attachComment: true,
loc: true,
ecmaFeatures: {
arrowFunctions: true,
blockBindings: true,
destructuring: true,
regexYFlag: true,
regexUFlag: true,
templateStrings: true,
binaryLiterals: true,
octalLiterals: true,
unicodeCodePointEscapes: true,
defaultParams: true,
restParams: true,
forOf: true,
objectLiteralComputedProperties: true,
objectLiteralShorthandMethods: true,
objectLiteralShorthandProperties: true,
objectLiteralDuplicateProperties: true,
generators: true,
spread: true,
classes: true,
modules: true,
jsx: true,
globalReturn: true
}
});
} catch (err) {
return;
}
function isGettext(node) {
return node !== null &&
node.type === 'CallExpression' &&
node.callee !== null &&
(self.options.markerNames.indexOf(node.callee.name) > -1 || (
node.callee.property &&
self.options.markerNames.indexOf(node.callee.property.name) > -1
)) &&
node.arguments !== null &&
node.arguments.length;
}
function isGetString(node) {
return node !== null &&
node.type === 'CallExpression' &&
node.callee !== null &&
node.callee.type === 'MemberExpression' &&
node.callee.object !== null && (
node.callee.object.name === self.options.moduleName || (
// also allow gettextCatalog calls on objects like this.gettextCatalog.getString()
node.callee.object.property &&
node.callee.object.property.name === self.options.moduleName)) &&
node.callee.property !== null &&
node.callee.property.name === self.options.moduleMethodString &&
node.arguments !== null &&
node.arguments.length;
}
function isGetPlural(node) {
return node !== null &&
node.type === 'CallExpression' &&
node.callee !== null &&
node.callee.type === 'MemberExpression' &&
node.callee.object !== null && (
node.callee.object.name === self.options.moduleName || (
// also allow gettextCatalog calls on objects like this.gettextCatalog.getPlural()
node.callee.object.property &&
node.callee.object.property.name === self.options.moduleName)) &&
node.callee.property !== null &&
node.callee.property.name === self.options.moduleMethodPlural &&
node.arguments !== null &&
node.arguments.length;
}
walkJs(syntax, function (node, parentComment) {
var str;
var context;
var singular;
var plural;
var extractedComments = [];
var reference = {
file: filename,
location: (function () {
if (!node || !node.loc || !node.loc.start) {
return null;
}
return {
start: {
line: node.loc.start.line + lineNumber
}
};
})()
};
if (isGettext(node) || isGetString(node)) {
str = getJSExpression(node.arguments[0]);
if (node.arguments[2]) {
context = getJSExpression(node.arguments[2]);
}
} else if (isGetPlural(node)) {
singular = getJSExpression(node.arguments[1]);
plural = getJSExpression(node.arguments[2]);
}
if (str || singular) {
var leadingComments = node.leadingComments || (parentComment ? parentComment.leadingComments : []);
leadingComments.forEach(function (comment) {
if (comment.value.match(/^\/ .*/)) {
extractedComments.push(comment.value.replace(/^\/ /, ''));
}
});
if (str) {
self.addString(reference, str, plural, comments2String(extractedComments), context);
} else if (singular) {
self.addString(reference, singular, plural, comments2String(extractedComments));
}
}
});
};
Extractor.prototype.extractHtml = function (filename, src) {
var extractHtml = function (src, lineNumber) {
var $ = cheerio.load(src, { decodeEntities: false, withStartIndices: true });
var self = this;
var newlines = function (index) {
return src.substr(0, index).match(/\n/g) || [];
};
var reference = function (index) {
return {
file: filename,
location: {
start: {
line: lineNumber + newlines(index).length + 1
}
}
};
};
$('*').each(function (index, n) {
var node = $(n);
var getAttr = function (attr) {
return node.attr(attr) || node.data(attr);
};
var str = node.html();
var extracted = {};
var possibleAttributes = self.options.attributes;
possibleAttributes.forEach(function (attr) {
extracted[attr] = {
plural: getAttr(attr + '-plural'),
extractedComment: getAttr(attr + '-comment'),
context: getAttr(attr + '-context')
};
});
if (n.name === 'script') {
if (n.attribs.type === 'text/ng-template') {
extractHtml(node.text(), newlines(n.startIndex).length);
return;
}
// In HTML5, type defaults to text/javascript.
// In HTML4, it's required, so if it's not there, just assume it's JS
if (!n.attribs.type || n.attribs.type === 'text/javascript') {
self.extractJs(filename, node.text(), newlines(n.startIndex).length);
return;
}
}
if (node.is(self.options.attribute)) {
self.addString(reference(n.startIndex), str, extracted[self.options.attribute].plural, extracted[self.options.attribute].extractedComment, extracted[self.options.attribute].context);
return;
}
for (var attr in node.attr()) {
attr = attr.replace(/^data-/, '');
if (possibleAttributes.indexOf(attr) > -1) {
var attrValue = extracted[attr];
str = node.html(); // this shouldn't be necessary, but it is
self.addString(reference(n.startIndex), str || getAttr(attr) || '', attrValue.plural, attrValue.extractedComment, attrValue.context);
} else if (matches = self.noDelimRegex.exec(node.attr(attr))) {
str = matches[2].replace(/\\\'/g, '\'');
self.addString(reference(n.startIndex), str);
self.noDelimRegex.lastIndex = 0;
}
}
});
var matches;
while (matches = this.attrRegex.exec(src)) {
var str = matches[2].replace(/\\\'/g, '\'');
this.addString(reference(matches.index), str);
}
}.bind(this);
extractHtml(src, 0);
};
Extractor.prototype.isSupportedByStrategy = function (strategy, extension) {
return (extension in this.options.extensions) && (this.options.extensions[extension] === strategy);
};
Extractor.prototype.parse = function (filename, content) {
var extension = filename.split('.').pop();
if (this.isSupportedByStrategy('html', extension)) {
this.extractHtml(filename, content);
}
if (this.isSupportedByStrategy('js', extension)) {
this.extractJs(filename, content);
}
};
Extractor.prototype.toString = function () {
var catalog = new Po();
catalog.headers = {
'Content-Type': 'text/plain; charset=UTF-8',
'Content-Transfer-Encoding': '8bit',
'Project-Id-Version': ''
};
for (var msgstr in this.strings) {
var msg = this.strings[msgstr];
var contexts = Object.keys(msg).sort();
for (var i = 0; i < contexts.length; i++) {
catalog.items.push(msg[contexts[i]]);
}
}
catalog.items.sort(function (a, b) {
return a.msgid.localeCompare(b.msgid);
});
this.options.postProcess(catalog);
return catalog.toString();
};
return Extractor;
})();
module.exports = Extractor;