UNPKG

2.6 kBJavaScriptView Raw
1/*
2 * grunt
3 * https://github.com/cowboy/grunt
4 *
5 * Copyright (c) 2012 "Cowboy" Ben Alman
6 * Licensed under the MIT license.
7 * http://benalman.com/about/license/
8 */
9
10var grunt = require('../grunt');
11
12// The module to be exported.
13var template = module.exports = {};
14
15// External libs.
16template.date = require('dateformat');
17
18// Format today's date.
19template.today = function(format) {
20 return template.date(new Date(), format);
21};
22
23// Set underscore template delimiters.
24template.delimiters = function(mode) {
25 var modes = {
26 // The underscore default template syntax should be a pretty sane default.
27 default: {
28 // Used by grunt.
29 opener: '<%',
30 // Used by underscore.
31 evaluate: /<%([\s\S]+?)%>/g,
32 interpolate: /<%=([\s\S]+?)%>/g,
33 escape: /<%-([\s\S]+?)%>/g
34 },
35 // The "init" task needs separate delimiters to avoid conflicts, so the <>
36 // are replaced with {}. Otherwise, they behave the same.
37 init: {
38 // Used by grunt.
39 opener: '{%',
40 // Used by underscore.
41 evaluate: /\{%([\s\S]+?)%\}/g,
42 interpolate: /\{%=([\s\S]+?)%\}/g,
43 escape: /\{%-([\s\S]+?)%\}/g
44 }
45 };
46 var settings = modes[mode in modes ? mode : 'default'];
47 grunt.utils._.templateSettings = settings;
48 // Get opener character for grunt to use.
49 var opener = settings.opener;
50 // Remove it from the underscore object and return it.
51 delete settings.opener;
52 return opener;
53};
54
55// Process template + data with underscore.
56template.process = function(template, data, mode) {
57 // Set delimiters, and get a opening match character.
58 var opener = grunt.template.delimiters(mode);
59 // Clone data, initializing to config data or empty object if omitted.
60 data = Object.create(data || grunt.config() || {});
61 // Expose grunt so that grunt utilities can be accessed, but only if it
62 // doesn't conflict with an existing .grunt property.
63 if (!('grunt' in data)) { data.grunt = grunt; }
64 // Keep track of last change.
65 var last = template;
66 try {
67 // As long as template contains template tags, render it and get the result,
68 // otherwise just use the template string.
69 while (template.indexOf(opener) >= 0) {
70 template = grunt.utils._.template(template)(data);
71 // Abort if template didn't change - nothing left to process!
72 if (template === last) { break; }
73 last = template;
74 }
75 } catch (e) {
76 grunt.warn('An error occurred while processing a template (' + e.message + ').');
77 }
78 // Normalize linefeeds and return.
79 return grunt.utils.normalizelf(template);
80};