Skip to content

Instantly share code, notes, and snippets.

@hao-ji-xing
Last active December 20, 2015 01:39
Show Gist options
  • Save hao-ji-xing/6051110 to your computer and use it in GitHub Desktop.
Save hao-ji-xing/6051110 to your computer and use it in GitHub Desktop.
lib css builder
/*
author: [email protected]
Usage:
$ node libcssbuild.js -o libcss.css -p
Options:
-o filename "output file name, default: lib-css.css"
-p "build minify version" default: none
*/
(function(){
var FS = require('fs');
var PATH = require('path');
var MINIFY = false;
var OUTPUT = 'lib-css.css';
var TEXTS = [];
var EXIT = false;
process.argv.forEach(function(item,index){
switch(item){
case '-o':
OUTPUT = process.argv[index+1] || OUTPUT;
break;
case '-p':
MINIFY = true;
break;
case '-h':
EXIT = true;
console.log('Usage:');
console.log('node tinycssbuild.js -o libcss.css -p');
console.log('\nOptions:')
console.log('-o filename "output file name, default: lib-css.css"');
console.log('-p "build minify version" default: none');
break;
}
});
if(EXIT){
return;
}
function line(name, value, options){
if(options === undefined){
TEXTS.push(
'.' + name + '{' + value + '}'
);
}else{
for(var start = options.start; start<=options.end; start += options.step){
TEXTS.push(
'.' + name + start + '{' + value.replace(/_/g,start) + '}'
);
}
}
}
var LASTCOMMENTS = '';
function comments(str){
if(MINIFY){
return;
}
if(LASTCOMMENTS){
TEXTS.push('/* ' + LASTCOMMENTS + ' END */');
}
TEXTS.push('/* ' + str + ' START */');
LASTCOMMENTS = str;
}
function eachLrtb(fn){
'left,right,top,bottom'.split(',').forEach(fn);
}
/*
下面是具体的定义部分
*/
comments('外边距');
line('m0','margin:0');
eachLrtb(function(item, index){
line('m'+item.charAt(0),'margin-'+ item +':_px',{
step : 5,
start : 0,
end : 30
});
})
eachLrtb(function(item, index){
line('m'+item.charAt(0)+'-','margin-'+ item +':_px',{
step : 5,
start : 0,
end : 30
});
})
comments('内边距')
line('p0','padding:0');
eachLrtb(function(item, index){
line('p'+item.charAt(0),'padding-'+ item +':_px',{
step : 5,
start : 0,
end : 30
});
});
comments('边框')
line('bdr0','border:none 0');
eachLrtb(function(item, index){
line('bdr' + item.charAt(0) + '0', 'border-' + item + ':none 0');
});
eachLrtb(function(item, index){
line(item.charAt(0) + '0', item + ':0');
});
//文字部分
comments('文字')
line('tal','text-align:left');
line('tar','text-align:right');
line('tac','text-align:center');
line('taj','text-align:justify');
line('fz','font-size:_px',{
step : 2,
start : 12,
end : 36
})
line('lh','line-height:_',{
step : 0.5,
start : 1,
end : 3
})
line('wsn','white-space:nowrap');
line('fwb','font-weight:bold');
line('fwb2','font-weight:bolder');
line('fwb3','font-weight:lighter');
comments('表格')
line('tbbc','border-collapse:collapse');
line('tbla','table-layout:automatic');
line('tblf','table-layout:fixed');
comments('定位')
line('poss','position:static');
line('posa','position:absolute');
line('posr','position:relative');
line('posf','position:fixed');
line('fll','float:left');
line('flr','float:right');
line('fln','float:none');
line('cll','clear:left');
line('clr','clear:right');
line('clb','clear:both');
comments('展示隐藏')
line('ovv','overflow:visible');
line('ovh','overflow:hidden');
line('ovs','overflow:scroll');
line('ovxs','overflow-x:scroll');
line('ovxs','overflow-y:scroll');
line('ovxa','overflow-x:auto');
line('ovxa','overflow-y:auto');
line('ovxh','overflow-x:hidden');
line('ovxh','overflow-y:hidden');
line('db','display:block')
line('di','display:inline')
line('dn','display:none')
line('dnp','position:absolute;left:-9999px;top:-9999px')
line('vh','visibility:hidden');
line('vv','visibility:visible');
//色值,默认为文字颜色,带b为背景色
comments('色值');
line('red','color:red');
line('blue','color:blue');
line('green','color:green');
line('white','color:white');
line('black','color:black');
line('gray','color:gray');
line('grey','color:grey');
line('bwhite','background:white')
line('bblack','background:black');
line('nobg','background:transparent');
//光标
comments('光标')
line('crp','cursor:pointer');
line('cra','cursor:auto');
line('crd','cursor:default');
line('crw','cursor:wait');
line('crh','cursor:help');
line('crt','cursor:text');
//垂直对齐
comments('垂直对齐')
line('vab','vertical-align:baseline')
line('vas','vertical-align:sub')
line('vasr','vertical-align:suber')
line('vat','vertical-align:top')
line('vam','vertical-align:middle')
line('vab','vertical-align:bottom')
//宽度和高度
comments('宽度和高度')
line('wa','width:auto');
line('ha','height:auto');
line('w100p','width:100%');
line('h100p','height:100%');
line('w25p','width:25%');
line('w50p','width:50%');
line('w33p','width:33.33%');
/*
上面是具体的定义部分
*/
try{
if(MINIFY){
FS.writeFileSync(OUTPUT, TEXTS.join(''));
}else{
FS.writeFileSync(OUTPUT, TEXTS.join('\n'));
}
console.log('build file: (' + OUTPUT + ') success!');
}catch(e){
console.log('build error: ' + e.message);
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment