-
-
Save deanlandolt/599434 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// clip from media handler/serializer | |
//load the template, should return a promise from the compile() method | |
if (response.status > 400) { | |
//print("Error Response: " + response.status); | |
template = templateEngine.compile("/error/"+response.status); | |
}else{ | |
template = templateEngine.compile(templateId, (mediaParams && mediaParams.template)); | |
} | |
//when we get the template function back, execute it which returns the rendered html | |
// i've tried a bunch of different ways to do this, but i'm not sure what should be happening in the when here | |
rendered = when(template, function(t){ print("t: " , t); return t(object)}); | |
//return the promise | |
return rendered; | |
//templify/TemplateEngine | |
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); | |
} | |
} | |
return engine; | |
} | |
//templify/Template | |
var functionText; | |
functionText = parse(source,state,options); | |
var p = promise(); | |
when(functionText, function(ft){; | |
print("functionText args len: ", ft); | |
if (!noWrapper){ | |
print("return func: "+ ft); | |
p.callback(new Function('$data', options.header + functionText + options.footer)); | |
} | |
print('returning function text'+ ft); | |
p.callback(ft); | |
}); | |
return p; | |
//template parser | |
exports.parse = function(source,state,options){ | |
//print("parse options: ", options, options.rules); | |
//src array is a promise | |
var functionText, rawSource = source, | |
srcArray= exports.toArray(source,state,options.rules); | |
var p = promise(); | |
when(srcArray, function(sourceArray){ | |
print("processingSourceArray: "+ sourceArray); | |
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); | |
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); | |
} | |
} | |
p.callback(functionText); | |
}); | |
return p; | |
//return functionText; | |
} | |
exports.toArray= function(src, state, rules) { | |
//print("toArray() rules:", rules); | |
print("toArray: " + src); | |
// for(var i in rules){ | |
// print("rules i: " + i); | |
// } | |
var opener, closer, delimiter; | |
var text = [], | |
tags = [], | |
all = []; | |
var p = new promise(); | |
when(src, function(source) { | |
print("processing source: " + 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; | |
} | |
//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]); | |
} | |
p.callback({text:text, tags:tags, all:all}); | |
}); | |
return p; | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment