Skip to content

Instantly share code, notes, and snippets.

@toracle
Last active October 1, 2015 08:59
Show Gist options
  • Save toracle/543b1c13156d4285b125 to your computer and use it in GitHub Desktop.
Save toracle/543b1c13156d4285b125 to your computer and use it in GitHub Desktop.
Premailer convert toolchain
#!/usr/bin/ruby
require 'premailer'
in_filename = ARGV[0]
out_filename = ARGV[1]
if in_filename.nil?
puts "Usage: ruby convert.rb <in_filename> <out_filename>"
return
end
if out_filename.nil?
puts "Usage: ruby convert.rb <in_filename> <out_filename>"
return
end
TEMP_FILENAME = 'temp.html'
REMOVE_LINE_TAG = "<!-- EXCLUDE -->"
INSERT_CSS_TAG = "/* INCLUDE:"
def get_css_filename_from_css_tag(line)
start_index = line.index(':') + 1
end_index = line.rindex('*') - start_index
return line.slice(start_index, end_index).strip
end
File.open(TEMP_FILENAME, 'w') do |fout_html|
File.open(in_filename) do |fin_html|
while (line = fin_html.gets)
if not line.include? REMOVE_LINE_TAG
fout_html.puts line
end
if line.include? INSERT_CSS_TAG
css_filename = get_css_filename_from_css_tag(line)
css_content = File.read(css_filename)
fout_html.puts css_content
end
end
end
end
premailer = Premailer.new(TEMP_FILENAME)
File.open(out_filename, 'w') do |fout|
fout.puts premailer.to_inline_css
end
File.delete(TEMP_FILENAME)
SRCS = password_reset_token-template.html
OBJS = $(SRCS:-template.html=.html)
all: $(OBJS)
$(OBJS):
ruby inliner.rb $(@:.html=-template.html) $@
clean:
rm -rf $(OBJS)
configure:
gem install premailer
/* CLIENT-SPECIFIC STYLES */
#outlook a{padding:0;} /* Force Outlook to provide a "view in browser" message */
.ReadMsgBody{width:100%;} .ExternalClass{width:100%;} /* Force Hotmail to display emails at full width */
.ExternalClass, .ExternalClass p, .ExternalClass span, .ExternalClass font, .ExternalClass td, .ExternalClass div {line-height: 100%;} /* Force Hotmail to display normal line spacing */
body, table, td, a{-webkit-text-size-adjust:100%; -ms-text-size-adjust:100%;} /* Prevent WebKit and Windows mobile changing default text sizes */
table, td{mso-table-lspace:0pt; mso-table-rspace:0pt;} /* Remove spacing between tables in Outlook 2007 and up */
img{-ms-interpolation-mode:bicubic;} /* Allow smoother rendering of resized image in Internet Explorer */
/* RESET STYLES */
body{margin:0; padding:0;}
img{border:0; height:auto; line-height:100%; outline:none; text-decoration:none;}
table{border-collapse:collapse !important;}
body{height:100% !important; margin:0; padding:0; width:100% !important;}
/* iOS BLUE LINKS */
.appleBody a {color:#68440a; text-decoration: none;}
.appleFooter a {color:#999999; text-decoration: none;}
/* MOBILE STYLES */
@media screen and (max-width: 525px) {
/* ALLOWS FOR FLUID TABLES */
table[class="wrapper"]{
width:100% !important;
}
/* ADJUSTS LAYOUT OF LOGO IMAGE */
td[class="logo"]{
text-align: left;
padding: 20px 0 20px 0 !important;
}
td[class="logo"] img{
margin:0 auto!important;
}
/* USE THESE CLASSES TO HIDE CONTENT ON MOBILE */
td[class="mobile-hide"]{
display:none;}
img[class="mobile-hide"]{
display: none !important;
}
img[class="img-max"]{
max-width: 100% !important;
height:auto !important;
}
/* FULL-WIDTH TABLES */
table[class="responsive-table"]{
width:100%!important;
}
/* UTILITY CLASSES FOR ADJUSTING PADDING ON MOBILE */
td[class="padding"]{
padding: 10px 5% 15px 5% !important;
}
td[class="padding-copy"]{
padding: 10px 5% 10px 5% !important;
text-align: center;
}
td[class="padding-meta"]{
padding: 30px 5% 0px 5% !important;
text-align: center;
}
td[class="no-pad"]{
padding: 0 0 20px 0 !important;
}
td[class="no-padding"]{
padding: 0 !important;
}
td[class="section-padding"]{
padding: 50px 15px 50px 15px !important;
}
td[class="section-padding-bottom-image"]{
padding: 50px 15px 0 15px !important;
}
/* ADJUST BUTTONS ON MOBILE */
td[class="mobile-wrapper"]{
padding: 10px 5% 15px 5% !important;
}
table[class="mobile-button-container"]{
margin:0 auto;
width:100% !important;
}
a[class="mobile-button"]{
width:80% !important;
padding: 15px !important;
border: 0 !important;
font-size: 16px !important;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment