juicer-min.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. (function() {
  2. var c = function() {
  3. var e = [].slice.call(arguments);
  4. e.push(c.options);
  5. if (e[0].match(/^\s*#([\w:\-\.]+)\s*$/igm)) {
  6. e[0].replace(/^\s*#([\w:\-\.]+)\s*$/igm, function(h, i) {
  7. var f = document;
  8. var g = f && f.getElementById(i);
  9. e[0] = g ? (g.value || g.innerHTML) : h;
  10. });
  11. }
  12. if (arguments.length == 1) {
  13. return c.compile.apply(c, e);
  14. }
  15. if (arguments.length >= 2) {
  16. return c.to_html.apply(c, e);
  17. }
  18. };
  19. var d = {
  20. escapehash: {
  21. "<": "&lt;",
  22. ">": "&gt;",
  23. "&": "&amp;",
  24. '"': "&quot;",
  25. "'": "&#x27;",
  26. "/": "&#x2f;"
  27. },
  28. escapereplace: function(e) {
  29. return d.escapehash[e];
  30. },
  31. escaping: function(e) {
  32. return typeof(e) !== "string" ? e : e.replace(/[&<>"]/igm, this.escapereplace);
  33. },
  34. detection: function(e) {
  35. return typeof(e) === "undefined" ? "" : e;
  36. }
  37. };
  38. var b = function(e) {
  39. if (typeof(console) !== "undefined") {
  40. if (console.warn) {
  41. console.warn(e);
  42. return;
  43. }
  44. if (console.log) {
  45. console.log(e);
  46. return;
  47. }
  48. }
  49. throw (e);
  50. };
  51. var a = function(h, f) {
  52. h = h !== Object(h) ? {} : h;
  53. if (h.__proto__) {
  54. h.__proto__ = f;
  55. return h;
  56. }
  57. var g = function() {};
  58. var j = Object.create ? Object.create(f) : new(g.prototype = f, g);
  59. for (var e in h) {
  60. if (h.hasOwnProperty(e)) {
  61. j[e] = h[e];
  62. }
  63. }
  64. return j;
  65. };
  66. c.__cache = {};
  67. c.version = "0.6.5-stable";
  68. c.settings = {};
  69. c.tags = {
  70. operationOpen: "{@",
  71. operationClose: "}",
  72. interpolateOpen: "\\${",
  73. interpolateClose: "}",
  74. noneencodeOpen: "\\$\\${",
  75. noneencodeClose: "}",
  76. commentOpen: "\\{#",
  77. commentClose: "\\}"
  78. };
  79. c.options = {
  80. cache: true,
  81. strip: true,
  82. errorhandling: true,
  83. detection: true,
  84. _method: a({
  85. __escapehtml: d,
  86. __throw: b,
  87. __juicer: c
  88. }, {})
  89. };
  90. c.tagInit = function() {
  91. var f = c.tags.operationOpen + "each\\s*([^}]*?)\\s*as\\s*(\\w*?)\\s*(,\\s*\\w*?)?" + c.tags.operationClose;
  92. var h = c.tags.operationOpen + "\\/each" + c.tags.operationClose;
  93. var i = c.tags.operationOpen + "if\\s*([^}]*?)" + c.tags.operationClose;
  94. var j = c.tags.operationOpen + "\\/if" + c.tags.operationClose;
  95. var n = c.tags.operationOpen + "else" + c.tags.operationClose;
  96. var o = c.tags.operationOpen + "else if\\s*([^}]*?)" + c.tags.operationClose;
  97. var k = c.tags.interpolateOpen + "([\\s\\S]+?)" + c.tags.interpolateClose;
  98. var l = c.tags.noneencodeOpen + "([\\s\\S]+?)" + c.tags.noneencodeClose;
  99. var m = c.tags.commentOpen + "[^}]*?" + c.tags.commentClose;
  100. var g = c.tags.operationOpen + "each\\s*(\\w*?)\\s*in\\s*range\\(([^}]+?)\\s*,\\s*([^}]+?)\\)" + c.tags.operationClose;
  101. var e = c.tags.operationOpen + "include\\s*([^}]*?)\\s*,\\s*([^}]*?)" + c.tags.operationClose;
  102. c.settings.forstart = new RegExp(f, "igm");
  103. c.settings.forend = new RegExp(h, "igm");
  104. c.settings.ifstart = new RegExp(i, "igm");
  105. c.settings.ifend = new RegExp(j, "igm");
  106. c.settings.elsestart = new RegExp(n, "igm");
  107. c.settings.elseifstart = new RegExp(o, "igm");
  108. c.settings.interpolate = new RegExp(k, "igm");
  109. c.settings.noneencode = new RegExp(l, "igm");
  110. c.settings.inlinecomment = new RegExp(m, "igm");
  111. c.settings.rangestart = new RegExp(g, "igm");
  112. c.settings.include = new RegExp(e, "igm");
  113. };
  114. c.tagInit();
  115. c.set = function(f, j) {
  116. var h = this;
  117. var e = function(i) {
  118. return i.replace(/[\$\(\)\[\]\+\^\{\}\?\*\|\.]/igm, function(l) {
  119. return "\\" + l;
  120. });
  121. };
  122. var k = function(l, m) {
  123. var i = l.match(/^tag::(.*)$/i);
  124. if (i) {
  125. h.tags[i[1]] = e(m);
  126. h.tagInit();
  127. return;
  128. }
  129. h.options[l] = m;
  130. };
  131. if (arguments.length === 2) {
  132. k(f, j);
  133. return;
  134. }
  135. if (f === Object(f)) {
  136. for (var g in f) {
  137. if (f.hasOwnProperty(g)) {
  138. k(g, f[g]);
  139. }
  140. }
  141. }
  142. };
  143. c.register = function(g, f) {
  144. var e = this.options._method;
  145. if (e.hasOwnProperty(g)) {
  146. return false;
  147. }
  148. return e[g] = f;
  149. };
  150. c.unregister = function(f) {
  151. var e = this.options._method;
  152. if (e.hasOwnProperty(f)) {
  153. return delete e[f];
  154. }
  155. };
  156. c.template = function(e) {
  157. var f = this;
  158. this.options = e;
  159. this.__interpolate = function(g, l, i) {
  160. var h = g.split("|"),
  161. k = h[0] || "",
  162. j;
  163. if (h.length > 1) {
  164. g = h.shift();
  165. j = h.shift().split(",");
  166. k = "_method." + j.shift() + ".call({}, " + [g].concat(j) + ")";
  167. }
  168. return "<%= " + (l ? "_method.__escapehtml.escaping" : "") + "(" + (!i || i.detection !== false ? "_method.__escapehtml.detection" : "") + "(" + k + ")) %>";
  169. };
  170. this.__removeShell = function(h, g) {
  171. var i = 0;
  172. h = h.replace(c.settings.forstart, function(n, k, m, l) {
  173. var m = m || "value",
  174. l = l && l.substr(1);
  175. var j = "i" + i++;
  176. return "<% ~function() {for(var " + j + " in " + k + ") {if(" + k + ".hasOwnProperty(" + j + ")) {var " + m + "=" + k + "[" + j + "];" + (l ? ("var " + l + "=" + j + ";") : "") + " %>";
  177. }).replace(c.settings.forend, "<% }}}(); %>").replace(c.settings.ifstart, function(j, k) {
  178. return "<% if(" + k + ") { %>";
  179. }).replace(c.settings.ifend, "<% } %>").replace(c.settings.elsestart, function(j) {
  180. return "<% } else { %>";
  181. }).replace(c.settings.elseifstart, function(j, k) {
  182. return "<% } else if(" + k + ") { %>";
  183. }).replace(c.settings.noneencode, function(k, j) {
  184. return f.__interpolate(j, false, g);
  185. }).replace(c.settings.interpolate, function(k, j) {
  186. return f.__interpolate(j, true, g);
  187. }).replace(c.settings.inlinecomment, "").replace(c.settings.rangestart, function(m, l, n, k) {
  188. var j = "j" + i++;
  189. return "<% ~function() {for(var " + j + "=" + n + ";" + j + "<" + k + ";" + j + "++) {{var " + l + "=" + j + "; %>";
  190. }).replace(c.settings.include, function(l, j, k) {
  191. return "<%= _method.__juicer(" + j + ", " + k + "); %>";
  192. });
  193. if (!g || g.errorhandling !== false) {
  194. h = "<% try { %>" + h;
  195. h += '<% } catch(e) {_method.__throw("Juicer Render Exception: "+e.message);} %>';
  196. }
  197. return h;
  198. };
  199. this.__toNative = function(h, g) {
  200. return this.__convert(h, !g || g.strip);
  201. };
  202. this.__lexicalAnalyze = function(k) {
  203. var j = [];
  204. var o = [];
  205. var n = "";
  206. var g = ["if", "each", "_", "_method", "console", "break", "case", "catch", "continue", "debugger", "default", "delete", "do", "finally", "for", "function", "in", "instanceof", "new", "return", "switch", "this", "throw", "try", "typeof", "var", "void", "while", "with", "null", "typeof", "class", "enum", "export", "extends", "import", "super", "implements", "interface", "let", "package", "private", "protected", "public", "static", "yield", "const", "arguments", "true", "false", "undefined", "NaN"];
  207. var m = function(r, q) {
  208. if (Array.prototype.indexOf && r.indexOf === Array.prototype.indexOf) {
  209. return r.indexOf(q);
  210. }
  211. for (var p = 0; p < r.length; p++) {
  212. if (r[p] === q) {
  213. return p;
  214. }
  215. }
  216. return -1;
  217. };
  218. var h = function(p, i) {
  219. i = i.match(/\w+/igm)[0];
  220. if (m(j, i) === -1 && m(g, i) === -1 && m(o, i) === -1) {
  221. if (typeof(window) !== "undefined" && typeof(window[i]) === "function" && window[i].toString().match(/^\s*?function \w+\(\) \{\s*?\[native code\]\s*?\}\s*?$/i)) {
  222. return p;
  223. }
  224. if (typeof(global) !== "undefined" && typeof(global[i]) === "function" && global[i].toString().match(/^\s*?function \w+\(\) \{\s*?\[native code\]\s*?\}\s*?$/i)) {
  225. return p;
  226. }
  227. if (typeof(c.options._method[i]) === "function" || c.options._method.hasOwnProperty(i)) {
  228. o.push(i);
  229. return p;
  230. }
  231. j.push(i);
  232. }
  233. return p;
  234. };
  235. k.replace(c.settings.forstart, h).replace(c.settings.interpolate, h).replace(c.settings.ifstart, h).replace(c.settings.elseifstart, h).replace(c.settings.include, h).replace(/[\+\-\*\/%!\?\|\^&~<>=,\(\)\[\]]\s*([A-Za-z_]+)/igm, h);
  236. for (var l = 0; l < j.length; l++) {
  237. n += "var " + j[l] + "=_." + j[l] + ";";
  238. }
  239. for (var l = 0; l < o.length; l++) {
  240. n += "var " + o[l] + "=_method." + o[l] + ";";
  241. }
  242. return "<% " + n + " %>";
  243. };
  244. this.__convert = function(h, i) {
  245. var g = [].join("");
  246. g += "'use strict';";
  247. g += "var _=_||{};";
  248. g += "var _out='';_out+='";
  249. if (i !== false) {
  250. g += h.replace(/\\/g, "\\\\").replace(/[\r\t\n]/g, " ").replace(/'(?=[^%]*%>)/g, "\t").split("'").join("\\'").split("\t").join("'").replace(/<%=(.+?)%>/g, "';_out+=$1;_out+='").split("<%").join("';").split("%>").join("_out+='") + "';return _out;";
  251. return g;
  252. }
  253. g += h.replace(/\\/g, "\\\\").replace(/[\r]/g, "\\r").replace(/[\t]/g, "\\t").replace(/[\n]/g, "\\n").replace(/'(?=[^%]*%>)/g, "\t").split("'").join("\\'").split("\t").join("'").replace(/<%=(.+?)%>/g, "';_out+=$1;_out+='").split("<%").join("';").split("%>").join("_out+='") + "';return _out.replace(/[\\r\\n]\\s+[\\r\\n]/g, '\\r\\n');";
  254. return g;
  255. };
  256. this.parse = function(h, g) {
  257. var i = this;
  258. if (!g || g.loose !== false) {
  259. h = this.__lexicalAnalyze(h) + h;
  260. }
  261. h = this.__removeShell(h, g);
  262. h = this.__toNative(h, g);
  263. this._render = new Function("_, _method", h);
  264. this.render = function(k, j) {
  265. if (!j || j !== f.options._method) {
  266. j = a(j, f.options._method);
  267. }
  268. return i._render.call(this, k, j);
  269. };
  270. return this;
  271. };
  272. };
  273. c.compile = function(g, f) {
  274. if (!f || f !== this.options) {
  275. f = a(f, this.options);
  276. }
  277. try {
  278. var h = this.__cache[g] ? this.__cache[g] : new this.template(this.options).parse(g, f);
  279. if (!f || f.cache !== false) {
  280. this.__cache[g] = h;
  281. }
  282. return h;
  283. } catch (i) {
  284. b("Juicer Compile Exception: " + i.message);
  285. return {
  286. render: function() {}
  287. };
  288. }
  289. };
  290. c.to_html = function(f, g, e) {
  291. if (!e || e !== this.options) {
  292. e = a(e, this.options);
  293. }
  294. return this.compile(f, e).render(g, e._method);
  295. };
  296. typeof(module) !== "undefined" && module.exports ? module.exports = c : this.juicer = c;
  297. })();