prawnto 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +20 -0
- data/README +12 -0
- data/Rakefile +47 -0
- data/lib/prawnto/action_controller_mixin.rb +45 -0
- data/lib/prawnto/action_view_mixin.rb +12 -0
- data/lib/prawnto/template_handler/compile_support.rb +72 -0
- data/lib/prawnto/template_handlers/base.rb +16 -0
- data/lib/prawnto/template_handlers/dsl.rb +16 -0
- data/lib/prawnto/template_handlers/raw.rb +64 -0
- data/lib/prawnto.rb +28 -0
- data/rails/init.rb +6 -0
- data/reference_pdfs +0 -0
- data/test/action_controller_test.rb +38 -0
- data/test/base_template_handler_test.rb +39 -0
- data/test/dsl_template_handler_test.rb +40 -0
- data/test/raw_template_handler_test.rb +163 -0
- data/test/template_handler_test_mocks.rb +77 -0
- metadata +90 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 cracklabs.com
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
Prawnto
|
2
|
+
=======
|
3
|
+
|
4
|
+
a rails plugin providing templating abilities
|
5
|
+
for generating pdf files leveraging the new kick-ass prawn library
|
6
|
+
|
7
|
+
full documentation/demos at: http://cracklabs.com/prawnto
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
Copyright (c) 2008 cracklabs.com, released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'rake/gempackagetask'
|
5
|
+
|
6
|
+
desc 'Default: run unit tests.'
|
7
|
+
task :default => :test
|
8
|
+
|
9
|
+
#desc 'Test the prawnto plugin.'
|
10
|
+
#Rake::TestTask.new(:test) do |t|
|
11
|
+
#t.libs << 'lib'
|
12
|
+
#t.pattern = 'test/**/*_test.rb'
|
13
|
+
#t.verbose = true
|
14
|
+
#end
|
15
|
+
|
16
|
+
desc 'Generate documentation for the prawnto plugin.'
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'Prawnto'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
rdoc.rdoc_files.include('README')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
24
|
+
|
25
|
+
PKG_FILES = FileList[ '[a-zA-Z]*', 'lib/**/*', 'test/*', 'rails/*' ]
|
26
|
+
|
27
|
+
spec = Gem::Specification.new do |s|
|
28
|
+
s.name = "prawnto"
|
29
|
+
s.version = "0.0.1"
|
30
|
+
s.author = "smecsia"
|
31
|
+
s.email = "smecsia@gmail.com"
|
32
|
+
#s.homepage = ""
|
33
|
+
s.platform = Gem::Platform::RUBY
|
34
|
+
s.summary = "Prawnto rails plugin implemented as a gem (see prawnto)"
|
35
|
+
s.add_dependency('rails', '>=2.1')
|
36
|
+
s.add_dependency('prawn')
|
37
|
+
s.files = PKG_FILES.to_a
|
38
|
+
s.require_path = "lib"
|
39
|
+
s.has_rdoc = true
|
40
|
+
s.extra_rdoc_files = ["README"]
|
41
|
+
end
|
42
|
+
|
43
|
+
desc 'Turn this plugin into a gem.'
|
44
|
+
Rake::GemPackageTask.new(spec) do |pkg|
|
45
|
+
pkg.gem_spec = spec
|
46
|
+
end
|
47
|
+
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Prawnto
|
2
|
+
module ActionControllerMixin
|
3
|
+
|
4
|
+
DEFAULT_PRAWNTO_OPTIONS = {:inline=>true}
|
5
|
+
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
def prawnto(options)
|
12
|
+
prawn_options, prawnto_options = breakdown_prawnto_options options
|
13
|
+
write_inheritable_hash(:prawn, prawn_options)
|
14
|
+
write_inheritable_hash(:prawnto, prawnto_options)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def breakdown_prawnto_options(options)
|
20
|
+
prawnto_options = options.dup
|
21
|
+
prawn_options = (prawnto_options.delete(:prawn) || {}).dup
|
22
|
+
[prawn_options, prawnto_options]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def prawnto(options)
|
27
|
+
@prawnto_options ||= DEFAULT_PRAWNTO_OPTIONS.dup
|
28
|
+
@prawnto_options.merge! options
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def compute_prawnto_options
|
35
|
+
@prawnto_options ||= DEFAULT_PRAWNTO_OPTIONS.dup
|
36
|
+
@prawnto_options[:prawn] ||= {}
|
37
|
+
@prawnto_options[:prawn].merge!(self.class.read_inheritable_attribute(:prawn) || {}) {|k,o,n| o}
|
38
|
+
@prawnto_options.merge!(self.class.read_inheritable_attribute(:prawnto) || {}) {|k,o,n| o}
|
39
|
+
@prawnto_options
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
|
@@ -0,0 +1,72 @@
|
|
1
|
+
module Prawnto
|
2
|
+
module TemplateHandler
|
3
|
+
|
4
|
+
class CompileSupport
|
5
|
+
extend ActiveSupport::Memoizable
|
6
|
+
|
7
|
+
attr_reader :options
|
8
|
+
|
9
|
+
def initialize(controller)
|
10
|
+
@controller = controller
|
11
|
+
@options = pull_options
|
12
|
+
set_headers
|
13
|
+
end
|
14
|
+
|
15
|
+
def pull_options
|
16
|
+
@controller.send :compute_prawnto_options || {}
|
17
|
+
end
|
18
|
+
|
19
|
+
def set_headers
|
20
|
+
set_pragma
|
21
|
+
set_cache_control
|
22
|
+
set_content_type
|
23
|
+
set_disposition
|
24
|
+
end
|
25
|
+
|
26
|
+
# TODO: kept around from railspdf-- maybe not needed anymore? should check.
|
27
|
+
def ie_request?
|
28
|
+
@controller.request.env['HTTP_USER_AGENT'] =~ /msie/i
|
29
|
+
end
|
30
|
+
memoize :ie_request?
|
31
|
+
|
32
|
+
# added to make ie happy with ssl pdf's (per naisayer)
|
33
|
+
def ssl_request?
|
34
|
+
@controller.request.env['SERVER_PROTOCOL'].downcase == "https"
|
35
|
+
end
|
36
|
+
memoize :ssl_request?
|
37
|
+
|
38
|
+
# TODO: kept around from railspdf-- maybe not needed anymore? should check.
|
39
|
+
def set_pragma
|
40
|
+
if ssl_request? && ie_request?
|
41
|
+
@controller.headers['Pragma'] = 'public' # added to make ie ssl pdfs work (per naisayer)
|
42
|
+
else
|
43
|
+
@controller.headers['Pragma'] ||= ie_request? ? 'no-cache' : ''
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# TODO: kept around from railspdf-- maybe not needed anymore? should check.
|
48
|
+
def set_cache_control
|
49
|
+
if ssl_request? && ie_request?
|
50
|
+
@controller.headers['Cache-Control'] = 'maxage=1' # added to make ie ssl pdfs work (per naisayer)
|
51
|
+
else
|
52
|
+
@controller.headers['Cache-Control'] ||= ie_request? ? 'no-cache, must-revalidate' : ''
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def set_content_type
|
57
|
+
@controller.response.content_type ||= Mime::PDF
|
58
|
+
end
|
59
|
+
|
60
|
+
def set_disposition
|
61
|
+
inline = options[:inline] ? 'inline' : 'attachment'
|
62
|
+
filename = options[:filename] ? "filename=#{options[:filename]}" : nil
|
63
|
+
@controller.headers["Content-Disposition"] = [inline,filename].compact.join(';')
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Prawnto
|
2
|
+
module TemplateHandlers
|
3
|
+
class Base < ::ActionView::TemplateHandler
|
4
|
+
include ::ActionView::TemplateHandlers::Compilable
|
5
|
+
|
6
|
+
def compile(template)
|
7
|
+
"_prawnto_compile_setup;" +
|
8
|
+
"pdf = Prawn::Document.new(@prawnto_options[:prawn]);" +
|
9
|
+
"#{template.source}\n" +
|
10
|
+
"pdf.render;"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Prawnto
|
2
|
+
module TemplateHandlers
|
3
|
+
class Dsl < Base
|
4
|
+
|
5
|
+
def compile(template)
|
6
|
+
"_prawnto_compile_setup(true);" +
|
7
|
+
"pdf = Prawn::Document.new(@prawnto_options[:prawn]);" +
|
8
|
+
"pdf.instance_eval do; #{template.source}\nend;" +
|
9
|
+
"pdf.render;"
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module Prawnto
|
2
|
+
module TemplateHandlers
|
3
|
+
class Raw < Base
|
4
|
+
|
5
|
+
def compile(template)
|
6
|
+
#TODO: what's up with filename here? not used is it?
|
7
|
+
source,filename = massage_template_source(template)
|
8
|
+
"_prawnto_compile_setup;" +
|
9
|
+
# (filename ? "@prawnto_options[:filename] = filename" : "") +
|
10
|
+
source
|
11
|
+
end
|
12
|
+
|
13
|
+
# attr_reader :run_environment
|
14
|
+
|
15
|
+
GENERATE_REGULAR_EXPRESSION = /^\s*Prawn\:\:Document\.generate(\(?)(.*?)(\,(.*))?(\s*\)?\s+do(.*?))$/m
|
16
|
+
RENDER_FILE_REGULAR_EXPRESSION = /(\w+)\.render_file\(?(.*?)\)?\s*$/
|
17
|
+
|
18
|
+
=begin
|
19
|
+
def render(template)
|
20
|
+
setup_run_environment
|
21
|
+
pull_prawnto_options
|
22
|
+
source,filename = massage_template_source(template)
|
23
|
+
@prawnto_options[:filename] = filename if filename
|
24
|
+
build_headers
|
25
|
+
@run_environment.instance_eval(source, template.filename, 0) #run in anonymous class
|
26
|
+
end
|
27
|
+
|
28
|
+
|
29
|
+
protected
|
30
|
+
|
31
|
+
def setup_run_environment
|
32
|
+
@run_environment = Object.new
|
33
|
+
end
|
34
|
+
|
35
|
+
=end
|
36
|
+
protected
|
37
|
+
def massage_template_source(template)
|
38
|
+
source = template.source.dup
|
39
|
+
variable_name = '_pdf'
|
40
|
+
filename = nil
|
41
|
+
|
42
|
+
source.gsub! /^(\s*?)(\$LOAD_PATH)/, '\1#\2'
|
43
|
+
source.gsub! /^(\s*?)(require\(?\s*['"]rubygems['"]\s*\)?\s*)$/, '\1#\2'
|
44
|
+
source.gsub! /^(\s*?)(require\(?\s*['"]prawn['"]\s*\)?\s*)$/, '\1#\2'
|
45
|
+
|
46
|
+
if (source =~ GENERATE_REGULAR_EXPRESSION)
|
47
|
+
filename = $2
|
48
|
+
source.sub! GENERATE_REGULAR_EXPRESSION, "#{variable_name} = Prawn::Document.new\\1\\4\\5"
|
49
|
+
elsif (source =~ RENDER_FILE_REGULAR_EXPRESSION)
|
50
|
+
variable_name = $1
|
51
|
+
filename = $2
|
52
|
+
source.sub! RENDER_FILE_REGULAR_EXPRESSION, '#\0'
|
53
|
+
end
|
54
|
+
source.gsub! /^(\s*)(class\s|def\s).*?\n\1end/m do |match|
|
55
|
+
eval "class <<@run_environment; #{match}; end;"
|
56
|
+
"\n" * match.count("\n")
|
57
|
+
end
|
58
|
+
source += "\n[#{variable_name}.render,#{filename}]\n"
|
59
|
+
source
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
data/lib/prawnto.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
module Prawnto
|
5
|
+
autoload :ActionControllerMixin, 'prawnto/action_controller_mixin'
|
6
|
+
autoload :ActionViewMixin, 'prawnto/action_view_mixin'
|
7
|
+
module TemplateHandlers
|
8
|
+
autoload :Base, 'prawnto/template_handlers/base'
|
9
|
+
autoload :Dsl, 'prawnto/template_handlers/dsl'
|
10
|
+
autoload :Raw, 'prawnto/template_handlers/raw'
|
11
|
+
end
|
12
|
+
|
13
|
+
module TemplateHandler
|
14
|
+
autoload :CompileSupport, 'prawnto/template_handler/compile_support'
|
15
|
+
end
|
16
|
+
|
17
|
+
class << self
|
18
|
+
def enable
|
19
|
+
ActionController::Base.send :include, Prawnto::ActionControllerMixin
|
20
|
+
ActionView::Base.send :include, Prawnto::ActionViewMixin
|
21
|
+
Mime::Type.register "application/pdf", :pdf
|
22
|
+
ActionView::Template.register_template_handler 'prawn', Prawnto::TemplateHandlers::Base
|
23
|
+
ActionView::Template.register_template_handler 'prawn_dsl', Prawnto::TemplateHandlers::Dsl
|
24
|
+
ActionView::Template.register_template_handler 'prawn_xxx', Prawnto::TemplateHandlers::Raw
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
data/rails/init.rb
ADDED
data/reference_pdfs
ADDED
File without changes
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'action_controller'
|
3
|
+
require 'action_controller/test_process'
|
4
|
+
require 'action_view'
|
5
|
+
|
6
|
+
require 'test/unit'
|
7
|
+
require File.dirname(__FILE__) + '/../lib/prawnto'
|
8
|
+
|
9
|
+
|
10
|
+
class ActionControllerTest < Test::Unit::TestCase
|
11
|
+
class PrawntoController < ActionController::Base
|
12
|
+
prawnto :inline=>true, :prawn=>{:page_orientation=>:landscape}
|
13
|
+
|
14
|
+
def test
|
15
|
+
prawnto :inline=>false, :prawn=>{:page_size=>'A4'}
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_inheritable_options
|
20
|
+
assert_equal({:page_orientation=>:landscape}, PrawntoController.read_inheritable_attribute(:prawn))
|
21
|
+
assert_equal({:inline=>true}, PrawntoController.read_inheritable_attribute(:prawnto))
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_computed_options
|
25
|
+
controller = PrawntoController.new
|
26
|
+
test_process(controller)
|
27
|
+
assert_equal({:inline=>false, :prawn=>{:page_orientation=>:landscape, :page_size=>'A4'}}, controller.send(:compute_prawnto_options))
|
28
|
+
end
|
29
|
+
|
30
|
+
protected
|
31
|
+
def test_process(controller, action = "test")
|
32
|
+
request = ActionController::TestRequest.new
|
33
|
+
request.action = action
|
34
|
+
controller.process(request, ActionController::TestResponse.new)
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require File.dirname(__FILE__) + '/template_handler_test_mocks'
|
4
|
+
require File.dirname(__FILE__) + '/../lib/prawnto'
|
5
|
+
#require File.dirname(__FILE__) + '/../init'
|
6
|
+
|
7
|
+
|
8
|
+
#TODO: ruby1.9: pull same testing scheme from Raw once we're on 1.9
|
9
|
+
class BaseTemplateHandlerTest < Test::Unit::TestCase
|
10
|
+
include TemplateHandlerTestMocks
|
11
|
+
|
12
|
+
def setup
|
13
|
+
@view = ActionView.new
|
14
|
+
@handler = Prawnto::TemplateHandlers::Base.new(@view)
|
15
|
+
@controller = @view.controller
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_headers_disposition_inline_and_filename
|
19
|
+
@controller.prawnto :filename=>'xxx.pdf', :inline=>true
|
20
|
+
@handler.pull_prawnto_options
|
21
|
+
@handler.set_disposition
|
22
|
+
assert_equal 'inline;filename=xxx.pdf', @view.headers['Content-Disposition']
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_headers_disposition_attachment_and_filename
|
26
|
+
@controller.prawnto :filename=>'xxx.pdf', :inline=>false
|
27
|
+
@handler.pull_prawnto_options
|
28
|
+
@handler.set_disposition
|
29
|
+
assert_equal 'attachment;filename=xxx.pdf', @view.headers['Content-Disposition']
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_headers_disposition_default
|
33
|
+
@handler.pull_prawnto_options
|
34
|
+
@handler.set_disposition
|
35
|
+
assert_equal 'inline', @view.headers['Content-Disposition']
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require File.dirname(__FILE__) + '/template_handler_test_mocks'
|
4
|
+
require File.dirname(__FILE__) + '/../lib/prawnto'
|
5
|
+
#require File.dirname(__FILE__) + '/../init'
|
6
|
+
|
7
|
+
|
8
|
+
#TODO: ruby1.9: pull same testing scheme from Raw once we're on 1.9
|
9
|
+
class DslTemplateHandlerTest < Test::Unit::TestCase
|
10
|
+
include TemplateHandlerTestMocks
|
11
|
+
|
12
|
+
def setup
|
13
|
+
@view = ActionView.new
|
14
|
+
@handler = Prawnto::TemplateHandlers::Dsl.new(@view)
|
15
|
+
@controller = @view.controller
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_prawnto_options_dsl_hash
|
19
|
+
@y = 3231; @x = 5322
|
20
|
+
@controller.prawnto :dsl=> {'x'=>:@x, :y=>'@y'}
|
21
|
+
@handler.pull_prawnto_options
|
22
|
+
source = @handler.build_source_to_establish_locals(Template.new(""))
|
23
|
+
|
24
|
+
assert_equal @x, eval(source + "\nx")
|
25
|
+
assert_equal @y, eval(source + "\ny")
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_prawnto_options_dsl_array
|
29
|
+
@y = 3231; @x = 5322
|
30
|
+
@controller.prawnto :dsl=> ['x', :@y]
|
31
|
+
@handler.pull_prawnto_options
|
32
|
+
source = @handler.build_source_to_establish_locals(Template.new(""))
|
33
|
+
|
34
|
+
assert_equal @x, eval(source + "\nx")
|
35
|
+
assert_equal @y, eval(source + "\ny")
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
end
|
40
|
+
|
@@ -0,0 +1,163 @@
|
|
1
|
+
# uncomment and edit below if you want to get off gem version
|
2
|
+
#$LOAD_PATH.unshift '~/cracklabs/vendor/gems/prawn-0.0.0.1/lib/' #to force picup of latest prawn (instead of stable gem)
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'action_controller'
|
6
|
+
require 'action_view'
|
7
|
+
|
8
|
+
require 'test/unit'
|
9
|
+
require File.dirname(__FILE__) + '/../lib/prawnto'
|
10
|
+
require File.dirname(__FILE__) + '/template_handler_test_mocks'
|
11
|
+
|
12
|
+
|
13
|
+
class RawTemplateHandlerTest < Test::Unit::TestCase
|
14
|
+
include TemplateHandlerTestMocks
|
15
|
+
class ::ApplicationHelper
|
16
|
+
end
|
17
|
+
|
18
|
+
def setup
|
19
|
+
@view = ActionView.new
|
20
|
+
@handler = Prawnto::TemplateHandlers::Raw.new(@view)
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
def test_massage_template_source_header_comments
|
25
|
+
expected_commented_lines = [0,2,3]
|
26
|
+
source = <<EOS
|
27
|
+
require 'prawn'
|
28
|
+
require 'hello'
|
29
|
+
require "rubygems"
|
30
|
+
$LOAD_PATH.unshift blah blah
|
31
|
+
LOAD_PATH.unshift blah blah
|
32
|
+
EOS
|
33
|
+
output_lines = @handler.send(:massage_template_source, Template.new(source)).split("\n")
|
34
|
+
output_lines.each_with_index do |line, i|
|
35
|
+
method = expected_commented_lines.include?(i) ? :assert_match : :assert_no_match
|
36
|
+
self.send method, /^\s*\#/, line
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_massage_template_source_generate
|
41
|
+
@handler.pull_prawnto_options
|
42
|
+
changed_lines = [0,2,3]
|
43
|
+
source = <<EOS
|
44
|
+
Prawn::Document.generate('hello.pdf') do |pdf|
|
45
|
+
end
|
46
|
+
EOS
|
47
|
+
output_lines = @handler.send(:massage_template_source, Template.new(source)).split("\n")
|
48
|
+
assert_match(/^\s*(\S+)\s*\=\s*Prawn\:\:Document\.new\(?\s*\)?\s*do\s*\|pdf\|/, output_lines.first)
|
49
|
+
variable = $1
|
50
|
+
assert_match(/^\s*\[(\S+)\.render\s*\,\s*\'hello\.pdf\'\s*\]\s*$/, output_lines.last)
|
51
|
+
assert_equal variable, $1
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_massage_template_source_new
|
55
|
+
@handler.pull_prawnto_options
|
56
|
+
unchanged_lines = [0,1,2]
|
57
|
+
source = <<EOS
|
58
|
+
x = Prawn::Document.new do |pdf|
|
59
|
+
text.blah blah blah
|
60
|
+
end
|
61
|
+
x.render_file('hello.pdf')
|
62
|
+
EOS
|
63
|
+
source_lines = source.split("\n")
|
64
|
+
output_lines = @handler.send(:massage_template_source, Template.new(source)).split("\n")
|
65
|
+
output_lines.each_with_index do |line, i|
|
66
|
+
method = unchanged_lines.include?(i) ? :assert_equal : :assert_not_equal
|
67
|
+
self.send method, source_lines[i], line
|
68
|
+
end
|
69
|
+
assert_match(/^\s*\#\s*x\.render\_file\(\'hello.pdf\'\)/, output_lines[3])
|
70
|
+
assert_match(/^\s*\[\s*x\.render\s*\,\s*\'hello\.pdf\'\s*\]\s*$/, output_lines.last)
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_massage_template_source_classes_methods
|
74
|
+
source = <<EOS
|
75
|
+
class Foo
|
76
|
+
def initialize
|
77
|
+
@foo = true
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def bar(*args)
|
82
|
+
if args[0]==true
|
83
|
+
z = false
|
84
|
+
end
|
85
|
+
end
|
86
|
+
EOS
|
87
|
+
@handler.send :setup_run_environment
|
88
|
+
output_lines = @handler.send(:massage_template_source, Template.new(source)).split("\n")
|
89
|
+
output_lines.pop
|
90
|
+
output_lines.each {|l| assert_match(/^\s*$/, l)}
|
91
|
+
assert @handler.run_environment.methods(false).include?('bar')
|
92
|
+
assert class <<@handler.run_environment; self; end.constants.include?('Foo')
|
93
|
+
end
|
94
|
+
|
95
|
+
CURRENT_PATH = Pathname('.').realpath
|
96
|
+
PRAWN_PATH = Pathname(Prawn::BASEDIR).realpath
|
97
|
+
REFERENCE_PATH = Pathname('reference_pdfs').realpath
|
98
|
+
INPUT_PATH = PRAWN_PATH + 'examples'
|
99
|
+
IGNORE_LIST = %w(table_bench ruport_formatter page_geometry)
|
100
|
+
INPUTS = INPUT_PATH.children.select {|p| p.extname==".rb" && !IGNORE_LIST.include?(p.basename('.rb').to_s)}
|
101
|
+
|
102
|
+
def self.ensure_reference_pdfs_are_recent
|
103
|
+
head_lines = (INPUT_PATH + "../.git/HEAD").read.split("\n")
|
104
|
+
head_hash = Hash[*head_lines.map {|line| line.split(':').map{|v| v.strip}}.flatten]
|
105
|
+
head_version = (INPUT_PATH + "../.git" + head_hash['ref'])
|
106
|
+
|
107
|
+
REFERENCE_PATH.mkpath
|
108
|
+
current_version = REFERENCE_PATH + 'HEAD'
|
109
|
+
if !current_version.exist? || current_version.read!=head_version.read
|
110
|
+
puts "\n!!!! reference pdfs are determined to be old-- repopulating...\n\n"
|
111
|
+
require 'fileutils'
|
112
|
+
FileUtils.instance_eval do
|
113
|
+
rm REFERENCE_PATH + '*', :force=>true
|
114
|
+
INPUTS.each do |path|
|
115
|
+
pre_brood = INPUT_PATH.children
|
116
|
+
cd INPUT_PATH
|
117
|
+
system("ruby #{path.basename}")
|
118
|
+
post_brood = INPUT_PATH.children
|
119
|
+
new_kids = post_brood - pre_brood
|
120
|
+
new_kids.each {|p| mv p, REFERENCE_PATH + p.basename}
|
121
|
+
cd CURRENT_PATH
|
122
|
+
end
|
123
|
+
cp head_version, current_version
|
124
|
+
end
|
125
|
+
else
|
126
|
+
puts "\n reference pdfs are current-- continuing...\n"
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
#TODO: ruby 1.9: uncomment below line when on 1.9
|
131
|
+
#ensure_reference_pdfs_are_recent
|
132
|
+
|
133
|
+
|
134
|
+
def assert_renders_correctly(name, path)
|
135
|
+
input_source = path.read
|
136
|
+
output_source = @handler.compile(Template.new(input_source))
|
137
|
+
value = @view.instance_eval output_source
|
138
|
+
reference = (REFERENCE_PATH + @view.prawnto_options[:filename]).read
|
139
|
+
|
140
|
+
message = "template: #{name}\n"
|
141
|
+
message += ">"*30 + " original template: " + ">"*20 + "\n"
|
142
|
+
message += input_source + "\n"*2
|
143
|
+
message += ">"*30 + " manipulated template: " + ">"*20 + "\n"
|
144
|
+
message += output_source + "\n" + "<"*60 + "\n"
|
145
|
+
|
146
|
+
assert_equal reference, value, message
|
147
|
+
end
|
148
|
+
|
149
|
+
#!!! Can't actually verify pdf equality until ruby 1.9
|
150
|
+
# (cuz hash orders are messed up otherwise and no other way to test equality at the moment)
|
151
|
+
INPUTS.each do |path|
|
152
|
+
name = path.basename('.rb')
|
153
|
+
define_method "test_template_should_render_correctly [template: #{name}] " do
|
154
|
+
# assert_renders_correctly name, path
|
155
|
+
assert true
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
|
162
|
+
end
|
163
|
+
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require File.dirname(__FILE__) + '/../lib/prawnto'
|
3
|
+
|
4
|
+
module TemplateHandlerTestMocks
|
5
|
+
|
6
|
+
class Template
|
7
|
+
attr_reader :source, :locals, :filename
|
8
|
+
|
9
|
+
def initialize(source, locals={})
|
10
|
+
@source = source
|
11
|
+
@locals = locals
|
12
|
+
@filename = "blah.pdf"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
class Response
|
18
|
+
def initialize
|
19
|
+
@headers = {}
|
20
|
+
end
|
21
|
+
|
22
|
+
def headers
|
23
|
+
@headers
|
24
|
+
end
|
25
|
+
|
26
|
+
def content_type=(value)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class Request
|
31
|
+
def env
|
32
|
+
{}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
class ActionController
|
37
|
+
|
38
|
+
include Prawnto::ActionController
|
39
|
+
|
40
|
+
def response
|
41
|
+
@response ||= Response.new
|
42
|
+
end
|
43
|
+
|
44
|
+
def request
|
45
|
+
@request ||= Request.new
|
46
|
+
end
|
47
|
+
|
48
|
+
def headers
|
49
|
+
response.headers
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class ActionView
|
54
|
+
def controller
|
55
|
+
@controller ||= ActionController.new
|
56
|
+
end
|
57
|
+
|
58
|
+
def response
|
59
|
+
controller.response
|
60
|
+
end
|
61
|
+
|
62
|
+
def request
|
63
|
+
controller.request
|
64
|
+
end
|
65
|
+
|
66
|
+
def headers
|
67
|
+
controller.headers
|
68
|
+
end
|
69
|
+
|
70
|
+
def prawnto_options
|
71
|
+
controller.get_instance_variable(:@prawnto_options)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
end
|
77
|
+
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: prawnto
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- smecsia
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2010-03-17 00:00:00 +03:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rails
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "2.1"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: prawn
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description:
|
36
|
+
email: smecsia@gmail.com
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README
|
43
|
+
files:
|
44
|
+
- reference_pdfs
|
45
|
+
- README
|
46
|
+
- Rakefile
|
47
|
+
- MIT-LICENSE
|
48
|
+
- lib/prawnto/action_view_mixin.rb
|
49
|
+
- lib/prawnto/action_controller_mixin.rb
|
50
|
+
- lib/prawnto/template_handler/compile_support.rb
|
51
|
+
- lib/prawnto/template_handlers/raw.rb
|
52
|
+
- lib/prawnto/template_handlers/dsl.rb
|
53
|
+
- lib/prawnto/template_handlers/base.rb
|
54
|
+
- lib/prawnto.rb
|
55
|
+
- test/base_template_handler_test.rb
|
56
|
+
- test/action_controller_test.rb
|
57
|
+
- test/dsl_template_handler_test.rb
|
58
|
+
- test/template_handler_test_mocks.rb
|
59
|
+
- test/raw_template_handler_test.rb
|
60
|
+
- rails/init.rb
|
61
|
+
has_rdoc: true
|
62
|
+
homepage:
|
63
|
+
licenses: []
|
64
|
+
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "0"
|
75
|
+
version:
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: "0"
|
81
|
+
version:
|
82
|
+
requirements: []
|
83
|
+
|
84
|
+
rubyforge_project:
|
85
|
+
rubygems_version: 1.3.5
|
86
|
+
signing_key:
|
87
|
+
specification_version: 3
|
88
|
+
summary: Prawnto rails plugin implemented as a gem (see prawnto)
|
89
|
+
test_files: []
|
90
|
+
|