Skip to content
This repository has been archived by the owner on Dec 31, 2024. It is now read-only.

Commit

Permalink
initial commit to stevedore
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Carpenter committed Apr 6, 2010
0 parents commit 2a68a2e
Show file tree
Hide file tree
Showing 13 changed files with 302 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .document
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
README.rdoc
lib/**/*.rb
bin/*
features/**/*.feature
LICENSE
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## MAC OS
.DS_Store

## TEXTMATE
*.tmproj
tmtags

## EMACS
*~
\#*
.\#*

## VIM
*.swp

## PROJECT::GENERAL
coverage
rdoc
pkg

## PROJECT::SPECIFIC
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2010 Andrew Carpenter, Critical Juncture LLC

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.
27 changes: 27 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
= Stevedore - Unload your docs

Get metadata about PDFs and extract full-quality images. Wrapper around `pdfimages` and `pdfinfo`.

== Usage

pdf = Stevedore::Pdf.new('/path/to/file.pdf')
pdf.total_pages # => 2

pdf.images.each do |image|
image.page_number
image.file_path
end

== Note on Patches/Pull Requests

* Fork the project.
* Make your feature addition or bug fix.
* Add tests for it. This is important so I don't break it in a
future version unintentionally.
* Commit, do not mess with rakefile, version, or history.
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
* Send us a pull request. Bonus points for topic branches.

== Copyright

Copyright (c) 2010 Andrew Carpenter, Critical Juncture LLC. See LICENSE for details.
46 changes: 46 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'rubygems'
require 'rake'

begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "stevedore"
gem.summary = %Q{Unload your docs}
gem.description = %Q{Get metadata about PDFs and extract full-quality images. Wrapper around `pdfimages` and `pdfinfo`.}
gem.email = "info@criticaljuncture.org"
gem.homepage = "http://github.com/criticaljuncture/stevedore"
gem.authors = ["Andrew Carpenter"]
gem.add_development_dependency "rspec", ">= 1.2.9"
gem.add_dependency 'activesupport'
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
end
Jeweler::GemcutterTasks.new
rescue LoadError
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
end

require 'spec/rake/spectask'
Spec::Rake::SpecTask.new(:spec) do |spec|
spec.libs << 'lib' << 'spec'
spec.spec_files = FileList['spec/**/*_spec.rb']
end

Spec::Rake::SpecTask.new(:rcov) do |spec|
spec.libs << 'lib' << 'spec'
spec.pattern = 'spec/**/*_spec.rb'
spec.rcov = true
end

task :spec => :check_dependencies

task :default => :spec

require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc|
version = File.exist?('VERSION') ? File.read('VERSION') : ""

rdoc.rdoc_dir = 'rdoc'
rdoc.title = "stevedore #{version}"
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
end
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.1
20 changes: 20 additions & 0 deletions lib/stevedore.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "rubygems" # TODO: remove me
require "activesupport"
require "tmpdir"

module Stevedore
class CommandLineError < StandardError #:nodoc:
end

private

def self.run(command, expected_outcodes = 0)
output = `#{command}`
unless [expected_outcodes].flatten.include?($?.exitstatus)
raise CommandLineError, "Error while running #{command}"
end
output
end
end

require "stevedore/pdf"
59 changes: 59 additions & 0 deletions lib/stevedore/pdf.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class Stevedore::Pdf
attr_reader :file_path

def initialize(file_path)
@file_path = file_path
end

def metadata
@metadata ||= Metadata.new(self)
end

delegate :num_pages, :to => :metadata

class Metadata
require 'yaml'

def initialize(pdf)
@pdf = pdf
output = Stevedore.run("pdfinfo #{@pdf.file_path}")
@raw_metadata = YAML::load(output)
end

def num_pages
@raw_metadata["Pages"].to_i
end
end

def images(base_dir = tmp_dir)
@images ||= Image.extract_all(self, base_dir)
end

def tmp_dir
@dir ||= Dir.mktmpdir
end

class Image
attr_reader :file_path, :page_number

def self.extract_all(pdf, base_dir)
@pdf = pdf
images = []
(1..pdf.num_pages).each do |page_number|
base_name = "#{base_dir}/page-#{sprintf("%00d", page_number)}"
Stevedore.run("pdfimages -f #{page_number} -l #{page_number} #{@pdf.file_path} #{base_name}")

Dir.glob("#{base_name}*").sort.each do |file_path|
images << new(file_path, page_number)
end
end

images
end

def initialize(file_path, page_number)
@file_path = file_path
@page_number = page_number
end
end
end
33 changes: 33 additions & 0 deletions spec/pdf_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

describe "pdf_instance" do
before :each do
@file_path = File.expand_path(File.dirname(__FILE__) + '/test.pdf')
@pdf = Stevedore::Pdf.new(@file_path)
end

describe ".file_path" do
it "should return the file path" do
@pdf.file_path.should == @file_path
end
end

describe ".num_pages" do
it "should return the number of pages" do
@pdf.num_pages.should == 2
end
end

describe ".images" do
it "should return 4 images" do
@pdf.images.size.should == 4
end

it "should return 2 images for page 1 and 2 images for page 2" do
@pdf.images[0].page_number.should == 1
@pdf.images[1].page_number.should == 1
@pdf.images[2].page_number.should == 2
@pdf.images[3].page_number.should == 2
end
end
end
1 change: 1 addition & 0 deletions spec/spec.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--color
9 changes: 9 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'stevedore'
require 'spec'
require 'spec/autorun'

Spec::Runner.configure do |config|

end
Binary file added spec/test.pdf
Binary file not shown.
60 changes: 60 additions & 0 deletions stevedore.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Generated by jeweler
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
# -*- encoding: utf-8 -*-

Gem::Specification.new do |s|
s.name = %q{stevedore}
s.version = "0.0.1"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Andrew Carpenter"]
s.date = %q{2010-04-06}
s.description = %q{Get metadata about PDFs and extract full-quality images. Wrapper around `pdfimages` and `pdfinfo`.}
s.email = %q{info@criticaljuncture.org}
s.extra_rdoc_files = [
"LICENSE",
"README.rdoc"
]
s.files = [
".document",
".gitignore",
"LICENSE",
"README.rdoc",
"Rakefile",
"VERSION",
"lib/stevedore.rb",
"lib/stevedore/pdf.rb",
"spec/pdf_spec.rb",
"spec/spec.opts",
"spec/spec_helper.rb",
"spec/test.pdf",
"stevedore.gemspec"
]
s.homepage = %q{http://github.com/criticaljuncture/stevedore}
s.rdoc_options = ["--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.6}
s.summary = %q{Unload your docs}
s.test_files = [
"spec/pdf_spec.rb",
"spec/spec_helper.rb"
]

if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3

if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
s.add_runtime_dependency(%q<activesupport>, [">= 0"])
else
s.add_dependency(%q<rspec>, [">= 1.2.9"])
s.add_dependency(%q<activesupport>, [">= 0"])
end
else
s.add_dependency(%q<rspec>, [">= 1.2.9"])
s.add_dependency(%q<activesupport>, [">= 0"])
end
end

0 comments on commit 2a68a2e

Please sign in to comment.