8

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}
3
  • can you show what is the input and waht you want the output like Commented Jan 15, 2013 at 5:32
  • INPUT: "body{background:#fff; color:#ccc}"...and many blocks like this. OUTPUT: "body{background:#000; color:#aaa}" [color changed according to specifications given] Commented Jan 15, 2013 at 5:36
  • 1
    As a tip, please be aware that some people can't see any colours at all. Simply changing the colours isn't enough. In order to be able to do this properly, you need to add additional cues so that colour alone isn't the only way to distinguish information. Commented Jan 15, 2013 at 7:00

2 Answers 2

2

try this:

http://cthedot.de/cssutils/

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

Sign up to request clarification or add additional context in comments.

2 Comments

it's library has a pretty documentation.
I am trying hard to do it but selector class is related to CSSStylerule class. I am having a hard time to figure out its significance here. Its really bugging me now :(
2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.