Reputation: 1450
I am experimenting with CSS files in Python to assist colour blind people. I need to get inside every selector block and change the 'background:' and 'color:'. I tried using CSS parsers like tinycss
but they are not concentrating on getting selectors.
Example input:
body {background:#fff; color:#ccc}
And output:
body {background:#000; color:#aaa}
Upvotes: 8
Views: 4690
Reputation: 1450
I solved this problem by using regular expressions. So I kind of ended up making my own parser. I formed a regular expression to search for color patterns such as #XXX, #XXXXXX, rgb(X,X,X), hsl(X,X,X) in the CSS file, maintained a list to keep the positions they are in the CSS file. Then I just re-wrote all the colors at the positions specified by list. This is the best summary I can give for what I did. Please add comment if you need a very detailed explanation. Thank you.
Upvotes: 2
Reputation: 8548
try this:
parser = CSSParser()
# optionally
parser.setFetcher(fetcher)
sheet = parser.parseFile('test1.css', 'ascii')
print sheet.cssText
it's pretty simple to use in css processing.
to work with selectors you could use cssutils.css.SelectorList
and cssutils.css.Selector
Upvotes: 2