Created
April 4, 2014 00:44
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for this script, however there is a little error on line 9:
output_obj is not defined, parsedCss is.