So, I ran into this guy that was very impressed with himself that he wrote a php script that reads a css file, parses it into a data structure, then prints it out, and that he wrote it in only 20 minutes. I was sitting at o'hare airport waiting for a plane so I decided to impress myself back by writing one in perl shorter, faster and parsing into a fuller data structure. Here it is:
perl -lne'chomp;@a=split/:/;push@{$h{$1}},[@a]if(/(.+){/../}/)&& length($a[1]);END{for(keys%h){$h{$_}=~s/$_.*{|\t//g;print"$_ {"; for(@{$h{$_}}){print" $_->[0] : $_->[1]";}print"}\n"}}' test.css

Replies are listed 'Best First'.
Re: CSS parsing
by ambrus (Abbot) on Jun 15, 2005 at 15:11 UTC

    I tried this on Common Theme CSS, but it doesn't completely work. It seems that it can't parse single-line blocks such as this:

    span.title { float: left }
      WAG: Could that be for lack of the (NON_required) semicolon in the block? Aemi is not spec'ed for a single element block (nor for the last of multiple items in a block, IIRC) but (mere anecdotal assertion) I've found it wise to include to keep some browsers happy.

        No, I doubt that. It has nothing to do with the semicolon, I think the semicolon would even make matters worse.

        As far as I understand the code, the parsing works line by line. Each line is expected to be either a condition and an opening brace, or two strings separated by a colon, or a closing brace. If a line is more complicated than this, this simple code can't break it apart.