Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update prawnto to support Rails 3.2.
Bump version to 0.0.5.
  • Loading branch information
mirasrael committed Feb 24, 2012
1 parent b160c51 commit e2bab49
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 82 deletions.
1 change: 0 additions & 1 deletion lib/prawnto.rb
Expand Up @@ -2,7 +2,6 @@
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))

module Prawnto
VERSION='0.0.4'
autoload :ActionControllerMixin, 'prawnto/action_controller_mixin'
autoload :ActionViewMixin, 'prawnto/action_view_mixin'
module TemplateHandlers
Expand Down
11 changes: 4 additions & 7 deletions lib/prawnto/template_handler/compile_support.rb
@@ -1,9 +1,6 @@
module Prawnto
module TemplateHandler

class CompileSupport
extend ActiveSupport::Memoizable

attr_reader :options

def initialize(controller)
Expand All @@ -25,16 +22,16 @@ def set_headers

# TODO: kept around from railspdf-- maybe not needed anymore? should check.
def ie_request?
@controller.request.env['HTTP_USER_AGENT'] =~ /msie/i
return @ie_request if instance_variable_defined?(:@ie_request)
@ie_request = @controller.request.env['HTTP_USER_AGENT'] =~ /msie/i
end
memoize :ie_request?

# added to make ie happy with ssl pdf's (per naisayer)
def ssl_request?
return @ssl_request if instance_variable_defined?(:@ssl_request)
protocol = @controller.request.env['SERVER_PROTOCOL']
protocol && protocol.downcase == "https"
@ssl_request = protocol && protocol.downcase == "https"
end
memoize :ssl_request?

# TODO: kept around from railspdf-- maybe not needed anymore? should check.
def set_pragma
Expand Down
12 changes: 9 additions & 3 deletions lib/prawnto/template_handlers/base.rb
Expand Up @@ -2,13 +2,19 @@ module Prawnto
module TemplateHandlers
class Base < (::Rails::VERSION::MAJOR < 3 ? ::ActionView::TemplateHandler : ::Object)
include ::ActionView::TemplateHandlers::Compilable if ::Rails::VERSION::MAJOR < 3
def compile(template)

def self.call(template)
"_prawnto_compile_setup;" +
"pdf = Prawn::Document.new(@prawnto_options[:prawn]);" +
"pdf = Prawn::Document.new(@prawnto_options[:prawn]);" +
"#{template.source}\n" +
"pdf.render;"
end

if ::Rails::VERSION::MAJOR < 3 || (::Rails::VERSION::MAJOR == 3 && ::Rails::VERSION::MINOR < 1)
def compile(template)
self.class.call(template)
end
end
end
end
end
Expand Down
16 changes: 9 additions & 7 deletions lib/prawnto/template_handlers/dsl.rb
@@ -1,14 +1,16 @@
module Prawnto
module TemplateHandlers
class Dsl < Base

def compile(template)
"_prawnto_compile_setup(true);" +
"pdf = Prawn::Document.new(@prawnto_options[:prawn]);" +
"pdf.instance_eval do; #{template.source}\nend;" +
"pdf.render;"
def self.call(template)
<<-RUBY
_prawnto_compile_setup(true)
pdf = Prawn::Document.new(@prawnto_options[:prawn])
pdf.instance_eval do
#{template.source}
end
pdf.render
RUBY
end

end
end
end
Expand Down
127 changes: 63 additions & 64 deletions lib/prawnto/template_handlers/raw.rb
@@ -1,64 +1,63 @@
module Prawnto
module TemplateHandlers
class Raw < Base

def compile(template)
#TODO: what's up with filename here? not used is it?
source,filename = massage_template_source(template)
"_prawnto_compile_setup;" +
# (filename ? "@prawnto_options[:filename] = filename" : "") +
source
end

# attr_reader :run_environment

GENERATE_REGULAR_EXPRESSION = /^\s*Prawn\:\:Document\.generate(\(?)(.*?)(\,(.*))?(\s*\)?\s+do(.*?))$/m
RENDER_FILE_REGULAR_EXPRESSION = /(\w+)\.render_file\(?(.*?)\)?\s*$/

=begin
def render(template)
setup_run_environment
pull_prawnto_options
source,filename = massage_template_source(template)
@prawnto_options[:filename] = filename if filename
build_headers
@run_environment.instance_eval(source, template.filename, 0) #run in anonymous class
end
protected
def setup_run_environment
@run_environment = Object.new
end
=end
protected
def massage_template_source(template)
source = template.source.dup
variable_name = '_pdf'
filename = nil

source.gsub! /^(\s*?)(\$LOAD_PATH)/, '\1#\2'
source.gsub! /^(\s*?)(require\(?\s*['"]rubygems['"]\s*\)?\s*)$/, '\1#\2'
source.gsub! /^(\s*?)(require\(?\s*['"]prawn['"]\s*\)?\s*)$/, '\1#\2'

if (source =~ GENERATE_REGULAR_EXPRESSION)
filename = $2
source.sub! GENERATE_REGULAR_EXPRESSION, "#{variable_name} = Prawn::Document.new\\1\\4\\5"
elsif (source =~ RENDER_FILE_REGULAR_EXPRESSION)
variable_name = $1
filename = $2
source.sub! RENDER_FILE_REGULAR_EXPRESSION, '#\0'
end
source.gsub! /^(\s*)(class\s|def\s).*?\n\1end/m do |match|
eval "class <<@run_environment; #{match}; end;"
"\n" * match.count("\n")
end
source += "\n[#{variable_name}.render,#{filename}]\n"
source
end

end
end
end
module Prawnto
module TemplateHandlers
class Raw < Base
def self.call(template)
#TODO: what's up with filename here? not used is it?
source,filename = massage_template_source(template)
"_prawnto_compile_setup;" +
# (filename ? "@prawnto_options[:filename] = filename" : "") +
source
end

# attr_reader :run_environment

GENERATE_REGULAR_EXPRESSION = /^\s*Prawn\:\:Document\.generate(\(?)(.*?)(\,(.*))?(\s*\)?\s+do(.*?))$/m
RENDER_FILE_REGULAR_EXPRESSION = /(\w+)\.render_file\(?(.*?)\)?\s*$/

=begin
def render(template)
setup_run_environment
pull_prawnto_options
source,filename = massage_template_source(template)
@prawnto_options[:filename] = filename if filename
build_headers
@run_environment.instance_eval(source, template.filename, 0) #run in anonymous class
end
protected
def setup_run_environment
@run_environment = Object.new
end
=end
protected
def self.massage_template_source(template)
source = template.source.dup
variable_name = '_pdf'
filename = nil

source.gsub! /^(\s*?)(\$LOAD_PATH)/, '\1#\2'
source.gsub! /^(\s*?)(require\(?\s*['"]rubygems['"]\s*\)?\s*)$/, '\1#\2'
source.gsub! /^(\s*?)(require\(?\s*['"]prawn['"]\s*\)?\s*)$/, '\1#\2'

if (source =~ GENERATE_REGULAR_EXPRESSION)
filename = $2
source.sub! GENERATE_REGULAR_EXPRESSION, "#{variable_name} = Prawn::Document.new\\1\\4\\5"
elsif (source =~ RENDER_FILE_REGULAR_EXPRESSION)
variable_name = $1
filename = $2
source.sub! RENDER_FILE_REGULAR_EXPRESSION, '#\0'
end
source.gsub! /^(\s*)(class\s|def\s).*?\n\1end/m do |match|
eval "class <<@run_environment; #{match}; end;"
"\n" * match.count("\n")
end
source += "\n[#{variable_name}.render,#{filename}]\n"
source
end

end
end
end

0 comments on commit e2bab49

Please sign in to comment.