handlebars.js 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  1. // lib/handlebars/base.js
  2. var Handlebars = {};
  3. Handlebars.VERSION = "1.0.beta.6";
  4. Handlebars.helpers = {};
  5. Handlebars.partials = {};
  6. Handlebars.registerHelper = function(name, fn, inverse) {
  7. if(inverse) { fn.not = inverse; }
  8. this.helpers[name] = fn;
  9. };
  10. Handlebars.registerPartial = function(name, str) {
  11. this.partials[name] = str;
  12. };
  13. Handlebars.registerHelper('helperMissing', function(arg) {
  14. if(arguments.length === 2) {
  15. return undefined;
  16. } else {
  17. throw new Error("Could not find property '" + arg + "'");
  18. }
  19. });
  20. var toString = Object.prototype.toString, functionType = "[object Function]";
  21. Handlebars.registerHelper('blockHelperMissing', function(context, options) {
  22. var inverse = options.inverse || function() {}, fn = options.fn;
  23. var ret = "";
  24. var type = toString.call(context);
  25. if(type === functionType) { context = context.call(this); }
  26. if(context === true) {
  27. return fn(this);
  28. } else if(context === false || context == null) {
  29. return inverse(this);
  30. } else if(type === "[object Array]") {
  31. if(context.length > 0) {
  32. for(var i=0, j=context.length; i<j; i++) {
  33. ret = ret + fn(context[i]);
  34. }
  35. } else {
  36. ret = inverse(this);
  37. }
  38. return ret;
  39. } else {
  40. return fn(context);
  41. }
  42. });
  43. Handlebars.registerHelper('each', function(context, options) {
  44. var fn = options.fn, inverse = options.inverse;
  45. var ret = "";
  46. if(context && context.length > 0) {
  47. for(var i=0, j=context.length; i<j; i++) {
  48. ret = ret + fn(context[i]);
  49. }
  50. } else {
  51. ret = inverse(this);
  52. }
  53. return ret;
  54. });
  55. Handlebars.registerHelper('if', function(context, options) {
  56. var type = toString.call(context);
  57. if(type === functionType) { context = context.call(this); }
  58. if(!context || Handlebars.Utils.isEmpty(context)) {
  59. return options.inverse(this);
  60. } else {
  61. return options.fn(this);
  62. }
  63. });
  64. Handlebars.registerHelper('unless', function(context, options) {
  65. var fn = options.fn, inverse = options.inverse;
  66. options.fn = inverse;
  67. options.inverse = fn;
  68. return Handlebars.helpers['if'].call(this, context, options);
  69. });
  70. Handlebars.registerHelper('with', function(context, options) {
  71. return options.fn(context);
  72. });
  73. Handlebars.registerHelper('log', function(context) {
  74. Handlebars.log(context);
  75. });
  76. ;
  77. // lib/handlebars/compiler/parser.js
  78. /* Jison generated parser */
  79. var handlebars = (function(){
  80. var parser = {trace: function trace() { },
  81. yy: {},
  82. symbols_: {"error":2,"root":3,"program":4,"EOF":5,"statements":6,"simpleInverse":7,"statement":8,"openInverse":9,"closeBlock":10,"openBlock":11,"mustache":12,"partial":13,"CONTENT":14,"COMMENT":15,"OPEN_BLOCK":16,"inMustache":17,"CLOSE":18,"OPEN_INVERSE":19,"OPEN_ENDBLOCK":20,"path":21,"OPEN":22,"OPEN_UNESCAPED":23,"OPEN_PARTIAL":24,"params":25,"hash":26,"param":27,"STRING":28,"INTEGER":29,"BOOLEAN":30,"hashSegments":31,"hashSegment":32,"ID":33,"EQUALS":34,"pathSegments":35,"SEP":36,"$accept":0,"$end":1},
  83. terminals_: {2:"error",5:"EOF",14:"CONTENT",15:"COMMENT",16:"OPEN_BLOCK",18:"CLOSE",19:"OPEN_INVERSE",20:"OPEN_ENDBLOCK",22:"OPEN",23:"OPEN_UNESCAPED",24:"OPEN_PARTIAL",28:"STRING",29:"INTEGER",30:"BOOLEAN",33:"ID",34:"EQUALS",36:"SEP"},
  84. productions_: [0,[3,2],[4,3],[4,1],[4,0],[6,1],[6,2],[8,3],[8,3],[8,1],[8,1],[8,1],[8,1],[11,3],[9,3],[10,3],[12,3],[12,3],[13,3],[13,4],[7,2],[17,3],[17,2],[17,2],[17,1],[25,2],[25,1],[27,1],[27,1],[27,1],[27,1],[26,1],[31,2],[31,1],[32,3],[32,3],[32,3],[32,3],[21,1],[35,3],[35,1]],
  85. performAction: function anonymous(yytext,yyleng,yylineno,yy,yystate,$$,_$) {
  86. var $0 = $$.length - 1;
  87. switch (yystate) {
  88. case 1: return $$[$0-1]
  89. break;
  90. case 2: this.$ = new yy.ProgramNode($$[$0-2], $$[$0])
  91. break;
  92. case 3: this.$ = new yy.ProgramNode($$[$0])
  93. break;
  94. case 4: this.$ = new yy.ProgramNode([])
  95. break;
  96. case 5: this.$ = [$$[$0]]
  97. break;
  98. case 6: $$[$0-1].push($$[$0]); this.$ = $$[$0-1]
  99. break;
  100. case 7: this.$ = new yy.InverseNode($$[$0-2], $$[$0-1], $$[$0])
  101. break;
  102. case 8: this.$ = new yy.BlockNode($$[$0-2], $$[$0-1], $$[$0])
  103. break;
  104. case 9: this.$ = $$[$0]
  105. break;
  106. case 10: this.$ = $$[$0]
  107. break;
  108. case 11: this.$ = new yy.ContentNode($$[$0])
  109. break;
  110. case 12: this.$ = new yy.CommentNode($$[$0])
  111. break;
  112. case 13: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1])
  113. break;
  114. case 14: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1])
  115. break;
  116. case 15: this.$ = $$[$0-1]
  117. break;
  118. case 16: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1])
  119. break;
  120. case 17: this.$ = new yy.MustacheNode($$[$0-1][0], $$[$0-1][1], true)
  121. break;
  122. case 18: this.$ = new yy.PartialNode($$[$0-1])
  123. break;
  124. case 19: this.$ = new yy.PartialNode($$[$0-2], $$[$0-1])
  125. break;
  126. case 20:
  127. break;
  128. case 21: this.$ = [[$$[$0-2]].concat($$[$0-1]), $$[$0]]
  129. break;
  130. case 22: this.$ = [[$$[$0-1]].concat($$[$0]), null]
  131. break;
  132. case 23: this.$ = [[$$[$0-1]], $$[$0]]
  133. break;
  134. case 24: this.$ = [[$$[$0]], null]
  135. break;
  136. case 25: $$[$0-1].push($$[$0]); this.$ = $$[$0-1];
  137. break;
  138. case 26: this.$ = [$$[$0]]
  139. break;
  140. case 27: this.$ = $$[$0]
  141. break;
  142. case 28: this.$ = new yy.StringNode($$[$0])
  143. break;
  144. case 29: this.$ = new yy.IntegerNode($$[$0])
  145. break;
  146. case 30: this.$ = new yy.BooleanNode($$[$0])
  147. break;
  148. case 31: this.$ = new yy.HashNode($$[$0])
  149. break;
  150. case 32: $$[$0-1].push($$[$0]); this.$ = $$[$0-1]
  151. break;
  152. case 33: this.$ = [$$[$0]]
  153. break;
  154. case 34: this.$ = [$$[$0-2], $$[$0]]
  155. break;
  156. case 35: this.$ = [$$[$0-2], new yy.StringNode($$[$0])]
  157. break;
  158. case 36: this.$ = [$$[$0-2], new yy.IntegerNode($$[$0])]
  159. break;
  160. case 37: this.$ = [$$[$0-2], new yy.BooleanNode($$[$0])]
  161. break;
  162. case 38: this.$ = new yy.IdNode($$[$0])
  163. break;
  164. case 39: $$[$0-2].push($$[$0]); this.$ = $$[$0-2];
  165. break;
  166. case 40: this.$ = [$$[$0]]
  167. break;
  168. }
  169. },
  170. table: [{3:1,4:2,5:[2,4],6:3,8:4,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,11],22:[1,13],23:[1,14],24:[1,15]},{1:[3]},{5:[1,16]},{5:[2,3],7:17,8:18,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,19],20:[2,3],22:[1,13],23:[1,14],24:[1,15]},{5:[2,5],14:[2,5],15:[2,5],16:[2,5],19:[2,5],20:[2,5],22:[2,5],23:[2,5],24:[2,5]},{4:20,6:3,8:4,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,11],20:[2,4],22:[1,13],23:[1,14],24:[1,15]},{4:21,6:3,8:4,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,11],20:[2,4],22:[1,13],23:[1,14],24:[1,15]},{5:[2,9],14:[2,9],15:[2,9],16:[2,9],19:[2,9],20:[2,9],22:[2,9],23:[2,9],24:[2,9]},{5:[2,10],14:[2,10],15:[2,10],16:[2,10],19:[2,10],20:[2,10],22:[2,10],23:[2,10],24:[2,10]},{5:[2,11],14:[2,11],15:[2,11],16:[2,11],19:[2,11],20:[2,11],22:[2,11],23:[2,11],24:[2,11]},{5:[2,12],14:[2,12],15:[2,12],16:[2,12],19:[2,12],20:[2,12],22:[2,12],23:[2,12],24:[2,12]},{17:22,21:23,33:[1,25],35:24},{17:26,21:23,33:[1,25],35:24},{17:27,21:23,33:[1,25],35:24},{17:28,21:23,33:[1,25],35:24},{21:29,33:[1,25],35:24},{1:[2,1]},{6:30,8:4,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,11],22:[1,13],23:[1,14],24:[1,15]},{5:[2,6],14:[2,6],15:[2,6],16:[2,6],19:[2,6],20:[2,6],22:[2,6],23:[2,6],24:[2,6]},{17:22,18:[1,31],21:23,33:[1,25],35:24},{10:32,20:[1,33]},{10:34,20:[1,33]},{18:[1,35]},{18:[2,24],21:40,25:36,26:37,27:38,28:[1,41],29:[1,42],30:[1,43],31:39,32:44,33:[1,45],35:24},{18:[2,38],28:[2,38],29:[2,38],30:[2,38],33:[2,38],36:[1,46]},{18:[2,40],28:[2,40],29:[2,40],30:[2,40],33:[2,40],36:[2,40]},{18:[1,47]},{18:[1,48]},{18:[1,49]},{18:[1,50],21:51,33:[1,25],35:24},{5:[2,2],8:18,9:5,11:6,12:7,13:8,14:[1,9],15:[1,10],16:[1,12],19:[1,11],20:[2,2],22:[1,13],23:[1,14],24:[1,15]},{14:[2,20],15:[2,20],16:[2,20],19:[2,20],22:[2,20],23:[2,20],24:[2,20]},{5:[2,7],14:[2,7],15:[2,7],16:[2,7],19:[2,7],20:[2,7],22:[2,7],23:[2,7],24:[2,7]},{21:52,33:[1,25],35:24},{5:[2,8],14:[2,8],15:[2,8],16:[2,8],19:[2,8],20:[2,8],22:[2,8],23:[2,8],24:[2,8]},{14:[2,14],15:[2,14],16:[2,14],19:[2,14],20:[2,14],22:[2,14],23:[2,14],24:[2,14]},{18:[2,22],21:40,26:53,27:54,28:[1,41],29:[1,42],30:[1,43],31:39,32:44,33:[1,45],35:24},{18:[2,23]},{18:[2,26],28:[2,26],29:[2,26],30:[2,26],33:[2,26]},{18:[2,31],32:55,33:[1,56]},{18:[2,27],28:[2,27],29:[2,27],30:[2,27],33:[2,27]},{18:[2,28],28:[2,28],29:[2,28],30:[2,28],33:[2,28]},{18:[2,29],28:[2,29],29:[2,29],30:[2,29],33:[2,29]},{18:[2,30],28:[2,30],29:[2,30],30:[2,30],33:[2,30]},{18:[2,33],33:[2,33]},{18:[2,40],28:[2,40],29:[2,40],30:[2,40],33:[2,40],34:[1,57],36:[2,40]},{33:[1,58]},{14:[2,13],15:[2,13],16:[2,13],19:[2,13],20:[2,13],22:[2,13],23:[2,13],24:[2,13]},{5:[2,16],14:[2,16],15:[2,16],16:[2,16],19:[2,16],20:[2,16],22:[2,16],23:[2,16],24:[2,16]},{5:[2,17],14:[2,17],15:[2,17],16:[2,17],19:[2,17],20:[2,17],22:[2,17],23:[2,17],24:[2,17]},{5:[2,18],14:[2,18],15:[2,18],16:[2,18],19:[2,18],20:[2,18],22:[2,18],23:[2,18],24:[2,18]},{18:[1,59]},{18:[1,60]},{18:[2,21]},{18:[2,25],28:[2,25],29:[2,25],30:[2,25],33:[2,25]},{18:[2,32],33:[2,32]},{34:[1,57]},{21:61,28:[1,62],29:[1,63],30:[1,64],33:[1,25],35:24},{18:[2,39],28:[2,39],29:[2,39],30:[2,39],33:[2,39],36:[2,39]},{5:[2,19],14:[2,19],15:[2,19],16:[2,19],19:[2,19],20:[2,19],22:[2,19],23:[2,19],24:[2,19]},{5:[2,15],14:[2,15],15:[2,15],16:[2,15],19:[2,15],20:[2,15],22:[2,15],23:[2,15],24:[2,15]},{18:[2,34],33:[2,34]},{18:[2,35],33:[2,35]},{18:[2,36],33:[2,36]},{18:[2,37],33:[2,37]}],
  171. defaultActions: {16:[2,1],37:[2,23],53:[2,21]},
  172. parseError: function parseError(str, hash) {
  173. throw new Error(str);
  174. },
  175. parse: function parse(input) {
  176. var self = this, stack = [0], vstack = [null], lstack = [], table = this.table, yytext = "", yylineno = 0, yyleng = 0, recovering = 0, TERROR = 2, EOF = 1;
  177. this.lexer.setInput(input);
  178. this.lexer.yy = this.yy;
  179. this.yy.lexer = this.lexer;
  180. if (typeof this.lexer.yylloc == "undefined")
  181. this.lexer.yylloc = {};
  182. var yyloc = this.lexer.yylloc;
  183. lstack.push(yyloc);
  184. if (typeof this.yy.parseError === "function")
  185. this.parseError = this.yy.parseError;
  186. function popStack(n) {
  187. stack.length = stack.length - 2 * n;
  188. vstack.length = vstack.length - n;
  189. lstack.length = lstack.length - n;
  190. }
  191. function lex() {
  192. var token;
  193. token = self.lexer.lex() || 1;
  194. if (typeof token !== "number") {
  195. token = self.symbols_[token] || token;
  196. }
  197. return token;
  198. }
  199. var symbol, preErrorSymbol, state, action, a, r, yyval = {}, p, len, newState, expected;
  200. while (true) {
  201. state = stack[stack.length - 1];
  202. if (this.defaultActions[state]) {
  203. action = this.defaultActions[state];
  204. } else {
  205. if (symbol == null)
  206. symbol = lex();
  207. action = table[state] && table[state][symbol];
  208. }
  209. if (typeof action === "undefined" || !action.length || !action[0]) {
  210. if (!recovering) {
  211. expected = [];
  212. for (p in table[state])
  213. if (this.terminals_[p] && p > 2) {
  214. expected.push("'" + this.terminals_[p] + "'");
  215. }
  216. var errStr = "";
  217. if (this.lexer.showPosition) {
  218. errStr = "Parse error on line " + (yylineno + 1) + ":\n" + this.lexer.showPosition() + "\nExpecting " + expected.join(", ") + ", got '" + this.terminals_[symbol] + "'";
  219. } else {
  220. errStr = "Parse error on line " + (yylineno + 1) + ": Unexpected " + (symbol == 1?"end of input":"'" + (this.terminals_[symbol] || symbol) + "'");
  221. }
  222. this.parseError(errStr, {text: this.lexer.match, token: this.terminals_[symbol] || symbol, line: this.lexer.yylineno, loc: yyloc, expected: expected});
  223. }
  224. }
  225. if (action[0] instanceof Array && action.length > 1) {
  226. throw new Error("Parse Error: multiple actions possible at state: " + state + ", token: " + symbol);
  227. }
  228. switch (action[0]) {
  229. case 1:
  230. stack.push(symbol);
  231. vstack.push(this.lexer.yytext);
  232. lstack.push(this.lexer.yylloc);
  233. stack.push(action[1]);
  234. symbol = null;
  235. if (!preErrorSymbol) {
  236. yyleng = this.lexer.yyleng;
  237. yytext = this.lexer.yytext;
  238. yylineno = this.lexer.yylineno;
  239. yyloc = this.lexer.yylloc;
  240. if (recovering > 0)
  241. recovering--;
  242. } else {
  243. symbol = preErrorSymbol;
  244. preErrorSymbol = null;
  245. }
  246. break;
  247. case 2:
  248. len = this.productions_[action[1]][1];
  249. yyval.$ = vstack[vstack.length - len];
  250. yyval._$ = {first_line: lstack[lstack.length - (len || 1)].first_line, last_line: lstack[lstack.length - 1].last_line, first_column: lstack[lstack.length - (len || 1)].first_column, last_column: lstack[lstack.length - 1].last_column};
  251. r = this.performAction.call(yyval, yytext, yyleng, yylineno, this.yy, action[1], vstack, lstack);
  252. if (typeof r !== "undefined") {
  253. return r;
  254. }
  255. if (len) {
  256. stack = stack.slice(0, -1 * len * 2);
  257. vstack = vstack.slice(0, -1 * len);
  258. lstack = lstack.slice(0, -1 * len);
  259. }
  260. stack.push(this.productions_[action[1]][0]);
  261. vstack.push(yyval.$);
  262. lstack.push(yyval._$);
  263. newState = table[stack[stack.length - 2]][stack[stack.length - 1]];
  264. stack.push(newState);
  265. break;
  266. case 3:
  267. return true;
  268. }
  269. }
  270. return true;
  271. }
  272. };/* Jison generated lexer */
  273. var lexer = (function(){
  274. var lexer = ({EOF:1,
  275. parseError:function parseError(str, hash) {
  276. if (this.yy.parseError) {
  277. this.yy.parseError(str, hash);
  278. } else {
  279. throw new Error(str);
  280. }
  281. },
  282. setInput:function (input) {
  283. this._input = input;
  284. this._more = this._less = this.done = false;
  285. this.yylineno = this.yyleng = 0;
  286. this.yytext = this.matched = this.match = '';
  287. this.conditionStack = ['INITIAL'];
  288. this.yylloc = {first_line:1,first_column:0,last_line:1,last_column:0};
  289. return this;
  290. },
  291. input:function () {
  292. var ch = this._input[0];
  293. this.yytext+=ch;
  294. this.yyleng++;
  295. this.match+=ch;
  296. this.matched+=ch;
  297. var lines = ch.match(/\n/);
  298. if (lines) this.yylineno++;
  299. this._input = this._input.slice(1);
  300. return ch;
  301. },
  302. unput:function (ch) {
  303. this._input = ch + this._input;
  304. return this;
  305. },
  306. more:function () {
  307. this._more = true;
  308. return this;
  309. },
  310. pastInput:function () {
  311. var past = this.matched.substr(0, this.matched.length - this.match.length);
  312. return (past.length > 20 ? '...':'') + past.substr(-20).replace(/\n/g, "");
  313. },
  314. upcomingInput:function () {
  315. var next = this.match;
  316. if (next.length < 20) {
  317. next += this._input.substr(0, 20-next.length);
  318. }
  319. return (next.substr(0,20)+(next.length > 20 ? '...':'')).replace(/\n/g, "");
  320. },
  321. showPosition:function () {
  322. var pre = this.pastInput();
  323. var c = new Array(pre.length + 1).join("-");
  324. return pre + this.upcomingInput() + "\n" + c+"^";
  325. },
  326. next:function () {
  327. if (this.done) {
  328. return this.EOF;
  329. }
  330. if (!this._input) this.done = true;
  331. var token,
  332. match,
  333. col,
  334. lines;
  335. if (!this._more) {
  336. this.yytext = '';
  337. this.match = '';
  338. }
  339. var rules = this._currentRules();
  340. for (var i=0;i < rules.length; i++) {
  341. match = this._input.match(this.rules[rules[i]]);
  342. if (match) {
  343. lines = match[0].match(/\n.*/g);
  344. if (lines) this.yylineno += lines.length;
  345. this.yylloc = {first_line: this.yylloc.last_line,
  346. last_line: this.yylineno+1,
  347. first_column: this.yylloc.last_column,
  348. last_column: lines ? lines[lines.length-1].length-1 : this.yylloc.last_column + match[0].length}
  349. this.yytext += match[0];
  350. this.match += match[0];
  351. this.matches = match;
  352. this.yyleng = this.yytext.length;
  353. this._more = false;
  354. this._input = this._input.slice(match[0].length);
  355. this.matched += match[0];
  356. token = this.performAction.call(this, this.yy, this, rules[i],this.conditionStack[this.conditionStack.length-1]);
  357. if (token) return token;
  358. else return;
  359. }
  360. }
  361. if (this._input === "") {
  362. return this.EOF;
  363. } else {
  364. this.parseError('Lexical error on line '+(this.yylineno+1)+'. Unrecognized text.\n'+this.showPosition(),
  365. {text: "", token: null, line: this.yylineno});
  366. }
  367. },
  368. lex:function lex() {
  369. var r = this.next();
  370. if (typeof r !== 'undefined') {
  371. return r;
  372. } else {
  373. return this.lex();
  374. }
  375. },
  376. begin:function begin(condition) {
  377. this.conditionStack.push(condition);
  378. },
  379. popState:function popState() {
  380. return this.conditionStack.pop();
  381. },
  382. _currentRules:function _currentRules() {
  383. return this.conditions[this.conditionStack[this.conditionStack.length-1]].rules;
  384. },
  385. topState:function () {
  386. return this.conditionStack[this.conditionStack.length-2];
  387. },
  388. pushState:function begin(condition) {
  389. this.begin(condition);
  390. }});
  391. lexer.performAction = function anonymous(yy,yy_,$avoiding_name_collisions,YY_START) {
  392. var YYSTATE=YY_START
  393. switch($avoiding_name_collisions) {
  394. case 0:
  395. if(yy_.yytext.slice(-1) !== "\\") this.begin("mu");
  396. if(yy_.yytext.slice(-1) === "\\") yy_.yytext = yy_.yytext.substr(0,yy_.yyleng-1), this.begin("emu");
  397. if(yy_.yytext) return 14;
  398. break;
  399. case 1: return 14;
  400. break;
  401. case 2: this.popState(); return 14;
  402. break;
  403. case 3: return 24;
  404. break;
  405. case 4: return 16;
  406. break;
  407. case 5: return 20;
  408. break;
  409. case 6: return 19;
  410. break;
  411. case 7: return 19;
  412. break;
  413. case 8: return 23;
  414. break;
  415. case 9: return 23;
  416. break;
  417. case 10: yy_.yytext = yy_.yytext.substr(3,yy_.yyleng-5); this.popState(); return 15;
  418. break;
  419. case 11: return 22;
  420. break;
  421. case 12: return 34;
  422. break;
  423. case 13: return 33;
  424. break;
  425. case 14: return 33;
  426. break;
  427. case 15: return 36;
  428. break;
  429. case 16: /*ignore whitespace*/
  430. break;
  431. case 17: this.popState(); return 18;
  432. break;
  433. case 18: this.popState(); return 18;
  434. break;
  435. case 19: yy_.yytext = yy_.yytext.substr(1,yy_.yyleng-2).replace(/\\"/g,'"'); return 28;
  436. break;
  437. case 20: return 30;
  438. break;
  439. case 21: return 30;
  440. break;
  441. case 22: return 29;
  442. break;
  443. case 23: return 33;
  444. break;
  445. case 24: yy_.yytext = yy_.yytext.substr(1, yy_.yyleng-2); return 33;
  446. break;
  447. case 25: return 'INVALID';
  448. break;
  449. case 26: return 5;
  450. break;
  451. }
  452. };
  453. lexer.rules = [/^[^\x00]*?(?=(\{\{))/,/^[^\x00]+/,/^[^\x00]{2,}?(?=(\{\{))/,/^\{\{>/,/^\{\{#/,/^\{\{\//,/^\{\{\^/,/^\{\{\s*else\b/,/^\{\{\{/,/^\{\{&/,/^\{\{![\s\S]*?\}\}/,/^\{\{/,/^=/,/^\.(?=[} ])/,/^\.\./,/^[\/.]/,/^\s+/,/^\}\}\}/,/^\}\}/,/^"(\\["]|[^"])*"/,/^true(?=[}\s])/,/^false(?=[}\s])/,/^[0-9]+(?=[}\s])/,/^[a-zA-Z0-9_$-]+(?=[=}\s\/.])/,/^\[[^\]]*\]/,/^./,/^$/];
  454. lexer.conditions = {"mu":{"rules":[3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26],"inclusive":false},"emu":{"rules":[2],"inclusive":false},"INITIAL":{"rules":[0,1,26],"inclusive":true}};return lexer;})()
  455. parser.lexer = lexer;
  456. return parser;
  457. })();
  458. if (typeof require !== 'undefined' && typeof exports !== 'undefined') {
  459. exports.parser = handlebars;
  460. exports.parse = function () { return handlebars.parse.apply(handlebars, arguments); }
  461. exports.main = function commonjsMain(args) {
  462. if (!args[1])
  463. throw new Error('Usage: '+args[0]+' FILE');
  464. if (typeof process !== 'undefined') {
  465. var source = require('fs').readFileSync(require('path').join(process.cwd(), args[1]), "utf8");
  466. } else {
  467. var cwd = require("file").path(require("file").cwd());
  468. var source = cwd.join(args[1]).read({charset: "utf-8"});
  469. }
  470. return exports.parser.parse(source);
  471. }
  472. if (typeof module !== 'undefined' && require.main === module) {
  473. exports.main(typeof process !== 'undefined' ? process.argv.slice(1) : require("system").args);
  474. }
  475. };
  476. ;
  477. // lib/handlebars/compiler/base.js
  478. Handlebars.Parser = handlebars;
  479. Handlebars.parse = function(string) {
  480. Handlebars.Parser.yy = Handlebars.AST;
  481. return Handlebars.Parser.parse(string);
  482. };
  483. Handlebars.print = function(ast) {
  484. return new Handlebars.PrintVisitor().accept(ast);
  485. };
  486. Handlebars.logger = {
  487. DEBUG: 0, INFO: 1, WARN: 2, ERROR: 3, level: 3,
  488. // override in the host environment
  489. log: function(level, str) {}
  490. };
  491. Handlebars.log = function(level, str) { Handlebars.logger.log(level, str); };
  492. ;
  493. // lib/handlebars/compiler/ast.js
  494. (function() {
  495. Handlebars.AST = {};
  496. Handlebars.AST.ProgramNode = function(statements, inverse) {
  497. this.type = "program";
  498. this.statements = statements;
  499. if(inverse) { this.inverse = new Handlebars.AST.ProgramNode(inverse); }
  500. };
  501. Handlebars.AST.MustacheNode = function(params, hash, unescaped) {
  502. this.type = "mustache";
  503. this.id = params[0];
  504. this.params = params.slice(1);
  505. this.hash = hash;
  506. this.escaped = !unescaped;
  507. };
  508. Handlebars.AST.PartialNode = function(id, context) {
  509. this.type = "partial";
  510. // TODO: disallow complex IDs
  511. this.id = id;
  512. this.context = context;
  513. };
  514. var verifyMatch = function(open, close) {
  515. if(open.original !== close.original) {
  516. throw new Handlebars.Exception(open.original + " doesn't match " + close.original);
  517. }
  518. };
  519. Handlebars.AST.BlockNode = function(mustache, program, close) {
  520. verifyMatch(mustache.id, close);
  521. this.type = "block";
  522. this.mustache = mustache;
  523. this.program = program;
  524. };
  525. Handlebars.AST.InverseNode = function(mustache, program, close) {
  526. verifyMatch(mustache.id, close);
  527. this.type = "inverse";
  528. this.mustache = mustache;
  529. this.program = program;
  530. };
  531. Handlebars.AST.ContentNode = function(string) {
  532. this.type = "content";
  533. this.string = string;
  534. };
  535. Handlebars.AST.HashNode = function(pairs) {
  536. this.type = "hash";
  537. this.pairs = pairs;
  538. };
  539. Handlebars.AST.IdNode = function(parts) {
  540. this.type = "ID";
  541. this.original = parts.join(".");
  542. var dig = [], depth = 0;
  543. for(var i=0,l=parts.length; i<l; i++) {
  544. var part = parts[i];
  545. if(part === "..") { depth++; }
  546. else if(part === "." || part === "this") { this.isScoped = true; }
  547. else { dig.push(part); }
  548. }
  549. this.parts = dig;
  550. this.string = dig.join('.');
  551. this.depth = depth;
  552. this.isSimple = (dig.length === 1) && (depth === 0);
  553. };
  554. Handlebars.AST.StringNode = function(string) {
  555. this.type = "STRING";
  556. this.string = string;
  557. };
  558. Handlebars.AST.IntegerNode = function(integer) {
  559. this.type = "INTEGER";
  560. this.integer = integer;
  561. };
  562. Handlebars.AST.BooleanNode = function(bool) {
  563. this.type = "BOOLEAN";
  564. this.bool = bool;
  565. };
  566. Handlebars.AST.CommentNode = function(comment) {
  567. this.type = "comment";
  568. this.comment = comment;
  569. };
  570. })();;
  571. // lib/handlebars/utils.js
  572. Handlebars.Exception = function(message) {
  573. var tmp = Error.prototype.constructor.apply(this, arguments);
  574. for (var p in tmp) {
  575. if (tmp.hasOwnProperty(p)) { this[p] = tmp[p]; }
  576. }
  577. this.message = tmp.message;
  578. };
  579. Handlebars.Exception.prototype = new Error;
  580. // Build out our basic SafeString type
  581. Handlebars.SafeString = function(string) {
  582. this.string = string;
  583. };
  584. Handlebars.SafeString.prototype.toString = function() {
  585. return this.string.toString();
  586. };
  587. (function() {
  588. var escape = {
  589. "<": "&lt;",
  590. ">": "&gt;",
  591. '"': "&quot;",
  592. "'": "&#x27;",
  593. "`": "&#x60;"
  594. };
  595. var badChars = /&(?!\w+;)|[<>"'`]/g;
  596. var possible = /[&<>"'`]/;
  597. var escapeChar = function(chr) {
  598. return escape[chr] || "&amp;";
  599. };
  600. Handlebars.Utils = {
  601. escapeExpression: function(string) {
  602. // don't escape SafeStrings, since they're already safe
  603. if (string instanceof Handlebars.SafeString) {
  604. return string.toString();
  605. } else if (string == null || string === false) {
  606. return "";
  607. }
  608. if(!possible.test(string)) { return string; }
  609. return string.replace(badChars, escapeChar);
  610. },
  611. isEmpty: function(value) {
  612. if (typeof value === "undefined") {
  613. return true;
  614. } else if (value === null) {
  615. return true;
  616. } else if (value === false) {
  617. return true;
  618. } else if(Object.prototype.toString.call(value) === "[object Array]" && value.length === 0) {
  619. return true;
  620. } else {
  621. return false;
  622. }
  623. }
  624. };
  625. })();;
  626. // lib/handlebars/compiler/compiler.js
  627. Handlebars.Compiler = function() {};
  628. Handlebars.JavaScriptCompiler = function() {};
  629. (function(Compiler, JavaScriptCompiler) {
  630. Compiler.OPCODE_MAP = {
  631. appendContent: 1,
  632. getContext: 2,
  633. lookupWithHelpers: 3,
  634. lookup: 4,
  635. append: 5,
  636. invokeMustache: 6,
  637. appendEscaped: 7,
  638. pushString: 8,
  639. truthyOrFallback: 9,
  640. functionOrFallback: 10,
  641. invokeProgram: 11,
  642. invokePartial: 12,
  643. push: 13,
  644. assignToHash: 15,
  645. pushStringParam: 16
  646. };
  647. Compiler.MULTI_PARAM_OPCODES = {
  648. appendContent: 1,
  649. getContext: 1,
  650. lookupWithHelpers: 2,
  651. lookup: 1,
  652. invokeMustache: 3,
  653. pushString: 1,
  654. truthyOrFallback: 1,
  655. functionOrFallback: 1,
  656. invokeProgram: 3,
  657. invokePartial: 1,
  658. push: 1,
  659. assignToHash: 1,
  660. pushStringParam: 1
  661. };
  662. Compiler.DISASSEMBLE_MAP = {};
  663. for(var prop in Compiler.OPCODE_MAP) {
  664. var value = Compiler.OPCODE_MAP[prop];
  665. Compiler.DISASSEMBLE_MAP[value] = prop;
  666. }
  667. Compiler.multiParamSize = function(code) {
  668. return Compiler.MULTI_PARAM_OPCODES[Compiler.DISASSEMBLE_MAP[code]];
  669. };
  670. Compiler.prototype = {
  671. compiler: Compiler,
  672. disassemble: function() {
  673. var opcodes = this.opcodes, opcode, nextCode;
  674. var out = [], str, name, value;
  675. for(var i=0, l=opcodes.length; i<l; i++) {
  676. opcode = opcodes[i];
  677. if(opcode === 'DECLARE') {
  678. name = opcodes[++i];
  679. value = opcodes[++i];
  680. out.push("DECLARE " + name + " = " + value);
  681. } else {
  682. str = Compiler.DISASSEMBLE_MAP[opcode];
  683. var extraParams = Compiler.multiParamSize(opcode);
  684. var codes = [];
  685. for(var j=0; j<extraParams; j++) {
  686. nextCode = opcodes[++i];
  687. if(typeof nextCode === "string") {
  688. nextCode = "\"" + nextCode.replace("\n", "\\n") + "\"";
  689. }
  690. codes.push(nextCode);
  691. }
  692. str = str + " " + codes.join(" ");
  693. out.push(str);
  694. }
  695. }
  696. return out.join("\n");
  697. },
  698. guid: 0,
  699. compile: function(program, options) {
  700. this.children = [];
  701. this.depths = {list: []};
  702. this.options = options;
  703. // These changes will propagate to the other compiler components
  704. var knownHelpers = this.options.knownHelpers;
  705. this.options.knownHelpers = {
  706. 'helperMissing': true,
  707. 'blockHelperMissing': true,
  708. 'each': true,
  709. 'if': true,
  710. 'unless': true,
  711. 'with': true,
  712. 'log': true
  713. };
  714. if (knownHelpers) {
  715. for (var name in knownHelpers) {
  716. this.options.knownHelpers[name] = knownHelpers[name];
  717. }
  718. }
  719. return this.program(program);
  720. },
  721. accept: function(node) {
  722. return this[node.type](node);
  723. },
  724. program: function(program) {
  725. var statements = program.statements, statement;
  726. this.opcodes = [];
  727. for(var i=0, l=statements.length; i<l; i++) {
  728. statement = statements[i];
  729. this[statement.type](statement);
  730. }
  731. this.isSimple = l === 1;
  732. this.depths.list = this.depths.list.sort(function(a, b) {
  733. return a - b;
  734. });
  735. return this;
  736. },
  737. compileProgram: function(program) {
  738. var result = new this.compiler().compile(program, this.options);
  739. var guid = this.guid++;
  740. this.usePartial = this.usePartial || result.usePartial;
  741. this.children[guid] = result;
  742. for(var i=0, l=result.depths.list.length; i<l; i++) {
  743. depth = result.depths.list[i];
  744. if(depth < 2) { continue; }
  745. else { this.addDepth(depth - 1); }
  746. }
  747. return guid;
  748. },
  749. block: function(block) {
  750. var mustache = block.mustache;
  751. var depth, child, inverse, inverseGuid;
  752. var params = this.setupStackForMustache(mustache);
  753. var programGuid = this.compileProgram(block.program);
  754. if(block.program.inverse) {
  755. inverseGuid = this.compileProgram(block.program.inverse);
  756. this.declare('inverse', inverseGuid);
  757. }
  758. this.opcode('invokeProgram', programGuid, params.length, !!mustache.hash);
  759. this.declare('inverse', null);
  760. this.opcode('append');
  761. },
  762. inverse: function(block) {
  763. var params = this.setupStackForMustache(block.mustache);
  764. var programGuid = this.compileProgram(block.program);
  765. this.declare('inverse', programGuid);
  766. this.opcode('invokeProgram', null, params.length, !!block.mustache.hash);
  767. this.declare('inverse', null);
  768. this.opcode('append');
  769. },
  770. hash: function(hash) {
  771. var pairs = hash.pairs, pair, val;
  772. this.opcode('push', '{}');
  773. for(var i=0, l=pairs.length; i<l; i++) {
  774. pair = pairs[i];
  775. val = pair[1];
  776. this.accept(val);
  777. this.opcode('assignToHash', pair[0]);
  778. }
  779. },
  780. partial: function(partial) {
  781. var id = partial.id;
  782. this.usePartial = true;
  783. if(partial.context) {
  784. this.ID(partial.context);
  785. } else {
  786. this.opcode('push', 'depth0');
  787. }
  788. this.opcode('invokePartial', id.original);
  789. this.opcode('append');
  790. },
  791. content: function(content) {
  792. this.opcode('appendContent', content.string);
  793. },
  794. mustache: function(mustache) {
  795. var params = this.setupStackForMustache(mustache);
  796. this.opcode('invokeMustache', params.length, mustache.id.original, !!mustache.hash);
  797. if(mustache.escaped && !this.options.noEscape) {
  798. this.opcode('appendEscaped');
  799. } else {
  800. this.opcode('append');
  801. }
  802. },
  803. ID: function(id) {
  804. this.addDepth(id.depth);
  805. this.opcode('getContext', id.depth);
  806. this.opcode('lookupWithHelpers', id.parts[0] || null, id.isScoped || false);
  807. for(var i=1, l=id.parts.length; i<l; i++) {
  808. this.opcode('lookup', id.parts[i]);
  809. }
  810. },
  811. STRING: function(string) {
  812. this.opcode('pushString', string.string);
  813. },
  814. INTEGER: function(integer) {
  815. this.opcode('push', integer.integer);
  816. },
  817. BOOLEAN: function(bool) {
  818. this.opcode('push', bool.bool);
  819. },
  820. comment: function() {},
  821. // HELPERS
  822. pushParams: function(params) {
  823. var i = params.length, param;
  824. while(i--) {
  825. param = params[i];
  826. if(this.options.stringParams) {
  827. if(param.depth) {
  828. this.addDepth(param.depth);
  829. }
  830. this.opcode('getContext', param.depth || 0);
  831. this.opcode('pushStringParam', param.string);
  832. } else {
  833. this[param.type](param);
  834. }
  835. }
  836. },
  837. opcode: function(name, val1, val2, val3) {
  838. this.opcodes.push(Compiler.OPCODE_MAP[name]);
  839. if(val1 !== undefined) { this.opcodes.push(val1); }
  840. if(val2 !== undefined) { this.opcodes.push(val2); }
  841. if(val3 !== undefined) { this.opcodes.push(val3); }
  842. },
  843. declare: function(name, value) {
  844. this.opcodes.push('DECLARE');
  845. this.opcodes.push(name);
  846. this.opcodes.push(value);
  847. },
  848. addDepth: function(depth) {
  849. if(depth === 0) { return; }
  850. if(!this.depths[depth]) {
  851. this.depths[depth] = true;
  852. this.depths.list.push(depth);
  853. }
  854. },
  855. setupStackForMustache: function(mustache) {
  856. var params = mustache.params;
  857. this.pushParams(params);
  858. if(mustache.hash) {
  859. this.hash(mustache.hash);
  860. }
  861. this.ID(mustache.id);
  862. return params;
  863. }
  864. };
  865. JavaScriptCompiler.prototype = {
  866. // PUBLIC API: You can override these methods in a subclass to provide
  867. // alternative compiled forms for name lookup and buffering semantics
  868. nameLookup: function(parent, name, type) {
  869. if (/^[0-9]+$/.test(name)) {
  870. return parent + "[" + name + "]";
  871. } else if (JavaScriptCompiler.isValidJavaScriptVariableName(name)) {
  872. return parent + "." + name;
  873. }
  874. else {
  875. return parent + "['" + name + "']";
  876. }
  877. },
  878. appendToBuffer: function(string) {
  879. if (this.environment.isSimple) {
  880. return "return " + string + ";";
  881. } else {
  882. return "buffer += " + string + ";";
  883. }
  884. },
  885. initializeBuffer: function() {
  886. return this.quotedString("");
  887. },
  888. namespace: "Handlebars",
  889. // END PUBLIC API
  890. compile: function(environment, options, context, asObject) {
  891. this.environment = environment;
  892. this.options = options || {};
  893. this.name = this.environment.name;
  894. this.isChild = !!context;
  895. this.context = context || {
  896. programs: [],
  897. aliases: { self: 'this' },
  898. registers: {list: []}
  899. };
  900. this.preamble();
  901. this.stackSlot = 0;
  902. this.stackVars = [];
  903. this.compileChildren(environment, options);
  904. var opcodes = environment.opcodes, opcode;
  905. this.i = 0;
  906. for(l=opcodes.length; this.i<l; this.i++) {
  907. opcode = this.nextOpcode(0);
  908. if(opcode[0] === 'DECLARE') {
  909. this.i = this.i + 2;
  910. this[opcode[1]] = opcode[2];
  911. } else {
  912. this.i = this.i + opcode[1].length;
  913. this[opcode[0]].apply(this, opcode[1]);
  914. }
  915. }
  916. return this.createFunctionContext(asObject);
  917. },
  918. nextOpcode: function(n) {
  919. var opcodes = this.environment.opcodes, opcode = opcodes[this.i + n], name, val;
  920. var extraParams, codes;
  921. if(opcode === 'DECLARE') {
  922. name = opcodes[this.i + 1];
  923. val = opcodes[this.i + 2];
  924. return ['DECLARE', name, val];
  925. } else {
  926. name = Compiler.DISASSEMBLE_MAP[opcode];
  927. extraParams = Compiler.multiParamSize(opcode);
  928. codes = [];
  929. for(var j=0; j<extraParams; j++) {
  930. codes.push(opcodes[this.i + j + 1 + n]);
  931. }
  932. return [name, codes];
  933. }
  934. },
  935. eat: function(opcode) {
  936. this.i = this.i + opcode.length;
  937. },
  938. preamble: function() {
  939. var out = [];
  940. // this register will disambiguate helper lookup from finding a function in
  941. // a context. This is necessary for mustache compatibility, which requires
  942. // that context functions in blocks are evaluated by blockHelperMissing, and
  943. // then proceed as if the resulting value was provided to blockHelperMissing.
  944. this.useRegister('foundHelper');
  945. if (!this.isChild) {
  946. var namespace = this.namespace;
  947. var copies = "helpers = helpers || " + namespace + ".helpers;";
  948. if(this.environment.usePartial) { copies = copies + " partials = partials || " + namespace + ".partials;"; }
  949. out.push(copies);
  950. } else {
  951. out.push('');
  952. }
  953. if (!this.environment.isSimple) {
  954. out.push(", buffer = " + this.initializeBuffer());
  955. } else {
  956. out.push("");
  957. }
  958. // track the last context pushed into place to allow skipping the
  959. // getContext opcode when it would be a noop
  960. this.lastContext = 0;
  961. this.source = out;
  962. },
  963. createFunctionContext: function(asObject) {
  964. var locals = this.stackVars;
  965. if (!this.isChild) {
  966. locals = locals.concat(this.context.registers.list);
  967. }
  968. if(locals.length > 0) {
  969. this.source[1] = this.source[1] + ", " + locals.join(", ");
  970. }
  971. // Generate minimizer alias mappings
  972. if (!this.isChild) {
  973. var aliases = []
  974. for (var alias in this.context.aliases) {
  975. this.source[1] = this.source[1] + ', ' + alias + '=' + this.context.aliases[alias];
  976. }
  977. }
  978. if (this.source[1]) {
  979. this.source[1] = "var " + this.source[1].substring(2) + ";";
  980. }
  981. // Merge children
  982. if (!this.isChild) {
  983. this.source[1] += '\n' + this.context.programs.join('\n') + '\n';
  984. }
  985. if (!this.environment.isSimple) {
  986. this.source.push("return buffer;");
  987. }
  988. var params = this.isChild ? ["depth0", "data"] : ["Handlebars", "depth0", "helpers", "partials", "data"];
  989. for(var i=0, l=this.environment.depths.list.length; i<l; i++) {
  990. params.push("depth" + this.environment.depths.list[i]);
  991. }
  992. if (asObject) {
  993. params.push(this.source.join("\n "));
  994. return Function.apply(this, params);
  995. } else {
  996. var functionSource = 'function ' + (this.name || '') + '(' + params.join(',') + ') {\n ' + this.source.join("\n ") + '}';
  997. Handlebars.log(Handlebars.logger.DEBUG, functionSource + "\n\n");
  998. return functionSource;
  999. }
  1000. },
  1001. appendContent: function(content) {
  1002. this.source.push(this.appendToBuffer(this.quotedString(content)));
  1003. },
  1004. append: function() {
  1005. var local = this.popStack();
  1006. this.source.push("if(" + local + " || " + local + " === 0) { " + this.appendToBuffer(local) + " }");
  1007. if (this.environment.isSimple) {
  1008. this.source.push("else { " + this.appendToBuffer("''") + " }");
  1009. }
  1010. },
  1011. appendEscaped: function() {
  1012. var opcode = this.nextOpcode(1), extra = "";
  1013. this.context.aliases.escapeExpression = 'this.escapeExpression';
  1014. if(opcode[0] === 'appendContent') {
  1015. extra = " + " + this.quotedString(opcode[1][0]);
  1016. this.eat(opcode);
  1017. }
  1018. this.source.push(this.appendToBuffer("escapeExpression(" + this.popStack() + ")" + extra));
  1019. },
  1020. getContext: function(depth) {
  1021. if(this.lastContext !== depth) {
  1022. this.lastContext = depth;
  1023. }
  1024. },
  1025. lookupWithHelpers: function(name, isScoped) {
  1026. if(name) {
  1027. var topStack = this.nextStack();
  1028. this.usingKnownHelper = false;
  1029. var toPush;
  1030. if (!isScoped && this.options.knownHelpers[name]) {
  1031. toPush = topStack + " = " + this.nameLookup('helpers', name, 'helper');
  1032. this.usingKnownHelper = true;
  1033. } else if (isScoped || this.options.knownHelpersOnly) {
  1034. toPush = topStack + " = " + this.nameLookup('depth' + this.lastContext, name, 'context');
  1035. } else {
  1036. this.register('foundHelper', this.nameLookup('helpers', name, 'helper'));
  1037. toPush = topStack + " = foundHelper || " + this.nameLookup('depth' + this.lastContext, name, 'context');
  1038. }
  1039. toPush += ';';
  1040. this.source.push(toPush);
  1041. } else {
  1042. this.pushStack('depth' + this.lastContext);
  1043. }
  1044. },
  1045. lookup: function(name) {
  1046. var topStack = this.topStack();
  1047. this.source.push(topStack + " = (" + topStack + " === null || " + topStack + " === undefined || " + topStack + " === false ? " +
  1048. topStack + " : " + this.nameLookup(topStack, name, 'context') + ");");
  1049. },
  1050. pushStringParam: function(string) {
  1051. this.pushStack('depth' + this.lastContext);
  1052. this.pushString(string);
  1053. },
  1054. pushString: function(string) {
  1055. this.pushStack(this.quotedString(string));
  1056. },
  1057. push: function(name) {
  1058. this.pushStack(name);
  1059. },
  1060. invokeMustache: function(paramSize, original, hasHash) {
  1061. this.populateParams(paramSize, this.quotedString(original), "{}", null, hasHash, function(nextStack, helperMissingString, id) {
  1062. if (!this.usingKnownHelper) {
  1063. this.context.aliases.helperMissing = 'helpers.helperMissing';
  1064. this.context.aliases.undef = 'void 0';
  1065. this.source.push("else if(" + id + "=== undef) { " + nextStack + " = helperMissing.call(" + helperMissingString + "); }");
  1066. if (nextStack !== id) {
  1067. this.source.push("else { " + nextStack + " = " + id + "; }");
  1068. }
  1069. }
  1070. });
  1071. },
  1072. invokeProgram: function(guid, paramSize, hasHash) {
  1073. var inverse = this.programExpression(this.inverse);
  1074. var mainProgram = this.programExpression(guid);
  1075. this.populateParams(paramSize, null, mainProgram, inverse, hasHash, function(nextStack, helperMissingString, id) {
  1076. if (!this.usingKnownHelper) {
  1077. this.context.aliases.blockHelperMissing = 'helpers.blockHelperMissing';
  1078. this.source.push("else { " + nextStack + " = blockHelperMissing.call(" + helperMissingString + "); }");
  1079. }
  1080. });
  1081. },
  1082. populateParams: function(paramSize, helperId, program, inverse, hasHash, fn) {
  1083. var needsRegister = hasHash || this.options.stringParams || inverse || this.options.data;
  1084. var id = this.popStack(), nextStack;
  1085. var params = [], param, stringParam, stringOptions;
  1086. if (needsRegister) {
  1087. this.register('tmp1', program);
  1088. stringOptions = 'tmp1';
  1089. } else {
  1090. stringOptions = '{ hash: {} }';
  1091. }
  1092. if (needsRegister) {
  1093. var hash = (hasHash ? this.popStack() : '{}');
  1094. this.source.push('tmp1.hash = ' + hash + ';');
  1095. }
  1096. if(this.options.stringParams) {
  1097. this.source.push('tmp1.contexts = [];');
  1098. }
  1099. for(var i=0; i<paramSize; i++) {
  1100. param = this.popStack();
  1101. params.push(param);
  1102. if(this.options.stringParams) {
  1103. this.source.push('tmp1.contexts.push(' + this.popStack() + ');');
  1104. }
  1105. }
  1106. if(inverse) {
  1107. this.source.push('tmp1.fn = tmp1;');
  1108. this.source.push('tmp1.inverse = ' + inverse + ';');
  1109. }
  1110. if(this.options.data) {
  1111. this.source.push('tmp1.data = data;');
  1112. }
  1113. params.push(stringOptions);
  1114. this.populateCall(params, id, helperId || id, fn, program !== '{}');
  1115. },
  1116. populateCall: function(params, id, helperId, fn, program) {
  1117. var paramString = ["depth0"].concat(params).join(", ");
  1118. var helperMissingString = ["depth0"].concat(helperId).concat(params).join(", ");
  1119. var nextStack = this.nextStack();
  1120. if (this.usingKnownHelper) {
  1121. this.source.push(nextStack + " = " + id + ".call(" + paramString + ");");
  1122. } else {
  1123. this.context.aliases.functionType = '"function"';
  1124. var condition = program ? "foundHelper && " : ""
  1125. this.source.push("if(" + condition + "typeof " + id + " === functionType) { " + nextStack + " = " + id + ".call(" + paramString + "); }");
  1126. }
  1127. fn.call(this, nextStack, helperMissingString, id);
  1128. this.usingKnownHelper = false;
  1129. },
  1130. invokePartial: function(context) {
  1131. params = [this.nameLookup('partials', context, 'partial'), "'" + context + "'", this.popStack(), "helpers", "partials"];
  1132. if (this.options.data) {
  1133. params.push("data");
  1134. }
  1135. this.pushStack("self.invokePartial(" + params.join(", ") + ");");
  1136. },
  1137. assignToHash: function(key) {
  1138. var value = this.popStack();
  1139. var hash = this.topStack();
  1140. this.source.push(hash + "['" + key + "'] = " + value + ";");
  1141. },
  1142. // HELPERS
  1143. compiler: JavaScriptCompiler,
  1144. compileChildren: function(environment, options) {
  1145. var children = environment.children, child, compiler;
  1146. for(var i=0, l=children.length; i<l; i++) {
  1147. child = children[i];
  1148. compiler = new this.compiler();
  1149. this.context.programs.push(''); // Placeholder to prevent name conflicts for nested children
  1150. var index = this.context.programs.length;
  1151. child.index = index;
  1152. child.name = 'program' + index;
  1153. this.context.programs[index] = compiler.compile(child, options, this.context);
  1154. }
  1155. },
  1156. programExpression: function(guid) {
  1157. if(guid == null) { return "self.noop"; }
  1158. var child = this.environment.children[guid],
  1159. depths = child.depths.list;
  1160. var programParams = [child.index, child.name, "data"];
  1161. for(var i=0, l = depths.length; i<l; i++) {
  1162. depth = depths[i];
  1163. if(depth === 1) { programParams.push("depth0"); }
  1164. else { programParams.push("depth" + (depth - 1)); }
  1165. }
  1166. if(depths.length === 0) {
  1167. return "self.program(" + programParams.join(", ") + ")";
  1168. } else {
  1169. programParams.shift();
  1170. return "self.programWithDepth(" + programParams.join(", ") + ")";
  1171. }
  1172. },
  1173. register: function(name, val) {
  1174. this.useRegister(name);
  1175. this.source.push(name + " = " + val + ";");
  1176. },
  1177. useRegister: function(name) {
  1178. if(!this.context.registers[name]) {
  1179. this.context.registers[name] = true;
  1180. this.context.registers.list.push(name);
  1181. }
  1182. },
  1183. pushStack: function(item) {
  1184. this.source.push(this.nextStack() + " = " + item + ";");
  1185. return "stack" + this.stackSlot;
  1186. },
  1187. nextStack: function() {
  1188. this.stackSlot++;
  1189. if(this.stackSlot > this.stackVars.length) { this.stackVars.push("stack" + this.stackSlot); }
  1190. return "stack" + this.stackSlot;
  1191. },
  1192. popStack: function() {
  1193. return "stack" + this.stackSlot--;
  1194. },
  1195. topStack: function() {
  1196. return "stack" + this.stackSlot;
  1197. },
  1198. quotedString: function(str) {
  1199. return '"' + str
  1200. .replace(/\\/g, '\\\\')
  1201. .replace(/"/g, '\\"')
  1202. .replace(/\n/g, '\\n')
  1203. .replace(/\r/g, '\\r') + '"';
  1204. }
  1205. };
  1206. var reservedWords = (
  1207. "break else new var" +
  1208. " case finally return void" +
  1209. " catch for switch while" +
  1210. " continue function this with" +
  1211. " default if throw" +
  1212. " delete in try" +
  1213. " do instanceof typeof" +
  1214. " abstract enum int short" +
  1215. " boolean export interface static" +
  1216. " byte extends long super" +
  1217. " char final native synchronized" +
  1218. " class float package throws" +
  1219. " const goto private transient" +
  1220. " debugger implements protected volatile" +
  1221. " double import public let yield"
  1222. ).split(" ");
  1223. var compilerWords = JavaScriptCompiler.RESERVED_WORDS = {};
  1224. for(var i=0, l=reservedWords.length; i<l; i++) {
  1225. compilerWords[reservedWords[i]] = true;
  1226. }
  1227. JavaScriptCompiler.isValidJavaScriptVariableName = function(name) {
  1228. if(!JavaScriptCompiler.RESERVED_WORDS[name] && /^[a-zA-Z_$][0-9a-zA-Z_$]+$/.test(name)) {
  1229. return true;
  1230. }
  1231. return false;
  1232. }
  1233. })(Handlebars.Compiler, Handlebars.JavaScriptCompiler);
  1234. Handlebars.precompile = function(string, options) {
  1235. options = options || {};
  1236. var ast = Handlebars.parse(string);
  1237. var environment = new Handlebars.Compiler().compile(ast, options);
  1238. return new Handlebars.JavaScriptCompiler().compile(environment, options);
  1239. };
  1240. Handlebars.compile = function(string, options) {
  1241. options = options || {};
  1242. var compiled;
  1243. function compile() {
  1244. var ast = Handlebars.parse(string);
  1245. var environment = new Handlebars.Compiler().compile(ast, options);
  1246. var templateSpec = new Handlebars.JavaScriptCompiler().compile(environment, options, undefined, true);
  1247. return Handlebars.template(templateSpec);
  1248. }
  1249. // Template is only compiled on first use and cached after that point.
  1250. return function(context, options) {
  1251. if (!compiled) {
  1252. compiled = compile();
  1253. }
  1254. return compiled.call(this, context, options);
  1255. };
  1256. };
  1257. ;
  1258. // lib/handlebars/runtime.js
  1259. Handlebars.VM = {
  1260. template: function(templateSpec) {
  1261. // Just add water
  1262. var container = {
  1263. escapeExpression: Handlebars.Utils.escapeExpression,
  1264. invokePartial: Handlebars.VM.invokePartial,
  1265. programs: [],
  1266. program: function(i, fn, data) {
  1267. var programWrapper = this.programs[i];
  1268. if(data) {
  1269. return Handlebars.VM.program(fn, data);
  1270. } else if(programWrapper) {
  1271. return programWrapper;
  1272. } else {
  1273. programWrapper = this.programs[i] = Handlebars.VM.program(fn);
  1274. return programWrapper;
  1275. }
  1276. },
  1277. programWithDepth: Handlebars.VM.programWithDepth,
  1278. noop: Handlebars.VM.noop
  1279. };
  1280. return function(context, options) {
  1281. options = options || {};
  1282. return templateSpec.call(container, Handlebars, context, options.helpers, options.partials, options.data);
  1283. };
  1284. },
  1285. programWithDepth: function(fn, data, $depth) {
  1286. var args = Array.prototype.slice.call(arguments, 2);
  1287. return function(context, options) {
  1288. options = options || {};
  1289. return fn.apply(this, [context, options.data || data].concat(args));
  1290. };
  1291. },
  1292. program: function(fn, data) {
  1293. return function(context, options) {
  1294. options = options || {};
  1295. return fn(context, options.data || data);
  1296. };
  1297. },
  1298. noop: function() { return ""; },
  1299. invokePartial: function(partial, name, context, helpers, partials, data) {
  1300. options = { helpers: helpers, partials: partials, data: data };
  1301. if(partial === undefined) {
  1302. throw new Handlebars.Exception("The partial " + name + " could not be found");
  1303. } else if(partial instanceof Function) {
  1304. return partial(context, options);
  1305. } else if (!Handlebars.compile) {
  1306. throw new Handlebars.Exception("The partial " + name + " could not be compiled when running in runtime-only mode");
  1307. } else {
  1308. partials[name] = Handlebars.compile(partial);
  1309. return partials[name](context, options);
  1310. }
  1311. }
  1312. };
  1313. Handlebars.template = Handlebars.VM.template;
  1314. ;