diff --git a/lib/prawnto.rb b/lib/prawnto.rb index 3c71420..353d0e5 100644 --- a/lib/prawnto.rb +++ b/lib/prawnto.rb @@ -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 diff --git a/lib/prawnto/template_handler/compile_support.rb b/lib/prawnto/template_handler/compile_support.rb index 5f1a130..7b15cd5 100644 --- a/lib/prawnto/template_handler/compile_support.rb +++ b/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) @@ -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 diff --git a/lib/prawnto/template_handlers/base.rb b/lib/prawnto/template_handlers/base.rb index 5038c39..9691ca1 100644 --- a/lib/prawnto/template_handlers/base.rb +++ b/lib/prawnto/template_handlers/base.rb @@ -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 diff --git a/lib/prawnto/template_handlers/dsl.rb b/lib/prawnto/template_handlers/dsl.rb index f8458f3..6e1b89e 100644 --- a/lib/prawnto/template_handlers/dsl.rb +++ b/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 diff --git a/lib/prawnto/template_handlers/raw.rb b/lib/prawnto/template_handlers/raw.rb index ef2f7e0..3f23d62 100644 --- a/lib/prawnto/template_handlers/raw.rb +++ b/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