Navigation Menu

Skip to content
This repository has been archived by the owner on May 16, 2021. It is now read-only.

Commit

Permalink
first working version
Browse files Browse the repository at this point in the history
  • Loading branch information
S. Brent Faulkner committed Mar 12, 2009
1 parent c4e7397 commit 9002a95
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 43 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,3 +1,4 @@
*.sw?
.DS_Store
coverage
coverage
rdoc/*
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,4 +1,4 @@
Copyright (c) 2008 S. Brent Faulkner
Copyright (c) 2009 unwwwired.net

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
7 changes: 0 additions & 7 deletions README

This file was deleted.

27 changes: 27 additions & 0 deletions README.rdoc
@@ -0,0 +1,27 @@
= sinatra-prawn

sinatra-prawn is an extension for sinatra to enable rendering of pdf files
using prawn templates.

== Example

require 'rubygems'
require 'sinatra'
require 'sinatra/prawn'

set :prawn, { :page_layout => :landscape }

get '/' do
content_type 'application/pdf'
prawn :pdf
end

__END__

@@ pdf
pdf.text "Hello world!!!!!"

== Legal

Author:: S. Brent Faulkner <brentf@unwwwired.net>
License:: Copyright (c) 2009 unwwwired.net, released under the MIT license
10 changes: 2 additions & 8 deletions Rakefile
Expand Up @@ -7,8 +7,9 @@ begin
s.summary = %Q{Sinatra extension to add support for pdf rendering with Prawn templates.}
s.email = "brentf@unwwwired.net"
s.homepage = "http://github.com/sbfaulkner/sinatra-prawn"
s.description = "TODO"
s.description = "Sinatra extension to add support for pdf rendering with Prawn templates."
s.authors = ["S. Brent Faulkner"]
s.add_dependency "prawn"
end
rescue LoadError
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
Expand Down Expand Up @@ -41,11 +42,4 @@ rescue LoadError
puts "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
end

begin
require 'cucumber/rake/task'
Cucumber::Rake::Task.new(:features)
rescue LoadError
puts "Cucumber is not available. In order to run features, you must: sudo gem install cucumber"
end

task :default => :test
4 changes: 4 additions & 0 deletions VERSION.yml
@@ -0,0 +1,4 @@
---
:major: 0
:minor: 9
:patch: 1
9 changes: 0 additions & 9 deletions features/sinatra_prawn.feature

This file was deleted.

Empty file.
13 changes: 0 additions & 13 deletions features/support/env.rb

This file was deleted.

31 changes: 31 additions & 0 deletions lib/sinatra/prawn.rb
@@ -0,0 +1,31 @@
require 'sinatra/base'

module Sinatra
module Prawn
# Generate pdf file using Prawn.
# Takes the name of a template to render as a Symbol and returns a String with the rendered output.
#
# Options for prawn may be specified in Sinatra using set :prawn, { ... }
def prawn(template=nil, options={}, &block)
require 'prawn' unless defined? ::Prawn
options, template = template, nil if template.is_a?(Hash)
template = lambda { block } if template.nil?
options[:layout] = false
options[:options] ||= self.class.prawn if self.class.respond_to? :prawn
render :prawn, template, options
end

protected
def render_prawn(template, data, options, &block)
pdf = ::Prawn::Document.new(options[:options] || {})
if data.respond_to?(:to_str)
eval data.to_str, binding, '<PRAWN>', 1
elsif data.kind_of?(Proc)
data.call(pdf)
end
pdf.render
end
end

helpers Prawn
end
Empty file removed lib/sinatra_prawn.rb
Empty file.
32 changes: 32 additions & 0 deletions sinatra-prawn.gemspec
@@ -0,0 +1,32 @@
# -*- encoding: utf-8 -*-

Gem::Specification.new do |s|
s.name = %q{sinatra-prawn}
s.version = "0.9.1"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["S. Brent Faulkner"]
s.date = %q{2009-03-11}
s.description = %q{Sinatra extension to add support for pdf rendering with Prawn templates.}
s.email = %q{brentf@unwwwired.net}
s.files = ["README.rdoc", "VERSION.yml", "lib/sinatra", "lib/sinatra/prawn.rb", "test/sinatra_prawn_test.rb", "test/test_helper.rb", "test/views", "test/views/hello.prawn"]
s.has_rdoc = true
s.homepage = %q{http://github.com/sbfaulkner/sinatra-prawn}
s.rdoc_options = ["--inline-source", "--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.1}
s.summary = %q{Sinatra extension to add support for pdf rendering with Prawn templates.}

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

if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<prawn>, [">= 0"])
else
s.add_dependency(%q<prawn>, [">= 0"])
end
else
s.add_dependency(%q<prawn>, [">= 0"])
end
end
48 changes: 46 additions & 2 deletions test/sinatra_prawn_test.rb
@@ -1,7 +1,51 @@
require File.dirname(__FILE__) + '/test_helper'

class SinatraPrawnTest < Test::Unit::TestCase
def test_something_for_real
flunk "hey buddy, you should probably rename this file and start testing for real"
include Sinatra::Test

def prawn_app(&block)
mock_app {
helpers Sinatra::Prawn
set :views, File.dirname(__FILE__) + '/views'
get '/', &block
}
get '/'
end

def test_renders_inline_strings
prawn_app { prawn 'pdf.text "Hello shrimp!"' }
assert ok?
text = PDF::TextInspector.analyze(body)
assert_equal "Hello shrimp!", text.strings.first
end

def test_renders_inline_blocks
prawn_app {
@name = "Frank & Mary"
prawn do |pdf|
pdf.text "Hello #{@name}!"
end
}
assert ok?
text = PDF::TextInspector.analyze(body)
assert_equal "Hello Frank & Mary!", text.strings.first
end

def test_renders_prawn_files_in_views_path
prawn_app {
@name = "World"
prawn :hello
}
assert ok?
text = PDF::TextInspector.analyze(body)
assert_equal "Hello, World!", text.strings.first
end

def test_raises_error_if_template_not_found
mock_app {
helpers Sinatra::Prawn
get('/') { prawn :no_such_template }
}
assert_raise(Errno::ENOENT) { get('/') }
end
end
58 changes: 56 additions & 2 deletions test/test_helper.rb
@@ -1,9 +1,63 @@
require 'rubygems'
require 'test/unit'
require 'mocha'
require 'sinatra/test'

$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'sinatra_prawn'
require 'sinatra/prawn'

require "pdf/reader"

module PDF
class TextInspector
attr_accessor :font_settings, :size, :strings

def initialize
@font_settings = []
@fonts = {}
@strings = []
end

def resource_font(*params)
@fonts[params[0]] = params[1].basefont
end

def set_text_font_and_size(*params)
@font_settings << { :name => @fonts[params[0]], :size => params[1] }
end

def show_text(*params)
@strings << params[0]
end

def show_text_with_positioning(*params)
# ignore kerning information
@strings << params[0].reject { |e| Numeric === e }.join
end

def self.analyze(output,*args,&block)
obs = self.new(*args, &block)
PDF::Reader.string(output,obs)
obs
end

def self.analyze_file(filename,*args,&block)
analyze(File.open(filename, "rb") { |f| f.read },*args,&block)
end

def self.parse(obj)
PDF::Reader::Parser.new(
PDF::Reader::Buffer.new(StringIO.new(obj)), nil).parse_token
end
end
end

class Test::Unit::TestCase
include Sinatra::Test

# Sets up a Sinatra::Base subclass defined with the block
# given. Used in setup or individual spec methods to establish
# the application.
def mock_app(base=Sinatra::Base, &block)
@app = Sinatra.new(base, &block)
end
end
1 change: 1 addition & 0 deletions test/views/hello.prawn
@@ -0,0 +1 @@
pdf.text "Hello, #{@name}!"

0 comments on commit 9002a95

Please sign in to comment.