-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
105 lines (90 loc) · 2.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*!
* handlebars-delimiters <https://github.com/jonschlinkert/handlebars-delimiters>
*
* Copyright (c) 2014-2017, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
/**
* RegExp cache
*/
var cache = {};
/**
* Pass `Handlebars` and the `delimiters` to use as replacements. This
* patches the `Handlebars.compile` method to automatically use the
* custom delimiters when compiling.
*
* ```js
* var delimiters = require('handlebars-delimiters');
* var handlebars = require('handlebars');
* delimiters(handlebars, ['<%', '%>']);
* // you can now use handlebars as usual, but with the new delimiters
* ```
* @param {Object} `Handlebars`
* @param {Array} `delimiters` Array with open and close delimiters, like `['<%', '%>']`
* @return {undefined}
* @api public
*/
module.exports = function(Handlebars, delimiters) {
if (delimiters[0].slice(-1) !== '=') {
delimiters[0] += '(?!=)';
}
var source = delimiters[0] + '([\\s\\S]+?)' + delimiters[1];
// Idea for compile method from http://stackoverflow.com/a/19181804/1267639
if (!Handlebars._compile) {
Handlebars._compile = Handlebars.compile;
}
Handlebars.compile = function(str) {
var args = [].slice.call(arguments);
if (typeof str === 'string') {
if(delimiters[0] !== '{{' && delimiters[1] !== '}}') {
args[0] = escapeDelimiters(args[0]);
}
args[0] = replaceDelimiters(args[0], source);
}
return Handlebars._compile.apply(Handlebars, args);
};
};
/**
* Replace or delimiters in the given string.
*
* ```js
* var replaced = delimiters.replace(str, ['<%=', '%>']);
* ```
* @name .replace
* @param {String} `str` String with handlebars to replace or escape.
* @param {String} `source` The delimiters regex source string to conver to a regular expression.
* @param {Boolean} `escape` If true, replacements are escaped with a double-slash.
* @return {String}
* @api public
*/
function replaceDelimiters(str, source, escape) {
var regex = cache[source] || (cache[source] = new RegExp(source, 'g'));
var match;
while ((match = regex.exec(str))) {
var prefix = str.slice(0, match.index);
var inner = (escape ? '\\' : '') + '{{' + match[1] + '}}';
var suffix = str.slice(match.index + match[0].length);
str = prefix + inner + suffix;
}
return str;
}
/**
* Escape handlebars delimiters in the given string.
*
* ```js
* var escaped = delimiters.escape(str);
* ```
* @name .escape
* @param {String} `str` String with handlebars to replace or escape.
* @return {String}
* @api public
*/
function escapeDelimiters(str) {
return replaceDelimiters(str, '{{([\\s\\S]+?)}}', true);
}
/**
* Expose `escapeDelimiters` and `replaceDelimiters`
*/
module.exports.replace = replaceDelimiters;
module.exports.escape = escapeDelimiters;