Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save choonkeat/83381 to your computer and use it in GitHub Desktop.
Save choonkeat/83381 to your computer and use it in GitHub Desktop.
def simple_inline_css(html, *css_files)
doc = Hpricot(html)
css_files.each do |css_file|
css_content_without_comments = IO.read(css_file).gsub(/\/\*.*?\*\//m, '')
css_content_without_comments.split('}').each do |rule|
(match, properties) = rule.split('{')
next unless properties
match.gsub!(/^\s*|\s*$/, '')
properties.gsub!(/^\s*|\s*$/, '')
(doc/match).each do |ele|
new_style_attribute = [properties.gsub(/[\r\n]/, ''), ele.get_attribute("style")].reject(&:blank?).join(';')
ele.set_attribute("style", new_style_attribute)
end
end
end
doc.to_s
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment