Apr 29 2013

New Stylesheet Gem
0

I’ve released the Stylesheet gem for parsing css stylesheets from a website. It is a Ruby implementation of DOM Style Sheets objects.

You can find all the stylesheets in a document:

page = Stylesheet::Document.new("http://sportspyder.com")
page.style_sheets
=> [#<Stylesheet::CssStyleSheet:0x007ff99da462a0>, 
#<Stylesheet::CssStyleSheet:0x007ff99da44ae0 >, 
#<Stylesheet::CssStyleSheet:0x007ff99da44018>]
 
stylesheet = page.style_sheets[0]
 
stylesheet.media
=> "screen"
 
stylesheet.href
=> "http://sportspyder.com/assets/application-26ff2c8d54ab9cd8e74af60fc650390e.css"

You can find the rules, and selectors on a stylesheet:

rule = stylesheet.css_rules[0]
=> #<Stylesheet::CssStyleRule css_text:#user_box .post_body .left{width:53px}>
 
rule.selector_text
=> "#user_box .post_body .left"
 
rule.css_text
=> "#user_box .post_body .left{width:53px}"
 
rule.style[0]
=> "width:53px"
 
rule.style.width
=> "53px"

To install:

gem install stylesheet

Or add to bundler:

gem "stylesheet", "~> 0.1.0"

Previous Articles