Skip to content

Instantly share code, notes, and snippets.

@jasonayre
Created July 9, 2014 18:51
Show Gist options
  • Save jasonayre/210609bdb02106e4fbde to your computer and use it in GitHub Desktop.
Save jasonayre/210609bdb02106e4fbde to your computer and use it in GitHub Desktop.
parse colors from http://www.google.com/design/spec/style/color.html and write scss file
require 'nokogiri'
require 'open-uri'
### Quick little script to parse all the colors form
### http://www.google.com/design/spec/style/color.html
### and write scss file
class GoogleColorParser
def initialize
sorted_colors
end
def color_groups
@color_groups ||= document.css("#ui-color-palette-extended-color-palette .color-group")
end
def colors
@colors ||= color_groups.map do |color_group|
main_color = color_group.css(".main-color .name")[0].text.parameterize
color_group.css(".color").inject({}) do |color_hash, color_node|
color_hash["#{main_color}-#{color_node.css(".shade")[0].text.strip}"] = color_node.css(".hex")[0].text.strip
color_hash
end
end.reduce({}, :update)
end
def document
@document ||= Nokogiri::HTML(open("http://www.google.com/design/spec/style/color.html"))
end
def sorted_colors
@sorted_colors ||= Hash[colors.sort_by(&:first)]
end
def to_scss
File.open("colors.scss", "w") do |file|
sorted_colors.each_pair do |name, hex|
file << "$#{name}: #{hex};\n"
end
end
end
def self.parse
new
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment