From 8e5f8de8c926105d71bfcf8dc88c927925f0181e Mon Sep 17 00:00:00 2001 From: Denis Seleznev Date: Sun, 26 Apr 2015 15:26:49 +0300 Subject: [PATCH] Refactoring --- lib/compiler.js | 334 +++++++++++++++++++------------------- lib/kernel.js | 354 +++++++++++++++++++++++++++++------------ template/all.jst.js | 120 +++++++------- template/block.jst.js | 102 ++++++------ template/filter.jst.js | 10 +- 5 files changed, 518 insertions(+), 402 deletions(-) diff --git a/lib/compiler.js b/lib/compiler.js index 7d3393f..8a9bee1 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -4,17 +4,13 @@ require('./kernel'); var fs = require('fs'), vm = require('vm'), log = require('./logger'); - + var Compiler = { - defaultNamespace: 'jst._tmpl', - _tab: ' ', - // Построение шаблонов build: function(text, fileName, prefs) { var res = []; - + this._sameTemplateName = {}; - - // Для размещения шаблонов из нескольких файлов в один общий файл + if(Array.isArray(text)) { text.forEach(function(el) { this._fileName = el[1]; @@ -25,45 +21,36 @@ var Compiler = { text += this.fileMark(); res.push(this._build(text)); } - + res = res.join(''); - - // Есть шаблоны в виде функций, чтобы не помещать в каждую функцию служебные переменные, - // вынесим их в одно место через замыкание + if(res.search(/function/) !== -1) { res = '\n(function() {' + ['', - 'var attr = jst.attr;', - 'var block = jst.block;', - 'var each = jst.each;', - 'var filter = jst.filter;', - 'var template = jst;' + 'var attr = jst.attr,', + ' block = jst.block,', + ' each = jst.each,', + ' filter = jst.filter,', + ' template = jst;' ].join('\n ') + res + '\n\n})();'; } - + if(prefs && !prefs.withoutKernel) { res = this.includeKernel(res); } - + return res; }, - fileMark: function() { - return '\n\n/* --- ' + this._fileName + ' --- */\n'; - }, - _sameTemplateName: {}, - _sameBlockName: {}, - _extend: [], - _init: [], _build: function(text) { var inFile = this._inFile(); - + if(!this.checkOpenCloseTag(text)) { return this.templateConsole('Unequal number of open and closed tags