Skip to content

Instantly share code, notes, and snippets.

@moorage
Created April 4, 2014 00:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save moorage/9965839 to your computer and use it in GitHub Desktop.
Save moorage/9965839 to your computer and use it in GitHub Desktop.
How to parse all custom class names from bootstrap (or any stylesheet file) using nodejs with the css-parse package.
var fs = require('fs')
, read = fs.readFileSync
, css_parse = require('css-parse');
var parsedCss = css_parse(read("./path/to/bootstrap.min.css", 'utf8'));
var collectedClassnames = {}
for (var i = 0; i < parsedCss.stylesheet.rules.length; ++i) {
var rule = output_obj.stylesheet.rules[i];
if (rule.selectors) {
for (var j = 0; j < rule.selectors.length; ++j) {
var selector = rule.selectors[j];
var classes = selector.split(/(\s|\+|\>|\:|\[|\])+/);
for (var k = 0; k < classes.length; ++k) {
var classname = classes[k];
if (classname.length > 0 && classname[0] == '.') {
var adjacentclassnames = classname.split('.');
for (var l = 0; l < adjacentclassnames.length; ++l) {
var singleclassname = adjacentclassnames[l];
if (singleclassname.length > 0) {
collectedClassnames[singleclassname] = true;
}
}
}
}
}
}
}
console.log(Object.keys(collectedClassnames).sort());
Copy link

ghost commented Apr 11, 2014

Thanks for this script, however there is a little error on line 9:

var rule = output_obj.stylesheet.rules[i];

output_obj is not defined, parsedCss is.

var rule = parsedCss.stylesheet.rules[i];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment