10

I want to implement a script that reads a CSS file and makes meaningful changes to it (adding/removing/replacing lines/words etc.). The basic logic is implement an RTL (right-to-left) transformation.

I could think of quite a few approaches to it:

  • file reader - read a line, analyze it and make the needed changes to it.
  • two phase scan - create in memory model, scan and change it, save model to text.
  • regular expressions - It might be quite difficult because some of them might be very complex.

basically what I'm wondering is which of those, or other methods, would be the python way to do it? are there any relevant libraries you think I should be familiar with for this kind of operation?

Edit: it's should be noted that this is a "learn python through this usable project" kind of project so I'm not familiar with most libraries you would mention here.

arikg
  • 402
  • 2
  • 4
  • 17
  • http://nedbatchelder.com/text/python-parsers.html contains a good survey of state of language parsing tools for Python. My best (non-analytic) guess is that CSS is a context-free (not regular) language and thus could only be transmogrified correctly with a stateful parser. – msw Jul 21 '12 at 13:07
  • 3
    have you tried an existing CSS parser in Python such as [cssutils](http://cthedot.de/cssutils/)? – jfs Jul 21 '12 at 13:12
  • do you want to keep the formatting, comments, etc ? – Karoly Horvath Jul 21 '12 at 13:14
  • first of all it's probably important to mention that this is a "learn python through this usable project" kind of project so I'm not familiar with most things you mentioned above (I will add this remark to the main post as well). By looking at the cssutils page and examples it seems like it belongs to the second approach I mentioned. and I will surely look into it, but still remains the question if that is that actually the best python practice in this case? – arikg Jul 21 '12 at 13:35
  • formatting is pretty flexible for me, it's not a 100% must at this time... – arikg Jul 21 '12 at 13:36

1 Answers1

21

If you want something "quick and dirty" there are many interesting ways to do this. (As you said: line-by-line, regular expressions, …)

But if you want to do it "right" (correct on all kinds of inputs) you’ll need a real parser based on the official CSS tokenization and grammar. In Python there is cssutils and tinycss. (Disclaimer: I’m tinycss’s author.) If you want to learn, I like to think that tinycss’s source code is straightforward and easy to learn :)

Simon Sapin
  • 9,790
  • 3
  • 35
  • 44
  • would love to hear from you about the difference between the two parser tools. maybe in the form of an asnwer to "why did you create tinycss when cssutils already exists?" – arikg Jul 27 '12 at 14:02
  • 2
    arikg: There it is: http://exyr.org/2012/tinycss-css-parser/ In short, it’s mostly that cssutils was not easy to extend for new syntax. – Simon Sapin Jul 27 '12 at 14:45
  • I just tried doing this with tinycss but writing back the css file is somehow not straightforward at all. I can write out rules with some hand written code but it's then missing all comments and all the things tinycss doesn't actually parse like @media... – n13 Aug 18 '16 at 11:29
  • 3
    Yes, tinycss doesn’t include a proper serializer. I suggest using https://github.com/SimonSapin/tinycss2 instead, which does. – Simon Sapin Aug 18 '16 at 21:39