Parser#

This parser has been done quickly standing only on cases involved by CSS manifest syntax so it is very naive and may breaks on everything else.

We assume CSS source only contains supported syntax for manifest, everything else could break process.

This flaw is tempered by the behavior of parser that ignores rules that don’t start with the manifest prefix, so CSS manifest could contains some other syntax for non styleguide rules.

class py_css_styleguide.parser.TinycssSourceParser[source]#

CSS parser using tinycss2

Since tinycss2 only return tokens, this parser is in charge to turn them to usable datas: a dict of properties for each selector.

digest_prelude(rule)[source]#

Walk on rule prelude (aka CSS selector) tokens to return a string of the value name (from css selector).

Actually only simple selector and selector with descendant combinator are supported. Using any other selector kind may leads to unexpected issues.

Parameters:

rule (tinycss2.ast.QualifiedRule) – Qualified rule object as returned by tinycss2.

Returns:

Selector name. If it’s a descendant combinator, items are

joined with __.

Return type:

string

digest_content(rule)[source]#

Walk on rule content tokens to return a dict of properties.

This is pretty naive and will choke/fail on everything that is more evolved than simple ident(string):value(string)

Parameters:

rule (tinycss2.ast.QualifiedRule) – Qualified rule object as returned by tinycss2.

Returns:

Dictionnary of retrieved variables and properties.

Return type:

dict

consume(source)[source]#

Parse source and consume tokens from tinycss2.

Parameters:

source (string) – Source content to parse.

Returns:

Retrieved rules.

Return type:

dict

parse(source)[source]#

Read and parse CSS source and return dict of rules.

Parameters:

source (string) – Source content to parse.

Returns:

Selectors with their properties.

Return type:

dict