Permalink
Browse files

Reimplemented being able to pass in convert/gs paths.

  • Loading branch information...
jonmagic committed Oct 4, 2011
1 parent 060b99c commit 3671c1d72d83f47baf328dc9a389c02558634325
Showing with 25 additions and 108 deletions.
  1. +2 −1 Gemfile
  2. +3 −3 README.textile
  3. +0 −1 grim.gemspec
  4. +1 −1 lib/grim.rb
  5. +14 −22 lib/grim/image_magick_processor.rb
  6. +4 −4 lib/grim/multi_processor.rb
  7. +1 −1 lib/grim/page.rb
  8. +0 −75 spec/lib/grim/image_magick_processor_spec.rb
View
@@ -1,4 +1,5 @@
source :rubygems
gemspec
gem 'rake'
-gem 'rspec'
+gem 'rspec'
+gem 'ruby-debug'
View
@@ -52,12 +52,12 @@ We also support using other processors (the default is whatever version of Image
<pre><code>
# specifying one processor with specific ImageMagick and GhostScript paths
- Grim.processor = Grim::ImageMagickProcessor.new('/path/to/convert', /path/to/gs/bin/')
+ Grim.processor = Grim::ImageMagickProcessor.new({:imagemagick_path => "/path/to/convert", :ghostscript_path => "/path/to/gs"})
# multiple processors with fallback if first fails, useful if you need multiple versions of convert/gs
Grim.processor = Grim::MultiProcessor.new(
- Grim::ImageMagickProcessor.new('/path/to/convert/bin/', /path/to/ghostscript/bin/'),
- Grim::ImageMagickProcessor.new('/path/to/convert/bin/', /path/to/ghostscript/bin/')
+ Grim::ImageMagickProcessor.new({:imagemagick_path => "/path/to/6.7/convert", :ghostscript_path => "/path/to/9.04/gs"}),
+ Grim::ImageMagickProcessor.new({:imagemagick_path => "/path/to/6.6/convert", :ghostscript_path => "/path/to/9.02/gs"})
)
pdf = Grim.reap('/path/to/pdf)
View
@@ -12,7 +12,6 @@ Gem::Specification.new do |s|
s.description = %q{Grim is a simple gem for extracting a page from a pdf and converting it to an image as well as extract the text from the page as a string. It basically gives you an easy to use api to ghostscript, imagemagick, and pdftotext specific to this use case.}
s.rubyforge_project = "grim"
- s.add_dependency 'safe_shell', '~> 1.0.0'
s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
View
@@ -1,5 +1,5 @@
# encoding: UTF-8
-require 'safe_shell'
+require 'shellwords'
module Grim
extend self
@@ -5,41 +5,33 @@ class ImageMagickProcessor
WarningRegex = /\*\*\*\*.*\n/
def initialize(options={})
- @imagemagick_bin_path = options[:imagemagick_bin_path]
- @ghostscript_bin_path = options[:ghostscript_bin_path]
+ @imagemagick_path = options[:imagemagick_path] || 'convert'
+ @ghostscript_path = options[:ghostscript_path]
@original_path = ENV['PATH']
end
def count(path)
- modify_path
- result = SafeShell.execute("gs", "-dNODISPLAY", "-q", "-sFile=#{path}", File.expand_path('../../../lib/pdf_info.ps', __FILE__))
- release_path
+ command = ["-dNODISPLAY", "-q",
+ "-sFile=#{Shellwords.shellescape(path)}",
+ File.expand_path('../../../lib/pdf_info.ps', __FILE__)]
+ @ghostscript_path ? command.unshift(@ghostscript_path) : command.unshift('gs')
+ result = `#{command.join(' ')}`
result.gsub(WarningRegex, '').to_i
end
def save(pdf, index, path, options)
- modify_path
-
width = options.fetch(:width, Grim::WIDTH)
density = options.fetch(:density, Grim::DENSITY)
quality = options.fetch(:quality, Grim::QUALITY)
- output = SafeShell.execute('convert', "-resize", width, "-antialias", "-render",
- "-quality", quality, "-colorspace", "RGB",
- "-interlace", "none", "-density", density,
- "#{pdf.path}[#{index}]", path)
-
- release_path
+ command = [@imagemagick_path, "-resize", width.to_s, "-antialias", "-render",
+ "-quality", quality.to_s, "-colorspace", "RGB",
+ "-interlace", "none", "-density", density.to_s,
+ "#{Shellwords.shellescape(pdf.path)}[#{index}]", path]
+ command.unshift("PATH=#{File.dirname(@ghostscript_path)}:#{ENV['PATH']}") if @ghostscript_path
- $? == 0 || raise(UnprocessablePage, output)
- end
-
- def modify_path
- ENV['PATH'] = "#{@imagemagick_bin_path}:#{ENV['PATH']}" if @imagemagick_bin_path
- ENV['PATH'] = "#{@ghostscript_bin_path}:#{ENV['PATH']}" if @ghostscript_bin_path
- end
+ result = `#{command.join(' ')}`
- def release_path
- ENV['PATH'] = @original_path
+ $? == 0 || raise(UnprocessablePage, result)
end
end
end
@@ -6,17 +6,17 @@ def initialize(processors)
def count(path)
result = ""
- for i in 0..(@processors.length - 1)
- result = @processors[i].count(path)
+ @processors.each do |processor|
+ result = processor.count(path)
break if result != ""
end
result
end
def save(pdf, index, path, options)
result = true
- for i in 0..(@processors.length - 1)
- result = @processors[i].save(pdf, index, path, options)
+ @processors.each do |processor|
+ result = processor.save(pdf, index, path, options)
break if result
end
raise UnprocessablePage unless result
View
@@ -45,7 +45,7 @@ def save(path, options={})
# Returns a String.
#
def text
- SafeShell.execute("pdftotext", "-enc", "UTF-8", "-f", @number, "-l", @number, @pdf.path, "-")
+ `#{["pdftotext", "-enc", "UTF-8", "-f", @number, "-l", @number, Shellwords.escape(@pdf.path), "-"].join(' ')}`
end
end
end
@@ -18,16 +18,6 @@
it "should return page count" do
@processor.count(fixture_path("smoker.pdf")).should == 25
end
-
- it "should call modify_path" do
- @processor.should_receive(:modify_path)
- @processor.count(fixture_path("smoker.pdf"))
- end
-
- it "should call release_path" do
- @processor.should_receive(:release_path)
- @processor.count(fixture_path("smoker.pdf"))
- end
end
describe "#save" do
@@ -48,18 +38,6 @@
width, height = dimensions_for_path(@path)
width.should == 1024
end
-
- it "should call modify_path" do
- SafeShell.stub(:execute){true}
- @processor.should_receive(:modify_path)
- @processor.save(@pdf, 0, @path, {})
- end
-
- it "should call release_path" do
- SafeShell.stub(:execute){true}
- @processor.should_receive(:release_path)
- @processor.save(@pdf, 0, @path, {})
- end
end
describe "#save with width option" do
@@ -106,57 +84,4 @@
(lower_time < higher_time).should be_true
end
end
-
- describe "#modify_path" do
- describe "imagemagick_bin_path set" do
- it "should add /path/to/convert/ to beginning of path" do
- processor = Grim::ImageMagickProcessor.new({:imagemagick_bin_path => '/path/to/convert/'})
- processor.modify_path
- ENV['PATH'].split(':')[0].should == '/path/to/convert/'
- end
-
- after(:each) do
- ENV['PATH'] = @reset_to
- end
- end
-
- describe "ghostscript_bin_path set" do
- it "should add /path/to/convert/ to beginning of path" do
- processor = Grim::ImageMagickProcessor.new({:ghostscript_bin_path => '/path/to/gs/'})
- processor.modify_path
- ENV['PATH'].split(':')[0].should == '/path/to/gs/'
- end
-
- after(:each) do
- ENV['PATH'] = @reset_to
- end
- end
-
- describe "imagemagick_bin_path and ghostscript_bin_path set" do
- it "should add /path/to/convert/ to beginning of path" do
- processor = Grim::ImageMagickProcessor.new({:imagemagick_bin_path => '/path/to/convert/', :ghostscript_bin_path => '/path/to/gs/'})
- processor.modify_path
- ENV['PATH'].split(':')[0].should == '/path/to/gs/'
- ENV['PATH'].split(':')[1].should == '/path/to/convert/'
- end
-
- after(:each) do
- ENV['PATH'] = @reset_to
- end
- end
- end
-
- describe "#release_path" do
- before(:each) do
- ENV['PATH'] = '/grim/reaper/:/the/scythe/is/inevitable/'
- @processor = Grim::ImageMagickProcessor.new({:imagemagick_bin_path => '/path/to/convert/', :ghostscript_bin_path => '/path/to/gs/'})
- @processor.modify_path
- end
-
- it "should set path back to original" do
- ENV['PATH'].should == '/path/to/gs/:/path/to/convert/:/grim/reaper/:/the/scythe/is/inevitable/'
- @processor.release_path
- ENV['PATH'].should == '/grim/reaper/:/the/scythe/is/inevitable/'
- end
- end
end

0 comments on commit 3671c1d

Please sign in to comment.