Re: tinycss: a new CSS parser for Python

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

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

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

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

Received on Friday, 6 April 2012 17:21:30 UTC