Permalink
Browse files

fix lazy compiliation of included templates

  • Loading branch information...
Dustin Machi
Dustin Machi committed Sep 29, 2010
1 parent 40018ed commit 370053f46a3b455903dce92ad57e369cfc7cb8c0
Showing with 280 additions and 266 deletions.
  1. +41 −31 lib/parse.js
  2. +2 −1 lib/resolver.js
  3. +230 −227 lib/rules.js
  4. +7 −7 lib/template.js
View
@@ -1,21 +1,24 @@
var when = require("promised-io/promise").when,
defer = require("promised-io/promise").defer,
- toJSON = require("commonjs-utils/json-ext").stringify;
-
+ lazyArray = require("commonjs-utils/lazy-array").LazyArray,
+ toJSON = require("commonjs-utils/json-ext").stringify,
+ all = require("promised-io/promise").all;
exports.parse = function(source,state,options){
- var functionText, rawSource = source,
+ var functionText,
srcArray= exports.toArray(source,state,options.rules);
+ source = (typeof source=='array')?source:[source];
+
return when(srcArray, function(sourceArray){
if ((!sourceArray.tags) || (sourceArray.tags.length<1)){
- functionText = ["_write(", source, ");"].join("");
+ return ["_write(", source, ");"].join('');
}else{
var sourceTree = exports.toTree(sourceArray.all, state, options.rules);
-
functionText = exports.toScript(sourceTree, escapeChars, state,options.rules);
+
if (state._extend){
//print("perform _extend: ");
- functionText = extendTemplate(state,options.rules);
+ lazyScriptParts = extendTemplate(state,options.rules);
//print("EXTENDED FUNCTION TEXT: " + this.functionText);
}
}
@@ -28,10 +31,11 @@ var escapeChars = ['\\', '\'', '"', ['\n','\\n'], ['\t','\\t'], ['\r', '\\r']];
var escape = function(source, list) {
for(var i=0; i<list.length; i++) {
- if(list[i] instanceof Array)
+ if(list[i] instanceof Array){
source = source.replace(new RegExp(list[i][0],'gi'), list[i][1]);
- else
+ }else{
source = source.replace(new RegExp('\\'+list[i],'gi'), '\\'+list[i]);
+ }
}
return source;
};
@@ -42,7 +46,6 @@ exports.toArray= function(src, state, rules) {
var text = [],
tags = [],
all = [];
-
return when(src.forEach(function(source){
source = source.decodeToString ? source.decodeToString() : '' + source;
while(source) {
@@ -77,7 +80,7 @@ exports.toArray= function(src, state, rules) {
};
exports.parseArguments = function(source, expr) {
- var args = exports.toArray(expr, {}, {expr:{opener:'\{',closer:'\}'}}).tags;
+ var args = exports.toArray([expr], {}, {expr:{opener:'\{',closer:'\}'}}).tags;
expr = escape(expr, ['(' ,')' ,'[' ,']', ',', '.', '<', '>', '*', '$', '@']);
for(var i=0; i<args.length; i++) {
expr = expr.replace(args[i],'(.*)');
@@ -200,29 +203,36 @@ exports.toScript = function(tree, list, state, rules) {
if(typeof tree == 'string'){
return '_write(\''+escape(tree, list)+'\');';
}
-
- var result, content = [];
-
- if(tree.children){
- for(var i=0; i<tree.children.length; i++){
- content.push(exports.toScript(tree.children[i], list, state, rules));
- }
- }
- if(!tree.isDocument) {
- if(tree.tagname){
- return rules[tree.delimiter].tags[tree.tagname].handler(tree, content.join(''), state);
- }else{
- try {
- return rules[tree.delimiter].handler(tree, content.join(''), state);
- }catch(err){
- print("Caught error : " + err);
- }
- }
- } else {
- return content.join('');
+ var content = [];
+
+ if(tree.children){
+ content = tree.children.map(function(child){
+ var script =exports.toScript(child, list, state, rules);
+ return when(script, function(script){
+ return script;
+ });
+ });
}
-};
+ return when(all(content), function(content){
+ content=content.join('');
+ if(!tree.isDocument) {
+ if(tree.tagname){
+ return when(content, function(content){
+ var h = rules[tree.delimiter].tags[tree.tagname].handler(tree, content, state);
+ return when(h, function(h){return h;});
+ });
+ }else{
+ return when(content, function(content){
+ var h = rules[tree.delimiter].handler(tree, content, state);
+ return when(h, function(h){return h;});
+ });
+ }
+ } else {
+ return content;
+ }
+ });
+}
exports.XMLToJSON = function(xml) {
var result;
View
@@ -20,6 +20,7 @@ exports.Resolver = function(store,options) {
var templateString="";
template = store.get(tid);
if (template){
+ //print("Found Template: " + tid);
return template;
}
if (exact){return}
@@ -34,7 +35,7 @@ exports.Resolver = function(store,options) {
}
//no default template give a really basic default
- return "<html><body>{{$data}}<body></html>";
+ return ["<html><body>{{$data}}<body></html>"];
}
}
Oops, something went wrong.

0 comments on commit 370053f

Please sign in to comment.