Skip to content

Instantly share code, notes, and snippets.

@kirbysayshi
Created April 26, 2014 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kirbysayshi/11331730 to your computer and use it in GitHub Desktop.
Save kirbysayshi/11331730 to your computer and use it in GitHub Desktop.
requirebin sketch
var vash = require('vash');
var str = ''
+ 'p {\n'
+ ' @if(model.can==\'123\') {'
+ ' float-offset: 3px;'
+ ' } else {'
+ ' float-offset: 6px;'
+ ' }'
+ '}';
// This will throw...
try {
var tpl = vash.compile(str);
console.log(tpl.toClientString());
console.log(tpl({ can: '123' }));
} catch(e) { console.log(e); }
// This will not!
try {
var tpl = vash.compile(str, { favorText: true });
console.log(tpl.toClientString());
console.log(tpl({ can: '123' }));
} catch(e) { console.log(e); }
require=function e(t,n,s){function r(o,a){if(!n[o]){if(!t[o]){var h="function"==typeof require&&require;if(!a&&h)return h(o,!0);if(i)return i(o,!0);throw Error("Cannot find module '"+o+"'")}var u=n[o]={exports:{}};t[o][0].call(u.exports,function(e){var n=t[o][1][e];return r(n?n:e)},u,u.exports,e,t,n,s)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;s.length>o;o++)r(s[o]);return r}({1:[function(){},{}],2:[function(t,e){var n=e.exports={};n.nextTick=function(){var t="undefined"!=typeof window&&window.setImmediate,e="undefined"!=typeof window&&window.postMessage&&window.addEventListener;if(t)return function(t){return window.setImmediate(t)};if(e){var n=[];return window.addEventListener("message",function(t){var e=t.source;if((e===window||null===e)&&"process-tick"===t.data&&(t.stopPropagation(),n.length>0)){var s=n.shift();s()}},!0),function(t){n.push(t),window.postMessage("process-tick","*")}}return function(t){setTimeout(t,0)}}(),n.title="browser",n.browser=!0,n.env={},n.argv=[],n.binding=function(){throw Error("process.binding is not supported")},n.cwd=function(){return"/"},n.chdir=function(){throw Error("process.chdir is not supported")}},{}],3:[function(t,e,n){(function(t){function e(t,e){for(var n=0,s=t.length-1;s>=0;s--){var r=t[s];"."===r?t.splice(s,1):".."===r?(t.splice(s,1),n++):n&&(t.splice(s,1),n--)}if(e)for(;n--;n)t.unshift("..");return t}function s(t,e){if(t.filter)return t.filter(e);for(var n=[],s=0;t.length>s;s++)e(t[s],s,t)&&n.push(t[s]);return n}var r=/^(\/?|)([\s\S]*?)((?:\.{1,2}|[^\/]+?|)(\.[^.\/]*|))(?:[\/]*)$/,i=function(t){return r.exec(t).slice(1)};n.resolve=function(){for(var n="",r=!1,i=arguments.length-1;i>=-1&&!r;i--){var o=i>=0?arguments[i]:t.cwd();if("string"!=typeof o)throw new TypeError("Arguments to path.resolve must be strings");o&&(n=o+"/"+n,r="/"===o.charAt(0))}return n=e(s(n.split("/"),function(t){return!!t}),!r).join("/"),(r?"/":"")+n||"."},n.normalize=function(t){var r=n.isAbsolute(t),i="/"===o(t,-1);return t=e(s(t.split("/"),function(t){return!!t}),!r).join("/"),t||r||(t="."),t&&i&&(t+="/"),(r?"/":"")+t},n.isAbsolute=function(t){return"/"===t.charAt(0)},n.join=function(){var t=Array.prototype.slice.call(arguments,0);return n.normalize(s(t,function(t){if("string"!=typeof t)throw new TypeError("Arguments to path.join must be strings");return t}).join("/"))},n.relative=function(t,e){function s(t){for(var e=0;t.length>e&&""===t[e];e++);for(var n=t.length-1;n>=0&&""===t[n];n--);return e>n?[]:t.slice(e,n-e+1)}t=n.resolve(t).substr(1),e=n.resolve(e).substr(1);for(var r=s(t.split("/")),i=s(e.split("/")),o=Math.min(r.length,i.length),a=o,h=0;o>h;h++)if(r[h]!==i[h]){a=h;break}for(var u=[],h=a;r.length>h;h++)u.push("..");return u=u.concat(i.slice(a)),u.join("/")},n.sep="/",n.delimiter=":",n.dirname=function(t){var e=i(t),n=e[0],s=e[1];return n||s?(s&&(s=s.substr(0,s.length-1)),n+s):"."},n.basename=function(t,e){var n=i(t)[2];return e&&n.substr(-1*e.length)===e&&(n=n.substr(0,n.length-e.length)),n},n.extname=function(t){return i(t)[3]};var o="b"==="ab".substr(-1)?function(t,e,n){return t.substr(e,n)}:function(t,e,n){return 0>e&&(e=t.length+e),t.substr(e,n)}}).call(this,t("/home/admin/browserify-cdn/node_modules/browserify/node_modules/insert-module-globals/node_modules/process/browser.js"))},{"/home/admin/browserify-cdn/node_modules/browserify/node_modules/insert-module-globals/node_modules/process/browser.js":2}],yYZNuU:[function(t,e){(function(t){"function"==typeof define&&define.amd?define(t):"object"==typeof e&&e.exports?e.exports=t:window.vash=t})(function(n){function s(t){this.input=this.originalInput=t.replace(/^\uFEFF/,"").replace(/\r\n|\r/g,"\n"),this.lineno=1,this.charno=0}function r(t,e){this.options=e||{},this.tokens=t,this.ast=B(K),this.prevTokens=[],this.inCommentLine=!1}function i(t,e,n){this.ast=t,this.originalMarkup=e||"",this.options=n||{},this.reQuote=/(['"])/gi,this.reEscapedQuote=/\\+(["'])/gi,this.reLineBreak=/\r?\n/gi,this.reHelpersName=/HELPERSNAME/g,this.reModelName=/MODELNAME/g,this.reOriginalMarkup=/ORIGINALMARKUP/g,this.buffer=[]}var o=n,a="AT",h="ASSIGN_OPERATOR",u="AT_COLON",p="AT_STAR_CLOSE",c="AT_STAR_OPEN",l="BACKSLASH",f="BRACE_CLOSE",d="BRACE_OPEN",v="CONTENT",m="DOUBLE_QUOTE",g="EMAIL",y="ESCAPED_QUOTE",b="FORWARD_SLASH",E="FUNCTION",k="HARD_PAREN_CLOSE",A="HARD_PAREN_OPEN",M="HTML_TAG_CLOSE",_="HTML_TAG_OPEN",N="HTML_TAG_VOID_CLOSE",w="IDENTIFIER",x="KEYWORD",T="LOGICAL",S="NEWLINE",L="NUMERIC_CONTENT",P="OPERATOR",O="PAREN_CLOSE",R="PAREN_OPEN",C="PERIOD",H="SINGLE_QUOTE",F="TEXT_TAG_CLOSE",I="TEXT_TAG_OPEN",j="WHITESPACE",D={};D[c]=p,D[d]=f,D[m]=m,D[A]=k,D[R]=O,D[H]=H,D[u]=S,D[b]=b;var U=[g,/^([a-zA-Z0-9.%]+@[a-zA-Z0-9.\-]+\.(?:ca|co\.uk|com|edu|net|org))\b/,c,/^(@\*)/,p,/^(\*@)/,u,/^(@\:)/,a,/^(@)/,R,/^(\()/,O,/^(\))/,A,/^(\[)/,k,/^(\])/,d,/^(\{)/,f,/^(\})/,I,/^(<text>)/,F,/^(<\/text>)/,_,function(){var t=/^(<[a-zA-Z@]+?[^>]*?["a-zA-Z]*>)/,e=/([a-zA-Z0-9.%]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,4})\b/,n=this.scan(t,_);return n&&(this.spewIf(n,e),this.spewIf(n,/(@)/),this.spewIf(n,/(\/\s*>)/)),n},M,/^(<\/[^>@\b]+?>)/,N,/^(\/\s*>)/,C,/^(\.)/,S,function(){var t=this.scan(/^(\n)/,S);return t&&(this.lineno++,this.charno=0),t},j,/^(\s)/,E,/^(function)(?![\d\w])/,x,/^(case|catch|do|else|finally|for|function|goto|if|instanceof|return|switch|try|typeof|var|while|with)(?![\d\w])/,w,/^([_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*)/,b,/^(\/)/,P,/^(===|!==|==|!==|>>>|<<|>>|>=|<=|>|<|\+|-|\/|\*|\^|%|\:|\?)/,h,/^(\|=|\^=|&=|>>>=|>>=|<<=|-=|\+=|%=|\/=|\*=|=)/,T,/^(&&|\|\||&|\||\^)/,y,/^(\\+['"])/,l,/^(\\)/,m,/^(\")/,H,/^(\')/,L,/^([0-9]+)/,v,/^([^\s})@.]+?)/];s.prototype={scan:function(t,e){var n,s;return(n=t.exec(this.input))?(this.input=this.input.substr(n[1].length),s={type:e,line:this.lineno,chr:this.charno,val:n[1]||"",toString:function(){return"["+this.type+" ("+this.line+","+this.chr+"): "+this.val+"]"}},this.charno+=n[0].length,s):void 0},spewIf:function(t,e){var n,s,r;return t&&(n=e.exec(t.val),n&&(s=t.val.indexOf(n[1]),r=t.val.substring(s),this.input=r+this.input,this.charno-=r.length,t.val=t.val.substring(0,s))),t},advance:function(){var t,e,n;for(t=0;U.length>t;t+=2)if(e=U[t+1],e.displayName=U[t],"function"==typeof e&&(n=e.call(this)),"function"==typeof e.exec&&(n=this.scan(e,U[t])),n)return n}};var B=function(t){return new B.fn.init(t)};B.prototype.init=function(t){"string"==typeof t&&(this.mode=t),this.maxCheck()},B.fn=B.prototype.init.prototype=B.prototype,B.fn.vquery="yep",B.fn.constructor=B,B.fn.length=0,B.fn.parent=null,B.fn.mode=null,B.fn.tagName=null,B.fn.beget=function(t,e){var n=B(t);return n.parent=this,this.push(n),e&&(n.tagName=e),this.maxCheck(),n},B.fn.closest=function(t,e){for(var n=this;n&&n.tagName!==e&&n.parent;)n=n.parent;return n},B.fn.pushFlatten=function(t){for(var e,n=t;1===n.length&&n[0].vquery;)n=n[0];if(n.mode!==K)this.push(n);else for(e=0;n.length>e;e++)this.push(n[e]);return this.maxCheck(),this},B.fn.push=function(t){return B.isArray(t)?(t.vquery&&t.forEach(function(t){t.parent=this},this),Array.prototype.push.apply(this,t)):(t.vquery&&(t.parent=this),Array.prototype.push.call(this,t)),this.maxCheck(),this.length},B.fn.root=function(){for(var t=this;t&&t.parent&&(t=t.parent););return t},B.fn.toTreeString=function(){function t(s){var r,i;for(e.push(Array(n).join(" |")+" +"+s.mode+" "+(s.tagName||"")),n+=1,r=s.slice();i=r.shift();)i.vquery===B.fn.vquery?t(i):e.push(Array(n).join(" |")+" "+(i?(""+i).replace(/(\r|\n)/g,""):"[empty]"));n-=1,e.push(Array(n).join(" |")+" -"+s.mode+" "+(s.tagName||""))}var e=[],n=1;return t(this),e.join("\n")},B.fn.maxCheck=function(){if(this.length>=B.maxSize){var t=Error();throw t.message="Maximum number of elements exceeded.\nThis is typically caused by an unmatched character or tag. Parse tree follows:\n"+this.toTreeString(),t.name="vQueryDepthException",t}},B.maxSize=1e5,B.fn.flatten=function(){var t;return this.reduce(function e(n,s,r,i){return s.vquery?(n.push({type:"META",val:"START"+s.mode,tagName:s.tagName}),t=s.reduce(e,n),t.push({type:"META",val:"END"+s.mode,tagName:s.tagName}),t):(s.mode=i.mode,n.push(s),n)},[])},B.reconstitute=function(t){return t.reduce(function(t,e){return"META"===e.type?t=t.parent:(e.mode!==t.mode&&(t=t.beget(e.mode,e.tagName)),t.push(e)),t},B(K))},B.isArray=function(t){return"[object Array]"==Object.prototype.toString.call(t)},B.extend=function(t){var e,n,s;for(n=1;arguments.length>n;n++){e=arguments[n];for(s in e)t[s]=e[s]}return t},B.takeMethodsFromArray=function(){for(var t,e=["pop","push","reverse","shift","sort","splice","unshift","concat","join","slice","indexOf","lastIndexOf","filter","forEach","every","map","some","reduce","reduceRight"],n=[],s=0;e.length>s;s++){if(t=e[s],"function"!=typeof n[t])throw Error("Vash requires ES5 array iteration methods, missing: "+t);B.fn[t]||function(t){B.fn[t]=function(){return n[t].apply(this,Array.prototype.slice.call(arguments,0))}}(t)}},B.takeMethodsFromArray();var K="PROGRAM",q="MARKUP",z="BLOCK",G="EXPRESSION";r.prototype={parse:function(){for(var t;this.prevTokens.push(t),t=this.tokens.pop();)this.options.debugParser&&console.log(this.ast&&this.ast.mode,t.type,""+t,t.val),(this.ast.mode===K||null===this.ast.mode)&&(this.ast=this.ast.beget(this.options.initialMode||q),this.options.initialMode===G&&(this.ast=this.ast.beget(G))),this.ast.mode!==q?this.ast.mode!==z?this.ast.mode!==G||this.handleEXP(t):this.handleBLK(t):this.handleMKP(t);return this.ast=this.ast.root(),this.options.debugParser&&!this.options.initialMode&&(console.log(""+this.ast),console.log(this.ast.toTreeString())),this.ast},exceptionFactory:function(t,e,n){return"UNMATCHED"==e&&(t.name="UnmatchedCharacterError",this.ast=this.ast.root(),n&&(t.message="Unmatched "+n.type+" at line "+n.line+", character "+n.chr+". Value: "+n.val+"\n "+this.ast.toTreeString(),t.lineNumber=n.line)),t},advanceUntilNot:function(t){for(var e,n,s=[];(n=this.tokens[this.tokens.length-1])&&n.type===t;)e=this.tokens.pop(),s.push(e);return s},advanceUntilMatched:function(t,e,n,s,r){for(var i=t,o=null,a=0,h=0,u=[];i&&(i.type===e?o&&o.type!==s&&e!==n||!o?a++:e===n&&o.type!==s&&h++:i.type===n&&(h++,o&&o.type===r&&h--),u.push(i),a!==h);)if(o=i,i=this.tokens.pop(),!i)throw this.exceptionFactory(Error(),"UNMATCHED",t);return u.reverse()},subParse:function(t,e,n){var s,i,o,h=B.extend({},this.options);h.initialMode=e,s=this.advanceUntilMatched(t,t.type,D[t.type],null,a),s.pop(),i=s.shift(),n||this.ast.push(t),o=new r(s,h),o.parse(),n&&(o.ast[0].unshift(t),o.ast[0].push(i)),this.ast.pushFlatten(o.ast),n||this.ast.push(i)},handleMKP:function(t){var e,n=this.tokens[this.tokens.length-1],s=this.tokens[this.tokens.length-2],r=null;switch(t.type){case c:this.advanceUntilMatched(t,c,p,a,a);break;case a:if(n)switch(this.options.saveAT&&this.ast.push(t),n.type){case R:case w:0===this.ast.length&&(this.ast=this.ast.parent,this.ast.pop()),this.ast=this.ast.beget(G);break;case x:case E:case d:0===this.ast.length&&(this.ast=this.ast.parent,this.ast.pop()),this.ast=this.ast.beget(z);break;case a:case u:n.type="CONTENT",this.ast.push(this.tokens.pop());break;default:this.ast.push(this.tokens.pop())}break;case I:case _:r=t.val.match(/^<([^\/ >]+)/i),null===r&&n&&n.type===a&&s&&(r=s.val.match(/(.*)/)),this.ast.tagName?this.ast=this.ast.beget(q,r[1]):this.ast.tagName=r[1],(_===t.type||this.options.saveTextTag)&&this.ast.push(t);break;case F:case M:r=t.val.match(/^<\/([^>]+)/i),null===r&&n&&n.type===a&&s&&(r=s.val.match(/(.*)/)),e=this.ast.closest(q,r[1]),null===e||e.tagName!==r[1]||(this.ast=e),(M===t.type||this.options.saveTextTag)&&this.ast.push(t),this.ast.parent&&this.ast.parent.mode===z&&(this.ast=this.ast.parent);break;case N:this.ast.push(t),this.ast=this.ast.parent;break;case l:t.val+="\\",this.ast.push(t);break;default:this.ast.push(t)}},handleBLK:function(t){var e,n,s=this.tokens[this.tokens.length-1];switch(t.type){case a:s.type===a||this.inCommentLine?(s.type=v,this.ast.push(s),this.tokens.pop()):(this.tokens.push(t),this.ast=this.ast.beget(q));break;case c:this.advanceUntilMatched(t,c,p,a,a);break;case u:this.subParse(t,q,!0);break;case I:case F:case _:case M:this.ast=this.ast.beget(q),this.tokens.push(t);break;case b:case H:case m:t.type===b&&s&&s.type===b&&(this.inCommentLine=!0),this.inCommentLine?this.ast.push(t):(n=this.advanceUntilMatched(t,t.type,D[t.type],l,l).map(function(t){return t.type===a&&(t.type=v),t}),this.ast.pushFlatten(n.reverse()));break;case S:this.inCommentLine&&(this.inCommentLine=!1),this.ast.push(t);break;case d:case R:e=this.options.favorText&&t.type===d?q:z,this.subParse(t,e),n=this.advanceUntilNot(j),s=this.tokens[this.tokens.length-1],s&&s.type!==x&&s.type!==E&&s.type!==d&&t.type!==R?(this.tokens.push.apply(this.tokens,n.reverse()),this.ast=this.ast.parent):this.ast.push(n);break;default:this.ast.push(t)}},handleEXP:function(t){var e,n,s=null;switch(t.type){case x:case E:this.ast=this.ast.beget(z),this.tokens.push(t);break;case j:case T:case h:case P:case L:this.ast.parent&&this.ast.parent.mode===G?this.ast.push(t):(this.ast=this.ast.parent,this.tokens.push(t));break;case w:this.ast.push(t);break;case H:case m:this.ast.parent&&this.ast.parent.mode===G?(e=this.advanceUntilMatched(t,t.type,D[t.type],l,l),this.ast.pushFlatten(e.reverse())):(this.ast=this.ast.parent,this.tokens.push(t));break;case A:case R:if(n=this.prevTokens[this.prevTokens.length-1],s=this.tokens[this.tokens.length-1],t.type===A&&s.type===k){this.tokens.push(t),this.ast=this.ast.parent;break}this.subParse(t,G),s=this.tokens[this.tokens.length-1],(n&&n.type===a||s&&s.type===w)&&(this.ast=this.ast.parent);break;case d:this.tokens.push(t),this.ast=this.ast.beget(z);break;case C:s=this.tokens[this.tokens.length-1],s&&(s.type===w||s.type===x||s.type===E||s.type===C||this.ast.parent&&this.ast.parent.mode===G)?this.ast.push(t):(this.ast=this.ast.parent,this.tokens.push(t));break;default:this.ast.parent&&this.ast.parent.mode!==G?(this.ast=this.ast.parent,this.tokens.push(t)):this.ast.push(t)}}};var V=i.prototype;V.insertDebugVars=function(t){this.options.debug&&this.buffer.push(this.options.helpersName+".vl = "+t.line+", ",this.options.helpersName+".vc = "+t.chr+"; \n")},V.visitMarkupTok=function(t){this.insertDebugVars(t),this.buffer.push("MKP("+t.val.replace(this.reEscapedQuote,"\\\\$1").replace(this.reQuote,"\\$1").replace(this.reLineBreak,"\\n")+")MKP")},V.visitBlockTok=function(t){this.buffer.push(t.val)},V.visitExpressionTok=function(t,e,n,s){var r="",i="",o=e.parent&&e.parent.mode!==G;this.options.htmlEscape!==!1&&(o&&0===n&&s&&(r+=this.options.helpersName+".escape("),o&&n===e.length-1&&s&&(i+=").toHtmlString()")),o&&0===n&&(this.insertDebugVars(t),r="__vbuffer.push("+r),o&&n===e.length-1&&(i+="); \n"),this.buffer.push(r+t.val+i),o&&n===e.length-1&&this.insertDebugVars(t)},V.visitNode=function(t){var e,n,s,r=t.slice(0);for(t.mode===G&&t.parent&&t.parent.mode!==G&&(e=t.filter(i.findNonExp).length),n=0;r.length>n;n++)s=r[n],s.type&&s.type===a||s.type===u||(s.vquery?this.visitNode(s):t.mode===q?this.visitMarkupTok(s,t,n):t.mode===z?this.visitBlockTok(s,t,n):t.mode===G&&this.visitExpressionTok(s,t,n,e>0?!1:!0))},V.escapeForDebug=function(t){return t.replace(this.reLineBreak,"!LB!").replace(this.reQuote,"\\$1").replace(this.reEscapedQuote,"\\$1")},V.replaceDevTokens=function(t){return t.replace(this.reHelpersName,this.options.helpersName).replace(this.reModelName,this.options.modelName)},V.addHead=function(t){var e=this.options,n=""+(e.debug?"try { \n":"")+"var __vbuffer = HELPERSNAME.buffer; \n"+"HELPERSNAME.options = __vopts; \n"+"MODELNAME = MODELNAME || {}; \n"+(e.useWith?"with( MODELNAME ){ \n":"");return n=this.replaceDevTokens(n),n+t},V.addHelperHead=function(t){var e=this.options,n=""+(e.debug?"try { \n":"")+"var __vbuffer = this.buffer; \n"+"var MODELNAME = this.model; \n"+"var HELPERSNAME = this; \n";return n=this.replaceDevTokens(n),n+t},V.addFoot=function(t){var e=this.options,n=""+(e.simple?'return HELPERSNAME.buffer.join(""); \n':"(__vopts && __vopts.onRenderEnd && __vopts.onRenderEnd(null, HELPERSNAME)); \nreturn (__vopts && __vopts.asContext) \n ? HELPERSNAME \n : HELPERSNAME.toString(); \n")+(e.useWith?"} \n":"")+(e.debug?'} catch( e ){ \n HELPERSNAME.reportError( e, HELPERSNAME.vl, HELPERSNAME.vc, "ORIGINALMARKUP" ); \n} \n':"");return n=this.replaceDevTokens(n).replace(this.reOriginalMarkup,this.escapeForDebug(this.originalMarkup)),t+n},V.addHelperFoot=function(t){var e=this.options,n=""+(e.debug?'} catch( e ){ \n HELPERSNAME.reportError( e, HELPERSNAME.vl, HELPERSNAME.vc, "ORIGINALMARKUP" ); \n} \n':"");return n=this.replaceDevTokens(n).replace(this.reOriginalMarkup,this.escapeForDebug(this.originalMarkup)),t+n},V.generate=function(){var t=this.options;this.buffer.length=0,this.visitNode(this.ast);var e=this.buffer.join("").split(")MKPMKP(").join("").split("MKP(").join("__vbuffer.push('").split(")MKP").join("'); \n");return t.asHelper?(e=this.addHelperHead(e),e=this.addHelperFoot(e)):(e=this.addHead(e),e=this.addFoot(e)),t.debugCompiler&&(console.log(e),console.log(t)),this.cmpFunc=o.link(e,t),this.cmpFunc},i.noop=function(){},i.findNonExp=function(t){return t.vquery&&t.mode===G?t.filter(i.findNonExp).length>0:t.vquery&&t.mode!==G?!0:!1},n.config={useWith:!1,modelName:"model",helpersName:"html",htmlEscape:!0,debug:!0,debugParser:!1,debugCompiler:!1,simple:!1,favorText:!1,externs:["window","document"],saveTextTag:!1,saveAT:!1},n.compile=function(t,e){if(""===t||"string"!=typeof t)throw Error("Empty or non-string cannot be compiled");var o,a,h,u,p,c=[];for(e=B.extend({},n.config,e||{}),o=new s(t);a=o.advance();)c.push(a);return c.reverse(),h=new r(c,e),h.parse(),u=new i(h.ast,t,e),p=u.generate()};var $=(Array.prototype.slice,/vash\.helpers\.([^= ]+?)\s*=\s*function([^(]*?)\(([^)]*?)\)\s*{/),Q=/\}$/,Z=/^\/\/\s*@\s*batch\s*=\s*(.*?)$/,W=function(t,e,n,s){var r=e.split(/[\n\r]/g),i={},o="";return r.forEach(function(e){var r=t.exec(e),a=r?n.apply(r,r):null;a&&(o=a,i[o]=[]),a&&!s||!e||i[o].push(e)}),Object.keys(i).forEach(function(t){i[t]=i[t].join("\n")}),i},X=function(t,e,n){var s="helper"===t?$:Z,r=W(s,e,function(t,e){return e.replace(/^\s+|\s+$/,"")},"helper"===t?!0:!1);return r&&(Object.keys(r).forEach(function(e){r[e]="helper"===t?Y(r[e],n):o.compile("@{"+r[e]+"}",n)}),r.toClientString=function(){return Object.keys(r).reduce(function(t,e){return"toClientString"===e?t:t+r[e].toClientString()+"\n"},"")}),r},Y=function(t,e){e=e||{};var n=t.replace(/^[\s\n\r]+|[\s\n\r]+$/,"").match($),s=n[3].split(",").map(function(t){return t.replace(" ","")}),r=n[1],i=t.replace($,"").replace(Q,"");return i="@{"+i+"}",e.args=s,e.asHelper=r,o.compile(i,e)};return n.compileHelper=X.bind(null,"helper"),n.compileBatch=n.batch=X.bind(null,"batch"),n.VLexer=s,n.VParser=r,n.VCompiler=i,n.vQuery=B,function(){function t(t,e){return"function"==typeof e&&(e={onRenderEnd:e}),t&&t.onRenderEnd&&(e=e||{},e.onRenderEnd||(e.onRenderEnd=t.onRenderEnd),delete t.onRenderEnd),e||(e={}),e}o=o===void 0?{}:o,o.compile||("function"==typeof define&&define.amd?define(function(){return o}):"object"==typeof e&&e.exports?e.exports=o:window.vash=o);var n=o.helpers,s=function(t){this.buffer=new h,this.model=t,this.options=null,this.vl=0,this.vc=0};o.helpers=n=s.prototype={constructor:s,config:{},tplcache:{}},n.toString=n.toHtmlString=function(){return this.buffer._vo.join("")};var r=/[&<>"'`]/g,i=function(t){return a[t]},a={"&":"&amp;","<":"&lt;",">":"&gt;",'"':"&quot;","'":"&#x27;","`":"&#x60;"};n.raw=function(t){var e=function(){return t};return t=null!=t?t:"",{toHtmlString:e,toString:e}},n.escape=function(t){var e=function(){return t};return t=null!=t?t:"","function"!=typeof t.toHtmlString?(t=(""+t).replace(r,i),{toHtmlString:e,toString:e}):t};var h=function(){this._vo=[]};h.prototype.mark=function(t){var e=new u(this,t);return e.markedIndex=this._vo.length,this._vo.push(e.uid),e},h.prototype.fromMark=function(t){var e=t.findInBuffer();return e>-1?(t.destroy(),this._vo.splice(e,this._vo.length)):[]},h.prototype.spliceMark=function(t){var e=t.findInBuffer();return e>-1?(t.destroy(),arguments[0]=e,this._vo.splice.apply(this._vo,arguments)):[]},h.prototype.empty=function(){return this._vo.splice(0,this._vo.length)},h.prototype.push=function(t){return this._vo.push(t)},h.prototype.pushConcat=function(t){var e;e=Array.isArray(t)?t:arguments.length>1?Array.prototype.slice.call(arguments):[t];for(var n=0;e.length>n;n++)this._vo.push(e[n]);return this.__vo},h.prototype.indexOf=function(t){for(var e=0;this._vo.length>e;e++)if(t.test&&this._vo[e].search(t)>-1||this._vo[e]==t)return e;return-1},h.prototype.lastIndexOf=function(t){for(var e=this._vo.length;--e>=0;)if(t.test&&this._vo[e].search(t)>-1||this._vo[e]==t)return e;return-1},h.prototype.splice=function(){return this._vo.splice.apply(this._vo,arguments)},h.prototype.index=function(t){return this._vo[t]},h.prototype.flush=function(){return this.empty().join("")},h.prototype.toString=h.prototype.toHtmlString=function(){return this._vo.join("")};var u=o.Mark=function(t,e){this.uid="[VASHMARK-"+~~(1e7*Math.random())+(e?":"+e:"")+"]",this.markedIndex=0,this.buffer=t,this.destroyed=!1},p=/\[VASHMARK\-\d{1,8}(?::[\s\S]+?)?]/g;u.uidLike=function(t){return(t||"").search(p)>-1},u.prototype.destroy=function(){var t=this.findInBuffer();t>-1&&(this.buffer.splice(t,1),this.markedIndex=-1),this.destroyed=!0},u.prototype.findInBuffer=function(){return this.destroyed?-1:this.markedIndex&&this.buffer.index(this.markedIndex)===this.uid?this.markedIndex:this.markedIndex=this.buffer.indexOf(this.uid)},n.constructor.reportError=function(t,e,n,s,r){r=r||"!LB!";var i=s.split(r),o=0===e&&0===n?i.length-1:3,a=Math.max(0,e-o),h=Math.min(i.length,e+o),u=i.slice(a,h).map(function(t,n){var s=n+a+1;return(s===e?" > ":" ")+(10>s?" ":"")+s+" | "+t}).join("\n");throw t.vashlineno=e,t.vashcharno=n,t.message="Problem while rendering template at line "+e+", character "+n+".\nOriginal message: "+t.message+"."+"\nContext: \n\n"+u+"\n\n",t},n.reportError=function(){this.constructor.reportError.apply(this,arguments)},o.link=function(e,r){var i,a;if(r.args||(r.args=[r.modelName,r.helpersName,"__vopts","vash"]),"string"==typeof e){i=e;try{a=r.args.slice(),a.push(e),e=Function.apply(null,a)}catch(h){n.reportError(h,0,0,i,/\n/)}}e.options={simple:r.simple,modelName:r.modelName,helpersName:r.helpersName};var u;return r.asHelper?(e.options.args=r.args,e.options.asHelper=r.asHelper,u=function(){return e.apply(this,c.call(arguments))},n[r.asHelper]=u):u=function(n,i){if(r.simple){var a={buffer:[],escape:s.prototype.escape,raw:s.prototype.raw};return e(n,a,i,o)}return i=t(n,i),e(n,i&&i.context||new s(n),i,o)},u.toString=function(){return""+e},u._toString=function(){return Function.prototype.toString.call(u)},u.toClientString=function(){return"vash.link( "+(""+e)+", "+JSON.stringify(e.options)+" )"},u};var c=Array.prototype.slice;o.lookup=function(t,e){var n=o.helpers.tplcache[t];if(!n)throw Error("Could not find template: "+t);return e?n(e):n},o.install=function(t,e){var n=o.helpers.tplcache;if("string"==typeof e){if(!o.compile)throw Error("vash.install(path, [string]) is not available in the standalone runtime.");e=o.compile(e)}else if("object"==typeof t)return e=t,Object.keys(e).forEach(function(t){n[t]=e[t]}),n;return n[t]=e},o.uninstall=function(t){var e=o.helpers.tplcache,n=!1;return"string"==typeof t?delete e[t]:(Object.keys(e).forEach(function(s){e[s]===t&&(n=delete e[s])}),n)}}(),function(){var t=o.helpers;t.trim=function(t){return t.replace(/^\s*|\s*$/g,"")},t.config.highlighter=null,t.highlight=function(e,n){var s=this.buffer.mark();n();var r=this.buffer.fromMark(s);this.buffer.push("<pre><code>"),t.config.highlighter?this.buffer.push(t.config.highlighter(e,r.join("")).value):this.buffer.push(r),this.buffer.push("</code></pre>")}}(),function(){if("undefined"==typeof window)var e=t("fs"),n=t("path");var s=o.helpers;s.config.browser=!1,o.loadFile=function(t,r,i){r=B.extend({},o.config,r||{});var a,h=s.config.browser;!h&&r.settings&&r.settings.views&&(t=n.normalize(t),-1===t.indexOf(n.normalize(r.settings.views))&&(t=n.join(r.settings.views,t)),n.extname(t)||(t+="."+(r.settings["view engine"]||"vash")));try{a=r.cache||h?s.tplcache[t]||(s.tplcache[t]=o.compile(e.readFileSync(t,"utf8"))):o.compile(e.readFileSync(t,"utf8")),i&&i(null,a)}catch(u){i&&i(u,null)}},o.renderFile=function(t,e,n){o.loadFile(t,e,function(t,s){var r=e.onRenderEnd;n(t,!t&&s(e,function(t,e){e.finishLayout(),r&&r(t,e)}))})},s._ensureLayoutProps=function(){this.appends=this.appends||{},this.prepends=this.prepends||{},this.blocks=this.blocks||{},this.blockMarks=this.blockMarks||{}},s.finishLayout=function(){this._ensureLayoutProps();var t,e,n,s,r,i,o,a,h=this;for(t in this.blockMarks)e=this.blockMarks[t],s=this.prepends[t],n=this.blocks[t],r=this.appends[t],i=e.pop(),o=this.buffer.mark(),s&&s.forEach(function(t){h.buffer.pushConcat(t)}),block=n.pop(),block&&this.buffer.pushConcat(block),r&&r.forEach(function(t){h.buffer.pushConcat(t)}),a=this.buffer.fromMark(o).join(""),this.buffer.spliceMark(i,0,a);for(t in this.blockMarks)this.blockMarks[t].forEach(function(t){t.destroy()});return delete this.blockMarks,delete this.prepends,delete this.blocks,delete this.appends,""+this},s.extend=function(t,e){var n=this,s=(this.buffer,this.model);this._ensureLayoutProps(),o.loadFile(t,this.model,function(t,s){var r=n.buffer.mark();e(n.model),n.buffer.fromMark(r),n.isExtending=!0,s(n.model,{context:n}),n.isExtending=!1}),this.model=s},s.include=function(t,e){var n=this,s=(this.buffer,this.model);o.loadFile(t,this.model,function(t,s){s(e||n.model,{context:n})}),this.model=s},s.block=function(t,e){this._ensureLayoutProps();var n,s,r=this.blockMarks[t]||(this.blockMarks[t]=[]),i=this.blocks[t]||(this.blocks[t]=[]);e&&(n=this.buffer.mark(),e(this.model),s=this.buffer.fromMark(n),s.length&&!this.isExtending&&i.push(s),s.length&&this.isExtending&&i.unshift(s)),r.push(this.buffer.mark("block-"+t))},s._handlePrependAppend=function(t,e,n){this._ensureLayoutProps();var s,r=this.buffer.mark(),i=this[t],o=i[e]||(i[e]=[]);n(this.model),s=this.buffer.fromMark(r),o.push(s)},s.append=function(t,e){this._handlePrependAppend("appends",t,e)},s.prepend=function(t,e){this._handlePrependAppend("prepends",t,e)}}(),n.__express=n.renderFile,n}({version:"0.7.10-2"}))},{fs:1,path:3}],vash:[function(t,e){e.exports=t("yYZNuU")},{}]},{},[]);var vash=require("vash"),str="p {\n @if(model.can=='123') { float-offset: 3px; } else { float-offset: 6px; }}";try{var tpl=vash.compile(str);console.log(tpl.toClientString()),console.log(tpl({can:"123"}))}catch(e){console.log(e)}try{var tpl=vash.compile(str,{favorText:!0});console.log(tpl.toClientString()),console.log(tpl({can:"123"}))}catch(e){console.log(e)}
{
"name": "requirebin-sketch",
"version": "1.0.0",
"dependencies": {
"vash": "0.7.10-2"
}
}
<style type='text/css'>html, body { margin: 0; padding: 0; border: 0; }
body, html { height: 100%; width: 100%; }</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment