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'),
Script = process.binding('evals').Script,
cache = { };
Script = process.binding('evals').Script,
cache = { };

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

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

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

cache = {};
cache = {};
}

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

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

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

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

// Use the global defaults
config = exports.config;
}
else {
// 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;
// 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) {
if (config.tags === undefined) {

// Default to the global tags
config.tags = exports.config.tags;
}
else {
// 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;
}
// 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) {
if (config.sandbox === undefined) {

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

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

// Loop through each item in the sandbox
for (var key in 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];
}
// And overwrite any existing sandbox item
sandbox[key] = config.sandbox[key];
}

// Replace the merged sandbox
config.sandbox = sandbox;
}
}
// 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 = '';
// 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();
// 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') {
// 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);
}
// Call the function with the data chunk
onprint(chunk);
}

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

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

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

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

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

// Compile the input into executable javascript
data = data
// Compile the input into executable javascript
data = data
.replace(new RegExp(':\\s*' + et, ['g', 'm']), '{ %>')
.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 + '(.+)' + et, ['g', 'm']), '"); $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 + '(.+)' + et, ['g', 'm']), '"); $1 print("')
.replace(new RegExp('\n', ['g', 'm']), '\\n')
.replace(new RegExp('\r', ['g', 'm']), '\\r')
.replace(new RegExp('\t', ['g', 'm']), '\\t');

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

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

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

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

// Delete the cache entry
delete cache[ident];
}, config.cache);
}
// 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') {
// 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 value
return onprint(output);
}

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

0 comments on commit 0984cb3

Please sign in to comment.