kwartz 3.0.0 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. data/ChangeLog +36 -1
  2. data/LGPL +504 -0
  3. data/README.txt +16 -2
  4. data/bin/kwartz +1 -1
  5. data/doc-api/classes/Kwartz.html +47 -1
  6. data/doc-api/classes/Kwartz/Assertion.html +6 -6
  7. data/doc-api/classes/Kwartz/AssertionError.html +6 -6
  8. data/doc-api/classes/Kwartz/AttrInfo.html +42 -42
  9. data/doc-api/classes/Kwartz/BaseError.html +12 -12
  10. data/doc-api/classes/Kwartz/BaseTranslator.html +210 -43
  11. data/doc-api/classes/Kwartz/CharacterType.html +24 -24
  12. data/doc-api/classes/Kwartz/CommandOptionError.html +7 -7
  13. data/doc-api/classes/Kwartz/CommandOptions.html +49 -49
  14. data/doc-api/classes/Kwartz/Config.html +6 -1
  15. data/doc-api/classes/Kwartz/ConvertError.html +14 -14
  16. data/doc-api/classes/Kwartz/Converter.html +31 -30
  17. data/doc-api/classes/Kwartz/CssStyleParser.html +79 -57
  18. data/doc-api/classes/Kwartz/Defun.html +313 -0
  19. data/doc-api/classes/Kwartz/DocumentRuleset.html +58 -58
  20. data/doc-api/classes/Kwartz/ElementExpander.html +32 -32
  21. data/doc-api/classes/Kwartz/ElementInfo.html +29 -29
  22. data/doc-api/classes/Kwartz/ElementRuleset.html +126 -96
  23. data/doc-api/classes/Kwartz/EperlHandler.html +108 -84
  24. data/doc-api/classes/Kwartz/EperlTranslator.html +7 -7
  25. data/doc-api/classes/Kwartz/ErubisHandler.html +2 -2
  26. data/doc-api/classes/Kwartz/ErubisTranslator.html +8 -8
  27. data/doc-api/classes/Kwartz/ErubyHandler.html +2 -220
  28. data/doc-api/classes/Kwartz/ErubyTranslator.html +7 -7
  29. data/doc-api/classes/Kwartz/ExpandStatement.html +20 -20
  30. data/doc-api/classes/Kwartz/Handler.html +122 -117
  31. data/doc-api/classes/Kwartz/HandlerArgument.html +215 -0
  32. data/doc-api/classes/Kwartz/Helper.html +112 -0
  33. data/doc-api/classes/Kwartz/Helper/ActionViewHelper.html +221 -0
  34. data/doc-api/classes/Kwartz/Helper/RailsTemplate.html +630 -0
  35. data/doc-api/classes/Kwartz/JstlHandler.html +208 -193
  36. data/doc-api/classes/Kwartz/JstlTranslator.html +15 -15
  37. data/doc-api/classes/Kwartz/KwartzError.html +6 -6
  38. data/doc-api/classes/Kwartz/Main.html +39 -23
  39. data/doc-api/classes/Kwartz/NativeExpression.html +18 -18
  40. data/doc-api/classes/Kwartz/NativeStatement.html +25 -25
  41. data/doc-api/classes/Kwartz/Node.html +6 -6
  42. data/doc-api/classes/Kwartz/PIErubisHandler.html +113 -0
  43. data/doc-api/classes/Kwartz/PIErubisTranslator.html +200 -0
  44. data/doc-api/classes/Kwartz/ParseError.html +6 -6
  45. data/doc-api/classes/Kwartz/PhpHandler.html +102 -83
  46. data/doc-api/classes/Kwartz/PhpTranslator.html +14 -14
  47. data/doc-api/classes/Kwartz/PresentationLogicParser.html +136 -128
  48. data/doc-api/classes/Kwartz/PrintStatement.html +19 -19
  49. data/doc-api/classes/Kwartz/RailsHandler.html +121 -118
  50. data/doc-api/classes/Kwartz/RailsTranslator.html +7 -7
  51. data/doc-api/classes/Kwartz/RubyHandler.html +349 -0
  52. data/doc-api/classes/Kwartz/RubyStyleParser.html +85 -85
  53. data/doc-api/classes/Kwartz/RubyTranslator.html +218 -0
  54. data/doc-api/classes/Kwartz/StatementHelper.html +479 -0
  55. data/doc-api/classes/Kwartz/StrutsTranslator.html +12 -12
  56. data/doc-api/classes/Kwartz/TagInfo.html +28 -28
  57. data/doc-api/classes/Kwartz/TextConverter.html +28 -28
  58. data/doc-api/classes/Kwartz/Translator.html +42 -42
  59. data/doc-api/classes/Kwartz/Util.html +284 -0
  60. data/doc-api/created.rid +1 -1
  61. data/doc-api/files/__/README_txt.html +30 -3
  62. data/doc-api/files/kwartz/assert_rb.html +1 -1
  63. data/doc-api/files/kwartz/binding/eperl_rb.html +2 -2
  64. data/doc-api/files/kwartz/binding/erubis_rb.html +3 -3
  65. data/doc-api/files/kwartz/binding/eruby_rb.html +3 -2
  66. data/doc-api/files/kwartz/binding/jstl_rb.html +2 -2
  67. data/doc-api/files/kwartz/binding/php_rb.html +2 -2
  68. data/doc-api/files/kwartz/binding/pierubis_rb.html +116 -0
  69. data/doc-api/files/kwartz/binding/rails_rb.html +2 -2
  70. data/doc-api/files/kwartz/binding/ruby_rb.html +115 -0
  71. data/doc-api/files/kwartz/binding/struts_rb.html +1 -1
  72. data/doc-api/files/kwartz/config_rb.html +2 -2
  73. data/doc-api/files/kwartz/converter_rb.html +3 -2
  74. data/doc-api/files/kwartz/defun_rb.html +116 -0
  75. data/doc-api/files/kwartz/error_rb.html +1 -1
  76. data/doc-api/files/kwartz/helper/rails_rb.html +123 -0
  77. data/doc-api/files/kwartz/main_rb.html +5 -2
  78. data/doc-api/files/kwartz/node_rb.html +2 -2
  79. data/doc-api/files/kwartz/parser_rb.html +2 -2
  80. data/doc-api/files/kwartz/translator_rb.html +2 -2
  81. data/doc-api/files/kwartz/util/assert-text-equal_rb.html +1 -1
  82. data/doc-api/files/kwartz/util/testcase-helper_rb.html +2 -2
  83. data/doc-api/files/kwartz/util_rb.html +107 -0
  84. data/doc-api/files/kwartz_rb.html +3 -2
  85. data/doc-api/fr_class_index.html +11 -0
  86. data/doc-api/fr_file_index.html +5 -0
  87. data/doc-api/fr_method_index.html +242 -190
  88. data/doc/p-pattern.html +94 -94
  89. data/doc/reference.html +847 -612
  90. data/doc/users-guide.html +744 -241
  91. data/examples/rails1/README +2 -2
  92. data/examples/rails1/application_helper.rb +2 -27
  93. data/examples/rails1/link_to.plogic +5 -5
  94. data/examples/rails1/list.plogic +2 -2
  95. data/examples/rails2/app/controllers/application.rb +14 -0
  96. data/examples/rails2/app/controllers/groups_controller.rb +47 -0
  97. data/examples/rails2/app/controllers/members_controller.rb +56 -0
  98. data/examples/rails2/app/helpers/application_helper.rb +6 -0
  99. data/examples/rails2/app/helpers/groups_helper.rb +2 -0
  100. data/examples/rails2/app/helpers/members_helper.rb +2 -0
  101. data/examples/rails2/app/models/group.rb +2 -0
  102. data/examples/rails2/app/models/member.rb +3 -0
  103. data/examples/rails2/app/views/groups/_form.html +30 -0
  104. data/examples/rails2/app/views/groups/_form.plogic +11 -0
  105. data/examples/rails2/app/views/groups/_link.plogic +19 -0
  106. data/examples/rails2/app/views/groups/_read.plogic +7 -0
  107. data/examples/rails2/app/views/groups/edit.cfg.yaml +2 -0
  108. data/examples/rails2/app/views/groups/edit.html +26 -0
  109. data/examples/rails2/app/views/groups/edit.plogic +9 -0
  110. data/examples/rails2/app/views/groups/list.html +48 -0
  111. data/examples/rails2/app/views/groups/list.plogic +32 -0
  112. data/examples/rails2/app/views/groups/new.cfg.yaml +2 -0
  113. data/examples/rails2/app/views/groups/new.html +27 -0
  114. data/examples/rails2/app/views/groups/new.plogic +9 -0
  115. data/examples/rails2/app/views/groups/show.html +27 -0
  116. data/examples/rails2/app/views/groups/show.plogic +2 -0
  117. data/examples/rails2/app/views/layouts/groups.html +23 -0
  118. data/examples/rails2/app/views/layouts/groups.plogic +11 -0
  119. data/examples/rails2/app/views/layouts/members.html +23 -0
  120. data/examples/rails2/app/views/layouts/members.plogic +11 -0
  121. data/examples/rails2/app/views/layouts/members2.html +24 -0
  122. data/examples/rails2/app/views/layouts/members2.plogic +11 -0
  123. data/examples/rails2/app/views/members/_form.plogic +27 -0
  124. data/examples/rails2/app/views/members/_link.plogic +19 -0
  125. data/examples/rails2/app/views/members/_read.plogic +29 -0
  126. data/examples/rails2/app/views/members/edit.cfg.yaml +6 -0
  127. data/examples/rails2/app/views/members/edit.html +27 -0
  128. data/examples/rails2/app/views/members/edit.plogic +10 -0
  129. data/examples/rails2/app/views/members/list.html +53 -0
  130. data/examples/rails2/app/views/members/list.plogic +32 -0
  131. data/examples/rails2/app/views/members/new.html +61 -0
  132. data/examples/rails2/app/views/members/new.plogic +17 -0
  133. data/examples/rails2/app/views/members/show.html +41 -0
  134. data/examples/rails2/app/views/members/show.plogic +3 -0
  135. data/examples/rails2/db/create-tables.mysql.sql +27 -0
  136. data/examples/rails2/db/create-tables.pgsql.sql +27 -0
  137. data/examples/rails2/db/insert-data.sql +21 -0
  138. data/examples/rails2/db/tabledef.yaml +51 -0
  139. data/kwartz.gemspec +4 -4
  140. data/lib/kwartz.rb +10 -2
  141. data/lib/kwartz/assert.rb +1 -1
  142. data/lib/kwartz/binding/eperl.rb +85 -61
  143. data/lib/kwartz/binding/erubis.rb +6 -8
  144. data/lib/kwartz/binding/eruby.rb +5 -120
  145. data/lib/kwartz/binding/jstl.rb +116 -101
  146. data/lib/kwartz/binding/php.rb +79 -60
  147. data/lib/kwartz/binding/pierubis.rb +58 -0
  148. data/lib/kwartz/binding/rails.rb +45 -42
  149. data/lib/kwartz/binding/ruby.rb +193 -0
  150. data/lib/kwartz/binding/struts.rb +1 -1
  151. data/lib/kwartz/config.rb +4 -3
  152. data/lib/kwartz/converter.rb +231 -68
  153. data/lib/kwartz/defun.rb +126 -0
  154. data/lib/kwartz/error.rb +1 -1
  155. data/lib/kwartz/helper/rails.rb +437 -0
  156. data/lib/kwartz/main.rb +30 -38
  157. data/lib/kwartz/node.rb +46 -31
  158. data/lib/kwartz/parser.rb +43 -15
  159. data/lib/kwartz/translator.rb +104 -6
  160. data/lib/kwartz/util.rb +113 -0
  161. data/lib/kwartz/util/assert-text-equal.rb +1 -1
  162. data/lib/kwartz/util/testcase-helper.rb +26 -8
  163. data/test/test-compile.rb +3 -3
  164. data/test/test-compile.yaml +135 -5
  165. data/test/test-converter.rb +16 -8
  166. data/test/test-converter.yaml +307 -76
  167. data/test/test-directives.rb +1 -1
  168. data/test/test-directives.yaml +964 -979
  169. data/test/test-main.rb +61 -146
  170. data/test/test-main.yaml +235 -0
  171. data/test/test-parser.rb +10 -12
  172. data/test/test-parser.yaml +484 -326
  173. data/test/test-rails.rb +1 -1
  174. data/test/test-rails.yaml +37 -37
  175. data/test/test-ruleset.rb +1 -1
  176. data/test/test-ruleset.yaml +34 -40
  177. data/test/test.rb +1 -1
  178. metadata +82 -3
  179. data/COPYING +0 -340
@@ -0,0 +1,126 @@
1
+ ###
2
+ ### $Rev: 136 $
3
+ ### $Release: 3.1.0 $
4
+ ### copyright(c) 2004-2006 kuwata-lab.com all rights reserved
5
+ ###
6
+
7
+
8
+ module Kwartz
9
+
10
+
11
+ ##
12
+ ## generate compiled code.
13
+ ## see defun() method for usage.
14
+ ##
15
+ module Defun
16
+
17
+ module_function
18
+
19
+
20
+ ##
21
+ ## generate compiled code
22
+ ##
23
+ ## ex.
24
+ ## args = %w[-l eruby -p ex.plogic ex.html]
25
+ ## eruby_src = Kwartz::Main.new(args).execute()
26
+ ## properties = { :trim_mode=>'>' }
27
+ ## code = Kwartz::Defun.defun('ex', eruby_src, 'eruby', properties)
28
+ ## print code
29
+ ##
30
+ ## command-line properties:
31
+ ## --module=name : module name (default 'View')
32
+ ## --method=name : method name (default 'expand_xxx')
33
+ ##
34
+ def defun(basename, output, lang, properties={})
35
+ lang_code = compile_into_lang_code(output, lang, properties)
36
+ s = build_defun_code(basename, lang_code, lang, properties)
37
+ return s
38
+ end
39
+
40
+
41
+ def compile_into_lang_code(output, lang, properties)
42
+ case lang
43
+ when 'ruby'
44
+ return output
45
+ when 'eruby', 'rails'
46
+ require 'erb'
47
+ trim_mode = properties[:trim_mode] || (lang == 'eruby' ? 1 : '-')
48
+ return ERB.new(output, nil, trim_mode).src
49
+ when 'erubis'
50
+ require 'erubis'
51
+ return Erubis::Eruby.new(nil, properties).convert(output)
52
+ when 'pierubis'
53
+ require 'erubis'
54
+ return Erubis::PI::Eruby.new(nil, properties).convert(output)
55
+ when 'php'
56
+ return output
57
+ else
58
+ raise "'#{lang}': not supported language."
59
+ end
60
+ end
61
+
62
+
63
+ def build_defun_code(basename, code, lang, properties)
64
+ case lang
65
+ when 'ruby', 'eruby', 'rails', 'erubis', 'pierubis'
66
+ return build_ruby_code(basename, code, properties)
67
+ when 'php'
68
+ return build_php_code(basename, code, properties)
69
+ else
70
+ raise "'#{lang}': not supported language."
71
+ end
72
+ end
73
+
74
+
75
+ def build_ruby_code(basename, ruby_code, properties)
76
+ basename = basename.gsub(/[^\w]/, '_')
77
+ module_name = properties.key?(:module) ? properties[:module] : 'View'
78
+ method_verb = properties[:verb] || 'expand'
79
+ method_name = properties[:method] || "#{method_verb}_#{basename}"
80
+ s = ''
81
+ s << "module #{module_name}\n" if module_name
82
+ s << "\n"
83
+ s << " (@@proc_table ||= {})['#{basename}'] = proc do\n"
84
+ s << ruby_code << (!ruby_code.empty? && ruby_code[-1] != ?\n ? "\n" : '')
85
+ s << " end#proc\n"
86
+ s << "\n"
87
+ s << " module_function\n" if module_name
88
+ s << " def #{method_name}(context={})\n"
89
+ s << " if context.is_a?(Hash)\n"
90
+ s << " hash = context\n"
91
+ s << " context = Object.new\n"
92
+ s << " hash.each { |key, val| context.instance_variable_set(\"@\#{key}\", val) }\n"
93
+ s << " end\n"
94
+ s << " proc_obj = @@proc_table['#{basename}']\n"
95
+ s << " context.instance_eval(&proc_obj)\n"
96
+ s << " end\n"
97
+ s << "\n"
98
+ s << "end\n" if module_name
99
+ return s
100
+ end
101
+
102
+
103
+ def build_php_code(basename, php_code, properties)
104
+ basename = basename.gsub(/[^\w]/, '_')
105
+ s = <<END
106
+ <?php
107
+ function print_view_#{basename}($context) {
108
+ explode($context);
109
+ ?>#{php_code}<?php
110
+ }
111
+ function expand_view_#{basename}($context) {
112
+ ob_start();
113
+ print_view_#{basename}($context);
114
+ $output = ob_get_clean();
115
+ ob_end_clean();
116
+ return $output;
117
+ }
118
+ ?>
119
+ END
120
+ return s
121
+ end
122
+
123
+
124
+ end
125
+
126
+ end
@@ -1,6 +1,6 @@
1
1
  ###
2
2
  ### $Rev: 112 $
3
- ### $Release: 3.0.0 $
3
+ ### $Release: 3.1.0 $
4
4
  ### copyright(c) 2004-2006 kuwata-lab.com all rights reserved
5
5
  ###
6
6
 
@@ -0,0 +1,437 @@
1
+ ###
2
+ ### $Rev: 134 $
3
+ ### $Release: 3.1.0 $
4
+ ### copyright(c) 2004-2006 kuwata-lab.com all rights reserved
5
+ ###
6
+
7
+
8
+ require 'kwartz'
9
+ require 'kwartz/binding/ruby'
10
+ require 'kwartz/binding/eruby'
11
+ require 'kwartz/binding/rails'
12
+ require 'kwartz/binding/erubis'
13
+ require 'kwartz/binding/pierubis'
14
+ require 'kwartz/util'
15
+ require 'erb'
16
+
17
+
18
+ module Kwartz
19
+
20
+ module Helper
21
+
22
+ ###
23
+ ### helper class to use Kwartz in Rails
24
+ ###
25
+ ### How to use Kwartz in Rails:
26
+ ###
27
+ ### 1. add the folliwng code in your 'app/controllers/application.rb'.
28
+ ### --------------------
29
+ ### require 'kwartz/helper/rails'
30
+ ### ActionView::Base.register_template_handler('html', Kwartz::Helper::RailsTemplate)
31
+ ### #Kwartz::Helper::RailsTemplate.pdata_suffix = '.html'
32
+ ### #Kwartz::Helper::RailsTemplate.plogic_suffix = '.plogic'
33
+ ### #Kwartz::Helper::RailsTemplate.default_properties = { :escape=>true }
34
+ ### #Kwartz::Helper::RailsTemplate.debug = false
35
+ ### --------------------
36
+ ### 2. restart web server.
37
+ ### 3. put template files '*.html' and '*.plogic' in 'app/views/xxx'.
38
+ ### layout files ('app/views/layouts/xxx.{html,plogic}') is also available.
39
+ ###
40
+ class RailsTemplate
41
+
42
+ @@default_properties = { }
43
+
44
+ def self.default_properties
45
+ return @@default_properties
46
+ end
47
+
48
+ def self.default_properties=(hash)
49
+ @@default_properties = hash
50
+ end
51
+
52
+
53
+ @@pdata_suffix = '.html'
54
+
55
+ def self.pdata_suffix
56
+ return @@pdata_suffix
57
+ end
58
+
59
+ def self.pdata_suffix=(suffix)
60
+ @@pdata_suffix = suffix
61
+ end
62
+
63
+
64
+ @@plogic_suffix = '.plogic'
65
+
66
+ def self.plogic_suffix
67
+ return @@plogic_suffix
68
+ end
69
+
70
+ def self.plogic_suffix=(suffix)
71
+ @@plogic_suffix = suffix
72
+ end
73
+
74
+
75
+ @@lang = 'rails' # or 'eruby', 'ruby', 'erubis', 'pierubis'
76
+
77
+ def self.lang
78
+ return @@lang
79
+ end
80
+
81
+ def self.lang=(lang)
82
+ case lang
83
+ when 'rails', 'ruby', 'eruby', 'erubis', 'pierubis'
84
+ @@lang = lang
85
+ else
86
+ raise "'#{lang}': invalid language name."
87
+ end
88
+ end
89
+
90
+
91
+ @@use_cache = nil
92
+
93
+ def self.use_cache
94
+ return @@use_cache
95
+ end
96
+
97
+ def self.use_cache=(flag)
98
+ @@use_cache = flag
99
+ end
100
+
101
+
102
+ @@cache_table = {}
103
+
104
+ def self.add_cache(_ruby_code, _filename)
105
+ _proc_obj = eval "proc do #{_ruby_code} end", binding(), _filename
106
+ @@cache_table[_filename] = _proc_obj
107
+ return _proc_obj
108
+ end
109
+
110
+ def self.get_cache(filename)
111
+ return @@cache_table[filename]
112
+ end
113
+
114
+
115
+ @@debug = true
116
+
117
+ def self.debug
118
+ return @@debug
119
+ end
120
+
121
+ def self.debug=(flag)
122
+ @@debug = flag
123
+ end
124
+
125
+
126
+ def initialize(view)
127
+ @view = view
128
+ end
129
+
130
+
131
+ def render(template, assigns)
132
+
133
+ ## reverse engineering
134
+ #$stderr.puts "*** debug: render() called."
135
+ #$stderr.puts "*** debug: RAILS_ENV=#{RAILS_ENV.inspect}"
136
+ #$stderr.puts "*** debug: self.class=#{self.class}"
137
+ #$stderr.puts "*** debug: @view.class=#{@view.class}"
138
+ #$stderr.puts "*** debug: self.__id__=#{self.__id__.inspect}"
139
+ #$stderr.puts "*** debug: @view.class.methods=#{(@view.class.methods - Class.methods).sort.inspect}"
140
+ #$stderr.puts "*** debug: @view.controller.class.methods=#{(@view.controller.class.methods - Class.methods).sort.inspect}"
141
+ #
142
+ #$stderr.puts "*** debug: instance_variables=#{instance_variables.inspect}" #=> [@views]
143
+ #c = @view.controller
144
+ #$stderr.puts "*** debug: @view.controller.instance_variables=#{c.instance_variables.inspect}"
145
+ #$stderr.puts "*** debug: @view.controller.action_name=#{c.action_name.inspect}"
146
+ #$stderr.puts "*** debug: @view.controller.render_layout_basename=#{c.render_layout_basename.inspect}"
147
+ #$stderr.puts "*** debug: @view.controller.render_template_basename=#{c.render_template_basename.inspect}"
148
+ #require 'pp'
149
+ #c = @view.controller
150
+ #$stderr.puts "*** debug: @view.controller.instance_variable_get('@template')="
151
+ #PP.pp(c.instance_variable_get('@template'), $stderr)
152
+ #$stderr.puts "*** debug: @view.controller.methods="
153
+ #PP.pp((c.methods - Object.new.methods).sort, $stderr)
154
+ #$stderr.puts "*** debug: @view.controller.class.methods="
155
+ #PP.pp((c.class.methods - Class.methods).sort, $stderr)
156
+
157
+ ## return if @content_for_layout is set
158
+ template_ = @view.controller.instance_variable_get("@template")
159
+ content_for_layout_ = template_.instance_variable_get("@content_for_layout")
160
+ return content_for_layout_ if content_for_layout_
161
+
162
+ ## template basename and layout basename
163
+ c = @view.controller
164
+ template_root = c.template_root # or c.class.template_root
165
+ #template_basename = "#{template_root}/#{c.controller_name}/#{c.action_name}"
166
+ #layout_basename = "#{template_root}/layouts/#{c.controller_name}"
167
+ template_basename = "#{template_root}/#{c.render_template_basename}"
168
+ layout_basename = "#{template_root}/#{c.render_layout_basename}"
169
+
170
+ ## check timestamps
171
+ convert_flag = true
172
+ cache_filename = template_basename + '.cache'
173
+ if use_cache? && test(?f, cache_filename)
174
+ filenames = [
175
+ template_basename + @@pdata_suffix,
176
+ template_basename + @@plogic_suffix,
177
+ layout_basename + @@pdata_suffix,
178
+ layout_basename + @@plogic_suffix,
179
+ ]
180
+ mtime = File.mtime(cache_filename)
181
+ convert_flag = filenames.any? { |filename|
182
+ test(?f, filename) && File.mtime(filename) > mtime
183
+ }
184
+ end
185
+
186
+ ## convert templates into ruby code, or get cached object
187
+ logger = @view.controller.logger
188
+ msgstr = "template='#{template_basename}#{@@pdata_suffix}'" if logger
189
+ logname = "*** #{self.class.name}" if logger
190
+ if convert_flag
191
+ logger.info "#{logname}: convert template file: #{msgstr}" if logger
192
+ ruby_code = convert(template, template_basename, layout_basename)
193
+ File.open(cache_filename, 'w') { |f| f.write(ruby_code) } # write cache
194
+ proc_obj = self.class.add_cache(ruby_code, cache_filename)
195
+ elsif (proc_obj = self.class.get_cache(cache_filename)).nil?
196
+ logger.info "#{logname}: read cache file: #{msgstr}" if logger
197
+ ruby_code = File.read(cache_filename)
198
+ proc_obj = self.class.add_cache(ruby_code, cache_filename)
199
+ else
200
+ logger.info "#{logname}: reuse cached proc object: #{msgstr}" if logger
201
+ end
202
+
203
+ ## use @view as context object
204
+ @view.__send__(:evaluate_assigns)
205
+ #or @view.instance_eval("evaluate_assigns()")
206
+ context = @view
207
+
208
+ ## evaluate ruby code with context object
209
+ if assigns && !assigns.empty?
210
+ #return _evaluate_string(ruby_code, context, assigns)
211
+ return evaluate_proc(proc_obj, context, assigns)
212
+ else
213
+ #return context.instance_eval(ruby_code)
214
+ return context.instance_eval(&proc_obj)
215
+ end
216
+ end
217
+
218
+
219
+ private
220
+
221
+
222
+ def use_cache?
223
+ if @@use_cache.nil?
224
+ return ENV['RAILS_ENV'] != 'development' # or == 'production'
225
+ else
226
+ return @@use_cache;
227
+ end
228
+ end
229
+
230
+
231
+ def parse_config_file(config_filename)
232
+ config = {}
233
+ return config unless test(?f, config_filename)
234
+ str = File.read(config_filename)
235
+ ydoc = YAML.load(Kwartz::Util.untabify(str))
236
+ template_dir = File.dirname(config_filename)
237
+ ['import_pdata', 'import_plogic'].each do |key|
238
+ val = ydoc[key]
239
+ next if val.nil?
240
+ val = [ val ] if val.is_a?(String)
241
+ list = []
242
+ val.each do |item|
243
+ if item.is_a?(Hash)
244
+ hash = {}
245
+ item.each do |k, v| hash[k.intern] = v end
246
+ elsif item =~ /<([\w,]*)>\z/
247
+ filename = $`
248
+ names = $1.split(/,/)
249
+ hash = { :filename=>filename, :patterns=>(names.empty? ? nil : names) }
250
+ else
251
+ hash = { :filename=>item }
252
+ end
253
+ hash[:filename] = template_dir + '/' + hash[:filename] if hash[:filename]
254
+ list << hash
255
+ end
256
+ config[key.intern] = list
257
+ end
258
+ return config
259
+ end
260
+
261
+
262
+ def convert(template, template_basename, layout_basename)
263
+
264
+ ## filenames
265
+ template_pdata_filename = template_basename + @@pdata_suffix
266
+ template_plogic_filename = template_basename + @@plogic_suffix
267
+ layout_pdata_filename = layout_basename + @@pdata_suffix
268
+ layout_plogic_filename = layout_basename + @@plogic_suffix
269
+
270
+ ## config file
271
+ config_filename = template_basename + '.cfg.yaml'
272
+ if test(?f, config_filename)
273
+ config = parse_config_file(config_filename)
274
+ else
275
+ config = {}
276
+ end
277
+
278
+ ## presentation logic files
279
+ rulesets = []
280
+ properties = @@default_properties
281
+ parser = Kwartz::CssStyleParser.new(properties)
282
+ config[:import_plogic].each do |hash|
283
+ filename = hash[:filename]
284
+ patterns = hash[:patterns]
285
+ tmp_rulesets = parser.parse(File.read(filename), filename)
286
+ if patterns
287
+ regexp_list = patterns.collect { |pattern| Kwartz::Util.pattern_to_regexp(pattern) }
288
+ tmp_rulesets = tmp_rulesets.select { |ruleset|
289
+ regexp_list.any? { |regexp| regexp =~ ruleset.name }
290
+ }
291
+ end
292
+ rulesets += tmp_rulesets
293
+ end if config[:import_plogic]
294
+ [layout_plogic_filename, template_plogic_filename].each do |filename|
295
+ rulesets += parser.parse(File.read(filename), filename) if test(?f, filename)
296
+ end
297
+
298
+ ## converter
299
+ handler = Kwartz::Handler.get_class(@@lang).new(rulesets, properties)
300
+ converter = Kwartz::TextConverter.new(handler, properties)
301
+
302
+ ## presentation data files
303
+ pdata = template
304
+ config[:import_pdata].each do |hash|
305
+ filename = hash[:filename]
306
+ patterns = hash[:patterns]
307
+ tmp_handler = Kwartz::Handler.get_class(@@lang).new(rulesets, properties)
308
+ tmp_converter = Kwartz::TextConverter.new(tmp_handler, properties)
309
+ tmp_converter.convert(File.read(filename), filename)
310
+ handler._import_element_info_from_handler(tmp_handler, patterns)
311
+ end if config[:import_pdata]
312
+ if test(?f, layout_pdata_filename)
313
+ converter.convert(pdata, template_pdata_filename)
314
+ layout_pdata = File.read(layout_pdata_filename)
315
+ stmts = converter.convert(layout_pdata, layout_pdata_filename)
316
+ else
317
+ stmts = converter.convert(pdata, template_pdata_filename)
318
+ end
319
+
320
+ ## translate statements into ruby code
321
+ translator = Kwartz::Translator.get_class(@@lang).new(properties)
322
+ code = translator.translate(stmts)
323
+
324
+ ## write cache
325
+ cache_filename = template_basename + '.cache'
326
+ case @@lang
327
+ when 'rails'
328
+ trim_mode = ActionView::Base.erb_trim_mode # or '-'
329
+ ruby_code = ERB.new(code, nil, trim_mode).src
330
+ when 'eruby'
331
+ trim_mode = 1
332
+ ruby_code = ERB.new(code, nil, trim_mode).src
333
+ when 'ruby'
334
+ ruby_code = code
335
+ when 'erubis'
336
+ require 'erubis'
337
+ ruby_code = Erubis::Eruby.new().convert(code)
338
+ when 'pierubis'
339
+ require 'erubis'
340
+ ruby_code = Erubis::PI::Eruby.new().convert(code)
341
+ else
342
+ internal_error
343
+ end
344
+ File.open(cache_filename, 'w') { |f| f.write(ruby_code) }
345
+
346
+ ## debug code
347
+ translator.extend(Kwartz::NoTextEnhancer)
348
+ debug_code = translator.translate(stmts)
349
+ debug_filename = template_basename + '.debug'
350
+ File.open(debug_filename, 'w') { |f| f.write(debug_code) }
351
+
352
+ return ruby_code
353
+ end
354
+
355
+ def _localvar_code(localvars)
356
+ list = localvars.collect { |name| "#{name} = localvars[#{name.inspect}]\n" }
357
+ code = list.join()
358
+ return code
359
+ end
360
+
361
+ def evaluate_proc(_proc_obj, _context, _localvars)
362
+ eval(_localvar_code(_localvars))
363
+ _context.instance_eval(&_proc_obj)
364
+ end
365
+
366
+
367
+ end
368
+
369
+
370
+ ##
371
+ ## helper module to add helper methods 'start_link_tag()' and 'start_remote_link_tag()'.
372
+ ##
373
+ module ActionViewHelper
374
+
375
+ ##
376
+ ## return '<a href="...">' start tag.
377
+ ##
378
+ ## ex.
379
+ ## start_link_tag :action=>'show', :controller=>'user', :id=>1
380
+ ## #=> "<a href=\"/user/show/1\">"
381
+ ##
382
+ def start_link_tag(options={}, html_options=nil, *parameters)
383
+ s = link_to('', options, html_options, *parameters)
384
+ s.sub!(/<\/a>\z/, '')
385
+ s
386
+ end
387
+
388
+ ##
389
+ ## ajax version of start_link_tag()
390
+ ##
391
+ def start_remote_link_tag(options={}, html_options={})
392
+ s = link_to_remote(options, html_options)
393
+ s.sub!(/<\/a>\z/, '')
394
+ s
395
+ end
396
+
397
+ alias anchor start_link_tag
398
+ alias anchor_remote start_remote_link_tag
399
+
400
+ end
401
+
402
+
403
+ end #module Helper
404
+
405
+ end #module Kwartz
406
+
407
+
408
+
409
+ # override ActionController::Base::render
410
+
411
+ module ActionController #:nodoc:
412
+
413
+ class Base #:nodoc:
414
+
415
+ attr_reader :render_layout_basename, :render_template_basename
416
+
417
+ alias _render_orig render
418
+ alias _render_file_orig render_file
419
+
420
+ protected
421
+
422
+ def render(options=nil, deprecated_status=nil, deprecated_layout=nil, &block) #:nodoc:
423
+ template_with_options = options.is_a?(Hash)
424
+ if apply_layout?(template_with_options, options)
425
+ @render_layout_basename = pick_layout(template_with_options, options, deprecated_layout)
426
+ end
427
+ _render_orig(options, deprecated_status, &block)
428
+ end
429
+
430
+ def render_file(template_path, status=nil, use_full_path=false, locals={}) #:nodoc:
431
+ @render_template_basename = template_path
432
+ _render_file_orig(template_path, status, use_full_path, locals)
433
+ end
434
+
435
+ end
436
+
437
+ end