Comparing version 0.1.1 to 0.2.0
@@ -0,1 +1,5 @@ | ||
/** | ||
* bemBehavior | ||
* 2012, Ershov Konstantin, https://github.com/ershov-konst/bemRender | ||
*/ | ||
var bem = new function () { | ||
@@ -2,0 +6,0 @@ /*! |
/** | ||
* bemRender | ||
* Autor: Ershov Konstantin | ||
* bemRender.js | ||
* 2012, Ershov Konstantin, https://github.com/ershov-konst/bemRender | ||
*/ | ||
@@ -13,3 +13,13 @@ var | ||
/** | ||
* | ||
* @param config | ||
* @param config.express | ||
* @param config.blocksPath | ||
* @param config.publicPath | ||
* @param config.nameRule | ||
* @return {Function} | ||
*/ | ||
module.exports = function(config){ | ||
//config checking | ||
if (!config.express){ | ||
@@ -27,7 +37,149 @@ throw new Error("express variable is not defined for bemRender"); | ||
} | ||
//block files are available as static files(important for the image specified in css) | ||
config.express.use("/" + path.basename(config.blocksPath), express.static(config.blocksPath)); | ||
function prepareJS (blocks, addJS, handler){ | ||
//templates functions cache | ||
var cache = {}; | ||
var bemRender = function(jsonPath, options, fn){ | ||
var self = this; | ||
this.bemJson = ""; | ||
this.blocksHash = {}; | ||
this.addJS = []; | ||
this.addCSS = []; | ||
this.baseName = path.basename(jsonPath, '.json'); | ||
if (jsonPath in cache && typeof cache[jsonPath] == "function"){ | ||
var | ||
dotFunc = cache[jsonPath], | ||
result; | ||
try{ | ||
result = dotFunc(options); | ||
fn(null, result); | ||
} | ||
catch(e){ | ||
fn(e); | ||
} | ||
} | ||
else{ | ||
fs.readFile(jsonPath, 'utf8', function(err, str){ | ||
if (err) return fn(err); | ||
try{ | ||
self.bemJson = JSON.parse(str); | ||
} | ||
catch(e){ | ||
fn(e); | ||
} | ||
options.js = path.join(self.baseName, "all.js"); | ||
options.css = path.join(self.baseName, "all.css"); | ||
//prepare page template | ||
var pageTpl = self.prepareTemplate(self.bemJson); | ||
self.prepareCSS(function(e, css){ | ||
if (e) fn(e); | ||
self.savePackage("all.css", css, function(err){ | ||
if (err) | ||
fn(err); | ||
else | ||
self.prepareJS(function(js){ | ||
if (!js.length) fn(null, result); | ||
self.savePackage("all.js", js, function(err){ | ||
if (err) fn(err); | ||
try{ | ||
cache[jsonPath] = doT.template(pageTpl); | ||
var result = cache[jsonPath](options); | ||
fn(null, result); | ||
} | ||
catch(e){ | ||
fn(e); | ||
} | ||
}) | ||
}) | ||
}); | ||
}); | ||
}); | ||
} | ||
}; | ||
/** | ||
* Prepare page template | ||
* @param json | ||
* @param parent | ||
* @return {*} | ||
*/ | ||
bemRender.prototype.prepareTemplate = function(json, parent){ | ||
var | ||
self = this, | ||
entity = ("block" in json) ? "block" : ("element" in json) ? "element" : "", | ||
isElement = entity == "element", | ||
name = json[entity], | ||
html = "", | ||
bPath = path.join(config.blocksPath, isElement ? parent : "", name, name); | ||
for (var i in json){ | ||
if (json.hasOwnProperty(i)){ | ||
if (i == "content" && json[i] instanceof Array){ | ||
var content = []; | ||
json[i].forEach(function(str){ | ||
content.push(self.prepareTemplate(str, name)); | ||
}); | ||
json["content"] = content.join(""); | ||
} | ||
else if(i == "mods" && json[i] instanceof Array){ | ||
var mods = []; | ||
json[i].forEach(function(mod){ | ||
for (var m in mod){ | ||
if (mod.hasOwnProperty(m)){ | ||
mods.push(self.getCssName(isElement, parent, name, m, mod[m])); | ||
break; | ||
} | ||
} | ||
}); | ||
json["mods"] = mods.join(" "); | ||
} | ||
else if(i == "js" && json[i] instanceof Array){ | ||
json[i].forEach(function(js){ | ||
self.addJS.push(js); | ||
}); | ||
json[i] = path.join(self.baseName, "all.js"); | ||
} | ||
else if(i == "css" && json[i] instanceof Array){ | ||
json[i].forEach(function(css){ | ||
self.addCSS.push(css); | ||
}); | ||
json[i] = path.join(self.baseName, "all.css"); | ||
} | ||
} | ||
json["blockName"] = self.getCssName(isElement, parent, name); | ||
} | ||
if (bPath in self.blocksHash) | ||
html = self.blocksHash[bPath]["html"]; | ||
else{ | ||
try{ | ||
html = fs.readFileSync(bPath + ".html", 'utf8'); | ||
self.blocksHash[bPath] = { | ||
"html": html, | ||
"css" : self.getCssName(isElement, parent, name) | ||
}; | ||
} | ||
catch(e){ | ||
console.log(e.message); | ||
html = ""; | ||
} | ||
} | ||
return doT.template(html)(json); | ||
}; | ||
/** | ||
* prepare string of all.js | ||
* @param {function} handler | ||
*/ | ||
bemRender.prototype.prepareJS = function(handler){ | ||
var | ||
blocks = this.blocksHash, | ||
addJS = this.addJS, | ||
bemObj = {}, | ||
@@ -65,5 +217,14 @@ indexJS = []; | ||
handler(indexJS.join('\n')); | ||
} | ||
function prepareCSS (blocks, addCSS, handler){ | ||
var indexLess = []; | ||
}; | ||
/** | ||
* prepare string of all.css | ||
* @param {function} handler | ||
*/ | ||
bemRender.prototype.prepareCSS = function(handler){ | ||
var | ||
blocks = this.blocksHash, | ||
addCSS = this.addCSS, | ||
indexLess = []; | ||
addCSS.forEach(function(css){ | ||
@@ -83,4 +244,14 @@ indexLess.push('@import "' + css + '";'); | ||
less.render(indexLess.join("\n"), handler); | ||
} | ||
function getCssName(isElement, parent, name, mod, modV){ | ||
}; | ||
/** | ||
* return css name of (block || element || modifier) | ||
* @param {Boolean} isElement | ||
* @param {String} parent | ||
* @param {String} name | ||
* @param {String} mod | ||
* @param {String} modV | ||
* @return {String} | ||
*/ | ||
bemRender.prototype.getCssName = function(isElement, parent, name, mod, modV){ | ||
var css = config.nameRule; | ||
@@ -109,111 +280,24 @@ if (mod && modV){ | ||
return css; | ||
} | ||
}; | ||
return function(jsonPath, options, fn){ | ||
fs.readFile(jsonPath, 'utf8', function(err, str){ | ||
if (err) return fn(err); | ||
var | ||
bemJson = "", | ||
blocksHash = {}, | ||
addJS = [], | ||
addCSS= [], | ||
baseName = path.basename(jsonPath, '.json'); | ||
options.js = path.join(baseName, "all.js"); | ||
options.css = path.join(baseName, "all.css"); | ||
try{ | ||
bemJson = JSON.parse(str); | ||
} | ||
catch(e){ | ||
fn(e); | ||
} | ||
var pageTpl = (function prepareTemplate(json, parent){ | ||
var | ||
entity = ("block" in json) ? "block" : ("element" in json) ? "element" : "", | ||
isElement = entity == "element", | ||
name = json[entity], | ||
html = "", | ||
bPath = path.join(config.blocksPath, isElement ? parent : "", name, name); | ||
for (var i in json){ | ||
if (json.hasOwnProperty(i)){ | ||
if (i == "content" && json[i] instanceof Array){ | ||
var content = []; | ||
json[i].forEach(function(str){ | ||
content.push(prepareTemplate(str, name)); | ||
}); | ||
json["content"] = content.join(""); | ||
} | ||
else if(i == "mods" && json[i] instanceof Array){ | ||
var mods = []; | ||
json[i].forEach(function(mod){ | ||
for (var m in mod){ | ||
if (mod.hasOwnProperty(m)){ | ||
mods.push(getCssName(isElement, parent, name, m, mod[m])); | ||
break; | ||
} | ||
} | ||
}); | ||
json["mods"] = mods.join(" "); | ||
} | ||
else if(i == "js" && json[i] instanceof Array){ | ||
json[i].forEach(function(js){ | ||
addJS.push(js); | ||
}); | ||
json[i] = path.join(baseName, "all.js"); | ||
} | ||
else if(i == "css" && json[i] instanceof Array){ | ||
json[i].forEach(function(css){ | ||
addCSS.push(css); | ||
}); | ||
json[i] = path.join(baseName, "all.css"); | ||
} | ||
} | ||
json["blockName"] = getCssName(isElement, parent, name); | ||
} | ||
if (bPath in blocksHash) | ||
html = blocksHash[bPath]["html"]; | ||
else{ | ||
try{ | ||
html = fs.readFileSync(bPath + ".html", 'utf8'); | ||
blocksHash[bPath] = { | ||
"html": html, | ||
"css" : getCssName(isElement, parent, name) | ||
}; | ||
} | ||
catch(e){ | ||
console.log(e.message); | ||
html = ""; | ||
} | ||
} | ||
return doT.template(html)(json); | ||
})(bemJson); | ||
var result = doT.template(pageTpl)(options); | ||
prepareCSS(blocksHash, addCSS, function(e, css){ | ||
if (e) throw e; | ||
var cssPath = path.join(config.publicPath, baseName); | ||
mkdipr(cssPath, function(){ | ||
fs.writeFile(path.join(cssPath, "all.css"), css, function (err) { | ||
if (err) throw err; | ||
}); | ||
}); | ||
/** | ||
* Save string in a file | ||
* @param name | ||
* @param str | ||
* @param handler | ||
*/ | ||
bemRender.prototype.savePackage = function(name, str, handler){ | ||
var | ||
jsPath = path.join(config.publicPath, this.baseName); | ||
mkdipr(jsPath, function(){ | ||
fs.writeFile(path.join(jsPath, name), str, function (err) { | ||
if (err) handler(err); | ||
handler(null); | ||
}); | ||
prepareJS(blocksHash, addJS, function(js){ | ||
if (!js.length) return; | ||
var jsPath = path.join(config.publicPath, baseName); | ||
mkdipr(jsPath, function(){ | ||
fs.writeFile(path.join(jsPath, "all.js"), js, function (err) { | ||
if (err) throw err; | ||
}); | ||
}); | ||
}); | ||
}); | ||
}; | ||
fn(null, result); | ||
}); | ||
return function(jsonPath, options, fn){ | ||
return new bemRender(jsonPath, options, fn); | ||
} | ||
}; |
{ | ||
"name": "bemrender", | ||
"description": "express.js view render based on BEM methodology", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"author": "Ershov Konstantin <ershov.konst@gmail.com>", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
bemRender | ||
========= | ||
express.js view render based on BEM methodology | ||
express.js view render based on BEM methodology | ||
more info: | ||
http://ershov-konst.github.com/bemRender/ |
11787
340
7