Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

CSS parsing

by bageler (Hermit)
on Jun 15, 2005 at 01:36 UTC ( #466752=obfuscated: print w/replies, xml ) Need Help??

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: obfuscated [id://466752]
Approved by ktross
[stonecolddevin]: Corion I haven't heard much of his work to be honest.
[erix]: here is a nice cover, stevieb
[planetscape]: hello, Corion
[Corion]: Hi planetscape!
[stevieb]: Corion I like the groundbreaking ones (guitar players). I have the ability to pick up on sounds that are groundbreaking or specific to a person, thanks to my years of doing recording/mixing/ sampling (hip-hop mind you, but years of it...
[stevieb]: ...has honed in my skills of recognizing sound
[stevieb]: All of the early members are coming out of the woodwork today :) Hey, planetscape
[Corion]: This cover version had so much promise but the singing is underwhelming :-/
[stevieb]: erix Thanks! I dislike remakes of songs usually, but my favourite remix of guitar/weeps is Jeff Healey. I'll take a listen to yours
[planetscape]: hi stevieb!

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (7)
As of 2017-06-22 21:28 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (531 votes). Check out past polls.