Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

new release (compiled, .prawn_dsl, etc.)

templates are now compiled
changed dsl to template: .prawn_dsl
now leveraging :locals render option instead of :dsl option
works with Rails2.2 (finally)
tests are all broken
  • Loading branch information...
commit 47e507e71780b12c087d01b23ad24ba06fc5d5ea 1 parent 9425cc0
authored December 29, 2008
5  init.rb
... ...
@@ -1,6 +1,7 @@
1 1
 require 'prawnto'
2 2
 
3 3
 Mime::Type.register "application/pdf", :pdf
4  
-ActionView::Template.register_template_handler 'prawn', Prawnto::TemplateHandler::Base
5  
-ActionView::Template.register_template_handler 'prawnx', Prawnto::TemplateHandler::Raw  # experimental
  4
+ActionView::Template.register_template_handler 'prawn', Prawnto::TemplateHandlers::Base
  5
+ActionView::Template.register_template_handler 'prawn_dsl', Prawnto::TemplateHandlers::Dsl
  6
+ActionView::Template.register_template_handler 'prawn_xxx', Prawnto::TemplateHandlers::Raw  
6 7
 
11  lib/prawnto.rb
@@ -3,9 +3,12 @@
3 3
 
4 4
 require 'prawn'
5 5
 require 'prawnto/action_controller'
  6
+require 'prawnto/action_view'
6 7
 
7  
-require 'prawnto/template_handler/base'
8  
-require 'prawnto/template_handler/raw'
  8
+require 'prawnto/template_handler/compile_support'
  9
+
  10
+require 'prawnto/template_handlers/base'
  11
+#require 'prawnto/template_handlers/raw'
9 12
 
10 13
 # for now applying to all Controllers
11 14
 # however, could reduce footprint by letting user mixin (i.e. include) only into controllers that need it
@@ -16,5 +19,9 @@ class ActionController::Base
16 19
   include Prawnto::ActionController
17 20
 end
18 21
 
  22
+class ActionView::Base
  23
+  include Prawnto::ActionView
  24
+end
  25
+
19 26
 
20 27
 
13  lib/prawnto/action_view.rb
... ...
@@ -0,0 +1,13 @@
  1
+module Prawnto
  2
+  module ActionView
  3
+
  4
+  private
  5
+    def _prawnto_compile_setup(dsl_setup = false)
  6
+      compile_support = Prawnto::TemplateHandler::CompileSupport.new(controller)
  7
+      @prawnto_options = compile_support.options
  8
+      compile_support.dsl_eval_string if dsl_setup
  9
+    end
  10
+
  11
+  end
  12
+end
  13
+
74  lib/prawnto/template_handler/base.rb
... ...
@@ -1,74 +0,0 @@
1  
-module Prawnto
2  
-  module TemplateHandler
3  
-    class Base < ActionView::TemplateHandler
4  
-
5  
-      attr_reader :prawnto_options
6  
-
7  
-      # TODO: kept around from railspdf-- maybe not needed anymore? should check.
8  
-      def ie_request?
9  
-        @view.request.env['HTTP_USER_AGENT'] =~ /msie/i
10  
-      end
11  
-
12  
-      # TODO: kept around from railspdf-- maybe not needed anymore? should check.
13  
-      def set_pragma
14  
-        @view.headers['Pragma'] ||= ie_request? ? 'no-cache' : ''
15  
-      end
16  
-
17  
-      # TODO: kept around from railspdf-- maybe not needed anymore? should check.
18  
-      def set_cache_control
19  
-        @view.headers['Cache-Control'] ||= ie_request? ? 'no-cache, must-revalidate' : ''
20  
-      end
21  
-
22  
-      def set_content_type
23  
-        @view.response.content_type = Mime::PDF
24  
-      end
25  
-
26  
-      def set_disposition
27  
-        inline = @prawnto_options[:inline] ? 'inline' : 'attachment'
28  
-        filename = @prawnto_options[:filename] ? "filename=#{@prawnto_options[:filename]}" : nil
29  
-        @view.headers["Content-Disposition"] = [inline,filename].compact.join(';')
30  
-      end
31  
-
32  
-      def build_headers
33  
-        set_pragma
34  
-        set_cache_control
35  
-        set_content_type
36  
-        set_disposition
37  
-      end
38  
-
39  
-      def build_source_to_establish_locals(template)
40  
-        prawnto_locals = {}
41  
-        if dsl = @prawnto_options[:dsl]
42  
-          if dsl.kind_of?(Array)
43  
-            dsl.each {|v| v = v.to_s.gsub(/^@/,''); prawnto_locals[v]="@#{v}"}
44  
-          elsif dsl.kind_of?(Hash)
45  
-            prawnto_locals.merge!(dsl)
46  
-          end
47  
-        end
48  
-        prawnto_locals.merge!(template.locals)
49  
-        prawnto_locals.map {|k,v| "#{k} = #{v};"}.join("")
50  
-      end
51  
-
52  
-      def pull_prawnto_options
53  
-        @prawnto_options = @view.controller.send :compute_prawnto_options
54  
-      end
55  
-
56  
-      def render(template)
57  
-        pull_prawnto_options
58  
-        build_headers
59  
-
60  
-        source = build_source_to_establish_locals(template)
61  
-        if @prawnto_options[:dsl]
62  
-          source += "pdf.instance_eval do\n#{template.source}\nend"
63  
-        else
64  
-          source += "\n#{template.source}"
65  
-        end
66  
-
67  
-        pdf = Prawn::Document.new(@prawnto_options[:prawn])
68  
-        @view.instance_eval source, template.filename, 1
69  
-        pdf.render
70  
-      end
71  
-
72  
-    end
73  
-  end
74  
-end
71  lib/prawnto/template_handler/compile_support.rb
... ...
@@ -0,0 +1,71 @@
  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
+      # TODO: kept around from railspdf-- maybe not needed anymore? should check.
  33
+      def set_pragma
  34
+        @controller.headers['Pragma'] ||= ie_request? ? 'no-cache' : ''
  35
+      end
  36
+
  37
+      # TODO: kept around from railspdf-- maybe not needed anymore? should check.
  38
+      def set_cache_control
  39
+        @controller.headers['Cache-Control'] ||= ie_request? ? 'no-cache, must-revalidate' : ''
  40
+      end
  41
+
  42
+      def set_content_type
  43
+        @controller.response.content_type ||= Mime::PDF
  44
+      end
  45
+
  46
+      def set_disposition
  47
+        inline = options[:inline] ? 'inline' : 'attachment'
  48
+        filename = options[:filename] ? "filename=#{options[:filename]}" : nil
  49
+        @controller.headers["Content-Disposition"] = [inline,filename].compact.join(';')
  50
+      end
  51
+
  52
+      def dsl_eval_string
  53
+        dsl = options[:dsl] || {}
  54
+        x = if dsl.kind_of?(Array)
  55
+          dsl.map {|v| v.to_s}.map {|v| v = v[1..-1] if v[0,1]=="@"; "#{v}=@#{v};"}.join('')
  56
+        elsif dsl.kind_of?(Hash)
  57
+          dsl.map {|k,v| "#{k}=#{v};"}.join('')
  58
+        else
  59
+          ""
  60
+        end
  61
+        puts x
  62
+        x
  63
+      end
  64
+
  65
+    end
  66
+
  67
+  end
  68
+end
  69
+
  70
+
  71
+
18  lib/prawnto/template_handlers/base.rb
... ...
@@ -0,0 +1,18 @@
  1
+module Prawnto
  2
+  module TemplateHandlers
  3
+    class Base < ::ActionView::TemplateHandler
  4
+      include ::ActionView::TemplateHandlers::Compilable
  5
+      
  6
+      def compile(template)
  7
+        "puts 'base';" + 
  8
+        "logger.warn 'BASE';" + 
  9
+        "_prawnto_compile_setup;" +
  10
+        "pdf = Prawn::Document.new(@prawnto_options[:prawn]);" + 
  11
+        template.source +
  12
+        "pdf.render;"
  13
+      end
  14
+    end
  15
+  end
  16
+end
  17
+
  18
+
18  lib/prawnto/template_handlers/dsl.rb
... ...
@@ -0,0 +1,18 @@
  1
+module Prawnto
  2
+  module TemplateHandlers
  3
+    class Dsl < Base
  4
+      
  5
+      def compile(template)
  6
+        "logger.warn local_variables;" +
  7
+        "z = _prawnto_compile_setup(true);" +
  8
+        "logger.warn local_assigns;" + 
  9
+        "pdf = Prawn::Document.new(@prawnto_options[:prawn]);" + 
  10
+        "pdf.instance_eval do; #{template.source}\nend;" +
  11
+        "pdf.render;"
  12
+      end
  13
+
  14
+    end
  15
+  end
  16
+end
  17
+
  18
+
18  lib/prawnto/template_handler/raw.rb → lib/prawnto/template_handlers/raw.rb
... ...
@@ -1,13 +1,21 @@
1 1
 module Prawnto
2  
-  module TemplateHandler
  2
+  module TemplateHandlers
3 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
4 12
 
5  
-      attr_reader :run_environment
  13
+#      attr_reader :run_environment
6 14
 
7 15
       GENERATE_REGULAR_EXPRESSION = /^\s*Prawn\:\:Document\.generate(\(?)(.*?)(\,(.*))?(\s*\)?\s+do(.*?))$/m
8 16
       RENDER_FILE_REGULAR_EXPRESSION = /(\w+)\.render_file\(?(.*?)\)?\s*$/
9 17
 
10  
-
  18
+=begin
11 19
       def render(template)
12 20
         setup_run_environment
13 21
         pull_prawnto_options
@@ -24,8 +32,10 @@ def setup_run_environment
24 32
         @run_environment = Object.new
25 33
       end
26 34
 
  35
+=end
  36
+    protected
27 37
       def massage_template_source(template)
28  
-        source = template.source
  38
+        source = template.source.dup
29 39
         variable_name = '_pdf'
30 40
         filename = nil
31 41
         
25  test/base_template_handler_test.rb
... ...
@@ -1,7 +1,8 @@
  1
+require 'rubygems'
1 2
 require 'test/unit'
2 3
 require File.dirname(__FILE__) + '/template_handler_test_mocks'
3 4
 require File.dirname(__FILE__) + '/../lib/prawnto'
4  
-require File.dirname(__FILE__) + '/../init'
  5
+#require File.dirname(__FILE__) + '/../init'
5 6
 
6 7
 
7 8
 #TODO: ruby1.9: pull same testing scheme from Raw once we're on 1.9
@@ -10,30 +11,10 @@ class BaseTemplateHandlerTest < Test::Unit::TestCase
10 11
   
11 12
   def setup
12 13
     @view = ActionView.new
13  
-    @handler = Prawnto::TemplateHandler::Base.new(@view)
  14
+    @handler = Prawnto::TemplateHandlers::Base.new(@view)
14 15
     @controller = @view.controller
15 16
   end
16 17
 
17  
-  def test_prawnto_options_dsl_hash
18  
-    @y = 3231; @x = 5322
19  
-    @controller.prawnto :dsl=> {'x'=>:@x, :y=>'@y'}
20  
-    @handler.pull_prawnto_options
21  
-    source = @handler.build_source_to_establish_locals(Template.new(""))
22  
-
23  
-    assert_equal @x, eval(source + "\nx")
24  
-    assert_equal @y, eval(source + "\ny")
25  
-  end
26  
-
27  
-  def test_prawnto_options_dsl_array
28  
-    @y = 3231; @x = 5322
29  
-    @controller.prawnto :dsl=> ['x', :@y]
30  
-    @handler.pull_prawnto_options
31  
-    source = @handler.build_source_to_establish_locals(Template.new(""))
32  
-
33  
-    assert_equal @x, eval(source + "\nx")
34  
-    assert_equal @y, eval(source + "\ny")
35  
-  end
36  
-
37 18
   def test_headers_disposition_inline_and_filename
38 19
     @controller.prawnto :filename=>'xxx.pdf', :inline=>true
39 20
     @handler.pull_prawnto_options
40  test/dsl_template_handler_test.rb
... ...
@@ -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
+
2  test/raw_template_handler_test.rb
@@ -17,7 +17,7 @@ class ::ApplicationHelper
17 17
 
18 18
   def setup
19 19
     @view = ActionView.new
20  
-    @handler = Prawnto::TemplateHandler::Raw.new(@view)
  20
+    @handler = Prawnto::TemplateHandlers::Raw.new(@view)
21 21
   end
22 22
 
23 23
 
1  test/template_handler_test_mocks.rb
... ...
@@ -1,3 +1,4 @@
  1
+require 'rubygems'
1 2
 require File.dirname(__FILE__) + '/../lib/prawnto'
2 3
 
3 4
 module TemplateHandlerTestMocks

0 notes on commit 47e507e

Please sign in to comment.
Something went wrong with that request. Please try again.