Created
March 12, 2012 21:16
-
-
Save kmiyashiro/2024733 to your computer and use it in GitHub Desktop.
mote grunt task
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*global config:true, task:true*/ | |
config.init({ | |
options: { | |
templates: { | |
namespace: "tmpl", | |
dir: 'templates' | |
} | |
}, | |
pkg: '<json:package.json>', | |
test: { | |
files: ['test/**/*.js'] | |
}, | |
lint: { | |
files: ['grunt.js', 'lib/**/*.js', 'test/**/*.js'] | |
}, | |
watch: { | |
files: 'templates/**/*.mote', | |
tasks: 'default' | |
}, | |
templates: { | |
'public/javascripts/templates.js': ['templates/**/*.mote'] | |
}, | |
jshint: { | |
options: { | |
curly: true, | |
eqeqeq: true, | |
immed: true, | |
latedef: true, | |
newcap: true, | |
noarg: true, | |
sub: true, | |
undef: true, | |
boss: true, | |
eqnull: true, | |
node: true | |
}, | |
globals: {} | |
} | |
}); | |
// Default task. | |
task.registerTask('default', 'templates'); | |
// Mote Templates | |
//---------------- | |
var mote = require('mote'); | |
task.registerBasicTask('templates', | |
'Compile templates', function(data, name) { | |
var namespace = config("options.templates.namespace") || "tmpl", | |
baseDir = config("options.templates.dir") || "templates"; | |
var files = file.expand(data); | |
data.forEach(function(file) { | |
log.writeln(file); | |
}); | |
// Create tmpl file. | |
file.write(name, task.helper("mote", files, namespace, baseDir)); | |
// Fail task if errors were logged. | |
if (task.hadErrors()) { return false; } | |
// Otherwise, print a success message. | |
log.writeln('Templates successfully compiled.'); | |
}); | |
// Much of this is ripped from backbone boilerplate JST helper | |
// https://github.com/tbranyen/backbone-boilerplate/blob/master/build/tasks/jst/jst.js | |
task.registerHelper('mote', function(files, namespace, baseDir) { | |
namespace = "this['" + namespace + "']"; | |
// Comes out looking like this["tmpl"] = this["tmpl"] || {}; | |
var contents = namespace + " = " + namespace + " || {};\n\n"; | |
contents += files ? files.map(function(filepath) { | |
// log.writeln(file.read(filepath)); | |
var templateString = file.read(filepath) | |
.replace(/\\/g, '\\\\') | |
.replace(/"/g, '\\"') | |
.replace(/\r/g, '\\r') | |
.replace(/\n/g, '\\n') | |
.replace(/\t/g, '\\t'), | |
templateFn = 'mote.compile("' + templateString + '");', | |
templateName = filepath | |
.replace(baseDir + '/', '') | |
.replace('.mote', ''), | |
partialFn = 'mote.compilePartial("' + templateName + '", "' + | |
templateString + '");'; | |
log.writeln(templateFn); | |
return namespace + "['" + templateName + "'] = " + templateFn + '\n' + | |
partialFn; | |
}).join("\n\n") : ""; | |
return contents; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment