Skip to content

Instantly share code, notes, and snippets.

@jimjeffers
Created March 5, 2011 20:22
Show Gist options
  • Select an option

  • Save jimjeffers/856686 to your computer and use it in GitHub Desktop.

Select an option

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.
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