-
Notifications
You must be signed in to change notification settings - Fork 7
/
parrot.js
executable file
·195 lines (154 loc) · 4.93 KB
/
parrot.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* License Information (MIT)
*
* Copyright (c) 2010 Oliver Morgan ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
var crypto = require('crypto'),
Script = require('vm'),
cache = { };
// Defines parrot's version
exports.version = '0.3.1';
// Global configuration
exports.config = {
sandbox: {},
cache: 5,
buffer: true,
tags: {
start: '<%',
end: '%>'
}
};
/**
* Clear's parrots internal cache
*
* @return undefined
*/
exports.clearCache = function() {
cache = {};
};
/**
* Renders a template
*
* @param data string The input data
* @param config object Any optional configuration options
* @return The rendered template
*/
exports.render = function(data, config, onprint) {
// If config is given as a function
if (typeof config === 'function') {
// Swap the parameters
onprint = config;
config = undefined;
}
if (config === undefined) {
// Use the global defaults
config = exports.config;
}
else {
// Set the cache and buffer configuration if none is defiend
config.cache = config.cache || exports.config.cache;
config.buffer = config.buffer || exports.config.buffer;
if (config.tags === undefined) {
// Default to the global tags
config.tags = exports.config.tags;
}
else {
// Default to the global tags if they aren't set
config.tags.start = config.tags.start || exports.config.tags.start;
config.tags.end = config.tags.end || exports.config.tags.end;
}
if (config.sandbox === undefined) {
// Set the sandbox defaults
config.sandbox = exports.config.sandbox;
}
else {
// Default to the global sandbox
var sandbox = exports.config.sandbox;
// Loop through each item in the sandbox
for (var key in config.sandbox) {
// And overwrite any existing sandbox item
sandbox[key] = config.sandbox[key];
}
// Replace the merged sandbox
config.sandbox = sandbox;
}
}
// Short forms for the start and end tags and get the parent callee
var et = config.tags.end,
st = config.tags.start,
ident = crypto.createHash('md5').update(data).digest('base64'),
output = '';
// Override the print function
config.sandbox.print = function(chunk) {
// We can only accept strings
chunk = chunk.toString();
// If the buffer configuration was set to false and the user defined a function
if ( ! config.buffer && typeof onprint === 'function') {
// Call the function with the data chunk
onprint(chunk);
}
// Append any data to the output buffer
output += chunk;
};
// If the output is already cached
if (cache[ident] !== undefined) {
// Print the entire output
config.sandbox.print(cache[ident]);
// And return the output
return output;
}
// Parrot can only process strings
data = data.toString();
// Compile the input into executable javascript
data = data
.replace(new RegExp('(^|' + et + ')[^]+?($|' + st + ')', 'g'), function(match) {
return match
.replace(/"/g, '\\"')
.replace(/\n/g, '\\n')
.replace(/\r/g, '\\r')
.replace(/\t/g, '\\t');
})
.replace(new RegExp(':\\s*' + et, 'gm'), '{ ' + et)
.replace(new RegExp(st + '=([^]+?)' + et, 'gm'), '");\nprint($1);\nprint("')
.replace(new RegExp(st + '\\s*end(if|while|for|switch);?\\s*' + et, 'gmi'), '");\n}\nprint("')
.replace(new RegExp(st + '([^]+?)' + et, 'gm'), '");\n$1\nprint("');
// Add outer print function
data = 'print("' + data + '");';
// Execute the script, rendering the template
Script.runInNewContext(data, config.sandbox);
// If we have a valid cache amount
if (config.cache > 0) {
// Cache the output
cache[ident] = output;
// Set a timeout of the time
setTimeout(function() {
// Delete the cache entry
delete cache[ident];
}, config.cache);
}
// If we have been buffering the output and onprint is a function
if (config.buffer && typeof onprint == 'function') {
// Return the output value
return onprint(output);
}
// Return the output
return output;
};