Code

Correct load order of user icons.svg icons with legacy names.
[inkscape.git] / share / extensions / yocto_css.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 #  yocto-css, an extremely bare minimum CSS parser
5 #
6 #  Copyright 2009 Jeff Schiller
7 #
8 #  This file is part of Scour, http://www.codedread.com/scour/
9 #
10 #   Licensed under the Apache License, Version 2.0 (the "License");
11 #   you may not use this file except in compliance with the License.
12 #   You may obtain a copy of the License at
13 #
14 #       http://www.apache.org/licenses/LICENSE-2.0
15 #
16 #   Unless required by applicable law or agreed to in writing, software
17 #   distributed under the License is distributed on an "AS IS" BASIS,
18 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 #   See the License for the specific language governing permissions and
20 #   limitations under the License.
22 # In order to resolve Bug 368716 (https://bugs.launchpad.net/scour/+bug/368716)
23 # scour needed a bare-minimum CSS parser in order to determine if some elements
24 # were still referenced by CSS properties.
26 # I looked at css-py (a CSS parser built in Python), but that library 
27 # is about 35k of Python and requires ply to be installed.  I just need 
28 # something very basic to suit scour's needs.
30 # yocto-css takes a string of CSS and tries to spit out a list of rules
31 # A rule is an associative array (dictionary) with the following keys:
32 # - selector: contains the string of the selector (see CSS grammar)
33 # - properties: contains an associative array of CSS properties for this rule
35 # TODO: need to build up some unit tests for yocto_css
37 # stylesheet  : [ CDO | CDC | S | statement ]*;
38 # statement   : ruleset | at-rule;
39 # at-rule     : ATKEYWORD S* any* [ block | ';' S* ];
40 # block       : '{' S* [ any | block | ATKEYWORD S* | ';' S* ]* '}' S*;
41 # ruleset     : selector? '{' S* declaration? [ ';' S* declaration? ]* '}' S*;
42 # selector    : any+;
43 # declaration : property S* ':' S* value;
44 # property    : IDENT;
45 # value       : [ any | block | ATKEYWORD S* ]+;
46 # any         : [ IDENT | NUMBER | PERCENTAGE | DIMENSION | STRING
47 #               | DELIM | URI | HASH | UNICODE-RANGE | INCLUDES
48 #               | DASHMATCH | FUNCTION S* any* ')' 
49 #               | '(' S* any* ')' | '[' S* any* ']' ] S*;
51 def parseCssString(str):
52         rules = []
53         # first, split on } to get the rule chunks
54         chunks = str.split('}')
55         for chunk in chunks:
56                 # second, split on { to get the selector and the list of properties
57                 bits = chunk.split('{')
58                 if len(bits) != 2: continue
59                 rule = {}
60                 rule['selector'] = bits[0].strip()
61                 # third, split on ; to get the property declarations
62                 bites = bits[1].strip().split(';')
63                 if len(bites) < 1: continue
64                 props = {}
65                 for bite in bites:
66                         # fourth, split on : to get the property name and value
67                         nibbles = bite.strip().split(':')
68                         if len(nibbles) != 2: continue
69                         props[nibbles[0].strip()] = nibbles[1].strip()
70                 rule['properties'] = props
71                 rules.append(rule)
72         return rules