UNPKG

4.62 kBJavaScriptView Raw
1!function(){var CoffeeScript,indentText,keywords,keywordsRegex,util,__slice=[].slice;CoffeeScript=require("coffee-script");indentText=function(text,indent){if(indent==null){indent=" "}return indent+text.replace(/\n/g,"\n"+indent)};keywords=["on","each","with","render"];keywordsRegex=RegExp("^\\s*("+keywords.join("|")+")\\s+");util={indent:indentText,filters:{verbatim:function(content,compiler){return compiler.buffer('"""'+content.replace(/(#)/,"\\$1")+'"""')},plain:function(content,compiler){return compiler.buffer(JSON.stringify(content))},coffeescript:function(content,compiler){return[content]},javascript:function(content,compiler){return["`",compiler.indent(content),"`"]}},styleTag:function(content){return this.element("style",[],["__element.innerHTML = "+JSON.stringify("\n"+this.indent(content))])},scriptTag:function(content){return this.element("script",[],["__element.innerHTML = "+JSON.stringify("\n"+this.indent(content))])},element:function(tag,attributes,contents){var attributeLines,lines;if(contents==null){contents=[]}attributeLines=attributes.map(function(_arg){var name,value;name=_arg.name,value=_arg.value;name=JSON.stringify(name);return"__attribute __element, "+name+", "+value});return lines=["__element = document.createElement("+JSON.stringify(tag)+")","__push(__element)"].concat(__slice.call(attributeLines),__slice.call(contents),["__pop()"])},buffer:function(value){return["__element = document.createTextNode('')","__text(__element, "+value+")","__push __element","__pop()"]},stringJoin:function(values){var dynamic;dynamic=this.dynamic;values=values.map(function(value){if(value.indexOf('"')===0){return JSON.parse(value)}else{return dynamic(value)}});return JSON.stringify(values.join(" "))},dynamic:function(value){return"#{"+value+"}"},attributes:function(node){var attributeId,attributes,classes,dynamic,id;dynamic=this.dynamic;id=node.id,classes=node.classes,attributes=node.attributes;classes=(classes||[]).map(JSON.stringify);attributeId=void 0;if(attributes){attributes=attributes.filter(function(_arg){var name,value;name=_arg.name,value=_arg.value;if(name==="class"){classes.push(value);return false}else if(name==="id"){attributeId=value;return false}else{return true}})}else{attributes=[]}if(classes.length){attributes.unshift({name:"class",value:this.stringJoin(classes)})}if(attributeId){attributes.unshift({name:"id",value:attributeId})}else if(id){attributes.unshift({name:"id",value:JSON.stringify(id)})}return attributes},render:function(node){var filter,tag,text;tag=node.tag,filter=node.filter,text=node.text;if(tag){return this.tag(node)}else if(filter){return this.filter(node)}else{return this.contents(node)}},replaceKeywords:function(codeString){return codeString.replace(keywordsRegex,"__$1 ")},filter:function(node){var filter,filterName;filterName=node.filter;if(filter=this.filters[filterName]){return[].concat.apply([],this.filters[filterName](node.content,this))}else{return["__filter("+JSON.stringify(filterName)+", "+JSON.stringify(node.content)+")"]}},contents:function(node){var bufferedCode,childContent,children,code,contents,indent,text,unbufferedCode;children=node.children,bufferedCode=node.bufferedCode,unbufferedCode=node.unbufferedCode,text=node.text;if(unbufferedCode){indent=true;code=this.replaceKeywords(unbufferedCode);contents=[code]}else if(bufferedCode){contents=this.buffer(bufferedCode)}else if(text){contents=this.buffer(JSON.stringify(text))}else if(node.tag){contents=[]}else if(node.comment){return[]}else{contents=[];console.warn("No content for node:",node)}if(children){childContent=this.renderNodes(children);if(indent){childContent=this.indent(childContent.join("\n"))}contents=contents.concat(childContent)}return contents},renderNodes:function(nodes){return[].concat.apply([],nodes.map(this.render,this))},tag:function(node){var attributes,contents,tag;tag=node.tag;attributes=this.attributes(node);contents=this.contents(node);return this.element(tag,attributes,contents)}};exports.compile=function(parseTree,_arg){var compiler,items,options,program,programSource,source;compiler=(_arg!=null?_arg:{}).compiler;if(compiler){CoffeeScript=compiler}items=util.renderNodes(parseTree);source="(data) ->\n (->\n {\n __push\n __pop\n __attribute\n __filter\n __text\n __on\n __each\n __with\n __render\n } = HAMLjr.Runtime(this)\n\n __push document.createDocumentFragment()\n"+util.indent(items.join("\n")," ")+"\n __pop()\n ).call(data)";options={bare:true};programSource=source;program=CoffeeScript.compile(programSource,options);return program}}.call(this);
\No newline at end of file