-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjade_compile.js
executable file
·257 lines (235 loc) · 6.98 KB
/
jade_compile.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
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
#!/usr/bin/env node
var fs = require('fs'),
path = require('path'),
jade = require('jade'),
utils = require('jade/lib/utils'),
nodes = jade.nodes,
runtime = require('jade/lib/runtime'),
selfClosing = require('jade/lib/self-closing'),
appConfig = require('./util').appConfig,
Compiler = jade.Compiler;
var cfg = {
TEMPLATE_EXT : ".jade",
TEMPLATE_CACHING : true,
TEMPLATE_DIR : './',
TEMPALTE_DEFAULT_LANG : 'en',
};
// :varstore filter
jade.filters.varstore = function(block, compiler){
return new VarStoreVisitor(block, compiler.options).compile();
};
function VarStoreVisitor(node, options) {
Compiler.call(this, node, options);
}
// Inherit from Compiler
VarStoreVisitor.prototype.__proto__ = Compiler.prototype;
// Overwrite visitTag method
VarStoreVisitor.prototype.visitTag = function(node){
var parent = Compiler.prototype.visitTag;
var id = node.getAttribute('id');
if(id) {
id = id.replace(/^[\"']/,'');
id = id.replace(/[\"']$/,'');
id = id.replace(/-/g,'_');
var val = '';
if (node.text) {
val += utils.escape(node.text.nodes[0].trimLeft());
}
if (node.block) {
var compiler = new Compiler(node.block, this.options)
, js = compiler.compile()
, fnBody = [
'var __jade = [{ lineno: -1, filename: "' + this.options.filename + '" }];'
, 'try {'
, 'var buf = [];\n'
, (this.options.self
? 'var self = locals || {};\n' + js
: 'with (locals || {}) {\n' + js + '\n}\n')
, 'return buf.join("");'
, '} catch (err) {'
, ' rethrow(err, __jade[0].filename, __jade[0].lineno);'
, '}'
].join('\n')
, fn = new Function('locals, attrs, escape, rethrow', fnBody);
val += utils.escape(fn(undefined, runtime.attrs, runtime.escape, runtime.rethrow)).replace(/\n/g, '\\n');
}
var varAssign = new nodes.Code("var " + id + " = '" + val +"';", false , false);
Compiler.prototype.visitCode.call(this, varAssign);
}
};
// :varstoreassign filter
jade.filters.varstoreassign = function(block, compiler){
return new VarStoreAssign(block, compiler.options).compile();
};
function VarStoreAssign(node, options) {
Compiler.call(this, node, options);
}
// Inherit from Compiler
VarStoreAssign.prototype.__proto__ = Compiler.prototype;
// Overwrite visitTag method, copied from Compiler with slight modification
VarStoreAssign.prototype.visitTag = function(tag){
this.indents++;
var name = tag.name;
if (!this.hasCompiledTag) {
if (!this.hasCompiledDoctype && 'html' == name) {
this.visitDoctype();
}
this.hasCompiledTag = true;
}
// pretty print
if (this.pp && inlineTags.indexOf(name) == -1) {
this.buffer('\\n' + Array(this.indents).join(' '));
}
if (~selfClosing.indexOf(name) && !this.xml) {
this.buffer('<' + name);
this.visitAttributes(tag.attrs);
this.terse
? this.buffer('>')
: this.buffer('/>');
} else {
// Optimize attributes buffering
if (tag.attrs.length) {
this.buffer('<' + name);
if (tag.attrs.length) this.visitAttributes(tag.attrs);
this.buffer('>');
} else {
this.buffer('<' + name + '>');
}
if (tag.code) this.visitCode(tag.code);
// >>> Here is the only difference from parent
if (tag.text) {
this.buffer(utils.text(tag.text.nodes[0].trimLeft()));
} else {
var id = tag.getAttribute('id');
if ( (tag.block.nodes.length == 0) && id ) {
id = id.replace(/^[\"']/,'');
id = id.replace(/[\"']$/,'');
id = id.replace(/-/g,'_');
var code = '!{(typeof('+id+')!="undefined"?'+id+':"")}';
this.buffer(utils.text(code));
}
}
// <<< end.
this.escape = 'pre' == tag.name;
this.visit(tag.block);
// pretty print
if (this.pp && !~inlineTags.indexOf(name) && !tag.textOnly) {
this.buffer('\\n' + Array(this.indents).join(' '));
}
this.buffer('</' + name + '>');
}
this.indents--;
};
var getTranslatedDirCache = {};
function getTranslatedDir(rootPath, lang){
if(!getTranslatedDirCache[path+lang]) {
var dirs = [
lang
];
var langOnly = lang.match(/^([^-_]+)[-_]/);
if(langOnly) {
dirs.push(langOnly[1]);
}
var trDir= '';
//console.log("test " + dirs);
for(var i in dirs) {
try {
var f = path.join(rootPath, dirs[i]);
//console.log("test " + f);
var stat = fs.statSync(f);
if(stat) {
trDir = dirs[i];
break;
}
} catch(e) {};
}
getTranslatedDirCache[path+lang] = trDir;
}
return getTranslatedDirCache[path+lang];
}
function getTranslatedFile(templateName , lang) {
var dir;
if(path.extname(templateName) == cfg.TEMPLATE_EXT) {
dir = path.dirname(templateName);
var p = path.basename(templateName);
templateName = p.replace(cfg.TEMPLATE_EXT,'');
} else {
dir = cfg.TEMPLATE_DIR;
}
var files = [
[ templateName +'.' + lang+ cfg.TEMPLATE_EXT, lang]
];
var langOnly = lang.match(/^([^-_]+)[-_]/);
if(langOnly) {
files.push(
[ templateName +'.' + langOnly[1]+ cfg.TEMPLATE_EXT, lang ]
);
}
files.push(
[ templateName +'.' + cfg.TEMPALTE_DEFAULT_LANG + cfg.TEMPLATE_EXT,
cfg.TEMPALTE_DEFAULT_LANG
]
);
files.push(
[ templateName + cfg.TEMPLATE_EXT, '' ]
);
for(var i in files) {
try {
var f = path.join(dir, files[i][0]);
var stat = fs.statSync(f);
if(stat) {
return {
fileRelative: files[i][0],
file: f,
lang: files[i][1],
}
}
} catch(e) {};
}
throw new Error("Template '" + templateName +"' not found in '" + dir +"'! Tried +" + JSON.stringify(files));
}
function jadeRender(templateName, lang , vars, callback) {
var templateInfo = getTranslatedFile(templateName, lang);
var templatePath = templateInfo.file;
data = {};
for(var i in vars){
data[i] = vars[i];
}
data.cache = data.cache || cfg.TEMPLATE_CACHING;
data.compileDebug = data.compileDebug || false;
data.locale = data.locale || templateInfo.lang || lang;
jade.renderFile(templatePath, data,function (err, str){
if( err ) {
throw (err);
} else {
callback(str);
}
});
}
module.exports.jadeRender = jadeRender;
module.exports.getTranslatedFile = getTranslatedFile;
module.exports.getTranslatedDir = getTranslatedDir;
module.exports.cfg = cfg;
if (!module.parent) {
var argv = require('optimist').argv;
var sys = require('sys'),
normalizeLocale = require('./util').normalizeLocale,
jsRoot = path.join(argv._[1] || "", '/'),
clientJsFiles = require('./client_js_files').clientJsFiles,
APP_CONFIG = appConfig(!argv.dev);
APP_CONFIG['JS_FILES'] = clientJsFiles(path.join(__dirname + '/prod_root/','js')).map(function(file){
return '/js/' + file;
});
APP_CONFIG['PRODUCTION'] = true;
if(argv._[0]) {
// Deduce the locale from fileName
var l = path.extname(argv._[0]);
if(l = path.extname(argv._[0].replace(l,''))) {
l = l.replace(/\./,'');
l = normalizeLocale(l);
}
jadeRender(argv._[0], l ,APP_CONFIG,function(str){
process.stdout.write(str);
})
}
}