Skip to content

Instantly share code, notes, and snippets.

@mazuhl
Created November 1, 2010 17:48
Show Gist options
  • Save mazuhl/658583 to your computer and use it in GitHub Desktop.
Save mazuhl/658583 to your computer and use it in GitHub Desktop.
Naively spiders a website to extract HTML style attributes and make a CSS stylesheet
#!/usr/bin/env ruby
require "rubygems"
require "spidr"
require "open-uri"
=begin
= CSS snowball
Rolls through your site, picking up style attributes and making you
a new stylesheet. Once you've got your stylesheet, run it through
something like http://www.cleancss.com/ to clean it up.
Outputs a CSS file and an instruction sheet.
=end
if ARGV.empty?
puts "CSS-Snowball - rolls through your site picking up styles"
puts "Usage: #{__FILE__} http://www.example > output.css"
exit
end
count = 1
styles = []
Spidr.site(ARGV[0]) do |spider|
spider.every_page do |page|
#url = page.url
body = page.search('body').first
if body.nil?
# probably not an HTML file, or a malformed one
next
else
style_attributes = body.search('[style]')
style_attributes.each do |s|
styles << s['style'].chomp(' ')
end
end
end
end
styles.uniq.each do |s|
puts ".snowball#{count} {"
s.split(';').each do |att|
puts " #{att};"
end
puts "}\n"
count = count + 1
end
@tconroy
Copy link

tconroy commented Nov 21, 2013

Great script. Found a link to it on a SO question. Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment