Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Replaced spaces with tabs

  • Loading branch information...
commit 0984cb39266aec9e76fc024177178af2d787285a 1 parent e6b51e1
Oliver Morgan authored

Showing 1 changed file with 103 additions and 103 deletions. Show diff stats Hide diff stats

  1. +103 103 index.js
206 index.js
@@ -23,21 +23,21 @@
23 23 */
24 24
25 25 var crypto = require('crypto'),
26   - Script = process.binding('evals').Script,
27   - cache = { };
  26 + Script = process.binding('evals').Script,
  27 + cache = { };
28 28
29 29 // Defines parrot's version
30 30 exports.version = '0.2.2';
31 31
32 32 // Global configuration
33 33 exports.config = {
34   - sandbox: {},
35   - cache: 5,
36   - buffer: true,
37   - tags: {
38   - start: '<%',
39   - end: '%>'
40   - }
  34 + sandbox: {},
  35 + cache: 5,
  36 + buffer: true,
  37 + tags: {
  38 + start: '<%',
  39 + end: '%>'
  40 + }
41 41 };
42 42
43 43 /**
@@ -47,7 +47,7 @@ exports.config = {
47 47 */
48 48 exports.clearCache = function() {
49 49
50   - cache = {};
  50 + cache = {};
51 51 }
52 52
53 53 /**
@@ -59,133 +59,133 @@ exports.clearCache = function() {
59 59 */
60 60 exports.render = function(data, config, onprint) {
61 61
62   - // If config is given as a function
63   - if (typeof config === 'function') {
  62 + // If config is given as a function
  63 + if (typeof config === 'function') {
64 64
65   - // Swap the parameters
66   - onprint = config;
67   - config = undefined;
68   - }
  65 + // Swap the parameters
  66 + onprint = config;
  67 + config = undefined;
  68 + }
69 69
70   - if (config === undefined) {
  70 + if (config === undefined) {
71 71
72   - // Use the global defaults
73   - config = exports.config;
74   - }
75   - else {
  72 + // Use the global defaults
  73 + config = exports.config;
  74 + }
  75 + else {
76 76
77   - // Set the cache and buffer configuration if none is defiend
78   - config.cache = config.cache || exports.config.cache;
  77 + // Set the cache and buffer configuration if none is defiend
  78 + config.cache = config.cache || exports.config.cache;
79 79 config.buffer = config.buffer || exports.config.buffer;
80 80
81   - if (config.tags === undefined) {
  81 + if (config.tags === undefined) {
82 82
83   - // Default to the global tags
84   - config.tags = exports.config.tags;
85   - }
86   - else {
  83 + // Default to the global tags
  84 + config.tags = exports.config.tags;
  85 + }
  86 + else {
87 87
88   - // Default to the global tags if they aren't set
89   - config.tags.start = config.tags.start || exports.config.tags.start;
90   - config.tags.end = config.tags.end || exports.config.tags.end;
91   - }
  88 + // Default to the global tags if they aren't set
  89 + config.tags.start = config.tags.start || exports.config.tags.start;
  90 + config.tags.end = config.tags.end || exports.config.tags.end;
  91 + }
92 92
93   - if (config.sandbox === undefined) {
  93 + if (config.sandbox === undefined) {
94 94
95   - // Set the sandbox defaults
96   - config.sandbox = exports.config.sandbox;
97   - }
98   - else {
  95 + // Set the sandbox defaults
  96 + config.sandbox = exports.config.sandbox;
  97 + }
  98 + else {
99 99
100   - // Default to the global sandbox
101   - var sandbox = exports.config.sandbox;
  100 + // Default to the global sandbox
  101 + var sandbox = exports.config.sandbox;
102 102
103   - // Loop through each item in the sandbox
104   - for (var key in config.sandbox) {
  103 + // Loop through each item in the sandbox
  104 + for (var key in config.sandbox) {
105 105
106   - // And overwrite any existing sandbox item
107   - sandbox[key] = config.sandbox[key];
108   - }
  106 + // And overwrite any existing sandbox item
  107 + sandbox[key] = config.sandbox[key];
  108 + }
109 109
110   - // Replace the merged sandbox
111   - config.sandbox = sandbox;
112   - }
113   - }
  110 + // Replace the merged sandbox
  111 + config.sandbox = sandbox;
  112 + }
  113 + }
114 114
115   - // Short forms for the start and end tags and get the parent callee
116   - var et = config.tags.end,
117   - st = config.tags.start,
118   - ident = crypto.createHash('md5').update(data).digest('base64'),
119   - output = '';
  115 + // Short forms for the start and end tags and get the parent callee
  116 + var et = config.tags.end,
  117 + st = config.tags.start,
  118 + ident = crypto.createHash('md5').update(data).digest('base64'),
  119 + output = '';
120 120
121   - // Override the print function
122   - config.sandbox.print = function(chunk) {
123   -
124   - // We can only accept strings
125   - chunk = chunk.toString();
  121 + // Override the print function
  122 + config.sandbox.print = function(chunk) {
  123 +
  124 + // We can only accept strings
  125 + chunk = chunk.toString();
126 126
127   - // If the buffer configuration was set to false and the user defined a function
128   - if ( ! config.buffer && typeof onprint === 'function') {
  127 + // If the buffer configuration was set to false and the user defined a function
  128 + if ( ! config.buffer && typeof onprint === 'function') {
129 129
130   - // Call the function with the data chunk
131   - onprint(chunk);
132   - }
  130 + // Call the function with the data chunk
  131 + onprint(chunk);
  132 + }
133 133
134   - // Append any data to the output buffer
135   - output += chunk;
136   - }
  134 + // Append any data to the output buffer
  135 + output += chunk;
  136 + }
137 137
138   - // If the output is already cached
139   - if (cache[ident] !== undefined) {
  138 + // If the output is already cached
  139 + if (cache[ident] !== undefined) {
140 140
141   - // Print the entire output
142   - config.sandbox.print(cache[ident]);
  141 + // Print the entire output
  142 + config.sandbox.print(cache[ident]);
143 143
144   - // And return the output
145   - return output;
146   - }
  144 + // And return the output
  145 + return output;
  146 + }
147 147
148   - // Parrot can only process strings
149   - data = data.toString();
150   -
151   - // Escape double quoted strings and default to print
152   - data = 'print("' + data.replace(/"/gm, '\\"') + '");';
  148 + // Parrot can only process strings
  149 + data = data.toString();
  150 +
  151 + // Escape double quoted strings and default to print
  152 + data = 'print("' + data.replace(/"/gm, '\\"') + '");';
153 153
154   - // Compile the input into executable javascript
155   - data = data
  154 + // Compile the input into executable javascript
  155 + data = data
156 156 .replace(new RegExp(':\\s*' + et, ['g', 'm']), '{ %>')
157   - .replace(new RegExp(st + '=(.+)' + et, ['g', 'm']), '"); print($1); print("')
158   - .replace(new RegExp(st + '\\s*end(if|while|for|switch);*\\s*' + et, ['g', 'm', 'i']), '"); } print("')
159   - .replace(new RegExp(st + '(.+)' + et, ['g', 'm']), '"); $1 print("')
  157 + .replace(new RegExp(st + '=(.+)' + et, ['g', 'm']), '"); print($1); print("')
  158 + .replace(new RegExp(st + '\\s*end(if|while|for|switch);*\\s*' + et, ['g', 'm', 'i']), '"); } print("')
  159 + .replace(new RegExp(st + '(.+)' + et, ['g', 'm']), '"); $1 print("')
160 160 .replace(new RegExp('\n', ['g', 'm']), '\\n')
161 161 .replace(new RegExp('\r', ['g', 'm']), '\\r')
162 162 .replace(new RegExp('\t', ['g', 'm']), '\\t');
163 163
164   - // Execute the script, rendering the template
165   - Script.runInNewContext(data, config.sandbox);
  164 + // Execute the script, rendering the template
  165 + Script.runInNewContext(data, config.sandbox);
166 166
167   - // If we have a valid cache amount
168   - if (config.cache > 0) {
  167 + // If we have a valid cache amount
  168 + if (config.cache > 0) {
169 169
170   - // Cache the output
171   - cache[ident] = output;
  170 + // Cache the output
  171 + cache[ident] = output;
172 172
173   - // Set a timeout of the time
174   - setTimeout(function() {
  173 + // Set a timeout of the time
  174 + setTimeout(function() {
175 175
176   - // Delete the cache entry
177   - delete cache[ident];
178   -
179   - }, config.cache);
180   - }
  176 + // Delete the cache entry
  177 + delete cache[ident];
  178 +
  179 + }, config.cache);
  180 + }
181 181
182   - // If we have been buffering the output and onprint is a function
183   - if (config.buffer && typeof onprint == 'function') {
  182 + // If we have been buffering the output and onprint is a function
  183 + if (config.buffer && typeof onprint == 'function') {
184 184
185   - // Return the output value
186   - return onprint(output);
187   - }
  185 + // Return the output value
  186 + return onprint(output);
  187 + }
188 188
189   - // Return the output
190   - return output;
  189 + // Return the output
  190 + return output;
191 191 };

0 comments on commit 0984cb3

Please sign in to comment.
Something went wrong with that request. Please try again.