Ruby Inline CSS Replacement Snippet
January 4th, 2007
Just a quick addendum to my previous Post. Quick and Dirty CSS-classes to inline-styles for newsletter/mailouts could be done with that obnoxious php code that I posted (I know, you obfuscated C-type of coders probably could do a nice perl-oneliner to do the replacement AND do you laundry), but, look how much nicer the same code looks in ruby..
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#!/usr/bin/env ruby html = File.read 'newsletter.html' css = File.read 'newsletter_stylesheet.css' def merge_html_and_stylesheet(html,css) css.scan(/\.(\w+)\s+\{([^\}]*)\}/m).each do |rule| html.gsub!(/class="#{rule[0]}"/,"style=\"#{rule[1]}\"") end html end puts merge_html_and_stylesheet(html,css) |
Aaaaaaah… Coding bliss, Ruby is just the greatest language. Rather pythonish, but, without those stupid lookin’ colon’s at the end of control structures. There should only be one colon at the end, and you Proctologists know EXACTLY what i’m talkin’ ‘bout!
Leave a Reply