3 var grunt = require('../grunt');
5 // The module to be exported.
6 var template = module.exports = {};
9 template.date = require('dateformat');
11 // Format today's date.
12 template.today = function(format) {
13 return template.date(new Date(), format);
16 // Template delimiters.
17 var allDelimiters = {};
19 // Initialize template delimiters.
20 template.addDelimiters = function(name, opener, closer) {
21 var delimiters = allDelimiters[name] = {};
23 delimiters.opener = opener;
24 delimiters.closer = closer;
25 // Generate RegExp patterns dynamically.
26 var a = delimiters.opener.replace(/(.)/g, '\\$1');
27 var b = '([\\s\\S]+?)' + delimiters.closer.replace(/(.)/g, '\\$1');
30 evaluate: new RegExp(a + b, 'g'),
31 interpolate: new RegExp(a + '=' + b, 'g'),
32 escape: new RegExp(a + '-' + b, 'g')
36 // The underscore default template syntax should be a pretty sane default for
38 template.addDelimiters('config', '<%', '%>');
40 // Set Lo-Dash template delimiters.
41 template.setDelimiters = function(name) {
42 // Get the appropriate delimiters.
43 var delimiters = allDelimiters[name in allDelimiters ? name : 'config'];
44 // Tell Lo-Dash which delimiters to use.
45 grunt.util._.extend(grunt.util._.templateSettings, delimiters.lodash);
46 // Return the delimiters.
50 // Process template + data with Lo-Dash.
51 template.process = function(tmpl, options) {
52 if (!options) { options = {}; }
53 // Set delimiters, and get a opening match character.
54 var delimiters = template.setDelimiters(options.delimiters);
55 // Clone data, initializing to config data or empty object if omitted.
56 var data = Object.create(options.data || grunt.config.data || {});
57 // Expose grunt so that grunt utilities can be accessed, but only if it
58 // doesn't conflict with an existing .grunt property.
59 if (!('grunt' in data)) { data.grunt = grunt; }
60 // Keep track of last change.
63 // As long as tmpl contains template tags, render it and get the result,
64 // otherwise just use the template string.
65 while (tmpl.indexOf(delimiters.opener) >= 0) {
66 tmpl = grunt.util._.template(tmpl, options)(data);
67 // Abort if template didn't change - nothing left to process!
68 if (tmpl === last) { break; }
72 // In upgrading to Lo-Dash (or Underscore.js 1.3.3), \n and \r in template
73 // tags now causes an exception to be thrown. Warn the user why this is
74 // happening. https://github.com/documentcloud/underscore/issues/553
75 if (String(e) === 'SyntaxError: Unexpected token ILLEGAL' && /\n|\r/.test(tmpl)) {
76 grunt.log.errorlns('A special character was detected in this template. ' +
77 'Inside template tags, the \\n and \\r special characters must be ' +
78 'escaped as \\\\n and \\\\r. (grunt 0.4.0+)');
80 // Slightly better error message.
81 e.message = 'An error occurred while processing a template (' + e.message + ').';
82 grunt.warn(e, grunt.fail.code.TEMPLATE_ERROR);
84 // Normalize linefeeds and return.
85 return grunt.util.normalizelf(tmpl);