This repository has been archived by the owner on May 16, 2021. It is now read-only.
forked from sbfaulkner/sinatra-prawn
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
S. Brent Faulkner
committed
Mar 12, 2009
1 parent
c4e7397
commit 9002a95
Showing
15 changed files
with
202 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
*.sw? | ||
.DS_Store | ||
coverage | ||
coverage | ||
rdoc/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.text "Hello world!!!!!" | ||
|
||
== Legal | ||
|
||
Author:: S. Brent Faulkner <brentf@unwwwired.net> | ||
License:: Copyright (c) 2009 unwwwired.net, released under the MIT license |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
--- | ||
:major: 0 | ||
:minor: 9 | ||
:patch: 1 |
This file was deleted.
Oops, something went wrong.
Empty file.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pdf.text "Hello, #{@name}!" |