| 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 |
10
10
10
10
10
10
1
1
10
10
10
10
10
10
3
10
10
10
8
8
1
1
1
1
2
2
2
1
1
1
7
8
2
10
2
2
2
2
2
2
1
1
1
2
2
10
10
10
10
10
10
10
5
10
9
7
2
9
9
9
9
9
9
9
10
5
5
5
| #!/usr/bin/env node
/**
* Module dependencies.
*/
var fs = require('fs')
, program = require('commander')
, path = require('path')
, basename = path.basename
, dirname = path.dirname
, resolve = path.resolve
, exists = fs.existsSync || /* istanbul ignore next */ path.existsSync
, join = path.join
, mkdirp = require('mkdirp')
, jade = require('../');
// jade options
var options = {};
// options
program
.version(require('../package.json').version)
.usage('[options] [dir|file ...]')
.option('-O, --obj <str>', 'javascript options object')
.option('-o, --out <dir>', 'output the compiled html to <dir>')
.option('-p, --path <path>', 'filename used to resolve includes')
.option('-P, --pretty', 'compile pretty html output')
.option('-c, --client', 'compile function for client-side runtime.js')
.option('-n, --name <str>', 'The name of the compiled template (requires --client)')
.option('-D, --no-debug', 'compile without debugging (smaller functions)')
.option('-w, --watch', 'watch files for changes and automatically re-render')
.option('-E, --extension <extension>', 'specify the output file extension')
.option('--name-after-file', 'Name the template after the last section of the file path (requires --client and overriden by --name)')
.option('--doctype <str>', 'Specify the doctype on the command line (useful if it is not specified by the template)')
program.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' # translate jade the templates dir');
console.log(' $ jade templates');
console.log('');
console.log(' # create {foo,bar}.html');
console.log(' $ jade {foo,bar}.jade');
console.log('');
console.log(' # jade over stdio');
console.log(' $ jade < my.jade > my.html');
console.log('');
console.log(' # jade over stdio');
console.log(' $ echo \'h1 Jade!\' | jade');
console.log('');
console.log(' # foo, bar dirs rendering to /tmp');
console.log(' $ jade foo bar --out /tmp ');
console.log('');
});
program.parse(process.argv);
// options given, parse them
if (program.obj) {
Iif (exists(program.obj)) {
options = JSON.parse(fs.readFileSync(program.obj));
} else {
options = eval('(' + program.obj + ')');
}
}
// --filename
Iif (program.path) options.filename = program.path;
// --no-debug
options.compileDebug = program.debug;
// --client
options.client = program.client;
// --pretty
options.pretty = program.pretty;
// --watch
options.watch = program.watch;
// --name
if (typeof program.name === 'string') {
options.name = program.name;
}
// --doctype
options.doctype = program.doctype;
// left-over args are file paths
var files = program.args;
// compile files
if (files.length) {
console.log();
if (options.watch) {
files.forEach(function(filename) {
try {
renderFile(filename);
} catch (e) {
// keep watching when error occured.
console.error(e.stack || e.message || e);
}
fs.watchFile(filename, {persistent: true, interval: 200},
function (curr, prev) {
Iif (curr.mtime.getTime() === prev.mtime.getTime()) return;
try {
renderFile(filename);
} catch (e) {
// keep watching when error occured.
console.error(e.stack || e.message || e);
}
});
});
process.on('SIGINT', function() {
process.exit(1);
});
} else {
files.forEach(renderFile);
}
process.on('exit', function () {
console.log();
});
// stdio
} else {
stdin();
}
/**
* Compile from stdin.
*/
function stdin() {
var buf = '';
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(chunk){ buf += chunk; });
process.stdin.on('end', function(){
var output;
if (options.client) {
output = jade.compileClient(buf, options);
} else {
var fn = jade.compile(buf, options);
var output = fn(options);
}
process.stdout.write(output);
}).resume();
process.on('SIGINT', function() {
process.stdout.write('\n');
process.stdin.emit('end');
process.stdout.write('\n');
process.exit();
})
}
/**
* Process the given path, compiling the jade files found.
* Always walk the subdirectories.
*/
function renderFile(path) {
var re = /\.jade$/;
var stat = fs.lstatSync(path);
// Found jade file/\.jade$/
Eif (stat.isFile() && re.test(path)) {
var str = fs.readFileSync(path, 'utf8');
options.filename = path;
if (program.nameAfterFile) {
options.name = getNameFromFileName(path);
}
var fn = options.client ? jade.compileClient(str, options) : jade.compile(str, options);
// --extension
if (program.extension) var extname = '.' + program.extension;
else if (options.client) var extname = '.js';
else var extname = '.html';
path = path.replace(re, extname);
Iif (program.out) path = join(program.out, basename(path));
var dir = resolve(dirname(path));
mkdirp.sync(dir, 0755);
var output = options.client ? fn : fn(options);
fs.writeFileSync(path, output);
console.log(' \033[90mrendered \033[36m%s\033[0m', path);
// Found directory
} else if (stat.isDirectory()) {
var files = fs.readdirSync(path);
files.map(function(filename) {
return path + '/' + filename;
}).forEach(renderFile);
}
}
/**
* Get a sensible name for a template function from a file path
*
* @param {String} filename
* @returns {String}
*/
function getNameFromFileName(filename) {
var file = path.basename(filename, '.jade');
return file.toLowerCase().replace(/[^a-z0-9]+([a-z])/g, function (_, character) {
return character.toUpperCase();
}) + 'Template';
}
|