Created
March 5, 2011 20:22
-
-
Save jimjeffers/856686 to your computer and use it in GitHub Desktop.
Formats a CSS file so the rules and selectors are printed clearly. Rules are sorted alphabetically, selectors are sorted by length.
This file contains hidden or 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
| nodes = File.open("path/to/stylesheet.css").readlines.join("").gsub(/\n/,"").split("}").map do |chunk| | |
| selectors = chunk.split("{")[0] | |
| rules = chunk.split("{")[1] | |
| chunk = { :selectors => selectors.split(','), :rules => rules.split(';').map{|r| [r.split(':')[0],r.split(':')[1]]} } unless rules.nil? or selectors.nil? | |
| end | |
| output = "" | |
| nodes.each do |node| | |
| unless node.nil? | |
| output += node[:selectors].sort {|x,y| x.length <=> y.length }.join(",\n") + " {\n" | |
| output += node[:rules].sort {|x,y| x[0] <=> y[0] }.map{|rule| " #{rule.join(": ")}"}.join(";\n") + "\n}\n\n" | |
| end | |
| end | |
| puts output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment