Cleanup stored template names.

This commit is contained in:
kpdecker
2011-08-22 01:39:10 -05:00
parent 17e1b1fab7
commit cbadb33d51
+15 -3
View File
@@ -21,6 +21,11 @@ var optimist = require('optimist')
'type': 'boolean', 'type': 'boolean',
'description': 'Minimize output', 'description': 'Minimize output',
'alias': 'min' 'alias': 'min'
},
'r': {
'type': 'string',
'description': 'Template root. Base value that will be stripped from template names.',
'alias': 'root'
} }
}) })
@@ -41,6 +46,7 @@ var optimist = require('optimist')
var fs = require('fs'), var fs = require('fs'),
handlebars = require('../lib/handlebars'), handlebars = require('../lib/handlebars'),
basename = require('path').basename,
uglify = require('uglify-js'); uglify = require('uglify-js');
var argv = optimist.argv, var argv = optimist.argv,
@@ -58,7 +64,7 @@ if (argv.known) {
} }
var output = ['(function() {\n var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};\n']; var output = ['(function() {\n var template = Handlebars.template, templates = Handlebars.templates = Handlebars.templates || {};\n'];
function processTemplate(template) { function processTemplate(template, root) {
var path = template, var path = template,
stat = fs.statSync(path); stat = fs.statSync(path);
if (stat.isDirectory()) { if (stat.isDirectory()) {
@@ -66,7 +72,7 @@ function processTemplate(template) {
var path = template + '/' + file; var path = template + '/' + file;
if (/\.handlebars$/.test(path) || fs.statSync(path).isDirectory()) { if (/\.handlebars$/.test(path) || fs.statSync(path).isDirectory()) {
processTemplate(path); processTemplate(path, root || template);
} }
}); });
} else { } else {
@@ -77,14 +83,20 @@ function processTemplate(template) {
knownHelpersOnly: argv.o knownHelpersOnly: argv.o
}; };
// Clean the template name
if (!root) {
template = basename(template);
} else if (template.indexOf(root) === 0) {
template = template.substring(root.length+1);
} }
template = template.replace(/\.handlebars$/, '');
output.push('templates[\'' + template + '\'] = template(' + handlebars.precompile(data, options) + ');\n'); output.push('templates[\'' + template + '\'] = template(' + handlebars.precompile(data, options) + ');\n');
} }
} }
argv._.forEach(function(template) { argv._.forEach(function(template) {
processTemplate(template); processTemplate(template, argv.root);
}); });
// Output the content // Output the content