-
Notifications
You must be signed in to change notification settings - Fork 6
/
just.min.js
24 lines (24 loc) · 9.3 KB
/
just.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*!
* JUST JavaScript template engine v0.1.8
* https://github.com/baryshev/just
*
* Copyright 2012, Vadim M. Baryshev <[email protected]>
* Licensed under the MIT license
* https://github.com/baryshev/just/LICENSE
*
* Includes parts of async
* https://github.com/caolan/async
* Copyright 2010 Caolan McMahon
* Released under the MIT license
*
* Includes parts of node
* https://github.com/joyent/node
* Copyright Joyent, Inc. and other Node contributors
* Released under the MIT license
*
* Includes Cross-Browser Split 1.0.1
* http://xregexp.com/
* Copyright Steven Levithan <stevenlevithan.com>
* Released under the MIT license
*/
(function(){"use strict";var fs,path,async=function(){var e={},t=function(e,t){var n;if(e.forEach)return e.forEach(t);for(n=0;n<e.length;n++)t(e[n],n,e)},n=function(e,n){if(e.map)return e.map(n);var r=[];return t(e,function(e,t,i){r.push(n(e,t,i))}),r},r=function(e,t,r,i){var s=[];t=n(t,function(e,t){return{index:t,value:e}}),e(t,function(e,t){r(e.value,function(n,r){s[e.index]=r,t(n)})},function(e){i(e,s)})},i=function(t){return function(){var n=Array.prototype.slice.call(arguments);return t.apply(null,[e.forEach].concat(n))}};return e.forEach=function(e,n,r){if(!e.length)return r();var i=0;t(e,function(t){n(t,function(t){t?(r(t),r=function(){}):(i++,i===e.length&&r())})})},e.map=i(r),e.parallel=function(t,n){n=n||function(){},t.constructor===Array&&e.map(t,function(e,t){e&&e(function(e){var n=Array.prototype.slice.call(arguments,1);n.length<=1&&(n=n[0]),t.call(null,e,n)})},n)},e}(),JUST=function(newOptions){var options={open:"<%",close:"%>",ext:"",useCache:!1,watchForChanges:!1,root:""},trimExp=/^\s+|\s+$/g,escapeExp=/([.*+?\^=!:${}()|\[\]\/\\])/g,STATE_RAW=0,STATE_PARTIAL=1,STATE_EXTEND=2,STATE_CONDITION=3,STATE_ELSE=4,STATE_SWITCH=5,STATE_CASE=6,STATE_DEFAULT=7,STATE_LOOP=8,cache={},loaders={},watchers={},regExpEscape=function(e){return String(e).replace(escapeExp,"\\$1")},parse=function(e){var t=1,n=["with (this.data) { with (this.customData) { this.buffer.push('"],r=e.split(new RegExp(regExpEscape(options.open)+"((?:.|[\r\n])+?)(?:"+regExpEscape(options.close)+"|$)")),i,s,o,u,a,f,l,c,h;for(s=0,i=r.length;s<i;s++){o=r[s];if(s%2===1){f="this.line="+t,c=1,h=STATE_RAW;switch(o.charAt(0)){case"@":u="',("+f+", this.partial(",a=")),'",h=STATE_PARTIAL;break;case"!":u="',("+f+", this.extend(",a=")),'",h=STATE_EXTEND;break;case"*":u="',("+f+", this.child('",a="')),'";break;case"[":u="');"+f+";this.blockStart('",a="');this.buffer.push('";break;case"]":u="');"+f+";this.blockEnd(",a=");this.buffer.push('";break;case"=":u="',("+f+", ",a="),'";break;case"?":u="');"+f+";",a="this.buffer.push('",h=STATE_CONDITION;break;case":":u="');"+f+";}else",a="this.buffer.push('",h=STATE_ELSE;break;case"|":u="');"+f+";",a="this.buffer.push('",h=STATE_LOOP;break;default:u="');"+f+";",a=";this.buffer.push('",c=0}switch(h){case STATE_RAW:n.push(u,o.substr(c).replace(trimExp,""),a);break;case STATE_CONDITION:l=o.substr(c).replace(trimExp,""),l.length?n.push(u,"if(",l,"){",a):n.push(u,"}",a),l=undefined;break;case STATE_ELSE:l=o.substr(c).replace(trimExp,""),l.length?n.push(u," if(",l,"){",a):n.push(u,"{",a),l=undefined;break;case STATE_PARTIAL:case STATE_EXTEND:l=o.substr(c).replace(trimExp,"").split(/\s+/),l=["'"+l[0]+"'",l.splice(1).join(" ")],l[1].length?l=l.join(","):l=l[0],n.push(u,l,a),l=undefined;break;case STATE_LOOP:l=o.substr(c).replace(trimExp,"").split(/\s+/),l[0].length?n.push(u,l[0],".forEach(function(",l[1],"){this.buffer.push('"):n.push("');"+f+";}, this);this.buffer.push('"),l=undefined}}else n.push(o.replace(/[\\']/g,"\\$&").replace(/\r/g," ").replace(/\n/g,"\\n"));t+=o.split(/\n/).length-1}return n.push("'); } } return this.buffer;"),n=n.join(""),new Function(n)},loaded=function(e,t,n){var r=loaders[t];delete loaders[t],async.forEach(r,function(t,r){t(e,n),r()},function(){})},read=function(file,callback){if(Object.prototype.toString.call(options.root)==="[object Object]")try{var data=eval("(options.root."+file+")");Object.prototype.toString.call(data)==="[object String]"?callback(undefined,data):callback("Failed to load template")}catch(e){callback(e)}else fs.readFile(file,"utf8",callback)},load=function(e,t){options.useCache&&cache[e]?t&&t(undefined,cache[e]):loaders[e]?t&&loaders[e].push(t):(loaders[e]=[],t&&loaders[e].push(t),read(e,function(t,n){if(t)loaded(t,e,undefined);else try{var r=parse(n);options.useCache&&(cache[e]=r),loaded(undefined,e,r),options.watchForChanges&&(watchers[e]=fs.watch(e,function(){watchers[e].close(),delete watchers[e],delete cache[e]}))}catch(i){i.message=i.message+" in "+e,loaded(i,e,undefined)}}))},Template=function(e,t,n){this.file=e,Object.prototype.toString.call(options.root)==="[object String]"&&(this.file=path.normalize((options.root.length?options.root+"/":"")+e+options.ext)),this.data=t,this.customData=n||{},this.buffer=[],this.tmpBuffer=undefined,this.watcher=undefined,this.line=1,this.partials=[],this.childData=[],this.childError=undefined,this.childCallback=undefined,this.callback=undefined,this.blocks={}};Template.prototype.blockStart=function(e){this.tmpBuffer=this.buffer,this.blocks[e]||(this.blocks[e]=[]),this.blocks[e].length?this.buffer=[]:this.buffer=this.blocks[e]},Template.prototype.blockEnd=function(){this.buffer=this.tmpBuffer,delete this.tmpBuffer},Template.prototype.partial=function(e,t){var n=[],r=new Template(e,this.data,t);return this.partials.push(function(e){r.render(function(t,r){t||n.push(r),e(t)})}),n},Template.prototype.extend=function(e,t){var n=new Template(e,this.data,t),r=this.callback;return n.blocks=this.blocks,this.callback=function(e,t){e?(n.childError=e,n.childCallback&&n.childCallback(e)):(n.childData.push(t),n.childCallback&&n.childCallback())},n.partials.push(function(e){n.childError?e(n.childError):n.childData.length?e():n.childCallback=e}),n.render(r),""},Template.prototype.child=function(e){return e&&e.length?(this.blocks[e]||(this.blocks[e]=[]),this.blocks[e]):this.childData},Template.prototype.render=function(e){var t=this;this.callback=e,load(this.file,function(e,n){if(e)t.callback&&t.callback(e,undefined);else try{var r=n.call(t);async.parallel(t.partials,function(e){var n="",i,s;if(!e)for(s=0,i=r.length;s<i;s++)n+=Array.isArray(r[s])?r[s].join(""):r[s];t.callback&&t.callback(e,n)})}catch(i){i.message=i.message+" in "+t.file+" on line "+t.line,t.callback&&t.callback(i,undefined)}})},this.configure=function(e){var t;e=e||{};for(t in options)options[t]=e[t]||options[t]},this.render=function(e,t,n){"function"==typeof t&&(n=t,t={});var r=new Template(e,t);r.render(n)},this.configure(newOptions)};if(typeof module!="undefined"&&module.exports)fs=require("fs"),path=require("path"),module.exports=JUST;else{Array.prototype.filter||(Array.prototype.filter=function(e,t){var n=this.length,r=[],i,s;if(typeof e!="function")throw new TypeError;for(i=0;i<n;i++)i in this&&(s=this[i],e.call(t,s,i,this)&&r.push(s));return r}),Array.prototype.forEach||(Array.prototype.forEach=function(e,t){var n=this.length,r;if(typeof e!="function")throw new TypeError;for(r=0;r<n;r++)r in this&&e.call(t,this[r],r,this)}),Array.isArray||(Array.isArray=function(e){return Object.prototype.toString.call(e)==="[object Array]"});var cbSplit;cbSplit||(cbSplit=function(e,t,n){if(Object.prototype.toString.call(t)!=="[object RegExp]")return cbSplit.nativeSplit.call(e,t,n);var r=[],i=0,s=(t.ignoreCase?"i":"")+(t.multiline?"m":"")+(t.sticky?"y":""),o,u,a,f;t=new RegExp(t.source,s+"g"),e+="",cbSplit.compliantExecNpcg||(o=new RegExp("^"+t.source+"$(?!\\s)",s));if(n===undefined||+n<0)n=Infinity;else{n=Math.floor(+n);if(!n)return[]}while(u=t.exec(e)){a=u.index+u[0].length;if(a>i){r.push(e.slice(i,u.index)),!cbSplit.compliantExecNpcg&&u.length>1&&u[0].replace(o,function(){var e;for(e=1;e<arguments.length-2;e++)arguments[e]===undefined&&(u[e]=undefined)}),u.length>1&&u.index<e.length&&Array.prototype.push.apply(r,u.slice(1)),f=u[0].length,i=a;if(r.length>=n)break}t.lastIndex===u.index&&t.lastIndex++}return i===e.length?(f||!t.test(""))&&r.push(""):r.push(e.slice(i)),r.length>n?r.slice(0,n):r},cbSplit.compliantExecNpcg=/()??/.exec("")[1]===undefined,cbSplit.nativeSplit=String.prototype.split),String.prototype.split=function(e,t){return cbSplit(this,e,t)},window.JUST=JUST,path=function(){var e=function(e,t){var n=0,r,i;for(r=e.length-1;r>=0;r--)i=e[r],i==="."?e.splice(r,1):i===".."?(e.splice(r,1),n++):n&&(e.splice(r,1),n--);if(t)while(n)e.unshift(".."),n--;return e},t=function(t){var n=t.charAt(0)==="/",r=t.slice(-1)==="/";return t=e(t.split("/").filter(function(e){return!!e}),!n).join("/"),!t&&!n&&(t="."),t&&r&&(t+="/"),(n?"/":"")+t};return{normalize:t}}();var AjaxObject=function(e,t){var n=this;this.updating=!1,this.abort=function(){n.updating&&(n.updating=!1,n.AJAX.abort(),n.AJAX=null)},this.update=function(){return n.updating?!1:(n.AJAX=null,window.XMLHttpRequest?(n.AJAX=new XMLHttpRequest,n.AJAX.overrideMimeType&&n.AJAX.overrideMimeType("text/html")):n.AJAX=new ActiveXObject("Microsoft.XMLHTTP"),n.AJAX===null?!1:(n.AJAX.onreadystatechange=function(){n.AJAX.readyState===4&&(n.updating=!1,n.callback(n.AJAX.responseText,n.AJAX.status,n.AJAX.responseXML),n.AJAX=null)},n.updating=new Date,n.AJAX.open("GET",e,!0),n.AJAX.send(null),!0))},this.callback=t||function(){}};fs=function(){var e=function(e,t,n){var r=new AjaxObject(e,function(e,t){t<200||t>399?n("Failed to load template"):n(undefined,e)});try{r.update()}catch(i){n(i)}},t=function(){};return{readFile:e,watch:t}}()}})();