Created
March 23, 2009 01:44
-
-
Save choonkeat/83381 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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