I forgot to mention. I am going to give a presentation at RejectConf tonight, and here are the slides.
See the CSSPool and Hpricot integration after the jump.
require 'rubygems'
require 'hpricot'
class Selector
attr_accessor :sac_selector, :properties
def initialize(sac_selector)
@sac_selector = sac_selector
@properties = []
end
end
class ComplexDocHandler < CSS::SAC::DocumentHandler
attr_reader :selectors
def initialize
super
@selectors = []
@current_selectors = nil
end
def start_selector(selectors)
@current_selectors = selectors.map { |x| Selector.new(x) }
end
def property(name, value, important)
@current_selectors.each { |x|
x.properties << "#{name} #{value} #{important}"
}
end
def end_selector(selectors)
@selectors += @current_selectors
end
end
csspool = CSS::SAC::Parser.new(ComplexDocHandler.new)
csspool.parse(File.read(ARGV[0]))
html_doc = Hpricot.parse(File.read(ARGV[1]))
csspool.document_handler.selectors.each do |selector|
selector_xpath = selector.sac_selector.to_xpath
# Show the xpath
puts selector_xpath
# Search the HTML doc for the xpath
html_doc.search(selector_xpath).each do |tag|
p tag
# Print out properties that should be applied
selector.properties.each do |property|
puts property
end
end
puts '#' * 50
end
3 Comments
I was at RejectConf but I didn’t quite grok CSSPool … how would you use it in a Rails or Camping project? Validate your CSS? Create XPaths?
Right now, you would just use it for validating CSS. Eventually, I want to use csspool to check css usage (basically a css coverage tool). I wan to be able to give csspool an XPath to a tag and have it tell me the styles that should be used for that tag (like firebug shows you).
heeey aaron. i totally hpricot it when you don’t call me! word.