stumped by tricky logic
Max
rabkin at mweb[DOT]co[DOT]za
Sun Jan 29 13:24:01 EST 2006
Dave wrote:
> So I'm trying to write a CSS preprocessor.
>
> I want to add the ability to append a selector onto other selectors.
> So, given the following code:
> =========================================
> #selector {
>
> { property: value; property: value; }
> .other_selector { property: value; property: value; }
>
> #selector_2 {
>
> .more_selector { property: value; }
>
> }
>
> }
> =========================================
>
> I want to return the following:
> =========================================
> #selector { property: value; property: value; }
> #selector .other_selector { property: value; property: value; }
> #selector #selector_2 .more_selector { property: value; }
> =========================================
>
Should the properties of #selector be "inherited" by .other_selector?
That's what I'd think the most logical approach, but by your example it
seems not.
> What I think I need to do is match the "{" character with its "}" pair,
> then see if there's another "{" before a matching "}" - but that's
> about as far as my brain will go. The actually code to make this
> actually happen is not coming out when I type.
>
> Any suggestions would be very appreciated. And handsomely rewarded (by
> karma, not me).
>
I'd use a class called Style or somesuch with three attributes:
-Selector (containing "#selector" for example)
-Props (containing the property: value pairs, either as-is or in a
dictionary)
-Parent (containing a reference to the style that contains this one, or
None)
Use a recursive function to read the file, and pass the containing Style
object to it. It reads the props into the class, and recurses on any
"sub-styles".
After this, you'll have constructed a tree (keep a reference to root).
Now you can use an extension of a standard pre-order traversal to output it.
> - Dave
>
Hope this helps.
--Max
More information about the Python-list
mailing list