Permalink
Browse files

promises, promises

  • Loading branch information...
Dustin Machi
Dustin Machi committed Sep 28, 2010
1 parent f9790c5 commit a966b924b61e5728e86c37a4288a39d2bb0ba469
Showing with 83 additions and 89 deletions.
  1. +50 −65 lib/parse.js
  2. +15 −11 lib/resolver.js
  3. +14 −12 lib/template.js
  4. +4 −1 lib/templify.js
View
@@ -1,26 +1,26 @@
+var when = require("promised-io/promise").when,
+ defer = require("promised-io/promise").defer,
+ toJSON = require("commonjs-utils/json-ext").stringify;
exports.parse = function(source,state,options){
- //print("parse options: ", options, options.rules);
var functionText, rawSource = source,
- sourceArray = exports.toArray(source,state,options.rules);
+ srcArray= exports.toArray(source,state,options.rules);
- if ((!sourceArray.tags) || (sourceArray.tags.length<1)){
- //print(" no sourceArray tags")
- functionText = ["_write(", source, ");"].join("");
- }else{
- //print("call toTree()");
- var sourceTree = exports.toTree(sourceArray.all, state, options.rules);
+ return when(srcArray, function(sourceArray){
+ if ((!sourceArray.tags) || (sourceArray.tags.length<1)){
+ functionText = ["_write(", source, ");"].join("");
+ }else{
+ var sourceTree = exports.toTree(sourceArray.all, state, options.rules);
- //print("call toScript()");
- functionText = exports.toScript(sourceTree, escapeChars, state,options.rules);
- if (state._extend){
- //print("perform _extend: ");
- functionText = extendTemplate(state,options.rules);
- //print("EXTENDED FUNCTION TEXT: " + this.functionText);
+ functionText = exports.toScript(sourceTree, escapeChars, state,options.rules);
+ if (state._extend){
+ //print("perform _extend: ");
+ functionText = extendTemplate(state,options.rules);
+ //print("EXTENDED FUNCTION TEXT: " + this.functionText);
+ }
}
- }
-
- return functionText;
+ return functionText;
+ });
}
//TODO there is probably a lib for this somewhere already
@@ -37,48 +37,46 @@ var escape = function(source, list) {
};
-exports.toArray= function(source, state, rules) {
- //print("toArray() rules:", rules);
-// for(var i in rules){
-// print("rules i: " + i);
-// }
+exports.toArray= function(src, state, rules) {
var opener, closer, delimiter;
var text = [],
- tags = [],
- all = [];
-
- while(source) {
- for(var i in rules) {
- if(!delimiter || source.indexOf(rules[delimiter].opener) == -1)
- delimiter = i;
- if(source.indexOf(rules[i].opener) != -1)
- if(source.indexOf(rules[delimiter].opener) > source.indexOf(rules[i].opener))
+ tags = [],
+ all = [];
+
+ return when(src.forEach(function(source){
+ source = source.decodeToString ? source.decodeToString() : '' + source;
+ while(source) {
+ for(var i in rules) {
+ if(!delimiter || source.indexOf(rules[delimiter].opener) == -1)
delimiter = i;
+ if(source.indexOf(rules[i].opener) != -1)
+ if(source.indexOf(rules[delimiter].opener) > source.indexOf(rules[i].opener))
+ delimiter = i;
+ }
+ opener = source.indexOf(rules[delimiter].opener);
+ closer = source.indexOf(rules[delimiter].closer) + rules[delimiter].closer.length;
+
+ if(opener != -1) {
+ text.push(source.substring(0,opener));
+ tags.push(source.substring(opener,closer));
+ source = source.substring(closer);
+ } else {
+ text.push(source);
+ source = '';
+ }
+ }
+ for(var i=0; i<text.length; i++) {
+
+ all.push(text[i]);
+ if(tags[i]){
+ all.push(tags[i]);
+ }
}
- //print("delimiter:" + delimiter);
- opener = source.indexOf(rules[delimiter].opener);
- closer = source.indexOf(rules[delimiter].closer) + rules[delimiter].closer.length;
-
- if(opener != -1) {
- text.push(source.substring(0,opener));
- tags.push(source.substring(opener,closer));
- source = source.substring(closer);
- } else {
- text.push(source);
- source = '';
- }
- }
-
- for(var i=0; i<text.length; i++) {
- all.push(text[i]);
- if(tags[i])
- all.push(tags[i]);
- }
- return {text:text, tags:tags, all:all};
+
+ }), function(){return {text: text, tags: tags, all: all}});
};
exports.parseArguments = function(source, expr) {
- //print("calling toArray with expr from parseArguments", expr);
var args = exports.toArray(expr, {}, {expr:{opener:'\{',closer:'\}'}}).tags;
expr = escape(expr, ['(' ,')' ,'[' ,']', ',', '.', '<', '>', '*', '$', '@']);
for(var i=0; i<args.length; i++) {
@@ -99,10 +97,6 @@ exports.parseTag = function(source, rules ) {
// Parse Tag
var delimiter;
for(var i in rules) {
- //print(" testing for opener: " + rules[i].opener);
- //for (var j in rules[i]){
- // print(" i: " + j + "::::" + rules[i][j]);
- //}
if(source.indexOf(rules[i].opener) == 0) {
delimiter = i;
break;
@@ -115,11 +109,9 @@ exports.parseTag = function(source, rules ) {
// Parse tag name
var tagname = '';
var closer = '';
- //print(" delimiter: " + delimiter);
if(rules[delimiter].tags) {
var tagArray = [];
for(var i in rules[delimiter].tags)
- //print(" tag: " + i);
tagArray.push(i);
var regex = new RegExp('^(\/){0,1}('+tagArray.join('|')+')\\\s*(.*)');
var res = source.match(regex);
@@ -132,7 +124,6 @@ exports.parseTag = function(source, rules ) {
// Parse tag type
if(tagname) {
- //print("TagName: " + tagname);
if(rules[delimiter].tags[tagname].type == 'single' && closer)
return false;
if(rules[delimiter].tags[tagname].type == 'block' && closer)
@@ -165,12 +156,7 @@ exports.toTree = function(array, state, rules) {
current = doc;
for(var i=0; i<array.length; i++) {
- //print("array[i]: " + array[i]);
res = exports.parseTag(array[i], rules);
- //print ("parsed Tag: " );
- //for (var p in res){
- // print(" --- " + p + "--> " + res[p]);
- //}
if(!res) {
if(array[i]) {
array[i].parent = current;
@@ -227,7 +213,6 @@ exports.toScript = function(tree, list, state, rules) {
if(tree.tagname){
return rules[tree.delimiter].tags[tree.tagname].handler(tree, content.join(''), state);
}else{
- //print("exports.toScript w/o tree.tagname: " + serialize(content));
try {
return rules[tree.delimiter].handler(tree, content.join(''), state);
}catch(err){
View
@@ -1,6 +1,3 @@
-/**
- * A very simple filesystem based storage
- */
var when = require("promised-io/promise").when;
/**
@@ -13,7 +10,6 @@ var when = require("promised-io/promise").when;
**/
exports.Resolver = function(store,options) {
- //print("Setup Resolver: " + store);
var defaultTemplate = require("commonjs-utils/settings").defaultTemplate || "default"
return function(id,exact){
print("Resolve Template for ID: " + id);
@@ -22,14 +18,12 @@ exports.Resolver = function(store,options) {
while(parts.length>0){
var tid = parts.join("/");
var templateString="";
- //print("getFrom Store");
+ print("getFrom Store");
+ print("tid: "+ tid);
template = store.get(tid);
if (template){
- print("Found Template: " + tid);
- template.forEach(function(c){templateString+=c.decodeToString()});
- return templateString;
+ return template;
}
-
if (exact){return}
parts.pop();
@@ -38,12 +32,22 @@ exports.Resolver = function(store,options) {
//attempt to load the default template
template = store.get(defaultTemplate);
if (template){
- template.forEach(function(c){templateString+=c.decodeToString()});
- return templateString;
+ return template;
}
//no default template give a really basic default
return "<html><body>{{$data}}<body></html>";
}
}
+function bufferText(obj){
+ var buffered = "";
+ if (obj.forEach){
+ obj.forEach(function(c){buffered+=c.decodeToString?c.decodeToString():c});
+ }else if (typeof obj=='string'){
+ buffered = obj;
+ }else{
+ buffered = buffered;
+ }
+ return buffered;
+}
View
@@ -1,6 +1,6 @@
-var parse = require("./parse").parse;
-
-var defaultRules = require("./rules");
+var parse = require("./parse").parse,
+ when = require("promise").when,
+ defaultRules = require("./rules");
var Template = exports.Template = function(source, options, noWrapper){
if (!options){
@@ -31,15 +31,17 @@ var Template = exports.Template = function(source, options, noWrapper){
].join('');
// footer for compiled template function
- options.footer = '} return $text.join("");'
-
- var functionText = parse(source,state,options);
-
-
- if (!noWrapper){
- return new Function('$data', options.header + functionText + options.footer);
- }
- return functionText;
+ options.footer = '} return $text.join("");';
+ var parsed;
+ return when(source, function(source){
+ parsed = parse(source, state, options);
+ return when(parsed, function(parsed){
+ if (!noWrapper){
+ return new Function('$data', options.header + parsed + options.footer);
+ }
+ return parsed;
+ });
+ });
}
var extendTemplate=function(state,options){
View
@@ -1,13 +1,15 @@
var Template = require("./template").Template;
var Rules = require("./rules").Rules;
+var when = require("promised-io/promise").when;
exports.TemplateEngine=function(options){
var resolver = options.resolver(options.store)
options.resolver = resolver;
options.rules = Rules(options);
var engine = {
compile: function(id){
- return Template(resolver(id),options);
+ var template = Template(resolver(id), options);
+ return template;
}
}
@@ -17,6 +19,7 @@ exports.TemplateEngine=function(options){
exports.Resolver = require("./resolver").Resolver;
exports.renderTemplate = function(templateString, context, options){
+ print("renderTemplate");
var template = new Template(templateString, options);
return template(context);
};

0 comments on commit a966b92

Please sign in to comment.