code.dunae.ca

Premailer: Local Edition

Premailer is now available to run on your own machine. This is a preview release. Kick the tires, don’t expect too much and send any problems you encounter to code@dunae.ca. NB: this version of Premailer does not follow @import rules.

After some clean up, the source code will be available once again (though you can dig through the installed gem if you are so inclined).

Updates will be announced at http://twitter.com/TheCHANGELOG and on the Atom feed.

Requirements

You need to have Ruby installed to run Premailer. You can get it from the Ruby downloads page. Ruby is included with recent versions of Mac OS X.

Installation

From the command line:

gem install hpricot
gem install css_parser
gem install htmlentities
gem install text-reform
gem install premailer --source http://code.dunae.ca

Premailer should now be available in your path.

You can also download the Premailer gem.

Using Premailer from the command line

Premailer is used from the command line.

premailer inputfile outputfile [options]

Note the outputfile should not have an extension (e.g. .html); Premailer will add either .html or .txt.

Remote file example

premailer http://example.com/file.html myprocessedfile

Local file example

premailer mydocs/file.html mydocs/output

Options

-w, --warnings
Generate CSS/HTML warnings
Output in the same directory as outputfile
-q [STRING], --querystring [STRING]
Query string to append to links
-b [STRING], --baseurl [STRING]
Base URL; only applies to local files
-t, --plaintext
Create plain-text version (called outputfile.txt)

A more complete example

The following will process http://example.com/myfile.html and create out/myfile.html (with inline CSS and ?src=email appended to every link) plus a plain text version (out/myfile.txt). It will also create a text file with CSS warnings (out/myfile.warnings.txt).

premailer http://example.com/myfile.html out/myfile -t -w -q src=email

Using Premailer from Ruby

premailer = Premailer.new('http://example.com/myfile.html', :warn_level => Premailer::Warnings::SAFE)

# Write the HTML output
fout = File.open("output.html", "w")
fout.puts premailer.to_inline_css
fout.close

# Write the plain-text output
fout = File.open("ouput.txt", "w")
fout.puts premailer.to_plain_text
fout.close

# List any CSS warnings
puts premailer.warnings.length.to_s + ' warnings found'
premailer.warnings.each do |w|
puts "#{w[:message]} (#{w[:level]})\n     May not render properly in #{w[:clients]}\n\n"
end

premailer = Premailer.new(html_file, :warn_level => Premailer::Warnings::SAFE)
puts premailer.to_inline_css