Replaced rfpdf with prawn and prawnto. Start to convert pdf-views.
This commit is contained in:
parent
da309f03b0
commit
a6c7b04e33
165 changed files with 723 additions and 28123 deletions
27
vendor/plugins/prawnto/lib/prawnto.rb
vendored
Normal file
27
vendor/plugins/prawnto/lib/prawnto.rb
vendored
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
require 'action_controller'
|
||||
require 'action_view'
|
||||
|
||||
require 'prawn'
|
||||
require 'prawnto/action_controller'
|
||||
require 'prawnto/action_view'
|
||||
|
||||
require 'prawnto/template_handler/compile_support'
|
||||
|
||||
require 'prawnto/template_handlers/base'
|
||||
#require 'prawnto/template_handlers/raw'
|
||||
|
||||
# for now applying to all Controllers
|
||||
# however, could reduce footprint by letting user mixin (i.e. include) only into controllers that need it
|
||||
# but does it really matter performance wise to include in a controller that doesn't need it? doubtful-- depends how much of a hit the before_filter is i guess..
|
||||
#
|
||||
|
||||
class ActionController::Base
|
||||
include Prawnto::ActionController
|
||||
end
|
||||
|
||||
class ActionView::Base
|
||||
include Prawnto::ActionView
|
||||
end
|
||||
|
||||
|
||||
|
||||
45
vendor/plugins/prawnto/lib/prawnto/action_controller.rb
vendored
Normal file
45
vendor/plugins/prawnto/lib/prawnto/action_controller.rb
vendored
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
module Prawnto
|
||||
module ActionController
|
||||
|
||||
DEFAULT_PRAWNTO_OPTIONS = {:inline=>true}
|
||||
|
||||
def self.included(base)
|
||||
base.extend ClassMethods
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
def prawnto(options)
|
||||
prawn_options, prawnto_options = breakdown_prawnto_options options
|
||||
write_inheritable_hash(:prawn, prawn_options)
|
||||
write_inheritable_hash(:prawnto, prawnto_options)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def breakdown_prawnto_options(options)
|
||||
prawnto_options = options.dup
|
||||
prawn_options = (prawnto_options.delete(:prawn) || {}).dup
|
||||
[prawn_options, prawnto_options]
|
||||
end
|
||||
end
|
||||
|
||||
def prawnto(options)
|
||||
@prawnto_options ||= DEFAULT_PRAWNTO_OPTIONS.dup
|
||||
@prawnto_options.merge! options
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def compute_prawnto_options
|
||||
@prawnto_options ||= DEFAULT_PRAWNTO_OPTIONS.dup
|
||||
@prawnto_options[:prawn] ||= {}
|
||||
@prawnto_options[:prawn].merge!(self.class.read_inheritable_attribute(:prawn) || {}) {|k,o,n| o}
|
||||
@prawnto_options.merge!(self.class.read_inheritable_attribute(:prawnto) || {}) {|k,o,n| o}
|
||||
@prawnto_options
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
12
vendor/plugins/prawnto/lib/prawnto/action_view.rb
vendored
Normal file
12
vendor/plugins/prawnto/lib/prawnto/action_view.rb
vendored
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
module Prawnto
|
||||
module ActionView
|
||||
|
||||
private
|
||||
def _prawnto_compile_setup(dsl_setup = false)
|
||||
compile_support = Prawnto::TemplateHandler::CompileSupport.new(controller)
|
||||
@prawnto_options = compile_support.options
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
72
vendor/plugins/prawnto/lib/prawnto/template_handler/compile_support.rb
vendored
Normal file
72
vendor/plugins/prawnto/lib/prawnto/template_handler/compile_support.rb
vendored
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
module Prawnto
|
||||
module TemplateHandler
|
||||
|
||||
class CompileSupport
|
||||
extend ActiveSupport::Memoizable
|
||||
|
||||
attr_reader :options
|
||||
|
||||
def initialize(controller)
|
||||
@controller = controller
|
||||
@options = pull_options
|
||||
set_headers
|
||||
end
|
||||
|
||||
def pull_options
|
||||
@controller.send :compute_prawnto_options || {}
|
||||
end
|
||||
|
||||
def set_headers
|
||||
set_pragma
|
||||
set_cache_control
|
||||
set_content_type
|
||||
set_disposition
|
||||
end
|
||||
|
||||
# TODO: kept around from railspdf-- maybe not needed anymore? should check.
|
||||
def 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?
|
||||
@controller.request.env['SERVER_PROTOCOL'].downcase == "https"
|
||||
end
|
||||
memoize :ssl_request?
|
||||
|
||||
# TODO: kept around from railspdf-- maybe not needed anymore? should check.
|
||||
def set_pragma
|
||||
if ssl_request? && ie_request?
|
||||
@controller.headers['Pragma'] = 'public' # added to make ie ssl pdfs work (per naisayer)
|
||||
else
|
||||
@controller.headers['Pragma'] ||= ie_request? ? 'no-cache' : ''
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: kept around from railspdf-- maybe not needed anymore? should check.
|
||||
def set_cache_control
|
||||
if ssl_request? && ie_request?
|
||||
@controller.headers['Cache-Control'] = 'maxage=1' # added to make ie ssl pdfs work (per naisayer)
|
||||
else
|
||||
@controller.headers['Cache-Control'] ||= ie_request? ? 'no-cache, must-revalidate' : ''
|
||||
end
|
||||
end
|
||||
|
||||
def set_content_type
|
||||
@controller.response.content_type ||= Mime::PDF
|
||||
end
|
||||
|
||||
def set_disposition
|
||||
inline = options[:inline] ? 'inline' : 'attachment'
|
||||
filename = options[:filename] ? "filename=#{options[:filename]}" : nil
|
||||
@controller.headers["Content-Disposition"] = [inline,filename].compact.join(';')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
16
vendor/plugins/prawnto/lib/prawnto/template_handlers/base.rb
vendored
Normal file
16
vendor/plugins/prawnto/lib/prawnto/template_handlers/base.rb
vendored
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
module Prawnto
|
||||
module TemplateHandlers
|
||||
class Base < ::ActionView::TemplateHandler
|
||||
include ::ActionView::TemplateHandlers::Compilable
|
||||
|
||||
def compile(template)
|
||||
"_prawnto_compile_setup;" +
|
||||
"pdf = Prawn::Document.new(@prawnto_options[:prawn]);" +
|
||||
"#{template.source}\n" +
|
||||
"pdf.render;"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
16
vendor/plugins/prawnto/lib/prawnto/template_handlers/dsl.rb
vendored
Normal file
16
vendor/plugins/prawnto/lib/prawnto/template_handlers/dsl.rb
vendored
Normal file
|
|
@ -0,0 +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;"
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
64
vendor/plugins/prawnto/lib/prawnto/template_handlers/raw.rb
vendored
Normal file
64
vendor/plugins/prawnto/lib/prawnto/template_handlers/raw.rb
vendored
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue