Permalink
Browse files

Trying to adhere to TomDoc and also un-nested specs since we don't ha…

…ve a Pdf class anymore. Also added license.
  • Loading branch information...
jonmagic committed Sep 6, 2011
1 parent b288477 commit ecab1ed3dbec01b528d89fe565e8678b02c5dff6
Showing with 117 additions and 82 deletions.
  1. +20 −0 LICENSE
  2. +6 −2 README.textile
  3. +33 −20 lib/grim.rb
  4. +58 −60 spec/lib/grim_spec.rb
View
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2011 Jonathan Hoyt
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -4,7 +4,7 @@ Grim is a simple gem for extracting a page from a pdf and converting it to an im
h2. Prerequisites
-You will need ghostscript, imagemagick, and xpdf installed. On the Mac (OSX) I highly recommend using "Homebrew":http://mxcl.github.com/homebrew/ to get them installed, its as simple as "brew install ghostscript", "brew install imagemagick", and "brew install xpdf".
+You will need ghostscript, imagemagick, and xpdf installed. On the Mac (OSX) I highly recommend using "Homebrew":http://mxcl.github.com/homebrew/ to get them installed, its as simple as "brew install ghostscript imagemagick xpdf".
h2. Usage
@@ -14,4 +14,8 @@ h2. Usage
png = instance.page(1).to_image("/path/to/save/image.png") # saves png to path and returns File instance
jpeg = instance.page(2).to_image("/path/to/save/image.jpeg") # saves jpeg to path and returns File instance
text = instance.page(3).text # returns text as a string
-</pre></code>
+</pre></code>
+
+h2. License
+
+See LICENSE for details.
View
@@ -5,8 +5,8 @@
#
# instance = Grim.new("/path/to/pdf")
# page_count = instance.page_count
-# png = instance.page(1).to_png("/path/to/save/png")
-# jpeg = instance.page(2).to_jpeg("/path/to/save/jpeg")
+# png = instance.page(1).to_image("/path/to/save/image.png")
+# jpeg = instance.page(2).to_image("/path/to/save/image.jpeg")
# text = instance.page(3).text
#
class Grim
@@ -34,70 +34,83 @@ class PdfNotFound < Grim::Exception
attr_accessor :page_number
# initialize is called when a new instance is created and accepts path.
+ #
+ # path - A String or Path to the pdf
def initialize(path)
raise Grim::PdfNotFound unless File.exists?(path)
@page_number = 1
@path = path
end
- # page_count uses the memoized path and shells out to ghostscript
- # to read the pdf with the pdf_info.ps script as a filter,
- # returning the number of pages in the pdf as an integer.
+ # Shells out to ghostscript to read the pdf with the pdf_info.ps script
+ # as a filter, returning the number of pages in the pdf as an integer.
#
# For example:
#
- # instance.page_count
- # => 4
+ # instance.page_count
+ # # => 4
#
- # Returns an integer.
+ # Returns an Integer.
def page_count
@page_count ||= begin
`gs -dNODISPLAY -q -sFile=#{@path} ./lib/pdf_info.ps`.to_i
end
end
- # page just sets the page attribute on the instance.
+ # Sets the page attribute on the instance.
+ #
+ # number - An Integer of the page number to extract
#
# For example:
#
- # instance.page(1)
- # => instance
+ # instance.page(1)
+ # # => self
#
# Returns self.
def page(number)
@page_number = number
self
end
- # Returns page_number minus 1
+ # Page number minus 1
+ #
+ # For example:
+ #
+ # instance.page(2)
+ # instance.index
+ # # => 1
+ #
+ # Returns an Integer.
def index
@page_number - 1
end
- # to_image extracts the selected page and turns it into an image.
+ # Extracts the selected page and turns it into an image.
# Tested on png and jpeg.
#
+ # path - A String or Path to save image to
+ #
# For example:
#
- # instance.page(2).to_image(/path/to/save/image)
- # => File
+ # instance.page(2).to_image(/path/to/save/image)
+ # # => File
#
- # Returns an instance of File
+ # Returns a File.
def to_image(path)
`convert -resize #{Grim::WIDTH} -antialias -render -quality #{Grim::QUALITY} -colorspace RGB -interlace none -density #{Grim::DENSITY} #{@path}[#{index}] #{path}`
file = File.open(path)
file.rewind
file
end
- # text is an instance method that extracts the text from the selected page.
+ # Extracts the text from the selected page.
#
# For example:
#
- # instance.page(2).text
- # => "This is text from slide 2.\n\nAnd even more text from slide 2."
+ # instance.page(2).text
+ # # => "This is text from slide 2.\n\nAnd even more text from slide 2."
#
- # Returns a string
+ # Returns a String.
def text
`pdftotext -enc UTF-8 -f #{@page_number} -l #{@page_number} #{@path} -`
end
View
@@ -10,86 +10,84 @@
Grim.const_defined?('VERSION').should be_true
end
- describe "Pdf" do
- describe "#initialize" do
- it "should raise an error if pdf does not exist" do
- lambda { Grim.new(fixture_path("booboo.pdf")) }.should raise_error(Grim::PdfNotFound)
- end
+ describe "#initialize" do
+ it "should raise an error if pdf does not exist" do
+ lambda { Grim.new(fixture_path("booboo.pdf")) }.should raise_error(Grim::PdfNotFound)
end
+ end
- describe "#page_count" do
- it "should return an integer" do
- instance = Grim.new(fixture_path("smoker.pdf"))
- instance.page_count.should == 25
- end
+ describe "#page_count" do
+ it "should return an integer" do
+ instance = Grim.new(fixture_path("smoker.pdf"))
+ instance.page_count.should == 25
end
+ end
- describe "#page" do
- it "should be set to 1 by default" do
- instance = Grim.new(fixture_path("smoker.pdf"))
- instance.page_number.should == 1
- end
+ describe "#page" do
+ it "should be set to 1 by default" do
+ instance = Grim.new(fixture_path("smoker.pdf"))
+ instance.page_number.should == 1
+ end
- it "should set page attribute and return instance" do
- instance = Grim.new(fixture_path("smoker.pdf"))
- instance.page(2).should == instance
- instance.page_number.should == 2
- end
+ it "should set page attribute and return instance" do
+ instance = Grim.new(fixture_path("smoker.pdf"))
+ instance.page(2).should == instance
+ instance.page_number.should == 2
end
+ end
- describe "#index" do
- it "should return page minus 1" do
- instance = Grim.new(fixture_path("smoker.pdf"))
- instance.page(2)
- instance.index.should == 1
- end
+ describe "#index" do
+ it "should return page minus 1" do
+ instance = Grim.new(fixture_path("smoker.pdf"))
+ instance.page(2)
+ instance.index.should == 1
end
+ end
- describe "#to_image" do
- describe "output png" do
- before(:all) do
- instance = Grim.new(fixture_path("smoker.pdf"))
- @png = instance.to_image(tmp_path("to_png_spec.png"))
- end
+ describe "#to_image" do
+ describe "output png" do
+ before(:all) do
+ instance = Grim.new(fixture_path("smoker.pdf"))
+ @png = instance.to_image(tmp_path("to_png_spec.png"))
+ end
- it "should create the file" do
- File.exist?(tmp_path("to_png_spec.png")).should be_true
- end
+ it "should create the file" do
+ File.exist?(tmp_path("to_png_spec.png")).should be_true
+ end
- it "should return an instance of File" do
- @png.class.should == File
- end
+ it "should return an instance of File" do
+ @png.class.should == File
+ end
- it "should have the right file size" do
- @png.stat.size.should == 188515
- end
+ it "should have the right file size" do
+ @png.stat.size.should == 188515
end
+ end
- describe "output jpeg" do
- before(:all) do
- instance = Grim.new(fixture_path("smoker.pdf"))
- @jpeg = instance.to_image(tmp_path("to_jpeg_spec.jpeg"))
- end
+ describe "output jpeg" do
+ before(:all) do
+ instance = Grim.new(fixture_path("smoker.pdf"))
+ @jpeg = instance.to_image(tmp_path("to_jpeg_spec.jpeg"))
+ end
- it "should create the file" do
- File.exist?(tmp_path("to_jpeg_spec.jpeg")).should be_true
- end
+ it "should create the file" do
+ File.exist?(tmp_path("to_jpeg_spec.jpeg")).should be_true
+ end
- it "should return an instance of File" do
- @jpeg.class.should == File
- end
+ it "should return an instance of File" do
+ @jpeg.class.should == File
+ end
- it "should have the right file size" do
- @jpeg.stat.size.should == 53980
- end
+ it "should have the right file size" do
+ @jpeg.stat.size.should == 53980
end
end
+ end
- describe "#text" do
- it "should return the text from the selected page" do
- instance = Grim.new(fixture_path("smoker.pdf"))
- instance.page(2).text.should == "Step 1: get someone to print this curve for you to scale, 72\342\200\235 wide\n\nStep 2: Get a couple 55 gallon drums\n\n\f"
- end
+ describe "#text" do
+ it "should return the text from the selected page" do
+ instance = Grim.new(fixture_path("smoker.pdf"))
+ instance.page(2).text.should == "Step 1: get someone to print this curve for you to scale, 72\342\200\235 wide\n\nStep 2: Get a couple 55 gallon drums\n\n\f"
end
end
end

0 comments on commit ecab1ed

Please sign in to comment.