Skip to content
Browse files

Rubygems binaries should do not assume that they run on path of gem i…

…nstallation. So move any assumtions to executor code. Add local-premailer binary to run code outside of gem environment. Fixes #57.
  • Loading branch information...
1 parent 168a996 commit 4a22d29dacab09e6dee37062152caec94169c511 @akzhan akzhan committed May 12, 2011
Showing with 108 additions and 95 deletions.
  1. +3 −95 bin/premailer
  2. +96 −0 lib/premailer/executor.rb
  3. +9 −0 local-premailer
View
98 bin/premailer
@@ -1,99 +1,7 @@
#!/usr/bin/env ruby
-require 'optparse'
-require 'rubygems'
-require File.expand_path(File.join(__FILE__) + '/../../lib/premailer.rb')
-
-# defaults
-options = {
- :base_url => nil,
- :link_query_string => nil,
- :remove_classes => false,
- :verbose => false,
- :line_length => 65
-}
-
-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("-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 |v|
- options[:remove_classes] = v
- 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
+# This binary used in rubygems environment only as part of installed gem
- opts.on("-d", "--io-exceptions", "Abort on I/O errors") do |v|
- options[:io_exceptions] = v
- end
-
- opts.on("-v", "--verbose", "Print additional information at runtime") do |v|
- options[:verbose] = v
- 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 $stdin.tty?
- input = ARGV.shift
-else
- input = $stdin
- 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
+require 'rubygems'
+require 'premailer/executor'
-exit
View
96 lib/premailer/executor.rb
@@ -0,0 +1,96 @@
+require 'optparse'
+require 'premailer'
+
+# defaults
+options = {
+ :base_url => nil,
+ :link_query_string => nil,
+ :remove_classes => false,
+ :verbose => false,
+ :line_length => 65
+}
+
+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("-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 |v|
+ options[:remove_classes] = v
+ 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("-d", "--io-exceptions", "Abort on I/O errors") do |v|
+ options[:io_exceptions] = v
+ end
+
+ opts.on("-v", "--verbose", "Print additional information at runtime") do |v|
+ options[:verbose] = v
+ 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 $stdin.tty?
+ input = ARGV.shift
+else
+ input = $stdin
+ 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
View
9 local-premailer
@@ -0,0 +1,9 @@
+#!/usr/bin/env ruby
+
+# This binary used only in development environment
+
+require 'rubygems'
+$LOAD_PATH.unshift(File.expand_path('./lib', File.dirname(__FILE__)))
+
+require 'premailer/executor'
+

0 comments on commit 4a22d29

Please sign in to comment.
Something went wrong with that request. Please try again.