Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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