3

im compressing a stylesheet into 1 line, using

cat stylesheet.css | gsed "s|/\*\(\\\\\)\?\*/|/~\1~/|g" -e "s|/\*[^*]*\*\+\([^/][^*]*\*\+\)*/||g" -e "s|\([^:/]\)//.*$|\1|" -e "s|^//.*$||" | tr '\n' ' ' | sed -e "s|/\*[^*]*\*\+\([^/][^*]*\*\+\)*/||g" -e "s|/\~\(\\\\\)\?\~/|/*\1*/|g" -e "s|\s\+| |g" -e "s| \([{;:,]\)|\1|g" -e "s|\([{;:,]\) |\1|g"

im trying to reverse the process.

cat stylesheet-compress.css | gsed 's/\([;{]\)\([^\n]\)/\1\n\t\2/g' | gsed 's/\([^}]\)}/\1;\n}\n\n/g' | gsed 's/{/ {/g' | gsed 's/[,]/& /g' | gsed '/}/ { n;n; s/}\n\([^\n]\)/}\n\n\1/ }'

works for the most part but some of the css entries have indenting and extra ;'s

mu is too short
  • 426,620
  • 70
  • 833
  • 800
chris mccoy
  • 327
  • 1
  • 4
  • 11
  • 3
    I don't understand how this can be expected to be possible. Putting everything on one line removes information about the way in which it used to be on multiple lines. There are multiple ways to put the information on multiple lines. If you just choose one, it won't necessarily be the original. – Karl Knechtel Jun 09 '11 at 03:35
  • Duplicate problem area -- http://stackoverflow.com/questions/787789/any-recommendations-for-a-css-minifier What you really want to do is minify CSS while also reordering rules to get better gzip compression. – mrk Jun 09 '11 at 03:57
  • As a `sed` fan, added the question to favourites to analyse, when I'll be in a mood to !@#$ up my brain. – Rajish Jun 09 '11 at 08:17
  • save your-self some trouble and download a css-minifier and compressor – lovesh Jun 09 '11 at 19:00

1 Answers1

3

I think you're looking for a CSS formatter. There are lots of them online. E.g. http://www.lonniebest.com/FormatCSS/. If you have to do it from shell, I'd spawn to another program to do the heavy lifting. http://cthedot.de/cssutils/ will do the trick.

Steve Prentice
  • 23,230
  • 11
  • 54
  • 55