-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
135 lines (110 loc) · 2.87 KB
/
index.js
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
// through2 is a thin wrapper around node transform streams
var through = require('through2');
var gutil = require('gulp-util');
var PluginError = gutil.PluginError;
// Consts
const PLUGIN_NAME = 'gulp-ember-autolocalization';
var replaceSpecialChars=function(text) {
var chars=['.',':','?','!',' '];
chars.forEach(function(c) {
text=text.split(c).join('');
});
return text.trim();
};
var isHandlebarExpression=function(text){
var index=text.search("{{");
if(index==-1){
return false;
}
return true;
};
var handlebarExpressionHandler=function(input,helper){
var temp=input;
temp=temp.replace(/{{([^{}]+)}}/g,"!");
var texts=temp.split('!');
texts.forEach(function(text){
text=text.trim();
if(text){
text=replaceSpecialChars(text);
input=input.split(text).join("{{"+helper+" '"+text+"'}}");
}
});
return input;
};
var hasText=function(input){
input=input.replace(/{{([^{}]+)}}/g,"");
input=input.trim();
return input?true:false;
};
var localize=function(text,helper){
if(!isHandlebarExpression(text)){
return "{{"+helper+" '"+text+"'}}";
}
else{
return hasText(text)?handlebarExpressionHandler(text,helper):text;
}
};
var replaceHelper=function (node,helper) {
if(node.type=='text' && node.content){
node.content=localize(node.content,helper);
}
if(node.children){
var nodes = node.children;
for (var i = 0, m = nodes.length; i < m; i++) {
var n = nodes[i];
if (n.type=='text' && n.content) {
// do some swappy text to html here?
n.content=localize(n.content,helper);
} else {
replaceHelper(n,helper);
}
}
}
};
var process=function(input,helper){
var text=input.replace(/<(?:.|\n)*?>/gm,"*");
text=text.trim();
var texts=text.split('*');
var uniqueArray = texts.filter(function(elem, pos) {
return texts.indexOf(elem) == pos;
});
uniqueArray.forEach(function(t){
t=t.trim();
if(t){
if(!isHandlebarExpression(t)){
t=replaceSpecialChars(t);
}
var lt=localize(t,helper);
input=input.split(t).join(lt);
}
});
return input;
};
module.exports = function (helper) {
helper=helper||'loc';
return through.obj(function (file) {
if (file.isNull()) {
throw new PluginError(PLUGIN_NAME, 'File is empty!');
return;
}
if (file.isStream()) {
throw new PluginError(PLUGIN_NAME, 'Streaming not supported!');
return;
}
var src = file.contents.toString();
try {
var result = process(src,helper);
if (!result) {
// if something goes wrong return the file unchanged with original source
file.contents = new Buffer(src);
this.push(file);
}
else{
file.contents = new Buffer(result);
this.push(file);
}
} catch (err) {
this.emit('error', new gutil.PluginError('gulp-ember-autolocalization', err, {fileName: file.path}));
}
});
};