tags:

views:

65

answers:

7

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
Was looking at the file in chrome's dev tools, but it wasn't formatting it at all.
Herms
Yes, but Firebug formats CSS very well in its CSS tab.
floatless
+1  A: 

These programs are called 'beautifiers'. You should be able to google one that fits for you.

relet
*That*'s the word! I knew there was a term for it but I was drawing a complete blank.
Herms
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
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
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
+1  A: 

If you're looking for a locally-executable utility, as opposed to a web service, you want CSS Tidy.

Matt Ball
Locally-executable is preferable. CSST Tidy isn't perfect (doesn't indent), but it's the closest so far to what I want.
Herms
+1  A: 

This also indents: styleneat

Nimbuz
+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
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
@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
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
With the newlines like you had it I get `format.rb:25: syntax error, unexpected '.', expecting $end`
Herms
@Herms, oh, I'm sorry. I use Ruby 1.9.1 and it can handle this.
floatless
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
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
Thank you and good luck!
floatless