ramaze 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (193) hide show
  1. data/Rakefile +52 -19
  2. data/bin/ramaze +19 -6
  3. data/doc/CHANGELOG +33 -0
  4. data/doc/COPYING +1 -1
  5. data/doc/FAQ +92 -0
  6. data/doc/GPL +340 -0
  7. data/doc/INSTALL +34 -0
  8. data/doc/ProjectInfo +53 -0
  9. data/doc/README +187 -110
  10. data/doc/readme_chunks/appendix.txt +13 -0
  11. data/doc/readme_chunks/examples.txt +38 -0
  12. data/doc/readme_chunks/features.txt +82 -0
  13. data/doc/readme_chunks/getting_help.txt +5 -0
  14. data/doc/readme_chunks/getting_started.txt +18 -0
  15. data/doc/readme_chunks/installing.txt +41 -0
  16. data/doc/readme_chunks/introduction.txt +18 -0
  17. data/doc/readme_chunks/principles.txt +41 -0
  18. data/doc/readme_chunks/thanks.txt +59 -0
  19. data/doc/tutorial/todolist.txt +546 -0
  20. data/examples/blog/main.rb +1 -1
  21. data/examples/blog/src/controller.rb +13 -11
  22. data/examples/blog/src/element.rb +11 -6
  23. data/examples/blog/src/model.rb +8 -23
  24. data/examples/blog/template/edit.xhtml +3 -1
  25. data/examples/blog/template/index.xhtml +4 -4
  26. data/examples/caching.rb +4 -4
  27. data/examples/element.rb +10 -7
  28. data/examples/hello.rb +3 -4
  29. data/examples/simple.rb +5 -3
  30. data/examples/templates/template/external.amrita +19 -0
  31. data/examples/templates/template/{external.rmze → external.zmr} +2 -2
  32. data/examples/templates/template_amrita2.rb +48 -0
  33. data/examples/templates/template_erubis.rb +5 -2
  34. data/examples/templates/{template_ramaze.rb → template_ezamar.rb} +13 -7
  35. data/examples/templates/template_haml.rb +4 -1
  36. data/examples/templates/template_liquid.rb +2 -1
  37. data/examples/templates/template_markaby.rb +2 -1
  38. data/examples/todolist/conf/benchmark.yaml +35 -0
  39. data/examples/todolist/conf/debug.yaml +34 -0
  40. data/examples/todolist/conf/live.yaml +33 -0
  41. data/examples/todolist/conf/silent.yaml +31 -0
  42. data/examples/todolist/conf/stage.yaml +33 -0
  43. data/examples/todolist/main.rb +18 -0
  44. data/examples/todolist/public/404.jpg +0 -0
  45. data/examples/todolist/public/css/coderay.css +105 -0
  46. data/examples/todolist/public/css/ramaze_error.css +42 -0
  47. data/{lib/proto → examples/todolist}/public/error.xhtml +0 -0
  48. data/examples/todolist/public/favicon.ico +0 -0
  49. data/examples/todolist/public/js/jquery.js +1923 -0
  50. data/examples/todolist/public/ramaze.png +0 -0
  51. data/examples/todolist/src/controller/main.rb +56 -0
  52. data/examples/todolist/src/element/page.rb +26 -0
  53. data/examples/todolist/src/model.rb +14 -0
  54. data/examples/todolist/template/index.xhtml +17 -0
  55. data/examples/todolist/template/new.xhtml +7 -0
  56. data/examples/todolist/todolist.db +9 -0
  57. data/examples/whywiki/main.rb +3 -8
  58. data/examples/whywiki/template/show.xhtml +4 -0
  59. data/lib/proto/public/error.zmr +77 -0
  60. data/lib/proto/src/controller/main.rb +2 -1
  61. data/lib/proto/src/element/page.rb +2 -1
  62. data/lib/proto/src/model.rb +3 -2
  63. data/lib/ramaze.rb +7 -9
  64. data/lib/ramaze/adapter.rb +51 -0
  65. data/lib/ramaze/adapter/cgi.rb +23 -0
  66. data/lib/ramaze/adapter/fcgi.rb +22 -0
  67. data/lib/ramaze/adapter/mongrel.rb +7 -86
  68. data/lib/ramaze/adapter/webrick.rb +14 -133
  69. data/lib/ramaze/cache/memcached.rb +6 -0
  70. data/lib/ramaze/cache/yaml_store.rb +3 -1
  71. data/lib/ramaze/controller.rb +292 -2
  72. data/lib/ramaze/dispatcher.rb +85 -213
  73. data/lib/ramaze/error.rb +10 -0
  74. data/lib/ramaze/global.rb +8 -0
  75. data/lib/ramaze/helper/aspect.rb +30 -7
  76. data/lib/ramaze/helper/auth.rb +16 -9
  77. data/lib/ramaze/helper/cache.rb +40 -35
  78. data/lib/ramaze/helper/feed.rb +1 -1
  79. data/lib/ramaze/helper/flash.rb +34 -0
  80. data/lib/ramaze/helper/link.rb +8 -2
  81. data/lib/ramaze/helper/openid.rb +63 -0
  82. data/lib/ramaze/helper/redirect.rb +12 -11
  83. data/lib/ramaze/helper/stack.rb +5 -7
  84. data/lib/ramaze/inform.rb +12 -1
  85. data/lib/ramaze/snippets/kernel/aquire.rb +1 -1
  86. data/lib/ramaze/snippets/kernel/{self_method.rb → method.rb} +3 -18
  87. data/lib/ramaze/snippets/kernel/{rescue_require.rb → pretty_inspect.rb} +7 -6
  88. data/lib/ramaze/snippets/method/name.rb +22 -0
  89. data/lib/ramaze/snippets/{kernel → ramaze}/autoreload.rb +0 -0
  90. data/lib/ramaze/snippets/ramaze/caller_info.rb +14 -0
  91. data/lib/ramaze/snippets/{kernel → ramaze}/caller_lines.rb +3 -10
  92. data/lib/ramaze/snippets/rdoc/usage_no_exit.rb +49 -23
  93. data/lib/ramaze/snippets/string/DIVIDE.rb +0 -1
  94. data/lib/ramaze/store/default.rb +58 -2
  95. data/lib/ramaze/store/yaml.rb +161 -0
  96. data/lib/ramaze/template.rb +27 -86
  97. data/lib/ramaze/template/amrita2.rb +14 -19
  98. data/lib/ramaze/template/erubis.rb +15 -38
  99. data/lib/ramaze/template/ezamar.rb +100 -0
  100. data/lib/ramaze/template/ezamar/element.rb +166 -0
  101. data/lib/ramaze/template/ezamar/engine.rb +124 -0
  102. data/lib/ramaze/template/ezamar/morpher.rb +155 -0
  103. data/lib/ramaze/template/haml.rb +16 -43
  104. data/lib/ramaze/template/liquid.rb +11 -51
  105. data/lib/ramaze/template/markaby.rb +44 -42
  106. data/lib/ramaze/tool/mime.rb +18 -0
  107. data/lib/ramaze/tool/mime_types.yaml +615 -0
  108. data/lib/ramaze/trinity/request.rb +20 -196
  109. data/lib/ramaze/trinity/response.rb +4 -33
  110. data/lib/ramaze/trinity/session.rb +150 -72
  111. data/lib/ramaze/version.rb +1 -1
  112. data/spec/adapter_spec.rb +20 -0
  113. data/spec/public/favicon.ico +0 -0
  114. data/spec/public/ramaze.png +0 -0
  115. data/spec/public/test_download.css +141 -0
  116. data/spec/{tc_request.rb → request_tc_helper.rb} +45 -21
  117. data/spec/spec_all.rb +77 -34
  118. data/spec/spec_helper.rb +8 -157
  119. data/spec/spec_helper_context.rb +72 -0
  120. data/spec/spec_helper_requester.rb +52 -0
  121. data/spec/spec_helper_simple_http.rb +433 -0
  122. data/spec/tc_adapter_mongrel.rb +3 -15
  123. data/spec/tc_adapter_webrick.rb +4 -14
  124. data/spec/tc_cache.rb +3 -5
  125. data/spec/tc_controller.rb +22 -12
  126. data/spec/tc_dependencies.rb +13 -0
  127. data/spec/tc_element.rb +8 -7
  128. data/spec/tc_error.rb +13 -7
  129. data/spec/tc_global.rb +16 -18
  130. data/spec/tc_helper_aspect.rb +2 -4
  131. data/spec/tc_helper_auth.rb +15 -14
  132. data/spec/tc_helper_cache.rb +5 -7
  133. data/spec/tc_helper_feed.rb +0 -2
  134. data/spec/tc_helper_flash.rb +103 -0
  135. data/spec/tc_helper_form.rb +4 -6
  136. data/spec/tc_helper_link.rb +1 -3
  137. data/spec/tc_helper_redirect.rb +23 -8
  138. data/spec/tc_helper_stack.rb +31 -15
  139. data/spec/tc_morpher.rb +1 -3
  140. data/spec/tc_params.rb +48 -7
  141. data/spec/tc_request_mongrel.rb +9 -0
  142. data/spec/tc_request_webrick.rb +5 -0
  143. data/spec/tc_session.rb +41 -25
  144. data/spec/tc_store.rb +55 -6
  145. data/spec/tc_store_yaml.rb +71 -0
  146. data/spec/tc_template_amrita2.rb +3 -3
  147. data/spec/tc_template_erubis.rb +2 -3
  148. data/spec/{tc_template_ramaze.rb → tc_template_ezamar.rb} +15 -5
  149. data/spec/tc_template_haml.rb +4 -3
  150. data/spec/tc_template_liquid.rb +3 -4
  151. data/spec/tc_template_markaby.rb +4 -6
  152. data/spec/tc_tidy.rb +1 -3
  153. data/spec/template/amrita2/{data.html → data.amrita} +0 -0
  154. data/spec/template/amrita2/{index.html → index.amrita} +0 -0
  155. data/spec/template/amrita2/{sum.html → sum.amrita} +0 -0
  156. data/spec/template/ezamar/another/long/action.zmr +1 -0
  157. data/spec/template/ezamar/combined.zmr +1 -0
  158. data/spec/template/{ramaze/file_only.rmze → ezamar/file_only.zmr} +0 -0
  159. data/spec/template/{ramaze/index.rmze → ezamar/index.zmr} +0 -0
  160. data/spec/template/{ramaze/nested.rmze → ezamar/nested.zmr} +0 -0
  161. data/spec/template/ezamar/some__long__action.zmr +1 -0
  162. data/spec/template/{ramaze/sum.rmze → ezamar/sum.zmr} +0 -0
  163. metadata +181 -123
  164. data/doc/allison/LICENSE +0 -184
  165. data/doc/allison/README +0 -37
  166. data/doc/allison/allison.css +0 -300
  167. data/doc/allison/allison.gif +0 -0
  168. data/doc/allison/allison.js +0 -307
  169. data/doc/allison/allison.rb +0 -287
  170. data/doc/allison/cache/BODY +0 -588
  171. data/doc/allison/cache/CLASS_INDEX +0 -4
  172. data/doc/allison/cache/CLASS_PAGE +0 -1
  173. data/doc/allison/cache/FILE_INDEX +0 -4
  174. data/doc/allison/cache/FILE_PAGE +0 -1
  175. data/doc/allison/cache/FONTS +0 -1
  176. data/doc/allison/cache/FR_INDEX_BODY +0 -1
  177. data/doc/allison/cache/IMGPATH +0 -1
  178. data/doc/allison/cache/INDEX +0 -1
  179. data/doc/allison/cache/JAVASCRIPT +0 -307
  180. data/doc/allison/cache/METHOD_INDEX +0 -4
  181. data/doc/allison/cache/METHOD_LIST +0 -1
  182. data/doc/allison/cache/SRC_PAGE +0 -1
  183. data/doc/allison/cache/STYLE +0 -322
  184. data/doc/allison/cache/URL +0 -1
  185. data/doc/changes.txt +0 -2021
  186. data/doc/changes.xml +0 -2024
  187. data/lib/ramaze/snippets/kernel/silently.rb +0 -13
  188. data/lib/ramaze/snippets/thread/deadQUESTIONMARK.rb +0 -11
  189. data/lib/ramaze/template/haml/actionview_stub.rb +0 -20
  190. data/lib/ramaze/template/ramaze.rb +0 -177
  191. data/lib/ramaze/template/ramaze/element.rb +0 -166
  192. data/lib/ramaze/template/ramaze/morpher.rb +0 -156
  193. data/spec/tc_test.rb +0 -17
@@ -0,0 +1,124 @@
1
+ # Copyright (c) 2006 Michael Fellinger m.fellinger@gmail.com
2
+ # All files in this distribution are subject to the terms of the Ruby license.
3
+
4
+ require 'digest/sha1'
5
+
6
+ # Ezamar is a very simple (and at no means complete) reimplementation of the
7
+ # Templating-engine found in Nitro.
8
+ #
9
+ # Since Nitros templating is based on REXML and Ezamar is not there are vast
10
+ # differences, but it tries to keep the look and feel as close as possible.
11
+
12
+ module Ezamar
13
+
14
+ require 'ramaze/template/ezamar/element'
15
+ require 'ramaze/template/ezamar/morpher'
16
+
17
+ # This class is responsible for initializing and compiling the template.
18
+
19
+ class Template
20
+ attr_accessor :last_result, :file
21
+ attr_reader :original
22
+
23
+ # Start a new template with some string for your template
24
+ # that's going to be transformed.
25
+
26
+ def initialize source, options = {}
27
+ @original = @source = source
28
+ @binding, @file = options.values_at(:binding, :path)
29
+ @start_heredoc = "T" << Digest::SHA1.hexdigest(@source)
30
+ @start_heredoc, @end_heredoc = "\n<<#{@start_heredoc}\n", "\n#{@start_heredoc}\n"
31
+ @bufadd = "_out_ << "
32
+ @old = true
33
+ compile
34
+ end
35
+
36
+ # reset the original template you gave
37
+
38
+ def original=(original)
39
+ compile
40
+ end
41
+
42
+ # is the template old?
43
+
44
+ def old?
45
+ !!@old
46
+ end
47
+
48
+ # make the template old - mark it for recompilation.
49
+
50
+ def touch
51
+ @old = true
52
+ end
53
+
54
+ # transform a String to a final xhtml
55
+ #
56
+ # You can pass it a binding, for example from your controller.
57
+ #
58
+ # Example:
59
+ #
60
+ # class Controller
61
+ # def hello
62
+ # @hello = 'Hello, World!'
63
+ # end
64
+ # end
65
+ #
66
+ # controller = Controller.new
67
+ # controller.hello
68
+ # binding = controller.send(:binding)
69
+ #
70
+ # Ezamar.new('#{@hello}').transform(binding)
71
+
72
+ def transform(_binding_ = @binding)
73
+ @compiled = compile if old?
74
+
75
+ args = @file ? [@file] : []
76
+
77
+ @last_result = eval(@compiled, _binding_, *args)
78
+
79
+ @last_result.map! do |line|
80
+ line.to_s.chomp
81
+ end
82
+
83
+ @last_result = @last_result.join.strip
84
+ end
85
+
86
+ # The actual compilation of the @source
87
+ #
88
+ # All ye who seek magic, look elsewhere, this method is ASAP (as simple as possible)
89
+ #
90
+ # There are some simple gsubs that build a final template which is evaluated
91
+ #
92
+ # The rules are following:
93
+ # <?r rubycode ?>
94
+ # evaluate the code inside the tag, this is considered XHTML-valid and so is the
95
+ # preferred method for executing code inside your templates.
96
+ # The return-value is ignored
97
+ # <% rubycode %>
98
+ # The same as <?r ?>, ERB-style and not valid XHTML, but should give someone who
99
+ # is already familiar with ERB some common ground
100
+ # #{ rubycode }
101
+ # You know this from normal ruby already and it's actually nothing else.
102
+ # Interpolation at the position in the template, isn't any special taggy format
103
+ # and therefor safe to use.
104
+ # <%= rubycode %>
105
+ # The result of this will be interpolated at the position in the template.
106
+ # Not valid XHTML either.
107
+ #
108
+ # The result of the compilation will be stored in @compiled.
109
+
110
+ def compile
111
+ @source.gsub!(/<%\s+(.*?)\s+%>/m,
112
+ "#{@end_heredoc} \\1; #{@bufadd} #{@start_heredoc}")
113
+ @source.gsub!(/<\?r\s+(.*?)\s+\?>/m,
114
+ "#{@end_heredoc} \\1; #{@bufadd} #{@start_heredoc}")
115
+ @source.gsub!(/<%=\s+(.*?)\s+%>/m,
116
+ "#{@end_heredoc} #{@bufadd} (\\1); #{@bufadd} #{@start_heredoc}")
117
+
118
+ @source = [@bufadd, @start_heredoc, @source, @end_heredoc].join(' ')
119
+
120
+ @old = false
121
+ @compiled = "_out_ = []; #{@source}; _out_"
122
+ end
123
+ end
124
+ end
@@ -0,0 +1,155 @@
1
+ # Copyright (c) 2006 Michael Fellinger m.fellinger@gmail.com
2
+ # All files in this distribution are subject to the terms of the Ruby license.
3
+
4
+ require 'ramaze/template/ezamar/engine'
5
+
6
+ # This applies a morphing-replace for the template.
7
+ #
8
+ # To use the functionality of Morpher you will need to have hpricot
9
+ # installed, you will get one error in case you don't and the method
10
+ # will be replaced by a stub that simply returns the template.
11
+ #
12
+ # The method first checks if you use any morphers and just skips
13
+ # the step if you don't, this should give quite some speedup for
14
+ # smaller templates that don't use this functionality at all.
15
+ # the check works by searching the morphs with appended '='
16
+ # in the template. There may be a few cases where this won't work
17
+ # since we cannot make any assumptions on the format.
18
+ #
19
+ # If you want to turn this functionality off, either remove Morpher
20
+ # from:
21
+ # Ramaze::Template::Ezamar.trait[:transform_pipeline]
22
+ # or do:
23
+ # Ramaze::Morpher.trait[:morphs] = {}
24
+ #
25
+ # The latter is a tad slower, but i mention the possibility in case you
26
+ # find good use for it.
27
+ #
28
+ # You can add your own morphers in Ramaze::Morpher.trait[:morphs]
29
+ #
30
+ # For Example:
31
+ #
32
+ # Morpher.trait[:morphs]['if'] = '<?r %morph %expression ?>%content<?r end ?>'
33
+ #
34
+ # Now, assuming that some tag in your template is '<a if="@foo">x</a>'
35
+ #
36
+ # %morph stands for the name of your morph: 'if'
37
+ # %expression is the stuff you write in the attribute: '@foo'
38
+ # %content is the tag without the attribute (and all inside): '<a>x</a>'
39
+
40
+ class Ezamar::Morpher
41
+
42
+ # Use this trait to define your custom morphs.
43
+ trait :morphs => {
44
+ 'if' => '<?r %morph %expression ?>%content<?r end ?>',
45
+ 'unless' => '<?r %morph %expression ?>%content<?r end ?>',
46
+ 'for' => '<?r %morph %expression ?>%content<?r end ?>',
47
+ 'each' => '<?r %expression.%morph do |_e| ?>%content<?r end ?>',
48
+ 'times' => '<?r %expression.%morph do |_t| ?>%content<?r end ?>',
49
+ }
50
+
51
+ # Since the functionality is best explained by examples, here they come.
52
+ #
53
+ # Example:
54
+ #
55
+ # if:
56
+ # <div if="@name">#@name</div>
57
+ # morphs to:
58
+ # <?r if @name ?>
59
+ # <div>#@name</div>
60
+ # <?r end ?>
61
+ #
62
+ # unless:
63
+ # <div unless="@name">No Name</div>
64
+ # morphs to:
65
+ # <?r unless @name ?>
66
+ # <div>No Name</div>
67
+ # <?r end ?>
68
+ #
69
+ # for:
70
+ # <div for="name in @names">#{name}</div>
71
+ # morphs to:
72
+ # <?r for name in @names ?>
73
+ # <div>#{name}</div>
74
+ # <?r end ?>
75
+ #
76
+ # times:
77
+ # <div times="3">#{_t}<div>
78
+ # morphs to:
79
+ # <?r 3.times do |_t| ?>
80
+ # <div>#{_t}</div>
81
+ # <?r end ?>
82
+ #
83
+ # each:
84
+ # <div each="[1,2,3]">#{_e}</div>
85
+ # morphs to:
86
+ # <?r [1,2,3].each do |_e| ?>
87
+ # <div>#{_e}</div>
88
+ # <?r end ?>
89
+ #
90
+ # The latter two examples show you also one standard introduced by a
91
+ # limitation of the replacement-system.
92
+ #
93
+ # When you yield a value, please name it by the first character(s) of
94
+ # the morphs name, with an underscore prefixed.
95
+ #
96
+ # for each an _e, for times a _t.
97
+ #
98
+ # This is by far not the best way to handle it and might lead to problems
99
+ # due to the lack of proper scoping in ruby (if you define an _e or _t
100
+ # before the block it will be overwritten).
101
+ #
102
+ # So please be careful, I tried to come up with something that is both easy
103
+ # to write and doesn't look outright awful while keeping an easy to remember
104
+ # mnemonic.
105
+ #
106
+ # TODO:
107
+ # - Add pure Ruby implementation as a fall-back.
108
+
109
+ def self.transform template, options = {}
110
+ morphs =
111
+ trait[:morphs].map{|k,v| [k.to_s, v.to_s]}.select do |(k,v)|
112
+ template.to_s.include?("#{k}=")
113
+ end
114
+
115
+ morphs = Hash[*morphs.flatten]
116
+
117
+ return template if morphs.empty?
118
+
119
+ require 'hpricot'
120
+
121
+ hp = Hpricot(template)
122
+ hp.each_child do |child|
123
+ if child.elem?
124
+ morphs.each_pair do |morph, replacement|
125
+ if expression = child[morph]
126
+ old = child.to_html
127
+ child.remove_attribute(morph)
128
+
129
+ replacement = replacement.dup.
130
+ gsub('%morph', morph).
131
+ gsub('%expression', expression).
132
+ gsub('%content', child.to_html)
133
+
134
+ template.gsub!(old, replacement)
135
+ end
136
+ end
137
+ end
138
+ end
139
+
140
+ template
141
+
142
+ rescue LoadError => ex
143
+ error "Please install hpricot (for example via `gem install hpricot`) to get morphing"
144
+
145
+ # replace this method with a stub that only returns the template.
146
+
147
+ self.class_eval do
148
+ def self.transform(template, options = {})
149
+ template
150
+ end
151
+ end
152
+
153
+ template
154
+ end
155
+ end
@@ -1,65 +1,38 @@
1
1
  # Copyright (c) 2006 Michael Fellinger m.fellinger@gmail.com
2
2
  # All files in this distribution are subject to the terms of the Ruby license.
3
3
 
4
- require 'ramaze/template/haml/actionview_stub'
5
4
  require 'haml/engine'
6
5
 
7
6
  module Ramaze::Template
8
7
  class Haml < Template
9
8
 
10
- # Actions consist of both templates and methods on the controller.
11
- trait :actionless => false
12
-
13
- # Usual extensions for templates.
14
- trait :template_extensions => %w[haml]
15
-
16
9
  # Custom HAML-options for your controller to be merged.
10
+
17
11
  trait :haml_options => {
18
- :locals => {}
19
- }
12
+ :locals => {}
13
+ }
20
14
 
21
- class << self
15
+ Ramaze::Controller.register_engine self, %w[ haml ]
22
16
 
23
- # initializes the handling of a request on the controller.
24
- # Creates a new instances of itself and sends the action and params.
25
- # Also tries to render the template.
26
- # In Theory you can use this standalone, this has not been tested though.
17
+ class << self
27
18
 
28
- def handle_request action, *params
29
- controller = self.new
30
- controller.instance_variable_set('@action', action)
31
- result = controller.send(action, *params) if controller.respond_to?(action)
19
+ # Transform any String via Haml, takes optionally an hash with the haml_options
20
+ # that you can set also by
21
+ # trait :haml_options => {}
22
+ # if you pass the options it will merge the trait with them. (your options
23
+ # override the defaults from trait[:haml_options]
32
24
 
33
- file = find_template(action)
25
+ def transform controller, options = {}
26
+ action, parameter, file, bound = *super
34
27
 
35
- template =
36
- if file
37
- File.read(file)
38
- elsif result.respond_to? :to_str
39
- result
40
- end
28
+ reaction = controller.send(action, *parameter)
29
+ template = reaction_or_file(reaction, file)
41
30
 
42
31
  return '' unless template
43
32
 
44
- controller.send(:transform, template)
45
- rescue Object => ex
46
- puts ex
47
- Informer.error ex
48
- ''
33
+ haml = ::Haml::Engine.new(template, ancestral_trait[:haml_options])
34
+ haml.to_html(controller)
49
35
  end
50
36
  end
51
-
52
- private
53
-
54
- # Transform any String via Haml, takes optionally an hash with the haml_options
55
- # that you can set also by
56
- # trait :haml_options => {}
57
- # if you pass the options it will merge the trait with them. (your options
58
- # override the defaults from trait[:haml_options]
59
-
60
- def transform string, options = {}
61
- haml = ::Haml::Engine.new(string, ancestral_trait[:haml_options].merge(options))
62
- haml.to_html(self)
63
- end
64
37
  end
65
38
  end
@@ -5,70 +5,30 @@ require 'liquid'
5
5
 
6
6
  module Ramaze::Template
7
7
  class Liquid < Template
8
- extend Ramaze::Helper
9
8
 
10
- # Actions consist of both templates and methods on the controller.
11
- trait :actionless => false
12
-
13
- # Usual extensions for templates.
14
- trait :template_extensions => %w[liquid]
15
-
16
- # Custom options for Liquid to be merged for your controller.
17
- trait :liquid_options => {}
9
+ Ramaze::Controller.register_engine self, %w[ liquid ]
18
10
 
19
11
  class << self
12
+
20
13
  # initializes the handling of a request on the controller.
21
14
  # Creates a new instances of itself and sends the action and params.
22
15
  # Also tries to render the template.
23
16
  # In Theory you can use this standalone, this has not been tested though.
24
17
 
25
- def handle_request action, *params
26
- controller = self.new
27
- controller.instance_variable_set('@action', action)
28
- result = controller.send(action, *params) if controller.respond_to?(action)
29
-
30
- file = find_template(action)
31
-
32
- p file
18
+ def transform controller, options = {}
19
+ action, parameter, file, bound = *super
33
20
 
34
- template =
35
- if file
36
- File.read(file)
37
- elsif result.respond_to? :to_str
38
- result
39
- end
21
+ reaction = controller.send(action, *parameter)
22
+ template = reaction_or_file(reaction, file)
40
23
 
41
24
  return '' unless template
42
25
 
43
- controller.send(:transform, template)
44
- rescue Object => ex
45
- puts ex
46
- Informer.error ex
47
- ''
48
- end
49
- end
50
-
51
- private
26
+ hash = controller.instance_variable_get("@hash") || {}
27
+ template = ::Liquid::Template.parse(template)
28
+ options = controller.ancestral_trait[:liquid_options]
52
29
 
53
- # Transform any String via Liquid, takes an optional hash for the parameters
54
- # transform("hi {{name}}", 'name' => 'tobi') # => "hi tobi"
55
- #
56
- # Usually you will just do this in the controller:
57
- #
58
- # class MainController < Template::Liquid
59
- # def index
60
- # @hash = {'name' => 'tobi'}
61
- # end
62
- # end
63
- #
64
- # And the templating will use @hash by default, setting it to {} if it is
65
- # not set yet.
66
-
67
- def transform string, hash = {}, options = {}
68
- @hash ||= hash
69
- template = ::Liquid::Template.parse(string)
70
- options = ancestral_trait[:liquid_options].merge(options)
71
- template.render(@hash, options)
30
+ template.render(hash, options)
31
+ end
72
32
  end
73
33
  end
74
34
  end
@@ -5,14 +5,53 @@ require 'markaby'
5
5
 
6
6
  module Ramaze::Template
7
7
  class Markaby < Template
8
- extend Ramaze::Helper
9
8
 
10
- # Actions consist of both templates and methods on the controller.
11
- trait :actionless => false
9
+ Ramaze::Controller.register_engine self, %w[ mab ]
12
10
 
13
- # Usual extensions for templates.
14
- trait :template_extensions => %w[mab]
11
+ class << self
12
+ # initializes the handling of a request on the controller.
13
+ # Creates a new instances of itself and sends the action and params.
14
+ # Also tries to render the template.
15
+ # In Theory you can use this standalone, this has not been tested though.
16
+
17
+ def transform controller, options = {}
18
+ action, parameter, file, bound = *super
19
+
20
+ unless controller.private_methods.include?(action)
21
+ mixed_in = controller.class.ancestors === MarkabyMixin
22
+ controller.class.send(:include, MarkabyMixin) unless mixed_in
23
+
24
+ reaction = controller.send(action, *parameter)
25
+
26
+ mab = ::Markaby::Builder.new
27
+ template =
28
+ if file
29
+ transform_file(controller, file)
30
+ elsif reaction.respond_to? :to_str
31
+ reaction
32
+ end
33
+ end
34
+
35
+ return template if template
36
+ raise Ramaze::Error::NoAction, "No Action found for `#{action}' on #{controller.class}"
37
+ end
38
+
39
+ def transform_file controller, file
40
+ ivs = {}
41
+ controller.instance_variables.each do |iv|
42
+ ivs[iv.gsub('@', '').to_sym] = controller.instance_variable_get(iv)
43
+ end
44
+ controller.send(:mab, ivs) do
45
+ instance_eval(File.read(file))
46
+ end
47
+ rescue Object => ex
48
+ puts ex
49
+ raise Ramaze::Error::Template, ex.message, ex.backtrace
50
+ end
51
+ end
52
+ end
15
53
 
54
+ module MarkabyMixin
16
55
  private
17
56
 
18
57
  # use this inside your controller to directly build Markaby
@@ -27,42 +66,5 @@ module Ramaze::Template
27
66
  builder.send(:helper, :link)
28
67
  builder.new(*args, &block).to_s
29
68
  end
30
-
31
- class << self
32
-
33
- # Takes the action and parameter
34
- # creates a new instance of itself, sets the @action instance-variable
35
- # to the action just called, the sends the action and parameter further
36
- # on to the instance (if the instance responds to the action)
37
- #
38
- # uses the #find_template method for the action to locate the template
39
- # and uses the response from the template instead in case there is no
40
- # template (and the response from the template responds to to_str)
41
-
42
- def handle_request action, *params
43
- controller = self.new
44
- controller.instance_variable_set('@action', action)
45
-
46
- result = controller.send(action, *params) if controller.respond_to?(action)
47
- file = find_template(action)
48
-
49
- mab = ::Markaby::Builder.new
50
-
51
- template =
52
- if file
53
- ivs = {}
54
- controller.instance_variables.each do |iv|
55
- ivs[iv.gsub('@', '').to_sym] = controller.instance_variable_get(iv)
56
- end
57
- controller.send(:mab, ivs) do
58
- instance_eval(File.read(file))
59
- end
60
- elsif result.respond_to? :to_str
61
- result
62
- end
63
-
64
- template ? template : ''
65
- end
66
- end
67
69
  end
68
70
  end