swig.js
4.0 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
#!/usr/bin/env node
/*jslint es5: true */
var swig = require('../index'),
optimist = require('optimist'),
fs = require('fs'),
path = require('path'),
filters = require('../lib/filters'),
utils = require('../lib/utils'),
uglify = require('uglify-js');
var command,
argv = optimist
.usage('\n Usage:\n' +
' $0 compile [files] [options]\n' +
' $0 run [files] [options]\n' +
' $0 render [files] [options]\n'
)
.describe({
v: 'Show the Swig version number.',
o: 'Output location.',
h: 'Show this help screen.',
j: 'Variable context as a JSON file.',
c: 'Variable context as a CommonJS-style file. Used only if option `j` is not provided.',
m: 'Minify compiled functions with uglify-js',
'filters': 'Custom filters as a CommonJS-style file',
'tags': 'Custom tags as a CommonJS-style file',
'options': 'Customize Swig\'s Options from a CommonJS-style file',
'wrap-start': 'Template wrapper beginning for "compile".',
'wrap-end': 'Template wrapper end for "compile".',
'method-name': 'Method name to set template to and run from.'
})
.alias('v', 'version')
.alias('o', 'output')
.default('o', 'stdout')
.alias('h', 'help')
.alias('j', 'json')
.alias('c', 'context')
.alias('m', 'minify')
.default('wrap-start', 'var tpl = ')
.default('wrap-end', ';')
.default('method-name', 'tpl')
.check(function (argv) {
if (argv.v) {
return;
}
if (!argv._.length) {
throw new Error('');
}
command = argv._.shift();
if (command !== 'compile' && command !== 'render' && command !== 'run') {
throw new Error('Unrecognized command "' + command + '". Use -h for help.');
}
if (argv['method-name'] !== 'tpl' && argv['wrap-start'] !== 'var tpl =') {
throw new Error('Cannot use arguments "--method-name" and "--wrap-start" together.');
}
if (argv['method-name'] !== 'tpl') {
argv['wrap-start'] = 'var ' + argv['method-name'] + ' = ';
}
})
.argv,
ctx = {},
out = function (file, str) {
console.log(str);
},
efn = function () {},
anonymous,
files,
fn;
// What version?
if (argv.v) {
console.log(require('../package').version);
process.exit(0);
}
// Pull in any context data provided
if (argv.j) {
ctx = JSON.parse(fs.readFileSync(argv.j, 'utf8'));
} else if (argv.c) {
ctx = require(argv.c);
}
if (argv.o !== 'stdout') {
argv.o += '/';
argv.o = path.normalize(argv.o);
try {
fs.mkdirSync(argv.o);
} catch (e) {
if (e.errno !== 47) {
throw e;
}
}
out = function (file, str) {
file = path.basename(file);
fs.writeFileSync(argv.o + file, str, { flags: 'w' });
console.log('Wrote', argv.o + file);
};
}
// Set any custom filters
if (argv.filters) {
utils.each(require(path.resolve(argv.filters)), function (filter, name) {
swig.setFilter(name, filter);
});
}
// Set any custom tags
if (argv.tags) {
utils.each(require(path.resolve(argv.tags)), function (tag, name) {
swig.setTag(name, tag.parse, tag.compile, tag.ends, tag.block);
});
}
// Specify swig default options
if (argv.options) {
swig.setDefaults(require(argv.options));
}
switch (command) {
case 'compile':
fn = function (file, str) {
var r = swig.precompile(str, { filename: file, locals: ctx }).tpl.toString().replace('anonymous', '');
r = argv['wrap-start'] + r + argv['wrap-end'];
if (argv.m) {
r = uglify.minify(r, { fromString: true }).code;
}
out(file, r);
};
break;
case 'run':
fn = function (file, str) {
(function () {
eval(str);
var __tpl = eval(argv['method-name']);
out(file, __tpl(swig, ctx, filters, utils, efn));
}());
};
break;
case 'render':
fn = function (file, str) {
out(file, swig.render(str, { filename: file, locals: ctx }));
};
break;
}
argv._.forEach(function (file) {
var str = fs.readFileSync(file, 'utf8');
fn(file, str);
});