lib/imgkit/imgkit.rb in imgkit-1.3.7 vs lib/imgkit/imgkit.rb in imgkit-1.3.8
- old
+ new
@@ -6,11 +6,11 @@
msg = "No wkhtmltoimage executable found at #{IMGKit.configuration.wkhtmltoimage}\n"
msg << ">> Install wkhtmltoimage by hand or try running `imgkit --install-wkhtmltoimage`"
super(msg)
end
end
-
+
class ImproperSourceError < StandardError
def initialize(msg)
super("Improper Source: #{msg}")
end
end
@@ -27,35 +27,35 @@
class UnknownFormatError < StandardError
def initialize(format)
super("Unknown Format: #{format}")
end
end
-
+
attr_accessor :source, :stylesheets
attr_reader :options
-
+
def initialize(url_file_or_html, options = {})
@source = Source.new(url_file_or_html)
-
+
@stylesheets = []
@options = IMGKit.configuration.default_options.merge(options)
@options.merge! find_options_in_meta(url_file_or_html) unless source.url?
-
+
raise NoExecutableError.new unless File.exists?(IMGKit.configuration.wkhtmltoimage)
end
-
+
def command
args = [executable]
args += normalize_options(@options).to_a.flatten.compact
-
+
if @source.html?
args << '-' # Get HTML from stdin
else
args << @source.to_s
end
-
+
args << '-' # Read IMG from stdout
args
end
def executable
@@ -66,11 +66,11 @@
else
default.split('/').last
end
end
- if Open3.method_defined? :capture3
+ if Open3.respond_to? :capture3
def capture3(*opts)
Open3.capture3 *opts
end
else
# Lifted from ruby 1.9.2-p290 sources for ruby 1.8 compatibility
@@ -97,23 +97,23 @@
i.close
[out_reader.value, err_reader.value]
}
end
end
-
+
def to_img(format = nil)
append_stylesheets
set_format(format)
opts = @source.html? ? {:stdin_data => @source.to_s} : {}
result, stderr = capture3(*(command + [opts]))
result.force_encoding("ASCII-8BIT") if result.respond_to? :force_encoding
-
+
raise CommandFailedError.new(command.join(' '), stderr) if result.size == 0
result
end
-
+
def to_file(path)
format = File.extname(path).gsub(/^\./,'').to_sym
set_format(format)
File.open(path,'w:ASCII-8BIT') {|file| file << self.to_img}
end
@@ -123,11 +123,11 @@
self.send(:to_img, m[1].to_sym)
else
super
end
end
-
+
protected
def find_options_in_meta(body)
imgkit_meta_tags(body).inject({}) do |found, tag|
name = tag.attributes["name"].sub(/^#{IMGKit.configuration.meta_tag_prefix}/, '').to_sym
@@ -144,41 +144,41 @@
end
found
rescue # rexml random crash on invalid xml
[]
end
-
+
def style_tag_for(stylesheet)
"<style>#{stylesheet.respond_to?(:read) ? stylesheet.read : File.read(stylesheet)}</style>"
end
-
+
def append_stylesheets
raise ImproperSourceError.new('Stylesheets may only be added to an HTML source') if stylesheets.any? && !@source.html?
-
+
stylesheets.each do |stylesheet|
if @source.to_s.match(/<\/head>/)
@source.to_s.gsub!(/(<\/head>)/, style_tag_for(stylesheet)+'\1')
else
@source.to_s.insert(0, style_tag_for(stylesheet))
end
end
end
-
+
def normalize_options(options)
normalized_options = {}
options.each do |key, value|
next if !value
normalized_key = "--#{normalize_arg key}"
normalized_options[normalized_key] = normalize_value(value)
end
normalized_options
end
-
+
def normalize_arg(arg)
arg.to_s.downcase.gsub(/[^a-z0-9]/,'-')
end
-
+
def normalize_value(value)
case value
when TrueClass
nil
when Array