Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

Comment on CSS parsing
Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: obfuscated [id://466752]
Approved by ktross
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2015-07-04 10:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (59 votes), past polls