RubyConf 2007

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 'css/sac/parser'
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

  1. Posted November 5, 2007 at 10:04 am | Permalink

    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?

  2. Posted November 5, 2007 at 12:07 pm | Permalink

    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).

  3. jax
    Posted November 14, 2007 at 9:15 pm | Permalink

    heeey aaron. i totally hpricot it when you don’t call me! word.

Post a Comment

Your email is never shared. Required fields are marked *

*
*
Check Spelling
Activate Spell Check while Typing