Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Start of template language library.

  • Loading branch information...
commit e6e08df9e3c865e8f6847922c25077109f7ecfe4 1 parent 73c7421
@creationix authored
Showing with 103 additions and 0 deletions.
  1. +71 −0 base.js
  2. +25 −0 simpleCompiler.js
  3. +7 −0 test.js
View
71 base.js
@@ -0,0 +1,71 @@
+// This is a template function. It's decompiled, inserted into, and recompiled
+function compile(locals, callback) {
+ locals.__proto__ = module.exports.helpers;
+ var position = 0, stream, chunks;
+ if (!(typeof callback === 'function')) {
+ if (callback instanceof process.EventEmitter) {
+ stream = callback;
+ callback = undefined;
+ } else {
+ throw new Error("Type Error: last argument must be either a callback function or stream instance");
+ }
+ }
+ function check() {
+ var pieces = [];
+ while (chunks[position] && position < chunks.length) {
+ if (stream) {
+ pieces.push(chunks[position]);
+ }
+ position++;
+ }
+ if (stream && pieces.length > 0) {
+ stream.emit('data', new Buffer(pieces.join("")));
+ }
+
+ if (position === chunks.length) {
+ if (callback) {
+ callback(null, new Buffer(chunks.join("")));
+ }
+ if (stream) {
+ stream.emit('end');
+ }
+ }
+ }
+ function execute(position, fn) {
+ try {
+ chunks[position] = fn(function (err, result) {
+ if (err) {
+ if (stream) stream.emit('error', err);
+ if (callback) callback(err);
+ return;
+ }
+ chunks[position] = result;
+ check();
+ });
+ } catch (err) {
+ if (stream) stream.emit('error', err);
+ if (callback) callback(err);
+ }
+ }
+
+ process.nextTick(function () {
+ with(locals) {
+ // INSERT GENERATED CODE HERE
+ }
+ });
+}
+
+// Prepare variables for quick code replacement
+var base = compile.toString();
+var match = base.match(/^(\s*)\/\/ INSERT GENERATED CODE HERE/m);
+var indent = match[1];
+
+// Returns full source code for a "compile" function with generated code
+// inserted into it
+module.exports = function insert(parts) {
+ return base.substr(0, match.index) +
+ indent + "// START GENERATED CODE\n" +
+ indent + parts.join("\n" + indent) + "\n" +
+ indent + "// END GENERATED CODE" +
+ base.substr(match.index + match[0].length);
+}
View
25 simpleCompiler.js
@@ -0,0 +1,25 @@
+var base = require('./base');
+var Buffer = require('buffer').Buffer;
+var sys = require('sys');
+
+module.exports = function compile(template, locals, callback) {
+
+ var code = JSON.stringify(template).replace(/@[a-z$_][a-z0-9$_]*/g, function (match) {
+ return '" + ' + match.substr(1) + ' + "';
+ });
+ eval(base(["chunks = new Array(1)", "chunks[0] = " + code, 'check()']));
+
+ // // Generate the code
+ // eval(base([
+ // 'chunks = new Array(5);',
+ // 'chunks[0] = "Hello " + planet + ", my name is ";',
+ // 'execute(1, name);',
+ // 'chunks[2] = " and I am ";',
+ // 'execute(3, age);',
+ // 'chunks[4] = " years old";',
+ // 'check();'
+ // ]));
+
+ // Do a little currying for the user
+ return locals ? compile(locals, callback): compile;
+}
View
7 test.js
@@ -0,0 +1,7 @@
+var compiler = require('./simpleCompiler');
+
+var fn = compiler("Hello @world, today is @today");
+console.log(fn +"");
+fn({world:"Earth",today:new Date()}, function (err, text) {
+ console.log(text);
+});
Please sign in to comment.
Something went wrong with that request. Please try again.