Skip to content

Commit

Permalink
Replaced spaces with tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver Morgan committed Sep 17, 2010
1 parent e6b51e1 commit 0984cb3
Showing 1 changed file with 103 additions and 103 deletions.
206 changes: 103 additions & 103 deletions index.js
Expand Up @@ -23,21 +23,21 @@
*/ */


var crypto = require('crypto'), var crypto = require('crypto'),
Script = process.binding('evals').Script, Script = process.binding('evals').Script,
cache = { }; cache = { };


// Defines parrot's version // Defines parrot's version
exports.version = '0.2.2'; exports.version = '0.2.2';


// Global configuration // Global configuration
exports.config = { exports.config = {
sandbox: {}, sandbox: {},
cache: 5, cache: 5,
buffer: true, buffer: true,
tags: { tags: {
start: '<%', start: '<%',
end: '%>' end: '%>'
} }
}; };


/** /**
Expand All @@ -47,7 +47,7 @@ exports.config = {
*/ */
exports.clearCache = function() { exports.clearCache = function() {


cache = {}; cache = {};
} }


/** /**
Expand All @@ -59,133 +59,133 @@ exports.clearCache = function() {
*/ */
exports.render = function(data, config, onprint) { exports.render = function(data, config, onprint) {


// If config is given as a function // If config is given as a function
if (typeof config === 'function') { if (typeof config === 'function') {


// Swap the parameters // Swap the parameters
onprint = config; onprint = config;
config = undefined; config = undefined;
} }


if (config === undefined) { if (config === undefined) {


// Use the global defaults // Use the global defaults
config = exports.config; config = exports.config;
} }
else { else {


// Set the cache and buffer configuration if none is defiend // Set the cache and buffer configuration if none is defiend
config.cache = config.cache || exports.config.cache; config.cache = config.cache || exports.config.cache;
config.buffer = config.buffer || exports.config.buffer; config.buffer = config.buffer || exports.config.buffer;


if (config.tags === undefined) { if (config.tags === undefined) {


// Default to the global tags // Default to the global tags
config.tags = exports.config.tags; config.tags = exports.config.tags;
} }
else { else {


// Default to the global tags if they aren't set // Default to the global tags if they aren't set
config.tags.start = config.tags.start || exports.config.tags.start; config.tags.start = config.tags.start || exports.config.tags.start;
config.tags.end = config.tags.end || exports.config.tags.end; config.tags.end = config.tags.end || exports.config.tags.end;
} }


if (config.sandbox === undefined) { if (config.sandbox === undefined) {


// Set the sandbox defaults // Set the sandbox defaults
config.sandbox = exports.config.sandbox; config.sandbox = exports.config.sandbox;
} }
else { else {


// Default to the global sandbox // Default to the global sandbox
var sandbox = exports.config.sandbox; var sandbox = exports.config.sandbox;


// Loop through each item in the sandbox // Loop through each item in the sandbox
for (var key in config.sandbox) { for (var key in config.sandbox) {


// And overwrite any existing sandbox item // And overwrite any existing sandbox item
sandbox[key] = config.sandbox[key]; sandbox[key] = config.sandbox[key];
} }


// Replace the merged sandbox // Replace the merged sandbox
config.sandbox = sandbox; config.sandbox = sandbox;
} }
} }


// Short forms for the start and end tags and get the parent callee // Short forms for the start and end tags and get the parent callee
var et = config.tags.end, var et = config.tags.end,
st = config.tags.start, st = config.tags.start,
ident = crypto.createHash('md5').update(data).digest('base64'), ident = crypto.createHash('md5').update(data).digest('base64'),
output = ''; output = '';


// Override the print function // Override the print function
config.sandbox.print = function(chunk) { config.sandbox.print = function(chunk) {
// We can only accept strings // We can only accept strings
chunk = chunk.toString(); chunk = chunk.toString();


// If the buffer configuration was set to false and the user defined a function // If the buffer configuration was set to false and the user defined a function
if ( ! config.buffer && typeof onprint === 'function') { if ( ! config.buffer && typeof onprint === 'function') {


// Call the function with the data chunk // Call the function with the data chunk
onprint(chunk); onprint(chunk);
} }


// Append any data to the output buffer // Append any data to the output buffer
output += chunk; output += chunk;
} }


// If the output is already cached // If the output is already cached
if (cache[ident] !== undefined) { if (cache[ident] !== undefined) {


// Print the entire output // Print the entire output
config.sandbox.print(cache[ident]); config.sandbox.print(cache[ident]);


// And return the output // And return the output
return output; return output;
} }


// Parrot can only process strings // Parrot can only process strings
data = data.toString(); data = data.toString();
// Escape double quoted strings and default to print // Escape double quoted strings and default to print
data = 'print("' + data.replace(/"/gm, '\\"') + '");'; data = 'print("' + data.replace(/"/gm, '\\"') + '");';


// Compile the input into executable javascript // Compile the input into executable javascript
data = data data = data
.replace(new RegExp(':\\s*' + et, ['g', 'm']), '{ %>') .replace(new RegExp(':\\s*' + et, ['g', 'm']), '{ %>')
.replace(new RegExp(st + '=(.+)' + et, ['g', 'm']), '"); print($1); print("') .replace(new RegExp(st + '=(.+)' + et, ['g', 'm']), '"); print($1); print("')
.replace(new RegExp(st + '\\s*end(if|while|for|switch);*\\s*' + et, ['g', 'm', 'i']), '"); } print("') .replace(new RegExp(st + '\\s*end(if|while|for|switch);*\\s*' + et, ['g', 'm', 'i']), '"); } print("')
.replace(new RegExp(st + '(.+)' + et, ['g', 'm']), '"); $1 print("') .replace(new RegExp(st + '(.+)' + et, ['g', 'm']), '"); $1 print("')
.replace(new RegExp('\n', ['g', 'm']), '\\n') .replace(new RegExp('\n', ['g', 'm']), '\\n')
.replace(new RegExp('\r', ['g', 'm']), '\\r') .replace(new RegExp('\r', ['g', 'm']), '\\r')
.replace(new RegExp('\t', ['g', 'm']), '\\t'); .replace(new RegExp('\t', ['g', 'm']), '\\t');


// Execute the script, rendering the template // Execute the script, rendering the template
Script.runInNewContext(data, config.sandbox); Script.runInNewContext(data, config.sandbox);


// If we have a valid cache amount // If we have a valid cache amount
if (config.cache > 0) { if (config.cache > 0) {


// Cache the output // Cache the output
cache[ident] = output; cache[ident] = output;


// Set a timeout of the time // Set a timeout of the time
setTimeout(function() { setTimeout(function() {


// Delete the cache entry // Delete the cache entry
delete cache[ident]; delete cache[ident];
}, config.cache); }, config.cache);
} }


// If we have been buffering the output and onprint is a function // If we have been buffering the output and onprint is a function
if (config.buffer && typeof onprint == 'function') { if (config.buffer && typeof onprint == 'function') {


// Return the output value // Return the output value
return onprint(output); return onprint(output);
} }


// Return the output // Return the output
return output; return output;
}; };

0 comments on commit 0984cb3

Please sign in to comment.