Skip to content
This repository has been archived by the owner on Jan 13, 2018. It is now read-only.

bem create: Support b__e_m form of declaration of BEM-entities to create #319

Merged
merged 1 commit into from
Nov 29, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions lib/commands/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ module.exports = function() {
.opt()
.name('block').short('b').long('block')
.title('block name')
.req()
.arr()
.end()
.opt()
Expand All @@ -40,6 +39,11 @@ module.exports = function() {
.title('force files creation')
.flag()
.end()
.arg()
.name('entities')
.title('BEM entities to create in form of block__elem_mod_val')
.arr()
.end()
.act(function(opts, args) {

var context = new Context(opts.level, opts),
Expand Down Expand Up @@ -88,11 +92,32 @@ module.exports = function() {
items.push(bemUtil.extend({ val: val }, itemBase));
},

prev = {},
eachEntity = function(key) {
var partial = bemUtil.bemParseKey(key),
item = {};

// Try to reconstruct base item object from previous item
if (!partial.block && prev.block) {
item.block = prev.block;

if (partial.mod && prev.elem) {
item.elem = prev.elem;
}
}

prev = bemUtil.extend(item, partial);
if (!prev.block) return;

items.push(prev);
},

techs = context.getDefaultTechs();

if (!techs.length) return Q.reject('You should specify techs to create using --force-tech, -T or --add-tech, -t options');

opts.block.forEach(eachBlock);
opts.block && opts.block.forEach(eachBlock);
args.entities && args.entities.forEach(eachEntity);

return Q.all(items.map(function(item) {
return Q.all(techs.map(function(t) {
Expand Down
16 changes: 12 additions & 4 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,10 +616,17 @@ exports.bemFullKey = function(item) {
*/
exports.bemType = function(item) {

var type = item.elem? 'elem' : 'block';
var type = '';

if (item.block) {
type += 'block';
}
else if (item.elem) {
type += 'elem';
}

if (item.mod) {
type += '-mod';
type += (type? '-' : '') + 'mod';
item.val && (type += '-val');
}

Expand All @@ -628,7 +635,7 @@ exports.bemType = function(item) {
};

var bemItemRe = '([^_.]+)',
bemKeyRe = new RegExp('^' + bemItemRe +
bemKeyRe = new RegExp('^(?:' + bemItemRe + ')?' +
'(?:__' + bemItemRe + ')?(?:_' + bemItemRe + '(?:_' + bemItemRe + ')?)?' +
'(?:\.' + bemItemRe + ')?$');

Expand All @@ -641,8 +648,9 @@ var bemItemRe = '([^_.]+)',
exports.bemParseKey = function(key) {

var m = bemKeyRe.exec(key),
item = { block: m[1] };
item = {};

m[1] && (item.block = m[1]);
m[2] && (item.elem = m[2]);
m[3] && (item.mod = m[3]);
m[4] && (item.val = m[4]);
Expand Down