diff --git a/package.sh b/package.sh index ee0d0c6..0b646a6 100755 --- a/package.sh +++ b/package.sh @@ -1,6 +1,6 @@ #!/bin/bash SCOURVER="0.20" cd .. -zip scour/tarballs/scour-$SCOURVER.zip scour/scour.py scour/svg_regex.py scour/LICENSE scour/NOTICE scour/README.txt scour/release-notes.html +zip scour/tarballs/scour-$SCOURVER.zip scour/scour.py scour/yocto_css.py scour/svg_regex.py scour/LICENSE scour/NOTICE scour/README.txt scour/release-notes.html cd scour -zip tarballs/scour-inkscape-extension-$SCOURVER.zip scour.inx scour.inkscape.py scour.py svg_regex.py +zip tarballs/scour-inkscape-extension-$SCOURVER.zip scour.inx scour.inkscape.py scour.py svg_regex.py scour/yocto_css.py diff --git a/release-notes.html b/release-notes.html index 0b22608..3122217 100644 --- a/release-notes.html +++ b/release-notes.html @@ -13,9 +13,11 @@

Version 0.20

-

Aug ??th, 2009

+

Aug 31st, 2009

diff --git a/scour.py b/scour.py index 74d351a..d032bd6 100755 --- a/scour.py +++ b/scour.py @@ -35,13 +35,13 @@ # Next Up: # + remove unused attributes in parent elements +# + prevent elements from being stripped if they are referenced in a + + + + + + + \ No newline at end of file diff --git a/yocto_css.py b/yocto_css.py new file mode 100644 index 0000000..a73e5f2 --- /dev/null +++ b/yocto_css.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# yocto-css, an extremely bare minimum CSS parser +# +# Copyright 2009 Jeff Schiller +# +# This file is part of Scour, http://www.codedread.com/scour/ +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# In order to resolve Bug 368716 (https://bugs.launchpad.net/scour/+bug/368716) +# scour needed a bare-minimum CSS parser in order to determine if some elements +# were still referenced by CSS properties. + +# I looked at css-py (a CSS parser built in Python), but that library +# is about 35k of Python and requires ply to be installed. I just need +# something very basic to suit scour's needs. + +# yocto-css takes a string of CSS and tries to spit out a list of rules +# A rule is an associative array (dictionary) with the following keys: +# - selector: contains the string of the selector (see CSS grammar) +# - properties: contains an associative array of CSS properties for this rule + +# TODO: need to build up some unit tests for yocto_css + +# stylesheet : [ CDO | CDC | S | statement ]*; +# statement : ruleset | at-rule; +# at-rule : ATKEYWORD S* any* [ block | ';' S* ]; +# block : '{' S* [ any | block | ATKEYWORD S* | ';' S* ]* '}' S*; +# ruleset : selector? '{' S* declaration? [ ';' S* declaration? ]* '}' S*; +# selector : any+; +# declaration : property S* ':' S* value; +# property : IDENT; +# value : [ any | block | ATKEYWORD S* ]+; +# any : [ IDENT | NUMBER | PERCENTAGE | DIMENSION | STRING +# | DELIM | URI | HASH | UNICODE-RANGE | INCLUDES +# | DASHMATCH | FUNCTION S* any* ')' +# | '(' S* any* ')' | '[' S* any* ']' ] S*; + +def parseCssString(str): + rules = [] + # first, split on } to get the rule chunks + chunks = str.split('}') + for chunk in chunks: + # second, split on { to get the selector and the list of properties + bits = chunk.split('{') + if len(bits) != 2: continue + rule = {} + rule['selector'] = bits[0].strip() + # third, split on ; to get the property declarations + bites = bits[1].strip().split(';') + if len(bites) < 1: continue + props = {} + for bite in bites: + # fourth, split on : to get the property name and value + nibbles = bite.strip().split(':') + if len(nibbles) != 2: continue + props[nibbles[0].strip()] = nibbles[1].strip() + rule['properties'] = props + rules.append(rule) + return rules