ramaze 0.0.7 → 0.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +52 -19
- data/bin/ramaze +19 -6
- data/doc/CHANGELOG +33 -0
- data/doc/COPYING +1 -1
- data/doc/FAQ +92 -0
- data/doc/GPL +340 -0
- data/doc/INSTALL +34 -0
- data/doc/ProjectInfo +53 -0
- data/doc/README +187 -110
- data/doc/readme_chunks/appendix.txt +13 -0
- data/doc/readme_chunks/examples.txt +38 -0
- data/doc/readme_chunks/features.txt +82 -0
- data/doc/readme_chunks/getting_help.txt +5 -0
- data/doc/readme_chunks/getting_started.txt +18 -0
- data/doc/readme_chunks/installing.txt +41 -0
- data/doc/readme_chunks/introduction.txt +18 -0
- data/doc/readme_chunks/principles.txt +41 -0
- data/doc/readme_chunks/thanks.txt +59 -0
- data/doc/tutorial/todolist.txt +546 -0
- data/examples/blog/main.rb +1 -1
- data/examples/blog/src/controller.rb +13 -11
- data/examples/blog/src/element.rb +11 -6
- data/examples/blog/src/model.rb +8 -23
- data/examples/blog/template/edit.xhtml +3 -1
- data/examples/blog/template/index.xhtml +4 -4
- data/examples/caching.rb +4 -4
- data/examples/element.rb +10 -7
- data/examples/hello.rb +3 -4
- data/examples/simple.rb +5 -3
- data/examples/templates/template/external.amrita +19 -0
- data/examples/templates/template/{external.rmze → external.zmr} +2 -2
- data/examples/templates/template_amrita2.rb +48 -0
- data/examples/templates/template_erubis.rb +5 -2
- data/examples/templates/{template_ramaze.rb → template_ezamar.rb} +13 -7
- data/examples/templates/template_haml.rb +4 -1
- data/examples/templates/template_liquid.rb +2 -1
- data/examples/templates/template_markaby.rb +2 -1
- data/examples/todolist/conf/benchmark.yaml +35 -0
- data/examples/todolist/conf/debug.yaml +34 -0
- data/examples/todolist/conf/live.yaml +33 -0
- data/examples/todolist/conf/silent.yaml +31 -0
- data/examples/todolist/conf/stage.yaml +33 -0
- data/examples/todolist/main.rb +18 -0
- data/examples/todolist/public/404.jpg +0 -0
- data/examples/todolist/public/css/coderay.css +105 -0
- data/examples/todolist/public/css/ramaze_error.css +42 -0
- data/{lib/proto → examples/todolist}/public/error.xhtml +0 -0
- data/examples/todolist/public/favicon.ico +0 -0
- data/examples/todolist/public/js/jquery.js +1923 -0
- data/examples/todolist/public/ramaze.png +0 -0
- data/examples/todolist/src/controller/main.rb +56 -0
- data/examples/todolist/src/element/page.rb +26 -0
- data/examples/todolist/src/model.rb +14 -0
- data/examples/todolist/template/index.xhtml +17 -0
- data/examples/todolist/template/new.xhtml +7 -0
- data/examples/todolist/todolist.db +9 -0
- data/examples/whywiki/main.rb +3 -8
- data/examples/whywiki/template/show.xhtml +4 -0
- data/lib/proto/public/error.zmr +77 -0
- data/lib/proto/src/controller/main.rb +2 -1
- data/lib/proto/src/element/page.rb +2 -1
- data/lib/proto/src/model.rb +3 -2
- data/lib/ramaze.rb +7 -9
- data/lib/ramaze/adapter.rb +51 -0
- data/lib/ramaze/adapter/cgi.rb +23 -0
- data/lib/ramaze/adapter/fcgi.rb +22 -0
- data/lib/ramaze/adapter/mongrel.rb +7 -86
- data/lib/ramaze/adapter/webrick.rb +14 -133
- data/lib/ramaze/cache/memcached.rb +6 -0
- data/lib/ramaze/cache/yaml_store.rb +3 -1
- data/lib/ramaze/controller.rb +292 -2
- data/lib/ramaze/dispatcher.rb +85 -213
- data/lib/ramaze/error.rb +10 -0
- data/lib/ramaze/global.rb +8 -0
- data/lib/ramaze/helper/aspect.rb +30 -7
- data/lib/ramaze/helper/auth.rb +16 -9
- data/lib/ramaze/helper/cache.rb +40 -35
- data/lib/ramaze/helper/feed.rb +1 -1
- data/lib/ramaze/helper/flash.rb +34 -0
- data/lib/ramaze/helper/link.rb +8 -2
- data/lib/ramaze/helper/openid.rb +63 -0
- data/lib/ramaze/helper/redirect.rb +12 -11
- data/lib/ramaze/helper/stack.rb +5 -7
- data/lib/ramaze/inform.rb +12 -1
- data/lib/ramaze/snippets/kernel/aquire.rb +1 -1
- data/lib/ramaze/snippets/kernel/{self_method.rb → method.rb} +3 -18
- data/lib/ramaze/snippets/kernel/{rescue_require.rb → pretty_inspect.rb} +7 -6
- data/lib/ramaze/snippets/method/name.rb +22 -0
- data/lib/ramaze/snippets/{kernel → ramaze}/autoreload.rb +0 -0
- data/lib/ramaze/snippets/ramaze/caller_info.rb +14 -0
- data/lib/ramaze/snippets/{kernel → ramaze}/caller_lines.rb +3 -10
- data/lib/ramaze/snippets/rdoc/usage_no_exit.rb +49 -23
- data/lib/ramaze/snippets/string/DIVIDE.rb +0 -1
- data/lib/ramaze/store/default.rb +58 -2
- data/lib/ramaze/store/yaml.rb +161 -0
- data/lib/ramaze/template.rb +27 -86
- data/lib/ramaze/template/amrita2.rb +14 -19
- data/lib/ramaze/template/erubis.rb +15 -38
- data/lib/ramaze/template/ezamar.rb +100 -0
- data/lib/ramaze/template/ezamar/element.rb +166 -0
- data/lib/ramaze/template/ezamar/engine.rb +124 -0
- data/lib/ramaze/template/ezamar/morpher.rb +155 -0
- data/lib/ramaze/template/haml.rb +16 -43
- data/lib/ramaze/template/liquid.rb +11 -51
- data/lib/ramaze/template/markaby.rb +44 -42
- data/lib/ramaze/tool/mime.rb +18 -0
- data/lib/ramaze/tool/mime_types.yaml +615 -0
- data/lib/ramaze/trinity/request.rb +20 -196
- data/lib/ramaze/trinity/response.rb +4 -33
- data/lib/ramaze/trinity/session.rb +150 -72
- data/lib/ramaze/version.rb +1 -1
- data/spec/adapter_spec.rb +20 -0
- data/spec/public/favicon.ico +0 -0
- data/spec/public/ramaze.png +0 -0
- data/spec/public/test_download.css +141 -0
- data/spec/{tc_request.rb → request_tc_helper.rb} +45 -21
- data/spec/spec_all.rb +77 -34
- data/spec/spec_helper.rb +8 -157
- data/spec/spec_helper_context.rb +72 -0
- data/spec/spec_helper_requester.rb +52 -0
- data/spec/spec_helper_simple_http.rb +433 -0
- data/spec/tc_adapter_mongrel.rb +3 -15
- data/spec/tc_adapter_webrick.rb +4 -14
- data/spec/tc_cache.rb +3 -5
- data/spec/tc_controller.rb +22 -12
- data/spec/tc_dependencies.rb +13 -0
- data/spec/tc_element.rb +8 -7
- data/spec/tc_error.rb +13 -7
- data/spec/tc_global.rb +16 -18
- data/spec/tc_helper_aspect.rb +2 -4
- data/spec/tc_helper_auth.rb +15 -14
- data/spec/tc_helper_cache.rb +5 -7
- data/spec/tc_helper_feed.rb +0 -2
- data/spec/tc_helper_flash.rb +103 -0
- data/spec/tc_helper_form.rb +4 -6
- data/spec/tc_helper_link.rb +1 -3
- data/spec/tc_helper_redirect.rb +23 -8
- data/spec/tc_helper_stack.rb +31 -15
- data/spec/tc_morpher.rb +1 -3
- data/spec/tc_params.rb +48 -7
- data/spec/tc_request_mongrel.rb +9 -0
- data/spec/tc_request_webrick.rb +5 -0
- data/spec/tc_session.rb +41 -25
- data/spec/tc_store.rb +55 -6
- data/spec/tc_store_yaml.rb +71 -0
- data/spec/tc_template_amrita2.rb +3 -3
- data/spec/tc_template_erubis.rb +2 -3
- data/spec/{tc_template_ramaze.rb → tc_template_ezamar.rb} +15 -5
- data/spec/tc_template_haml.rb +4 -3
- data/spec/tc_template_liquid.rb +3 -4
- data/spec/tc_template_markaby.rb +4 -6
- data/spec/tc_tidy.rb +1 -3
- data/spec/template/amrita2/{data.html → data.amrita} +0 -0
- data/spec/template/amrita2/{index.html → index.amrita} +0 -0
- data/spec/template/amrita2/{sum.html → sum.amrita} +0 -0
- data/spec/template/ezamar/another/long/action.zmr +1 -0
- data/spec/template/ezamar/combined.zmr +1 -0
- data/spec/template/{ramaze/file_only.rmze → ezamar/file_only.zmr} +0 -0
- data/spec/template/{ramaze/index.rmze → ezamar/index.zmr} +0 -0
- data/spec/template/{ramaze/nested.rmze → ezamar/nested.zmr} +0 -0
- data/spec/template/ezamar/some__long__action.zmr +1 -0
- data/spec/template/{ramaze/sum.rmze → ezamar/sum.zmr} +0 -0
- metadata +181 -123
- data/doc/allison/LICENSE +0 -184
- data/doc/allison/README +0 -37
- data/doc/allison/allison.css +0 -300
- data/doc/allison/allison.gif +0 -0
- data/doc/allison/allison.js +0 -307
- data/doc/allison/allison.rb +0 -287
- data/doc/allison/cache/BODY +0 -588
- data/doc/allison/cache/CLASS_INDEX +0 -4
- data/doc/allison/cache/CLASS_PAGE +0 -1
- data/doc/allison/cache/FILE_INDEX +0 -4
- data/doc/allison/cache/FILE_PAGE +0 -1
- data/doc/allison/cache/FONTS +0 -1
- data/doc/allison/cache/FR_INDEX_BODY +0 -1
- data/doc/allison/cache/IMGPATH +0 -1
- data/doc/allison/cache/INDEX +0 -1
- data/doc/allison/cache/JAVASCRIPT +0 -307
- data/doc/allison/cache/METHOD_INDEX +0 -4
- data/doc/allison/cache/METHOD_LIST +0 -1
- data/doc/allison/cache/SRC_PAGE +0 -1
- data/doc/allison/cache/STYLE +0 -322
- data/doc/allison/cache/URL +0 -1
- data/doc/changes.txt +0 -2021
- data/doc/changes.xml +0 -2024
- data/lib/ramaze/snippets/kernel/silently.rb +0 -13
- data/lib/ramaze/snippets/thread/deadQUESTIONMARK.rb +0 -11
- data/lib/ramaze/template/haml/actionview_stub.rb +0 -20
- data/lib/ramaze/template/ramaze.rb +0 -177
- data/lib/ramaze/template/ramaze/element.rb +0 -166
- data/lib/ramaze/template/ramaze/morpher.rb +0 -156
- data/spec/tc_test.rb +0 -17
|
@@ -1,13 +0,0 @@
|
|
|
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
|
-
module Kernel
|
|
4
|
-
|
|
5
|
-
# sets $VERBOSE temporarily to nil so you can redefine constants in peace
|
|
6
|
-
|
|
7
|
-
def silently
|
|
8
|
-
old_verbose, $VERBOSE = $VERBOSE, nil
|
|
9
|
-
yield
|
|
10
|
-
ensure
|
|
11
|
-
$VERBOSE = old_verbose
|
|
12
|
-
end
|
|
13
|
-
end
|
|
@@ -1,20 +0,0 @@
|
|
|
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
|
-
# stub, in case the requires for that active_* stuff of Haml doesn't fail
|
|
5
|
-
# but we are not running Rails ;)
|
|
6
|
-
|
|
7
|
-
module ActionView
|
|
8
|
-
class Base
|
|
9
|
-
|
|
10
|
-
# stub
|
|
11
|
-
|
|
12
|
-
def concat
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
# stub
|
|
16
|
-
|
|
17
|
-
def form_tag
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
end
|
|
@@ -1,177 +0,0 @@
|
|
|
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
|
-
require 'ramaze/template/ramaze/element'
|
|
6
|
-
require 'ramaze/template/ramaze/morpher'
|
|
7
|
-
|
|
8
|
-
module Ramaze::Template
|
|
9
|
-
|
|
10
|
-
# The usual Template for use of Ramaze.
|
|
11
|
-
# It supports erb-style interpolation and a pipeline for the transform
|
|
12
|
-
|
|
13
|
-
class Ramaze < Template
|
|
14
|
-
|
|
15
|
-
# Actions consist of both templates and methods on the controller.
|
|
16
|
-
trait :actionless => false
|
|
17
|
-
|
|
18
|
-
# Usual extensions for templates.
|
|
19
|
-
trait :template_extensions => %w[rmze xhtml rhtml html]
|
|
20
|
-
|
|
21
|
-
# Each Object in this Array will be called :transform upon.
|
|
22
|
-
trait :transform_pipeline => [ Element, Morpher, self ]
|
|
23
|
-
|
|
24
|
-
class << self
|
|
25
|
-
# initializes the handling of a request on the controller.
|
|
26
|
-
# Creates a new instances of itself and sends the action and params.
|
|
27
|
-
# Also tries to render the template.
|
|
28
|
-
# In Theory you can use this standalone, this has not been tested though.
|
|
29
|
-
|
|
30
|
-
def handle_request(action, *params)
|
|
31
|
-
controller = self.new
|
|
32
|
-
controller.send(:render, action, *params)
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
# in case someone wants to call directly (pipeline)
|
|
36
|
-
|
|
37
|
-
def transform(template, bound)
|
|
38
|
-
self.new.send(:transform, template, bound)
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
private
|
|
43
|
-
|
|
44
|
-
# render an action
|
|
45
|
-
# this looks up the file depending on your Global.template_root
|
|
46
|
-
# and also takes the mapping of the controller in account.
|
|
47
|
-
# To use the exception of method_missing as flow-control might seem odd,
|
|
48
|
-
# but it works perfect for method_missing defined in the controller...
|
|
49
|
-
# otherwise one has to modify respond_to?... maybe add a special take-all method?
|
|
50
|
-
#
|
|
51
|
-
# for example:
|
|
52
|
-
# / => FooController, /main => MainController
|
|
53
|
-
# an action to /bar would search directly in the template_root for a file called bar
|
|
54
|
-
# and the extensions rmze, xhtml or html (in that order)
|
|
55
|
-
# if it finds a file it will transform it and return the result
|
|
56
|
-
# otherwise nothing happens
|
|
57
|
-
#
|
|
58
|
-
# TODO:
|
|
59
|
-
# - maybe add some way to define a custom template-file per action via traits
|
|
60
|
-
# trait :methodname_template => :foo
|
|
61
|
-
# would point to the template-file of action :foo - template_root/foo.ext
|
|
62
|
-
# - add extensive tests!
|
|
63
|
-
#
|
|
64
|
-
|
|
65
|
-
def render(action, *params)
|
|
66
|
-
alternate = render_template(params.last) if params.size == 1 and action == 'index'
|
|
67
|
-
file_template = render_template(action, *params)
|
|
68
|
-
ctrl_template = render_action(action, *params)
|
|
69
|
-
|
|
70
|
-
pipeline(alternate || file_template || ctrl_template)
|
|
71
|
-
end
|
|
72
|
-
|
|
73
|
-
# Render an action, usually a method on the controller.
|
|
74
|
-
|
|
75
|
-
def render_action(action, *params)
|
|
76
|
-
ctrl_template = send(action, *params).to_s
|
|
77
|
-
rescue => e
|
|
78
|
-
Informer.error e unless e.message =~ /undefined method `#{Regexp.escape(action.to_s)}'/
|
|
79
|
-
|
|
80
|
-
unless caller.select{|bt| bt[/`render_action'/]}.size > 3
|
|
81
|
-
Dispatcher.respond_action([action, *params].join('/'))
|
|
82
|
-
ctrl_template = response.out
|
|
83
|
-
end
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
# Render the template.
|
|
87
|
-
|
|
88
|
-
def render_template(action, *params)
|
|
89
|
-
File.read(find_template(action)) rescue nil
|
|
90
|
-
end
|
|
91
|
-
|
|
92
|
-
# go through the pipeline and call #transform on every object found there,
|
|
93
|
-
# passing the template at that point.
|
|
94
|
-
# the order and contents of the pipeline are determined by an array
|
|
95
|
-
# in trait[:template_pipeline]
|
|
96
|
-
# the default being [Element, Morpher, self]
|
|
97
|
-
|
|
98
|
-
def pipeline(template)
|
|
99
|
-
transform_pipeline = ancestral_trait[:transform_pipeline]
|
|
100
|
-
|
|
101
|
-
transform_pipeline.inject(template) do |memo, current|
|
|
102
|
-
current.transform(memo, binding)
|
|
103
|
-
end
|
|
104
|
-
end
|
|
105
|
-
|
|
106
|
-
# transform a String to a final xhtml
|
|
107
|
-
#
|
|
108
|
-
# All ye who seek magic, look elsewhere, this method is ASAP (as simple as possible)
|
|
109
|
-
#
|
|
110
|
-
# There are some simple gsubs that build a final template which is evaluated
|
|
111
|
-
#
|
|
112
|
-
# The rules are following:
|
|
113
|
-
# <?r rubycode ?>
|
|
114
|
-
# evaluate the code inside the tag, this is considered XHTML-valid and so is the
|
|
115
|
-
# preferred method for executing code inside your templates.
|
|
116
|
-
# The return-value is ignored
|
|
117
|
-
# <% rubycode %>
|
|
118
|
-
# The same as <?r ?>, ERB-style and not valid XHTML, but should give someone who
|
|
119
|
-
# is already familiar with ERB some common ground
|
|
120
|
-
# #{ rubycode }
|
|
121
|
-
# You know this from normal ruby already and it's actually nothing else.
|
|
122
|
-
# Interpolation at the position in the template, isn't any special taggy format
|
|
123
|
-
# and therefor safe to use.
|
|
124
|
-
# <%= rubycode %>
|
|
125
|
-
# The result of this will be interpolated at the position in the template.
|
|
126
|
-
# Not valid XHTML either.
|
|
127
|
-
#
|
|
128
|
-
# Warning:
|
|
129
|
-
# the variables used in here have the schema _variable_ to make it harder to break stuff
|
|
130
|
-
# however, you should take care.
|
|
131
|
-
# At the time of writing, the variables used are:
|
|
132
|
-
# _start_heredoc_, _end_heredoc_, _template_, _out_, _file_ and _bufadd_
|
|
133
|
-
# However, you may reuse _ivs_ if you desperatly need it and just can't live without.
|
|
134
|
-
#
|
|
135
|
-
|
|
136
|
-
def transform(_template_, _binding_ = binding)
|
|
137
|
-
_start_heredoc_ = "T" << Digest::SHA1.hexdigest(_template_)
|
|
138
|
-
_start_heredoc_, _end_heredoc_ = "\n<<#{_start_heredoc_}\n", "\n#{_start_heredoc_}\n"
|
|
139
|
-
_bufadd_ = "_out_ << "
|
|
140
|
-
|
|
141
|
-
_file_, _template_ = _template_, File.read(_template_) if File.file?(_template_)
|
|
142
|
-
|
|
143
|
-
_template_.gsub!(/<%\s+(.*?)\s+%>/m,
|
|
144
|
-
"#{_end_heredoc_} \\1; #{_bufadd_} #{_start_heredoc_}")
|
|
145
|
-
_template_.gsub!(/<\?r\s+(.*?)\s+\?>/m,
|
|
146
|
-
"#{_end_heredoc_} \\1; #{_bufadd_} #{_start_heredoc_}")
|
|
147
|
-
|
|
148
|
-
_template_.gsub!(/<%=\s+(.*?)\s+%>/m,
|
|
149
|
-
"#{_end_heredoc_} #{_bufadd_} (\\1); #{_bufadd_} #{_start_heredoc_}")
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
# this one should not be used until we find and solution
|
|
153
|
-
# that allows for stuff like
|
|
154
|
-
# #[@foo]!
|
|
155
|
-
# we just don't allow anything except space or newline
|
|
156
|
-
# after the expression #[] to make it sane
|
|
157
|
-
#_template_.gsub!(/#\[(.*?)\]\s*$/) do |m|
|
|
158
|
-
# "#{_end_heredoc_} #{_bufadd_} (#{$1}); #{_bufadd_} #{_start_heredoc_}"
|
|
159
|
-
#end
|
|
160
|
-
|
|
161
|
-
_template_ = [_bufadd_, _start_heredoc_, _template_, _end_heredoc_].join(' ')
|
|
162
|
-
_out_ = eval(*["_out_ = []; #{_template_}; _out_", _binding_, _file_].compact)
|
|
163
|
-
|
|
164
|
-
_out_.map! do |line|
|
|
165
|
-
line.to_s.chomp
|
|
166
|
-
end
|
|
167
|
-
|
|
168
|
-
_template_ = _out_.join.strip
|
|
169
|
-
rescue Object => ex
|
|
170
|
-
Informer.error "something bad happened while transformation"
|
|
171
|
-
Informer.error ex
|
|
172
|
-
#raise Error::Template, "Problem during transformation for: #{request.request_path}"
|
|
173
|
-
{ex.message => _template_}.inspect
|
|
174
|
-
end
|
|
175
|
-
|
|
176
|
-
end
|
|
177
|
-
end
|
|
@@ -1,166 +0,0 @@
|
|
|
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
|
-
module Ramaze
|
|
5
|
-
|
|
6
|
-
# an Element is almost like an Controller, however, instead
|
|
7
|
-
# of connecting actions to templates it is only used in
|
|
8
|
-
# Ramaze::Template::Ramaze and can be used inside the
|
|
9
|
-
# templates of the Controller as a simple wrapper.
|
|
10
|
-
#
|
|
11
|
-
# Example:
|
|
12
|
-
#
|
|
13
|
-
# Your Element called Page:
|
|
14
|
-
#
|
|
15
|
-
# class Page < Element
|
|
16
|
-
# def render
|
|
17
|
-
# %{
|
|
18
|
-
# <html>
|
|
19
|
-
# <h1>
|
|
20
|
-
# #{@hash['title']}
|
|
21
|
-
# </h1>
|
|
22
|
-
# #{content}
|
|
23
|
-
# </html>
|
|
24
|
-
# }
|
|
25
|
-
# end
|
|
26
|
-
# end
|
|
27
|
-
#
|
|
28
|
-
# and one called SideBar
|
|
29
|
-
#
|
|
30
|
-
# class SideBar < Element
|
|
31
|
-
# def render
|
|
32
|
-
# %{
|
|
33
|
-
# <a href="http://something.com">something</a>
|
|
34
|
-
# }
|
|
35
|
-
# end
|
|
36
|
-
# end
|
|
37
|
-
#
|
|
38
|
-
# and your template (any template for any action):
|
|
39
|
-
#
|
|
40
|
-
# <Page title="Test">
|
|
41
|
-
# <SideBar />
|
|
42
|
-
# <p>
|
|
43
|
-
# Hello, World!
|
|
44
|
-
# </p>
|
|
45
|
-
# </Page>
|
|
46
|
-
#
|
|
47
|
-
# would result in:
|
|
48
|
-
#
|
|
49
|
-
# <html>
|
|
50
|
-
# <h1>
|
|
51
|
-
# Test
|
|
52
|
-
# </h1>
|
|
53
|
-
# <p>
|
|
54
|
-
# Hello, World!
|
|
55
|
-
# </p>
|
|
56
|
-
# </html>
|
|
57
|
-
|
|
58
|
-
class Element
|
|
59
|
-
extend Ramaze::Helper
|
|
60
|
-
|
|
61
|
-
helper :link, :redirect
|
|
62
|
-
|
|
63
|
-
attr_accessor :content
|
|
64
|
-
|
|
65
|
-
# this will be called by #transform, passes along the
|
|
66
|
-
# stuff inside the tags for the element
|
|
67
|
-
|
|
68
|
-
def initialize(content)
|
|
69
|
-
@content = content
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
# The method that will be called upon to render the things
|
|
73
|
-
# inside the element, you can access #content from here, which
|
|
74
|
-
# contains the contents between the tags.
|
|
75
|
-
#
|
|
76
|
-
# It should answer with a String.
|
|
77
|
-
|
|
78
|
-
def render *args
|
|
79
|
-
@content
|
|
80
|
-
end
|
|
81
|
-
|
|
82
|
-
class << self
|
|
83
|
-
# transforms all <Element> tags within the string, takes also
|
|
84
|
-
# a binding to be compatible to the transform-pipeline, won't have
|
|
85
|
-
# any use for it though.
|
|
86
|
-
#
|
|
87
|
-
# It also sets a instance-variable for you called @hash, which
|
|
88
|
-
# contains the parameters you gave the <Element> tag.
|
|
89
|
-
# See above for an example of writing and using them.
|
|
90
|
-
|
|
91
|
-
def transform string = '', binding = nil
|
|
92
|
-
string = string.to_s
|
|
93
|
-
matches = string.scan(/<([A-Z][a-zA-Z0-9]*)(.*?)?>/)
|
|
94
|
-
|
|
95
|
-
matches.each do |(klass, params)|
|
|
96
|
-
transformer = (params[-1,1] == '/' ? :without : :with)
|
|
97
|
-
string = send("transform_#{transformer}_content", string, klass)
|
|
98
|
-
end
|
|
99
|
-
string
|
|
100
|
-
end
|
|
101
|
-
|
|
102
|
-
# transforms elements like:
|
|
103
|
-
# <Page> some content </Page>
|
|
104
|
-
|
|
105
|
-
def transform_with_content(string, klass)
|
|
106
|
-
string.gsub(/<#{klass}( .*?)?>(.*?)<\/#{klass}>/m) do |m|
|
|
107
|
-
params, content = $1.to_s, $2.to_s
|
|
108
|
-
finish_transform(klass, params, content)
|
|
109
|
-
end
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
# transforms elements like:
|
|
113
|
-
# <Page />
|
|
114
|
-
|
|
115
|
-
def transform_without_content(string, klass)
|
|
116
|
-
string.gsub(/<#{klass}( .*?)?\/>/) do |m|
|
|
117
|
-
params = $1.to_s
|
|
118
|
-
finish_transform(klass, params, content = '')
|
|
119
|
-
end
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
# find the element, create an instance, pass it the content
|
|
123
|
-
# check if it responds to :render and set an instance-variable
|
|
124
|
-
# called @hash to hold the parameters passed to the element.
|
|
125
|
-
#
|
|
126
|
-
# Parameters look like:
|
|
127
|
-
# <Page foo="true"> bar </Page>
|
|
128
|
-
# <Page foo="true" />
|
|
129
|
-
|
|
130
|
-
def finish_transform(klass, params, content)
|
|
131
|
-
instance = constant(klass).new(content) rescue nil
|
|
132
|
-
|
|
133
|
-
return unless instance and instance.respond_to?(:render)
|
|
134
|
-
|
|
135
|
-
hash = demunge_passed_variables(params)
|
|
136
|
-
instance.instance_variable_set("@hash", hash)
|
|
137
|
-
|
|
138
|
-
instance.render
|
|
139
|
-
end
|
|
140
|
-
|
|
141
|
-
# basically processes stuff like
|
|
142
|
-
# 'foo="bar" foobar="baz"'
|
|
143
|
-
# do NOT pass actual objects that cannot be simply read as a string
|
|
144
|
-
# here, the information will be lost.
|
|
145
|
-
#
|
|
146
|
-
# Exceptions are true, false, Integers and Floats. They will appear
|
|
147
|
-
# in their real form (this again is also valid for strings that contain
|
|
148
|
-
# these values in a way that makes Integer/Float possible to parse them)
|
|
149
|
-
#
|
|
150
|
-
# Just remember, walk like a duck, talk like a duck.
|
|
151
|
-
|
|
152
|
-
def demunge_passed_variables(string)
|
|
153
|
-
string.scan(/\s?(.*?)="(.*?)"/).inject({}) do |hash, (key, value)|
|
|
154
|
-
value =
|
|
155
|
-
case value
|
|
156
|
-
when 'true' : true
|
|
157
|
-
when 'false' : false
|
|
158
|
-
else
|
|
159
|
-
Integer(value) rescue Float(value) rescue value
|
|
160
|
-
end
|
|
161
|
-
hash.merge key => value
|
|
162
|
-
end
|
|
163
|
-
end
|
|
164
|
-
end
|
|
165
|
-
end
|
|
166
|
-
end
|
|
@@ -1,156 +0,0 @@
|
|
|
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
|
-
module Ramaze
|
|
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::Ramaze.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 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, bound = nil
|
|
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, bound = nil)
|
|
149
|
-
template
|
|
150
|
-
end
|
|
151
|
-
end
|
|
152
|
-
|
|
153
|
-
template
|
|
154
|
-
end
|
|
155
|
-
end
|
|
156
|
-
end
|