1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
require 'optparse'
require 'premailer'
# defaults
options = {
:base_url => nil,
:link_query_string => nil,
:remove_classes => false,
:verbose => false,
:line_length => 65,
:adapter => :nokogiri,
}
mode = :html
opts = OptionParser.new do |opts|
opts.banner = "Improve the rendering of HTML emails by making CSS inline among other things. Takes a path to a local file, a URL or a pipe as input.\n\n"
opts.define_head "Usage: premailer <optional uri|optional path> [options]"
opts.separator ""
opts.separator "Examples:"
opts.separator " premailer http://example.com/ > out.html"
opts.separator " premailer http://example.com/ --mode txt > out.txt"
opts.separator " cat input.html | premailer -q src=email > out.html"
opts.separator " premailer ./public/index.html"
opts.separator ""
opts.separator "Options:"
opts.on("--mode MODE", [:html, :txt], "Output: html or txt") do |v|
mode = v
end
opts.on("--adapter ADAPTER", [:nokogiri, :nokogiri_fast, :nokogumbo], "Adapter: nokogiri, nokogiri_fast or nokogumbo (default: #{options[:adapter]}") do |v|
options[:adapter] = v
end
opts.on("-b", "--base-url STRING", String, "Base URL, useful for local files") do |v|
options[:base_url] = v
end
opts.on("-q", "--query-string STRING", String, "Query string to append to links") do |v|
options[:link_query_string] = v
end
opts.on("--css FILE,FILE", Array, "Additional CSS stylesheets") do |v|
options[:css] = v
end
opts.on("-r", "--remove-classes", "Remove HTML classes") do
options[:remove_classes] = true
end
opts.on("-j", "--remove-scripts", "Remove <script> elements") do
options[:remove_scripts] = true
end
opts.on("-l", "--line-length N", Integer, "Line length for plaintext (default: #{options[:line_length].to_s})") do |v|
options[:line_length] = v
end
opts.on("-e", "--entities", "Output HTML entities instead of UTF-8 when using Nokogiri") do |v|
options[:output_encoding] = "US-ASCII"
end
opts.on("-d", "--io-exceptions", "Abort on I/O errors") do
options[:io_exceptions] = true
end
opts.on("-v", "--verbose", "Print additional information at runtime") do
options[:verbose] = true
end
opts.on_tail("-?", "--help", "Show this message") do
puts opts
exit
end
opts.on_tail("-V", "--version", "Show version") do
puts "Premailer #{Premailer::VERSION} (c) 2008-2010 Alex Dunae"
exit
end
end
opts.parse!
$stderr.puts "Processing in #{mode} mode with options #{options.inspect}" if options[:verbose]
premailer = nil
input = nil
if ARGV.size > 0
# Executed via command line or shell script
input = ARGV.shift
else
# Called in piped command
input = $stdin.read
options[:with_html_string] = true
end
if input
premailer = Premailer.new(input, options)
else
puts opts
exit 1
end
if mode == :txt
print premailer.to_plain_text
else
print premailer.to_inline_css
end
exit
|