juicer.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. ********** Juicer **********
  3. ${A Fast template engine}
  4. Project Home: http://juicer.name
  5. Author: Guokai
  6. Gtalk: badkaikai@gmail.com
  7. Blog: http://benben.cc
  8. Licence: MIT License
  9. Version: 0.4.0-dev
  10. */
  11. (function() {
  12. var juicer = function() {
  13. var args = [].slice.call(arguments);
  14. args.push(juicer.options);
  15. if(arguments.length == 1) {
  16. return juicer.compile.apply(juicer, args);
  17. }
  18. if(arguments.length >= 2) {
  19. return juicer.to_html.apply(juicer, args);
  20. }
  21. };
  22. var __escapehtml = {
  23. escapehash: {
  24. '<': '&lt;',
  25. '>': '&gt;',
  26. '&': '&amp;',
  27. '"': '&quot;',
  28. "'": '&#x27;',
  29. '/': '&#x2f;'
  30. },
  31. escapereplace: function(k) {
  32. return __escapehtml.escapehash[k];
  33. },
  34. escaping: function(str) {
  35. return typeof(str) !== 'string' ? str : str.replace(/[&<>"]/igm, this.escapereplace);
  36. },
  37. detection: function(data) {
  38. return typeof(data) === 'undefined' ? '' : data;
  39. }
  40. };
  41. var __throw = function(error) {
  42. if(console) {
  43. if(console.warn) {
  44. console.warn(error);
  45. return;
  46. }
  47. if(console.log) {
  48. console.log(error);
  49. return;
  50. }
  51. }
  52. throw(error);
  53. };
  54. var __creator = function(o, proto) {
  55. o = o !== Object(o) ? {} : o;
  56. if(o.__proto__) {
  57. o.__proto__ = proto;
  58. return o;
  59. }
  60. var _Empty = function() {};
  61. var n = new((_Empty).prototype = proto, _Empty);
  62. for(var i in o) {
  63. if(o.hasOwnProperty(i)) {
  64. n[i] = o[i];
  65. }
  66. }
  67. return n;
  68. };
  69. juicer.__cache = {};
  70. juicer.version = '0.4.0-dev';
  71. juicer.settings = {
  72. forstart: /{@each\s*([\w\.]*?)\s*as\s*(\w*?)\s*(,\s*\w*?)?}/igm,
  73. forend: /{@\/each}/igm,
  74. ifstart: /{@if\s*([^}]*?)}/igm,
  75. ifend: /{@\/if}/igm,
  76. elsestart: /{@else}/igm,
  77. elseifstart: /{@else if\s*([^}]*?)}/igm,
  78. interpolate: /\${([\s\S]+?)}/igm,
  79. noneencode: /\$\${([\s\S]+?)}/igm,
  80. inlinecomment: /{#[^}]*?}/igm,
  81. rangestart: /{@each\s*(\w*?)\s*in\s*range\((\d+?),(\d+?)\)}/igm
  82. };
  83. juicer.options = {
  84. cache: true,
  85. strip: true,
  86. errorhandling: true,
  87. detection: true,
  88. _method: __creator({
  89. __escapehtml: __escapehtml,
  90. __throw: __throw
  91. }, this)
  92. };
  93. juicer.set = function(conf, value) {
  94. if(arguments.length === 2) {
  95. this.options[conf] = value;
  96. return;
  97. }
  98. if(conf === Object(conf)) {
  99. for(var i in conf) {
  100. if(conf.hasOwnProperty(i)) {
  101. this.options[i] = conf[i];
  102. }
  103. }
  104. }
  105. };
  106. juicer.register = function(fname, fn) {
  107. var _method = this.options._method;
  108. if(_method.hasOwnProperty(fname)) {
  109. return false;
  110. }
  111. return _method[fname] = fn;
  112. };
  113. juicer.unregister = function(fname) {
  114. var _method = this.options._method;
  115. if(_method.hasOwnProperty(fname)) {
  116. return delete _method[fname];
  117. }
  118. };
  119. juicer.template = function(options) {
  120. var that = this;
  121. this.options = options;
  122. this.__interpolate = function(_name, _escape, options) {
  123. var _define = _name.split('|'), _fn = '';
  124. if(_define.length > 1) {
  125. _name = _define.shift();
  126. _fn = '_method.' + _define.shift();
  127. }
  128. return '<%= ' + (_escape ? '_method.__escapehtml.escaping' : '') + '(' +
  129. (!options || options.detection !== false ? '_method.__escapehtml.detection' : '') + '(' +
  130. _fn + '(' +
  131. _name +
  132. ')' +
  133. ')' +
  134. ')' +
  135. ' %>';
  136. };
  137. this.__removeShell = function(tpl, options) {
  138. var _counter = 0;
  139. tpl = tpl
  140. //for expression
  141. .replace(juicer.settings.forstart, function($, _name, alias, key) {
  142. var alias = alias || 'value', key = key && key.substr(1);
  143. var _iterate = 'i' + _counter++;
  144. return '<% for(var ' + _iterate + '=0, l' + _iterate + '=' + _name + '.length;' + _iterate + '<l' + _iterate + ';' + _iterate + '++) {' +
  145. 'var ' + alias + '=' + _name + '[' + _iterate + '];' +
  146. (key ? ('var ' + key + '=' + _iterate + ';') : '') +
  147. ' %>';
  148. })
  149. .replace(juicer.settings.forend, '<% } %>')
  150. //if expression
  151. .replace(juicer.settings.ifstart, function($, condition) {
  152. return '<% if(' + condition + ') { %>';
  153. })
  154. .replace(juicer.settings.ifend, '<% } %>')
  155. //else expression
  156. .replace(juicer.settings.elsestart, function($) {
  157. return '<% } else { %>';
  158. })
  159. //else if expression
  160. .replace(juicer.settings.elseifstart, function($, condition) {
  161. return '<% } else if(' + condition + ') { %>';
  162. })
  163. //interpolate without escape
  164. .replace(juicer.settings.noneencode, function($, _name) {
  165. return that.__interpolate(_name, false, options);
  166. })
  167. //interpolate with escape
  168. .replace(juicer.settings.interpolate, function($, _name) {
  169. return that.__interpolate(_name, true, options);
  170. })
  171. //clean up comments
  172. .replace(juicer.settings.inlinecomment, '')
  173. //range expression
  174. .replace(juicer.settings.rangestart, function($, _name, start, end) {
  175. var _iterate = 'j' + _counter++;
  176. return '<% for(var ' + _iterate + '=0;' + _iterate + '<' + (end - start) + ';' + _iterate + '++) {' +
  177. 'var ' + _name + '=' + _iterate + ';' +
  178. ' %>';
  179. });
  180. //exception handling
  181. if(!options || options.errorhandling !== false) {
  182. tpl = '<% try { %>' + tpl;
  183. tpl += '<% } catch(e) {_method.__throw("Juicer Render Exception: "+e.message);} %>';
  184. }
  185. return tpl;
  186. };
  187. this.__toNative = function(tpl, options) {
  188. return this.__convert(tpl, !options || options.strip);
  189. };
  190. this.__lexicalAnalyze = function(tpl) {
  191. var buffer = [];
  192. var prefix = '';
  193. var indexOf = function(array, item) {
  194. if (Array.prototype.indexOf && array.indexOf === Array.prototype.indexOf) {
  195. return array.indexOf(item);
  196. }
  197. for(var i=0; i < array.length; i++) {
  198. if(array[i] === item) return i;
  199. }
  200. return -1;
  201. };
  202. var variableAnalyze = function($, statement) {
  203. statement = statement.match(/\w+/igm)[0];
  204. if(indexOf(buffer, statement) === -1) {
  205. buffer.push(statement); //fuck ie
  206. }
  207. };
  208. tpl.replace(juicer.settings.forstart, variableAnalyze).
  209. replace(juicer.settings.interpolate, variableAnalyze).
  210. replace(juicer.settings.ifstart, variableAnalyze);
  211. for(var i = 0;i < buffer.length; i++) {
  212. prefix += 'var ' + buffer[i] + '=_.' + buffer[i] + ';';
  213. }
  214. return '<% ' + prefix + ' %>';
  215. };
  216. this.__convert=function(tpl, strip) {
  217. var buffer = [].join('');
  218. buffer += "'use strict';"; //use strict mode
  219. buffer += "var _=_||{};";
  220. buffer += "var _out='';_out+='";
  221. if(strip !== false) {
  222. buffer += tpl
  223. .replace(/\\/g, "\\\\")
  224. .replace(/[\r\t\n]/g, " ")
  225. .replace(/'(?=[^%]*%>)/g, "\t")
  226. .split("'").join("\\'")
  227. .split("\t").join("'")
  228. .replace(/<%=(.+?)%>/g, "';_out+=$1;_out+='")
  229. .split("<%").join("';")
  230. .split("%>").join("_out+='")+
  231. "';return _out;";
  232. return buffer;
  233. }
  234. buffer += tpl
  235. .replace(/\\/g, "\\\\")
  236. .replace(/[\r]/g, "\\r")
  237. .replace(/[\t]/g, "\\t")
  238. .replace(/[\n]/g, "\\n")
  239. .replace(/'(?=[^%]*%>)/g, "\t")
  240. .split("'").join("\\'")
  241. .split("\t").join("'")
  242. .replace(/<%=(.+?)%>/g, "';_out+=$1;_out+='")
  243. .split("<%").join("';")
  244. .split("%>").join("_out+='")+
  245. "';return _out.replace(/[\\r\\n]\\s+[\\r\\n]/g, '\\r\\n');";
  246. return buffer;
  247. };
  248. this.parse = function(tpl, options) {
  249. var _that = this;
  250. if(!options || options.loose !== false) {
  251. tpl = this.__lexicalAnalyze(tpl) + tpl;
  252. }
  253. tpl = this.__removeShell(tpl, options);
  254. tpl = this.__toNative(tpl, options);
  255. this._render = new Function('_, _method', tpl);
  256. this.render = function(_, _method) {
  257. if(!_method || _method !== that.options._method) {
  258. _method = __creator(_method, that.options._method);
  259. }
  260. return _that._render.call(this, _, _method);
  261. };
  262. return this;
  263. };
  264. };
  265. juicer.compile = function(tpl, options) {
  266. if(!options || options !== this.options) {
  267. options = __creator(options, this.options);
  268. }
  269. try {
  270. var engine = this.__cache[tpl] ?
  271. this.__cache[tpl] :
  272. new this.template(this.options).parse(tpl, options);
  273. if(!options || options.cache !== false) {
  274. this.__cache[tpl] = engine;
  275. }
  276. return engine;
  277. } catch(e) {
  278. __throw('Juicer Compile Exception: ' + e.message);
  279. return {
  280. render: function() {} //noop
  281. };
  282. }
  283. };
  284. juicer.to_html = function(tpl, data, options) {
  285. if(!options || options !== this.options) {
  286. options = __creator(options, this.options);
  287. }
  288. return this.compile(tpl, options).render(data, options._method);
  289. };
  290. typeof(module) !== 'undefined' && module.exports ? module.exports = juicer : this.juicer = juicer;
  291. })();