CSS analysis, editing and output in Ruby

Is there a pearl for css editing and analysis in Ruby? I need to open a css file, execute find selector, modify it and save the output. A better way would be as follows:

 draft = CSSParser.load('structure.css') draft.find('#header a').style('color', '#333') draft.render 

What is the most suitable way to do this?

+4
source share
4 answers

I took several steps to solve my problem. The following code is currently capable of modifying CSS, although it lacks the beautiful search line draft.find('#header a').style('color', '#333') :

CSS

 $red: #900; #hello_world a{ font-size: 1pt + 3pt; &:hover{ color: $red + #333 } color: #444333; } 

code:

 require 'rubygems' require 'haml' require 'sass' def get_file_as_string(filename) data = '' File.open(filename, "r").each_line {|line| data += line} data end engine = Sass::Engine.new(template, :syntax => :scss) result, extends = engine.to_tree.perform(Sass::Environment.new).cssize @x = result.children[1] # nice search method should be here class Sass::Tree::RuleNode def set_property(property, value) prop = self.children.first{|child| child.class == 'Sass::Tree::PropNode' && child.instance_variable_get(:@resolved_name) == property } prop.instance_variable_set(:@resolved_value, value) end end @x.set_property('color', '#362') puts result.to_s 
+1
source

The ruby-css-parser on Google Code looks promising.

Their sample page displays parsing and processing CSS selectors.

0
source

Try Sass (Sintactically Awesome StyleSheets).

I use it and it is awesome!

0
source

Sass includes a CSS-> Sass converter, and you can create "mixins" and variables for use in your style sheets. I know that this is not what you asked, but depending on what you are trying to do, this may be the best solution.

Otherwise, it looks like ruby-css-parser is as close as you are to not write anything.

0
source

All Articles