Re: tinycss: a new CSS parser for Python

On Fri, Apr 6, 2012 at 6:21 PM, Simon Sapin <simon.sapin@kozea.fr> wrote:

> Le 06/04/2012 17:47, Jerome Leclanche a écrit :
>
>  Thanks for sharing :)
>>
>> How easily can it be extended? Eg to implement a superset of css (such
>> as scss).
>>
>
> Hi Jerome,
>
> By "extending", the use case I had in mind really was adding syntax like
> @font-face for new CSS modules.
>
> In this case, you just need to inherit from the CSS21Parser and override
> the methods that you care about. For an example, see the module that add
> support for margin rules inside @page:
>
> https://github.com/SimonSapin/**tinycss/blob/master/tinycss/**page3.py<https://github.com/SimonSapin/tinycss/blob/master/tinycss/page3.py>
>
> Once you have such a class, you can combine it with other CSS3 features
> with multiple inheritance. The make_parser() function is a shortcut to do
> so:
>
> http://packages.python.org/**tinycss/parsing.html#tinycss.**make_parser<http://packages.python.org/tinycss/parsing.html#tinycss.make_parser>
>
> You may also need to add classes / data structure (like MarginRule) if you
> have new parsed objects, but these are just containers.
>
>
> For SCSS however, I don’t know all the details its syntax so I’m not sure
> how compatible it is with CSS. The critical point is whether it it matches
> the CSS core grammar:
>
> http://www.w3.org/TR/CSS21/**syndata.html#tokenization<http://www.w3.org/TR/CSS21/syndata.html#tokenization>
>
> If it does not match, parsing SCSS would require significant changes to
> tinycss (maybe in the tokenizer). The issue would need much more study to
> decide if I want to do that or not :) (For a more extreme example, tinycss
> will never parse the indentation-based SASS syntax.)
>
> If it does match, extending tinycss for SCSS is probably doable. You might
> need a few more hooks in CSS21Parser (like a parse_ruleset_body that just
> calls parse_declaration_list but can be overridden separately.)
>
> Please let me know if you need such changes in the base class to make
> extending easier, I will gladly add them. The project is still very young,
> and I guess it still misses a few hooks even for some current W3C CSS
> specs/drafts.
>
> Regards,
> --
> Simon Sapin
>

Scss is a superset of css, and has few changes from its main syntax. It
does, however, support $foo: <value>; variables and things such as width:
10px / 2; The main syntax difference is nested rules ( #foo { p { color:
red; }} )

See http://www.kaelig.fr/bettersassdocs/

It might be an interesting project to support scss if you want to have a
very extensible parser :)

J. Leclanche

Received on Friday, 6 April 2012 18:14:23 UTC