description and source-codegrunt-contrib-pug = function (grunt) {
var lib = require('./lib/pug');
var chalk = require('chalk');
// content conversion for templates
var defaultProcessContent = function(content) {
return content;
};
// filename conversion for templates
var defaultProcessName = function(name) {
return name.replace('.pug', '');
};
grunt.registerMultiTask('pug', 'Compile pug templates.', function() {
var options = this.options({
namespace: 'JST',
separator: grunt.util.linefeed + grunt.util.linefeed,
amd: false
});
var data = options.data;
delete options.data;
var nsInfo;
if (options.namespace !== false) {
nsInfo = lib.getNamespaceDeclaration(options.namespace);
}
// assign transformation functions
var processContent = options.processContent || defaultProcessContent;
var processName = options.processName || defaultProcessName;
this.files.forEach(function(f) {
var templates = [];
f.src.filter(function(filepath) {
// warn on and remove invalid source files (if nonull was set)
if (!grunt.file.exists(filepath)) {
grunt.log.warn('Source file "' + filepath + '" not found.');
return false;
}
return true;
})
.forEach(function(filepath) {
var src = processContent(grunt.file.read(filepath), filepath);
var compiled, filename;
filename = processName(filepath);
options.filename = filepath;
try {
var pug = f.orig.pug = require('pug');
if (typeof data === 'function') {
// if data is function, bind to f.orig, passing f.dest and f.src
f.orig.data = data.call(f.orig, f.dest, f.src);
} else {
f.orig.data = data;
}
if (options.filters) {
if (!f.orig.data.filters) {
f.orig.data.filters = {};
}
Object.keys(options.filters).forEach(function(filter) {
f.orig.data.filters[filter] = options.filters[filter].bind(f.orig);
options.filters[filter] = options.filters[filter].bind(f.orig);
});
}
// if in client mode, return function source
if (options.client) {
compiled = pug.compileClient(src, options).toString();
} else {
compiled = pug.compile(src, options)(f.orig.data);
}
// if configured for AMD and the namespace has been explicitly set
// to false, the Pug template will be directly returned
if (options.client && options.amd && options.namespace === false) {
compiled = 'return ' + compiled;
}
} catch (e) {
grunt.log.error(e);
grunt.fail.warn('Pug failed to compile "' + filepath + '".');
return false;
}
if (options.client && options.namespace !== false) {
templates.push(nsInfo.namespace + '[' + JSON.stringify(filename) + '] = ' + compiled + ';');
} else {
templates.push(compiled);
}
});
var output = templates;
if (output.length < 1) {
grunt.log.warn('Destination not written because compiled files were empty.');
} else {
if (options.client && options.namespace !== false) {
output.unshift(nsInfo.declaration);
if (options.node) {
output.unshift('var pug = pug || require(\'pug/lib/runtime\');');
var nodeExport = 'if (typeof exports === \'object\' && exports) {';
nodeExport += 'module.exports = ' + nsInfo.namespace + ';}';
output.push(nodeExport);
}
}
if (options.amd) {
// wrap the file in an AMD define function
output.unshift('define([\'pug\'], function(pug) { if(pug && pug[\'runtime\'] !== undefined) { pug = pug.runtime; }');
if (options.namespace !== false) {
// namespace has not been explicitly set to false;
// the AMD wrapper will return the object containing the template
output.p ...