erbse 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGES.txt +828 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.md +51 -0
  5. data/benchmark/Makefile +6 -0
  6. data/benchmark/bench.rb +313 -0
  7. data/benchmark/bench_context.yaml +141 -0
  8. data/benchmark/templates/_footer.html +4 -0
  9. data/benchmark/templates/_header.html +52 -0
  10. data/benchmark/templates/bench_erb.rhtml +29 -0
  11. data/benchmark/templates/bench_erubis.rhtml +29 -0
  12. data/benchmark/templates/bench_eruby.rhtml +29 -0
  13. data/bin/erubis +10 -0
  14. data/erbse.gemspec +24 -0
  15. data/lib/erbse.rb +52 -0
  16. data/lib/erbse/context.rb +83 -0
  17. data/lib/erbse/converter.rb +357 -0
  18. data/lib/erbse/engine.rb +114 -0
  19. data/lib/erbse/engine/ec.rb +117 -0
  20. data/lib/erbse/engine/ecpp.rb +113 -0
  21. data/lib/erbse/engine/ejava.rb +110 -0
  22. data/lib/erbse/engine/ejavascript.rb +119 -0
  23. data/lib/erbse/engine/enhanced.rb +126 -0
  24. data/lib/erbse/engine/eperl.rb +95 -0
  25. data/lib/erbse/engine/ephp.rb +99 -0
  26. data/lib/erbse/engine/eruby.rb +119 -0
  27. data/lib/erbse/engine/escheme.rb +114 -0
  28. data/lib/erbse/engine/optimized.rb +127 -0
  29. data/lib/erbse/enhancer.rb +723 -0
  30. data/lib/erbse/error.rb +23 -0
  31. data/lib/erbse/evaluator.rb +88 -0
  32. data/lib/erbse/generator.rb +85 -0
  33. data/lib/erbse/helper.rb +47 -0
  34. data/lib/erbse/helpers/rails_form_helper.rb +197 -0
  35. data/lib/erbse/helpers/rails_helper.rb +353 -0
  36. data/lib/erbse/main.rb +516 -0
  37. data/lib/erbse/preprocessing.rb +58 -0
  38. data/lib/erbse/tiny.rb +144 -0
  39. data/lib/erbse/util.rb +22 -0
  40. data/lib/erbse/version.rb +3 -0
  41. data/test/assert-text-equal.rb +44 -0
  42. data/test/data/users-guide/Example.ejava +55 -0
  43. data/test/data/users-guide/array_example.result +9 -0
  44. data/test/data/users-guide/arraybuffer_example.result +9 -0
  45. data/test/data/users-guide/bipattern-example.rhtml +4 -0
  46. data/test/data/users-guide/bipattern_example.result +6 -0
  47. data/test/data/users-guide/bufvar-example.rb +10 -0
  48. data/test/data/users-guide/bufvar-example.result +17 -0
  49. data/test/data/users-guide/context.rb +6 -0
  50. data/test/data/users-guide/context.yaml +8 -0
  51. data/test/data/users-guide/def_method.rb +14 -0
  52. data/test/data/users-guide/def_method.result +3 -0
  53. data/test/data/users-guide/escape_example.result +9 -0
  54. data/test/data/users-guide/example.ec +27 -0
  55. data/test/data/users-guide/example.ecpp +30 -0
  56. data/test/data/users-guide/example.ejs +20 -0
  57. data/test/data/users-guide/example.eperl +18 -0
  58. data/test/data/users-guide/example.ephp +18 -0
  59. data/test/data/users-guide/example.eruby +6 -0
  60. data/test/data/users-guide/example.escheme +28 -0
  61. data/test/data/users-guide/example1.eruby +6 -0
  62. data/test/data/users-guide/example1.rb +17 -0
  63. data/test/data/users-guide/example1.result +16 -0
  64. data/test/data/users-guide/example10.rb +4 -0
  65. data/test/data/users-guide/example10.result +17 -0
  66. data/test/data/users-guide/example10.xhtml +14 -0
  67. data/test/data/users-guide/example10_x.result +17 -0
  68. data/test/data/users-guide/example11.php +20 -0
  69. data/test/data/users-guide/example11.result +23 -0
  70. data/test/data/users-guide/example11.rhtml +21 -0
  71. data/test/data/users-guide/example11_C.result +10 -0
  72. data/test/data/users-guide/example11_N.result +16 -0
  73. data/test/data/users-guide/example11_U.result +16 -0
  74. data/test/data/users-guide/example11_php.result +15 -0
  75. data/test/data/users-guide/example1_x.result +9 -0
  76. data/test/data/users-guide/example2.eruby +7 -0
  77. data/test/data/users-guide/example2.rb +10 -0
  78. data/test/data/users-guide/example2.result +27 -0
  79. data/test/data/users-guide/example2_trim.result +10 -0
  80. data/test/data/users-guide/example2_x.result +10 -0
  81. data/test/data/users-guide/example3.eruby +6 -0
  82. data/test/data/users-guide/example3.rb +10 -0
  83. data/test/data/users-guide/example31.result +22 -0
  84. data/test/data/users-guide/example32.result +4 -0
  85. data/test/data/users-guide/example3_e.result +8 -0
  86. data/test/data/users-guide/example4.eruby +3 -0
  87. data/test/data/users-guide/example4.rb +11 -0
  88. data/test/data/users-guide/example4.result +10 -0
  89. data/test/data/users-guide/example4_x.result +5 -0
  90. data/test/data/users-guide/example5.eruby +6 -0
  91. data/test/data/users-guide/example5.rb +16 -0
  92. data/test/data/users-guide/example5.result +7 -0
  93. data/test/data/users-guide/example6.rb +12 -0
  94. data/test/data/users-guide/example6.result +7 -0
  95. data/test/data/users-guide/example7.eruby +8 -0
  96. data/test/data/users-guide/example71.result +13 -0
  97. data/test/data/users-guide/example72.result +13 -0
  98. data/test/data/users-guide/example8.eruby +6 -0
  99. data/test/data/users-guide/example8_ruby.result +7 -0
  100. data/test/data/users-guide/example8_yaml.result +7 -0
  101. data/test/data/users-guide/example9.eruby +3 -0
  102. data/test/data/users-guide/example9.rb +8 -0
  103. data/test/data/users-guide/example9.result +9 -0
  104. data/test/data/users-guide/example91.result +5 -0
  105. data/test/data/users-guide/example92.result +4 -0
  106. data/test/data/users-guide/example_c.result +32 -0
  107. data/test/data/users-guide/example_java.result +56 -0
  108. data/test/data/users-guide/example_js.result +22 -0
  109. data/test/data/users-guide/example_perl.result +20 -0
  110. data/test/data/users-guide/example_php.result +19 -0
  111. data/test/data/users-guide/example_scheme.result +30 -0
  112. data/test/data/users-guide/example_scheme_display.result +29 -0
  113. data/test/data/users-guide/fasteruby-example.rb +8 -0
  114. data/test/data/users-guide/fasteruby-example.result +18 -0
  115. data/test/data/users-guide/fasteruby.rb +11 -0
  116. data/test/data/users-guide/fasteruby.result +38 -0
  117. data/test/data/users-guide/fasteruby.rhtml +15 -0
  118. data/test/data/users-guide/headerfooter-example.eruby +9 -0
  119. data/test/data/users-guide/headerfooter-example2.rb +8 -0
  120. data/test/data/users-guide/headerfooter-example2.rhtml +10 -0
  121. data/test/data/users-guide/headerfooter_example.result +11 -0
  122. data/test/data/users-guide/headerfooter_example2.result +13 -0
  123. data/test/data/users-guide/interpolation_example.result +9 -0
  124. data/test/data/users-guide/main_program1.rb +8 -0
  125. data/test/data/users-guide/main_program1.result +6 -0
  126. data/test/data/users-guide/main_program2.rb +8 -0
  127. data/test/data/users-guide/main_program2.result +6 -0
  128. data/test/data/users-guide/nocode-example.eruby +14 -0
  129. data/test/data/users-guide/nocode-php.result +20 -0
  130. data/test/data/users-guide/nocode_example.result +15 -0
  131. data/test/data/users-guide/normal-eruby-test.eruby +9 -0
  132. data/test/data/users-guide/normal_eruby_test.result +11 -0
  133. data/test/data/users-guide/notext-example.eruby +14 -0
  134. data/test/data/users-guide/notext-example.php +19 -0
  135. data/test/data/users-guide/notext-php.result +20 -0
  136. data/test/data/users-guide/notext_example.result +16 -0
  137. data/test/data/users-guide/percentline-example.rhtml +6 -0
  138. data/test/data/users-guide/percentline_example.result +9 -0
  139. data/test/data/users-guide/prefixedline-example.rb +9 -0
  140. data/test/data/users-guide/prefixedline-example.rhtml +6 -0
  141. data/test/data/users-guide/prefixedline_example.result +9 -0
  142. data/test/data/users-guide/printenable_example.result +4 -0
  143. data/test/data/users-guide/printenabled-example.eruby +3 -0
  144. data/test/data/users-guide/printenabled-example.rb +8 -0
  145. data/test/data/users-guide/printstatement_example.result +8 -0
  146. data/test/data/users-guide/simplify_example.result +9 -0
  147. data/test/data/users-guide/stderr.log +3 -0
  148. data/test/data/users-guide/stdout_exmple.result +9 -0
  149. data/test/data/users-guide/stringbuffer_example.result +9 -0
  150. data/test/data/users-guide/tail_260.result +4 -0
  151. data/test/data/users-guide/tailnewline.rhtml +3 -0
  152. data/test/data/users-guide/template1.rhtml +4 -0
  153. data/test/data/users-guide/template2.rhtml +4 -0
  154. data/test/test-engines.rb +425 -0
  155. data/test/test-enhancers.rb +646 -0
  156. data/test/test-erubis.rb +887 -0
  157. data/test/test-index-cgi.rb +191 -0
  158. data/test/test-main.rb +752 -0
  159. data/test/test-users-guide.rb +73 -0
  160. data/test/test.rb +45 -0
  161. data/test/testutil.rb +111 -0
  162. metadata +233 -0
@@ -0,0 +1,23 @@
1
+ ##
2
+ ## $Release: 2.7.0 $
3
+ ## copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
4
+ ##
5
+
6
+ module Erubis
7
+
8
+
9
+ ##
10
+ ## base error class
11
+ ##
12
+ class ErubisError < StandardError
13
+ end
14
+
15
+
16
+ ##
17
+ ## raised when method or function is not supported
18
+ ##
19
+ class NotSupportedError < ErubisError
20
+ end
21
+
22
+
23
+ end
@@ -0,0 +1,88 @@
1
+ ##
2
+ ## $Release: 2.7.0 $
3
+ ## copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
4
+ ##
5
+
6
+ require 'erubis/error'
7
+ require 'erubis/context'
8
+
9
+
10
+ module Erubis
11
+
12
+ EMPTY_BINDING = binding()
13
+
14
+
15
+ ##
16
+ ## evaluate code
17
+ ##
18
+ module Evaluator
19
+
20
+ def self.supported_properties # :nodoc:
21
+ return []
22
+ end
23
+
24
+ attr_accessor :src, :filename
25
+
26
+ def init_evaluator(properties)
27
+ @filename = properties[:filename]
28
+ end
29
+
30
+ def result(*args)
31
+ raise NotSupportedError.new("evaluation of code except Ruby is not supported.")
32
+ end
33
+
34
+ def evaluate(*args)
35
+ raise NotSupportedError.new("evaluation of code except Ruby is not supported.")
36
+ end
37
+
38
+ end
39
+
40
+
41
+ ##
42
+ ## evaluator for Ruby
43
+ ##
44
+ module RubyEvaluator
45
+ include Evaluator
46
+
47
+ def self.supported_properties # :nodoc:
48
+ list = Evaluator.supported_properties
49
+ return list
50
+ end
51
+
52
+ ## eval(@src) with binding object
53
+ def result(_binding_or_hash=TOPLEVEL_BINDING)
54
+ _arg = _binding_or_hash
55
+ if _arg.is_a?(Hash)
56
+ _b = binding()
57
+ eval _arg.collect{|k,v| "#{k} = _arg[#{k.inspect}]; "}.join, _b
58
+ elsif _arg.is_a?(Binding)
59
+ _b = _arg
60
+ elsif _arg.nil?
61
+ _b = binding()
62
+ else
63
+ raise ArgumentError.new("#{self.class.name}#result(): argument should be Binding or Hash but passed #{_arg.class.name} object.")
64
+ end
65
+ return eval(@src, _b, (@filename || '(erubis'))
66
+ end
67
+
68
+ ## invoke context.instance_eval(@src)
69
+ def evaluate(_context=Context.new)
70
+ _context = Context.new(_context) if _context.is_a?(Hash)
71
+ #return _context.instance_eval(@src, @filename || '(erubis)')
72
+ #@_proc ||= eval("proc { #{@src} }", Erubis::EMPTY_BINDING, @filename || '(erubis)')
73
+ @_proc ||= eval("proc { #{@src} }", binding(), @filename || '(erubis)')
74
+ return _context.instance_eval(&@_proc)
75
+ end
76
+
77
+ ## if object is an Class or Module then define instance method to it,
78
+ ## else define singleton method to it.
79
+ def def_method(object, method_name, filename=nil)
80
+ m = object.is_a?(Module) ? :module_eval : :instance_eval
81
+ object.__send__(m, "def #{method_name}; #{@src}; end", filename || @filename || '(erubis)')
82
+ end
83
+
84
+
85
+ end
86
+
87
+
88
+ end
@@ -0,0 +1,85 @@
1
+ ##
2
+ ## $Release: 2.7.0 $
3
+ ## copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
4
+ ##
5
+
6
+ require 'erubis/util'
7
+
8
+ module Erubis
9
+
10
+
11
+ ##
12
+ ## code generator, called by Converter module
13
+ ##
14
+ module Generator
15
+
16
+ def self.supported_properties() # :nodoc:
17
+ return [
18
+ [:escapefunc, nil, "escape function name"],
19
+ ]
20
+ end
21
+
22
+ attr_accessor :escapefunc
23
+
24
+ def init_generator(properties={})
25
+ @escapefunc = properties[:escapefunc]
26
+ end
27
+
28
+
29
+ ## (abstract) escape text string
30
+ ##
31
+ ## ex.
32
+ ## def escape_text(text)
33
+ ## return text.dump
34
+ ## # or return "'" + text.gsub(/['\\]/, '\\\\\&') + "'"
35
+ ## end
36
+ def escape_text(text)
37
+ not_implemented
38
+ end
39
+
40
+ ## return escaped expression code (ex. 'h(...)' or 'htmlspecialchars(...)')
41
+ def escaped_expr(code)
42
+ code.strip!
43
+ return "#{@escapefunc}(#{code})"
44
+ end
45
+
46
+ ## (abstract) add @preamble to src
47
+ def add_preamble(src)
48
+ not_implemented
49
+ end
50
+
51
+ ## (abstract) add text string to src
52
+ def add_text(src, text)
53
+ not_implemented
54
+ end
55
+
56
+ ## (abstract) add statement code to src
57
+ def add_stmt(src, code)
58
+ not_implemented
59
+ end
60
+
61
+ ## (abstract) add expression literal code to src. this is called by add_expr().
62
+ def add_expr_literal(src, code)
63
+ not_implemented
64
+ end
65
+
66
+ ## (abstract) add escaped expression code to src. this is called by add_expr().
67
+ def add_expr_escaped(src, code)
68
+ not_implemented
69
+ end
70
+
71
+ ## (abstract) add expression code to src for debug. this is called by add_expr().
72
+ def add_expr_debug(src, code)
73
+ not_implemented
74
+ end
75
+
76
+ ## (abstract) add @postamble to src
77
+ def add_postamble(src)
78
+ not_implemented
79
+ end
80
+
81
+
82
+ end
83
+
84
+
85
+ end
@@ -0,0 +1,47 @@
1
+ ##
2
+ ## $Release: 2.7.0 $
3
+ ## copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
4
+ ##
5
+
6
+
7
+ module Erubis
8
+
9
+ ##
10
+ ## helper for xml
11
+ ##
12
+ module XmlHelper
13
+
14
+ module_function
15
+
16
+ ESCAPE_TABLE = {
17
+ '&' => '&amp;',
18
+ '<' => '&lt;',
19
+ '>' => '&gt;',
20
+ '"' => '&quot;',
21
+ "'" => '&#039;',
22
+ }
23
+
24
+ def escape_xml(value)
25
+ value.to_s.gsub(/[&<>"]/) { |s| ESCAPE_TABLE[s] } # or /[&<>"']/
26
+ #value.to_s.gsub(/[&<>"]/) { ESCAPE_TABLE[$&] }
27
+ end
28
+
29
+ def escape_xml2(value)
30
+ return value.to_s.gsub(/\&/,'&amp;').gsub(/</,'&lt;').gsub(/>/,'&gt;').gsub(/"/,'&quot;')
31
+ end
32
+
33
+ alias h escape_xml
34
+ alias html_escape escape_xml
35
+
36
+ def url_encode(str)
37
+ return str.gsub(/[^-_.a-zA-Z0-9]+/) { |s|
38
+ s.unpack('C*').collect { |i| "%%%02X" % i }.join
39
+ }
40
+ end
41
+
42
+ alias u url_encode
43
+
44
+ end
45
+
46
+
47
+ end
@@ -0,0 +1,197 @@
1
+ ###
2
+ ### $Release: 2.7.0 $
3
+ ### copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
4
+ ###
5
+
6
+
7
+ module Erubis
8
+ module Helpers
9
+ module RailsFormHelper
10
+ end
11
+ end
12
+ end
13
+
14
+
15
+ module Erubis::Helpers::RailsFormHelper
16
+
17
+
18
+ if ActionPack::VERSION::MAJOR == 1 ### Rails 1.X
19
+ def pp_template_filename(basename)
20
+ return "#{RAILS_ROOT}/app/views/#{controller.controller_name}/#{basename}.rhtml"
21
+ end
22
+ else ### Rails 2.X
23
+ def pp_template_filename(basename)
24
+ fname = "#{RAILS_ROOT}/app/views/#{controller.controller_name}/#{basename}.html.erb"
25
+ return fname if test(?f, fname)
26
+ return "#{RAILS_ROOT}/app/views/#{controller.controller_name}/#{basename}.rhtml"
27
+ end
28
+ end
29
+
30
+ def pp_render_partial(basename)
31
+ basename = "_#{basename}" unless basename[0] == ?_
32
+ filename = pp_template_filename(basename)
33
+ preprocessor = _create_preprocessor(File.read(filename))
34
+ return preprocessor.evaluate(_preprocessing_context_object())
35
+ end
36
+
37
+ def pp_error_on(object_name, method)
38
+ s = ''
39
+ s << "<% _stag, _etag = _pp_error_tags(@#{object_name}.errors.on('#{method}')) %>"
40
+ s << "<%= _stag %>"
41
+ s << yield(object_name, method)
42
+ s << "<%= _etag %>"
43
+ return s
44
+ end
45
+
46
+ def _pp_error_tags(value)
47
+ return value ? ['<div class="fieldWithErrors">', '</div>'] : ['', '']
48
+ end
49
+
50
+ def _pp_remove_error_div(s)
51
+ s.sub!(/\A<div class="fieldWithErrors">(.*)<\/div>\z/, '\1')
52
+ return s
53
+ end
54
+
55
+ def pp_tag_helper(helper, object_name, method, options={})
56
+ if object_name.is_a?(ActionView::Helpers::FormHelper)
57
+ object_name = object_name.object_name
58
+ end
59
+ unless options.key?(:value) || options.key?('value')
60
+ options['value'] = _?("h @#{object_name}.#{method}")
61
+ end
62
+ #$stderr.puts "*** debug: pp_tag_helper(): options=#{options.inspect}"
63
+ return pp_error_on(object_name, method) {
64
+ s = __send__(helper, object_name, method, options)
65
+ _pp_remove_error_div(s)
66
+ }
67
+ end
68
+
69
+ def pp_form_tag(url_for_options={}, options={}, *parameters_for_url, &block)
70
+ return form_tag(url_for_options, options, *parameters_for_url, &block)
71
+ end
72
+
73
+ #--
74
+ #def pp_form_for(object_name, *args, &block)
75
+ # return form_for(object_name, *args, &block)
76
+ #end
77
+ #++
78
+
79
+ def pp_text_field(object_name, method, options={})
80
+ return pp_tag_helper(:text_field, object_name, method, options)
81
+ end
82
+
83
+ def pp_password_field(object_name, method, options={})
84
+ return pp_tag_helper(:password_field, object_name, method, options)
85
+ end
86
+
87
+ def pp_hidden_field(object_name, method, options={})
88
+ return pp_tag_helper(:hidden_field, object_name, method, options)
89
+ end
90
+
91
+ def pp_file_field(object_name, method, options={})
92
+ return pp_tag_helper(:file_field, object_name, method, options)
93
+ end
94
+
95
+ def pp_text_area(object_name, method, options={})
96
+ return pp_tag_helper(:text_area, object_name, method, options)
97
+ end
98
+
99
+ def pp_check_box(object_name, method, options={}, checked_value="1", unchecked_value="0")
100
+ s = check_box(object_name, method, options, checked_value, unchecked_value)
101
+ s.sub!(/\schecked=\"checked\"/, '')
102
+ s.sub!(/type="checkbox"/, "\\&<%= _pp_check_box_checked?(@#{object_name}.#{method}, #{checked_value.inspect}) ? ' checked=\"checked\"' : '' %>")
103
+ return pp_error_on(object_name, method) { _pp_remove_error_div(s) }
104
+ end
105
+
106
+ def _pp_check_box_checked?(value, checked_value)
107
+ return ActionView::Helpers::InstanceTag::check_box_checked?(value, checked_value)
108
+ end
109
+
110
+ def pp_radio_button(object_name, method, tag_value, options={})
111
+ s = radio_button(object_name, method, tag_value, options)
112
+ s.sub!(/\schecked=\"checked\"/, '')
113
+ s.sub!(/type="radio"/, "\\&<%= _pp_radio_button_checked?(@#{object_name}.#{method}, #{tag_value.inspect}) ? ' checked=\"checked\"' : '' %>")
114
+ return pp_error_on(object_name, method) { _pp_remove_error_div(s) }
115
+ end
116
+
117
+ def _pp_radio_button_checked?(value, tag_value)
118
+ return ActionView::Helpers::InstanceTag::radio_button_checked?(value, tag_value)
119
+ end
120
+
121
+ def _pp_select(object, method, collection, priority_collection, options={}, html_options={})
122
+ return pp_error_on(object, method) do
123
+ s = ""
124
+ ## start tag
125
+ s << "<select id=\"#{object}_#{method}\" name=\"#{object}[#{method}]\""
126
+ for key, val in html_options:
127
+ s << " #{key}=\"#{val}\""
128
+ end
129
+ s << ">\n"
130
+ ## selected table
131
+ key = options.key?(:value) ? :value : (options.key?('value') ? 'value' : nil)
132
+ if key.nil? ; selected = "@#{object}.#{method}"
133
+ elsif (val=options[key]).nil? ; selected = nil
134
+ elsif val =~ /\A<%=(.*)%>\z/ ; selected = $1
135
+ else ; selected = val.inspect
136
+ end
137
+ s << "<% _table = {#{selected}=>' selected=\"selected\"'} %>\n" if selected
138
+ ## <option> tags
139
+ if options[:include_blank] || options['include_blank']
140
+ s << "<option value=\"\"></option>\n"
141
+ end
142
+ unless priority_collection.blank?
143
+ _pp_select_options(s, priority_collection, selected, 'delete')
144
+ s << "<option value=\"\">-------------</option>\n"
145
+ end
146
+ _pp_select_options(s, collection, selected, '[]')
147
+ ## end tag
148
+ s << "</select>"
149
+ s
150
+ end
151
+ end
152
+
153
+ def _pp_select_options(s, collection, selected, operator)
154
+ for item in collection
155
+ value, text = item.is_a?(Array) ? item : [item, item]
156
+ if !selected
157
+ t = ''
158
+ elsif operator == 'delete'
159
+ t = "<%= _table.delete(#{value.inspect}) %>"
160
+ else
161
+ t = "<%= _table[#{value.inspect}] %>"
162
+ end
163
+ s << "<option value=\"#{h value}\"#{t}>#{h text}</option>\n"
164
+ end
165
+ end
166
+
167
+ def pp_select(object, method, collection, options={}, html_options={})
168
+ return _pp_select(object, method, collection, nil, options, html_options)
169
+ end
170
+
171
+ def pp_collection_select(object, method, collection, value_method, text_method, options={}, html_options={})
172
+ collection2 = collection.collect { |e|
173
+ [e.__send__(value_method), e.__send__(text_method)]
174
+ }
175
+ return _pp_select(object, method, collection2, nil, options, html_options)
176
+ end
177
+
178
+ def pp_country_select(object, method, priority_countries=nil, options={}, html_options={})
179
+ collection = ActionView::Helpers::FormOptionsHelper::COUNTRIES
180
+ return _pp_select(object, method, collection, priority_countries, options, html_options)
181
+ end
182
+
183
+ def pp_time_zone_select(object, method, priority_zones=nil, options={}, html_options={})
184
+ model = options[:model] || options['model'] || TimeZone
185
+ collection = model.all.collect { |e| [e.name, e.to_s] }
186
+ return _pp_select(object, method, collection, priority_zones, options, html_options)
187
+ end
188
+
189
+ def pp_submit_tag(value="Save changes", options={})
190
+ return submit_tag(value, options)
191
+ end
192
+
193
+ def pp_image_submit_tag(source, options={})
194
+ return image_submit_tag(source, options)
195
+ end
196
+
197
+ end
@@ -0,0 +1,353 @@
1
+ ###
2
+ ### $Release: 2.7.0 $
3
+ ### copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
4
+ ###
5
+
6
+
7
+ require 'erubis'
8
+ require 'erubis/preprocessing'
9
+
10
+
11
+ module Erubis
12
+
13
+ class Eruby
14
+ include ErboutEnhancer # will generate '_erbout = _buf = ""; '
15
+ end
16
+
17
+ class FastEruby
18
+ include ErboutEnhancer # will generate '_erbout = _buf = ""; '
19
+ end
20
+
21
+ module Helpers
22
+
23
+ ##
24
+ ## helper module for Ruby on Rails
25
+ ##
26
+ ## howto:
27
+ ##
28
+ ## 1. add the folliwng code in your 'config/environment.rb'
29
+ ##
30
+ ## require 'erubis/helpers/rails_helper'
31
+ ## #Erubis::Helpers::RailsHelper.engine_class = Erubis::Eruby # or Erubis::FastEruby
32
+ ## #Erubis::Helpers::RailsHelper.init_properties = {}
33
+ ## #Erubis::Helpers::RailsHelper.show_src = false # set true for debugging
34
+ ## #Erubis::Helpers::RailsHelper.preprocessing = true # set true to enable preprocessing
35
+ ##
36
+ ## 2. restart web server.
37
+ ##
38
+ ## if Erubis::Helper::Rails.show_src is true, Erubis prints converted Ruby code
39
+ ## into log file ('log/development.log' or so). if false, it doesn't.
40
+ ## if nil, Erubis prints converted Ruby code if ENV['RAILS_ENV'] == 'development'.
41
+ ##
42
+ module RailsHelper
43
+
44
+ #cattr_accessor :init_properties
45
+ @@engine_class = ::Erubis::Eruby
46
+ #@@engine_class = ::Erubis::FastEruby
47
+ def self.engine_class
48
+ @@engine_class
49
+ end
50
+ def self.engine_class=(klass)
51
+ @@engine_class = klass
52
+ end
53
+
54
+ #cattr_accessor :init_properties
55
+ @@init_properties = {}
56
+ def self.init_properties
57
+ @@init_properties
58
+ end
59
+ def self.init_properties=(hash)
60
+ @@init_properties = hash
61
+ end
62
+
63
+ #cattr_accessor :show_src
64
+ @@show_src = nil
65
+ def self.show_src
66
+ @@show_src
67
+ end
68
+ def self.show_src=(flag)
69
+ @@show_src = flag
70
+ end
71
+
72
+ #cattr_accessor :preprocessing
73
+ @@preprocessing = false
74
+ def self.preprocessing
75
+ @@preprocessing
76
+ end
77
+ def self.preprocessing=(flag)
78
+ @@preprocessing = flag
79
+ end
80
+
81
+
82
+ ## define class for backward-compatibility
83
+ class PreprocessingEruby < Erubis::PreprocessingEruby # :nodoc:
84
+ end
85
+
86
+
87
+ module TemplateConverter
88
+ ## covert eRuby string into ruby code
89
+ def _convert_template(template) # :nodoc:
90
+ #src = ::Erubis::Eruby.new(template).src
91
+ klass = ::Erubis::Helpers::RailsHelper.engine_class
92
+ properties = ::Erubis::Helpers::RailsHelper.init_properties
93
+ show_src = ::Erubis::Helpers::RailsHelper.show_src
94
+ show_src = ENV['RAILS_ENV'] == 'development' if show_src.nil?
95
+ ## preprocessing
96
+ if ::Erubis::Helpers::RailsHelper.preprocessing
97
+ preprocessor = _create_preprocessor(template)
98
+ template = preprocessor.evaluate(_preprocessing_context_object())
99
+ _logger_info "** Erubis: preprocessed==<<'END'\n#{template}END\n" if show_src
100
+ end
101
+ ## convert into ruby code
102
+ src = klass.new(template, properties).src
103
+ #src.insert(0, '_erbout = ')
104
+ _logger_info "** Erubis: src==<<'END'\n#{src}END\n" if show_src
105
+ return src
106
+ end
107
+ def _create_preprocessor(template)
108
+ return PreprocessingEruby.new(template, :escape=>true)
109
+ end
110
+ def _preprocessing_context_object
111
+ return self
112
+ end
113
+ def _logger_info(message)
114
+ logger.info message
115
+ end
116
+ end
117
+
118
+ end
119
+
120
+ end
121
+
122
+ end
123
+
124
+
125
+ class ActionView::Base # :nodoc:
126
+ include ::Erubis::Helpers::RailsHelper::TemplateConverter
127
+ include ::Erubis::PreprocessingHelper
128
+ private
129
+ # convert template into ruby code
130
+ def convert_template_into_ruby_code(template)
131
+ #ERB.new(template, nil, @@erb_trim_mode).src
132
+ return _convert_template(template)
133
+ end
134
+ end
135
+
136
+
137
+ require 'action_pack/version'
138
+
139
+ rails22 = false
140
+
141
+ if ActionPack::VERSION::MAJOR >= 2 ### Rails 2.X
142
+
143
+
144
+ if ActionPack::VERSION::MINOR >=2 ### Rails 2.2, 2.3 or higher
145
+
146
+ rails22 = true
147
+ module ActionView
148
+ module TemplateHandlers
149
+ class ErubisHandler < TemplateHandler
150
+ include Compilable
151
+ include ::Erubis::Helpers::RailsHelper::TemplateConverter
152
+ include ::Erubis::PreprocessingHelper
153
+ def compile(template)
154
+ #src = ::ERB.new("<% __in_erb_template=true %>#{template.source}", nil, erb_trim_mode, '@output_buffer').src
155
+ return _convert_template("<% __in_erb_template=true %>#{template.source}")
156
+ end
157
+ end
158
+ end
159
+ handler_klass = TemplateHandlers::ErubisHandler
160
+ Template.register_default_template_handler :erb, handler_klass
161
+ Template.register_template_handler :rhtml, handler_klass
162
+ end
163
+ module Erubis::Helpers::RailsHelper::TemplateConverter
164
+ def _logger_info(message)
165
+ #logger.info message # logger.info seems not available in Rails 2.2
166
+ ActionController::Base.new.logger.info message
167
+ end
168
+ end
169
+
170
+ elsif ActionPack::VERSION::MINOR >=1 ### Rails 2.1
171
+
172
+ module ActionView
173
+ module TemplateHandlers # :nodoc:
174
+ class ErubisHandler < TemplateHandler
175
+ include Compilable
176
+ include Erubis::Helpers::RailsHelper::TemplateConverter
177
+ include Erubis::PreprocessingHelper
178
+ #
179
+ def compile(template)
180
+ return _convert_template(template.source) # template.is_a?(ActionView::Template)
181
+ end
182
+ def logger #:nodoc:
183
+ return @view.controller.logger
184
+ end
185
+ def _preprocessing_context_object #:nodoc:
186
+ return @view.controller.instance_variable_get('@template')
187
+ end
188
+ #
189
+ def cache_fragment(block, name = {}, options = nil) #:nodoc:
190
+ @view.fragment_for(block, name, options) do
191
+ #eval(ActionView::Base.erb_variable, block.binding)
192
+ eval('_buf', block.binding)
193
+ end
194
+ end
195
+ end
196
+ end
197
+ handler_klass = TemplateHandlers::ErubisHandler
198
+ Template.register_default_template_handler :erb, handler_klass
199
+ Template.register_template_handler :rhtml, handler_klass
200
+ end
201
+
202
+ elsif ActionPack::VERSION::TINY >= 2 ### Rails 2.0.X (X >= 2)
203
+
204
+ module ActionView
205
+ module TemplateHandlers # :nodoc:
206
+ class ErubisHandler < TemplateHandler
207
+ include Erubis::Helpers::RailsHelper::TemplateConverter
208
+ include Erubis::PreprocessingHelper
209
+ def compile(template)
210
+ return _convert_template(template) # template.is_a?(String)
211
+ end
212
+ def logger #:nodoc:
213
+ return @view.controller.logger
214
+ end
215
+ def _preprocessing_context_object #:nodoc:
216
+ return @view.controller.instance_variable_get('@template')
217
+ end
218
+ end
219
+ end
220
+ Base.class_eval do
221
+ handler_klass = TemplateHandlers::ErubisHandler
222
+ register_default_template_handler :erb, handler_klass
223
+ register_template_handler :rhtml, handler_klass
224
+ end
225
+ end
226
+
227
+ else ### Rails 2.0.0 or 2.0.1
228
+
229
+ class ActionView::Base # :nodoc:
230
+ private
231
+ # Method to create the source code for a given template.
232
+ def create_template_source(extension, template, render_symbol, locals)
233
+ if template_requires_setup?(extension)
234
+ body = case extension.to_sym
235
+ when :rxml, :builder
236
+ content_type_handler = (controller.respond_to?(:response) ? "controller.response" : "controller")
237
+ "#{content_type_handler}.content_type ||= Mime::XML\n" +
238
+ "xml = Builder::XmlMarkup.new(:indent => 2)\n" +
239
+ template +
240
+ "\nxml.target!\n"
241
+ when :rjs
242
+ "controller.response.content_type ||= Mime::JS\n" +
243
+ "update_page do |page|\n#{template}\nend"
244
+ end
245
+ else
246
+ #body = ERB.new(template, nil, @@erb_trim_mode).src
247
+ body = convert_template_into_ruby_code(template)
248
+ end
249
+ #
250
+ @@template_args[render_symbol] ||= {}
251
+ locals_keys = @@template_args[render_symbol].keys | locals
252
+ @@template_args[render_symbol] = locals_keys.inject({}) { |h, k| h[k] = true; h }
253
+ #
254
+ locals_code = ""
255
+ locals_keys.each do |key|
256
+ locals_code << "#{key} = local_assigns[:#{key}]\n"
257
+ end
258
+ #
259
+ "def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend"
260
+ end
261
+ end
262
+
263
+ end #if
264
+
265
+
266
+ else ### Rails 1.X
267
+
268
+
269
+ if ActionPack::VERSION::MINOR > 12 ### Rails 1.2
270
+
271
+ class ActionView::Base # :nodoc:
272
+ private
273
+ # Create source code for given template
274
+ def create_template_source(extension, template, render_symbol, locals)
275
+ if template_requires_setup?(extension)
276
+ body = case extension.to_sym
277
+ when :rxml
278
+ "controller.response.content_type ||= 'application/xml'\n" +
279
+ "xml = Builder::XmlMarkup.new(:indent => 2)\n" +
280
+ template
281
+ when :rjs
282
+ "controller.response.content_type ||= 'text/javascript'\n" +
283
+ "update_page do |page|\n#{template}\nend"
284
+ end
285
+ else
286
+ #body = ERB.new(template, nil, @@erb_trim_mode).src
287
+ body = convert_template_into_ruby_code(template)
288
+ end
289
+ #
290
+ @@template_args[render_symbol] ||= {}
291
+ locals_keys = @@template_args[render_symbol].keys | locals
292
+ @@template_args[render_symbol] = locals_keys.inject({}) { |h, k| h[k] = true; h }
293
+ #
294
+ locals_code = ""
295
+ locals_keys.each do |key|
296
+ locals_code << "#{key} = local_assigns[:#{key}]\n"
297
+ end
298
+ #
299
+ "def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend"
300
+ end
301
+ end
302
+
303
+ else ### Rails 1.1
304
+
305
+ class ActionView::Base # :nodoc:
306
+ private
307
+ # Create source code for given template
308
+ def create_template_source(extension, template, render_symbol, locals)
309
+ if template_requires_setup?(extension)
310
+ body = case extension.to_sym
311
+ when :rxml
312
+ "xml = Builder::XmlMarkup.new(:indent => 2)\n" +
313
+ "@controller.headers['Content-Type'] ||= 'application/xml'\n" +
314
+ template
315
+ when :rjs
316
+ "@controller.headers['Content-Type'] ||= 'text/javascript'\n" +
317
+ "update_page do |page|\n#{template}\nend"
318
+ end
319
+ else
320
+ #body = ERB.new(template, nil, @@erb_trim_mode).src
321
+ body = convert_template_into_ruby_code(template)
322
+ end
323
+ #
324
+ @@template_args[render_symbol] ||= {}
325
+ locals_keys = @@template_args[render_symbol].keys | locals
326
+ @@template_args[render_symbol] = locals_keys.inject({}) { |h, k| h[k] = true; h }
327
+ #
328
+ locals_code = ""
329
+ locals_keys.each do |key|
330
+ locals_code << "#{key} = local_assigns[:#{key}] if local_assigns.has_key?(:#{key})\n"
331
+ end
332
+ #
333
+ "def #{render_symbol}(local_assigns)\n#{locals_code}#{body}\nend"
334
+ end
335
+ end
336
+
337
+ end #if
338
+
339
+ ## make h() method faster (only for Rails 1.X)
340
+ module ERB::Util # :nodoc:
341
+ ESCAPE_TABLE = { '&'=>'&amp;', '<'=>'&lt;', '>'=>'&gt;', '"'=>'&quot;', "'"=>'&#039;', }
342
+ def h(value)
343
+ value.to_s.gsub(/[&<>"]/) {|s| ESCAPE_TABLE[s] }
344
+ end
345
+ module_function :h
346
+ end
347
+
348
+ end ###
349
+
350
+
351
+ ## finish
352
+ ActionController::Base.new.logger.info "** Erubis #{::Erubis::VERSION}"
353
+ $stdout.puts "** Erubis #{::Erubis::VERSION}" if rails22