-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy pathextract.js
More file actions
271 lines (232 loc) · 9.24 KB
/
Copy pathextract.js
File metadata and controls
271 lines (232 loc) · 9.24 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
var cheerio = require('cheerio');
var Po = require('pofile');
var esprima = require('esprima');
var _ = require('lodash');
var escapeRegex = /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g;
var mkAttrRegex = function (startDelim, endDelim) {
var start = startDelim.replace(escapeRegex, '\\$&');
var end = endDelim.replace(escapeRegex, '\\$&');
if (start === '' && end === '') {
start = '^';
}
return new RegExp(start + '\\s*(\'|"|")(.*?)\\1\\s*\\|\\s*translate(Plural:.*:\\1(.*?)\\1)?\\s*(' + end + '|\\|)', 'g');
};
var noDelimRegex = mkAttrRegex('', '');
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: [],
extensions: {
htm: 'html',
html: 'html',
php: 'html',
phtml: 'html',
tml: 'html',
js: 'js'
},
postProcess: function (po) {}
}, options);
this.options.markerNames.unshift(this.options.markerName);
this.strings = {};
this.attrRegex = mkAttrRegex(this.options.startDelim, this.options.endDelim);
}
Extractor.isValidStrategy = function (strategy) {
return strategy === 'html' || strategy === 'js';
};
Extractor.mkAttrRegex = mkAttrRegex;
Extractor.prototype.addString = function (file, string, plural, extractedComment) {
string = string.trim();
if (string.length === 0) {
return;
}
if (!this.strings[string]) {
this.strings[string] = new Po.Item();
}
var item = this.strings[string];
item.msgid = string;
if (item.references.indexOf(file) < 0) {
item.references.push(file);
}
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 && extractedComment.length > 0) {
item.extractedComments.push(extractedComment);
}
};
Extractor.prototype.extractJs = function (filename, src) {
var self = this;
var syntax = esprima.parse(src, {
tolerant: true,
attachComment: true
});
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 === 'gettextCatalog' || (
// also allow gettextCatalog calls on objects like this.gettextCatalog.getString()
node.callee.object.property &&
node.callee.object.property.name === 'gettextCatalog')) &&
node.callee.property !== null &&
node.callee.property.name === 'getString' &&
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 === 'gettextCatalog' || (
// also allow gettextCatalog calls on objects like this.gettextCatalog.getPlural()
node.callee.object.property &&
node.callee.object.property.name === 'gettextCatalog')) &&
node.callee.property !== null &&
node.callee.property.name === 'getPlural' &&
node.arguments !== null &&
node.arguments.length;
}
walkJs(syntax, function (node, parentComment) {
var str;
var singular;
var plural;
var extractedComments = [];
if (isGettext(node) || isGetString(node)) {
str = getJSExpression(node.arguments[0]);
} else if (isGetPlural(node)) {
singular = getJSExpression(node.arguments[1]);
plural = getJSExpression(node.arguments[2]);
}
if (str || singular) {
if (parentComment) {
parentComment.leadingComments.forEach(function (comment) {
if (comment.value.match(/^\/ .*/)) {
extractedComments.push(comment.value.replace(/^\/ /, ''));
}
});
}
if (str) {
self.addString(filename, str, plural, extractedComments);
} else if (singular) {
self.addString(filename, singular, plural, extractedComments);
}
}
});
};
Extractor.prototype.extractHtml = function (filename, src) {
var $ = cheerio.load(src, { decodeEntities: false });
var self = this;
$('*').each(function (index, n) {
var node;
if (n.name === 'script' && n.attribs.type ==='text/ng-template') {
node = $(n);
self.extractHtml(filename, node.text());
return;
}
var str;
var plural;
var extractedComment;
node = $(n);
for (var attr in node.attr()) {
if (attr === 'translate') {
str = node.html();
plural = node.attr('translate-plural');
extractedComment = node.attr('translate-comment');
self.addString(filename, str, plural, extractedComment);
} else if (matches = noDelimRegex.exec(node.attr(attr))) {
str = matches[2].replace(/\\\'/g, '\'');
plural = (typeof matches[4] === 'string') ? matches[4].replace(/\\\'/g, '\'') : undefined;
self.addString(filename, str, plural);
}
// Regex with global modifier keeps lastIndex
// Reset it to make sure all strings are checked from index 0
noDelimRegex.lastIndex = 0;
}
if (typeof node.attr('data-translate') !== 'undefined') {
str = node.html();
plural = node.attr('data-translate-plural');
self.addString(filename, str, plural);
}
});
var matches;
while (matches = this.attrRegex.exec(src)) {
var str = matches[2].replace(/\\\'/g, '\'');
var plural = (typeof matches[4] === 'string') ? matches[4].replace(/\\\'/g, '\'') : undefined;
this.addString(filename, str, plural);
}
};
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'
};
for (var key in this.strings) {
catalog.items.push(this.strings[key]);
}
catalog.items.sort(function (a, b) {
return a.msgid.localeCompare(b.msgid);
});
this.options.postProcess(catalog);
return catalog.toString();
};
return Extractor;
})();
module.exports = Extractor;