Search code examples
cssrubyparsingcss-parsing

Ruby: parsing structured blocks of plaintext


I'm writing a simple ruby parser for CSS files and I'm kind of stumped on how I can get a block of CSS (that is, a selector and everything between curly braces immediately following it) as a ruby object on which I can perform my dark and nasty magic.

Ideally, I would like to get a ruby hash with each attribute / value and the selector. Is there any clear and easy to understand way how to do this?


Solution

  • As mentioned in the comments it's silly to do it yourself beyond a fun exercise. I'd read the css file into a string and then scan it with regular expressions.

    Ruby - Convert File to String

    http://www.ruby-doc.org/core-1.9.3/String.html#method-i-scan

    for testing the regex: http://rubular.com/