I have a compressed CSS file (all whitespace removed) that I want to inspect, but it's a huge pain inspecting it as-is. Is there any utility (preferably linux command line) that I can run the file through to format it nicely?
+4
A:
Try this online service.
You can also inspect any compressed file in Firebug.
floatless
2010-07-30 13:49:37
Was looking at the file in chrome's dev tools, but it wasn't formatting it at all.
Herms
2010-07-30 13:56:01
Yes, but Firebug formats CSS very well in its CSS tab.
floatless
2010-07-30 14:00:00
+1
A:
These programs are called 'beautifiers'. You should be able to google one that fits for you.
relet
2010-07-30 13:49:58
*That*'s the word! I knew there was a term for it but I was drawing a complete blank.
Herms
2010-07-30 13:56:24
A:
Here's a free windows app to "beautify" your file. I haven't used it so I don't know how well it works. http://www.blumentals.net/csstool/
Chuck
2010-07-30 13:56:16
Windows apps don't really help me. I've got an ubuntu machine and a mac laptop, so I really need something that would work on one of those. Thanks though.
Herms
2010-07-30 14:20:12
A:
It is specific, but Visual Studio does this on that file type. (by no means a generic solution to which you alude)
Mark Schultheiss
2010-07-30 14:04:40
+1
A:
If you're looking for a locally-executable utility, as opposed to a web service, you want CSS Tidy.
Matt Ball
2010-07-30 14:05:31
Locally-executable is preferable. CSST Tidy isn't perfect (doesn't indent), but it's the closest so far to what I want.
Herms
2010-07-30 14:21:36
+1
A:
I wrote a little formatter in Ruby for you. Save it as some .rb
file and use it via CLI like ruby format.rb input.css input-clean.css
:
#Formats CSS
input, output = ARGV
#Input
if input == nil or output == nil
puts "Syntax: #{$0} [input] [output]"
exit
end
#Opens file
unless File.exist? input
puts "File #{input} doesn't exist."
exit
end
#Reads file
input = File.read input
#Creates output file
output = File.new output, "w+"
#Processes input
input = input.gsub("{", "\n{\n\t")
.gsub(",", ", ")
.gsub(";", ";\n\t")
.gsub(/\t?}/, "}\n\n\n")
.gsub(/\t([^:]+):/, "\t" + '\1: ')
#Writes output
output.write input
#Closes output
output.close
floatless
2010-07-30 14:28:23
Not working 100% for me, but I can probably tweak it easily (doesn't seem to like the newlines before the .gsub calls and is complaining about non-escaped {} in the regex. Maybe a ruby version difference?). Thanks!
Herms
2010-07-30 14:40:44
@Herms, the String#gsub method takes as the first argument simple strings ("" or '') or regular expressions (//) (http://ruby-doc.org/core/classes/String.html#M000817). Newlines are just a syntax sugar in Ruby. ;-)
floatless
2010-07-30 14:45:58
Yea, but for some reason stringing them together like you did fails. If I remove the newlines so it's just input.gsub().gsub().gsub()... it works.
Herms
2010-07-30 14:47:48
With the newlines like you had it I get `format.rb:25: syntax error, unexpected '.', expecting $end`
Herms
2010-07-30 14:48:31
Here's a link to what I got working and behaving the way I needed (ruby 1.8.7): http://gist.github.com/500644
Herms
2010-07-30 14:50:38
Going to accept this one as, one, you were awesome and actually built something for me, and it's also the one I can get to behave the most like I need.
Herms
2010-07-30 15:00:32