malline 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +674 -0
- data/COPYING.LESSER +165 -0
- data/README +24 -0
- data/bin/malline +20 -0
- data/lib/malline.rb +108 -0
- data/lib/malline/erb_out.rb +29 -0
- data/lib/malline/form_builder.rb +33 -0
- data/lib/malline/rails.rb +45 -0
- data/lib/malline/template.rb +124 -0
- data/lib/malline/view_proxy.rb +54 -0
- data/lib/malline/view_wrapper.rb +91 -0
- data/lib/malline/view_xhtml.rb +71 -0
- data/scripts/html2mn.rb +93 -0
- data/test/examples/_action.mn +4 -0
- data/test/examples/_action.target +1 -0
- data/test/examples/_one.mn +1 -0
- data/test/examples/_one.target +1 -0
- data/test/examples/_partial.mn +2 -0
- data/test/examples/_partial.target +1 -0
- data/test/examples/_three.rhtml +2 -0
- data/test/examples/_two.mn +1 -0
- data/test/examples/_two.target +1 -0
- data/test/examples/capture.mn +13 -0
- data/test/examples/capture.target +1 -0
- data/test/examples/class.mn +6 -0
- data/test/examples/class.target +1 -0
- data/test/examples/escape.mn +4 -0
- data/test/examples/escape.target +1 -0
- data/test/examples/fragment_cache.mn +10 -0
- data/test/examples/fragment_cache.target +1 -0
- data/test/examples/frontpage.mn +6 -0
- data/test/examples/frontpage.target +1 -0
- data/test/examples/hello_world.mn +2 -0
- data/test/examples/hello_world.target +1 -0
- data/test/examples/helper.mn +5 -0
- data/test/examples/helper.target +1 -0
- data/test/examples/helper2.mn +5 -0
- data/test/examples/helper2.target +1 -0
- data/test/examples/helper_shortcut.mn +5 -0
- data/test/examples/helper_shortcut.target +1 -0
- data/test/examples/id.mn +3 -0
- data/test/examples/id.target +1 -0
- data/test/examples/layout.mn +8 -0
- data/test/examples/layout.target +4 -0
- data/test/examples/lists.mn +13 -0
- data/test/examples/lists.target +1 -0
- data/test/examples/nested.mn +6 -0
- data/test/examples/nested.target +1 -0
- data/test/examples/options.mn +10 -0
- data/test/examples/options.target +1 -0
- data/test/examples/partials.mn +5 -0
- data/test/examples/partials.target +1 -0
- data/test/examples/self.mn +2 -0
- data/test/examples/self.target +1 -0
- data/test/examples/text.mn +4 -0
- data/test/examples/text.target +1 -0
- data/test/examples/whitespace.mn +9 -0
- data/test/examples/whitespace.target +1 -0
- data/test/examples/xhtml.mn +11 -0
- data/test/examples/xhtml.target +4 -0
- data/test/kernel.org.html +107 -0
- data/test/kernel.org.mn +657 -0
- data/test/malline_test.rb +192 -0
- data/test/malline_test_helpers.rb +57 -0
- metadata +110 -0
data/COPYING.LESSER
ADDED
@@ -0,0 +1,165 @@
|
|
1
|
+
GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
+
Version 3, 29 June 2007
|
3
|
+
|
4
|
+
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
5
|
+
Everyone is permitted to copy and distribute verbatim copies
|
6
|
+
of this license document, but changing it is not allowed.
|
7
|
+
|
8
|
+
|
9
|
+
This version of the GNU Lesser General Public License incorporates
|
10
|
+
the terms and conditions of version 3 of the GNU General Public
|
11
|
+
License, supplemented by the additional permissions listed below.
|
12
|
+
|
13
|
+
0. Additional Definitions.
|
14
|
+
|
15
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
|
+
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
|
+
General Public License.
|
18
|
+
|
19
|
+
"The Library" refers to a covered work governed by this License,
|
20
|
+
other than an Application or a Combined Work as defined below.
|
21
|
+
|
22
|
+
An "Application" is any work that makes use of an interface provided
|
23
|
+
by the Library, but which is not otherwise based on the Library.
|
24
|
+
Defining a subclass of a class defined by the Library is deemed a mode
|
25
|
+
of using an interface provided by the Library.
|
26
|
+
|
27
|
+
A "Combined Work" is a work produced by combining or linking an
|
28
|
+
Application with the Library. The particular version of the Library
|
29
|
+
with which the Combined Work was made is also called the "Linked
|
30
|
+
Version".
|
31
|
+
|
32
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
33
|
+
Corresponding Source for the Combined Work, excluding any source code
|
34
|
+
for portions of the Combined Work that, considered in isolation, are
|
35
|
+
based on the Application, and not on the Linked Version.
|
36
|
+
|
37
|
+
The "Corresponding Application Code" for a Combined Work means the
|
38
|
+
object code and/or source code for the Application, including any data
|
39
|
+
and utility programs needed for reproducing the Combined Work from the
|
40
|
+
Application, but excluding the System Libraries of the Combined Work.
|
41
|
+
|
42
|
+
1. Exception to Section 3 of the GNU GPL.
|
43
|
+
|
44
|
+
You may convey a covered work under sections 3 and 4 of this License
|
45
|
+
without being bound by section 3 of the GNU GPL.
|
46
|
+
|
47
|
+
2. Conveying Modified Versions.
|
48
|
+
|
49
|
+
If you modify a copy of the Library, and, in your modifications, a
|
50
|
+
facility refers to a function or data to be supplied by an Application
|
51
|
+
that uses the facility (other than as an argument passed when the
|
52
|
+
facility is invoked), then you may convey a copy of the modified
|
53
|
+
version:
|
54
|
+
|
55
|
+
a) under this License, provided that you make a good faith effort to
|
56
|
+
ensure that, in the event an Application does not supply the
|
57
|
+
function or data, the facility still operates, and performs
|
58
|
+
whatever part of its purpose remains meaningful, or
|
59
|
+
|
60
|
+
b) under the GNU GPL, with none of the additional permissions of
|
61
|
+
this License applicable to that copy.
|
62
|
+
|
63
|
+
3. Object Code Incorporating Material from Library Header Files.
|
64
|
+
|
65
|
+
The object code form of an Application may incorporate material from
|
66
|
+
a header file that is part of the Library. You may convey such object
|
67
|
+
code under terms of your choice, provided that, if the incorporated
|
68
|
+
material is not limited to numerical parameters, data structure
|
69
|
+
layouts and accessors, or small macros, inline functions and templates
|
70
|
+
(ten or fewer lines in length), you do both of the following:
|
71
|
+
|
72
|
+
a) Give prominent notice with each copy of the object code that the
|
73
|
+
Library is used in it and that the Library and its use are
|
74
|
+
covered by this License.
|
75
|
+
|
76
|
+
b) Accompany the object code with a copy of the GNU GPL and this license
|
77
|
+
document.
|
78
|
+
|
79
|
+
4. Combined Works.
|
80
|
+
|
81
|
+
You may convey a Combined Work under terms of your choice that,
|
82
|
+
taken together, effectively do not restrict modification of the
|
83
|
+
portions of the Library contained in the Combined Work and reverse
|
84
|
+
engineering for debugging such modifications, if you also do each of
|
85
|
+
the following:
|
86
|
+
|
87
|
+
a) Give prominent notice with each copy of the Combined Work that
|
88
|
+
the Library is used in it and that the Library and its use are
|
89
|
+
covered by this License.
|
90
|
+
|
91
|
+
b) Accompany the Combined Work with a copy of the GNU GPL and this license
|
92
|
+
document.
|
93
|
+
|
94
|
+
c) For a Combined Work that displays copyright notices during
|
95
|
+
execution, include the copyright notice for the Library among
|
96
|
+
these notices, as well as a reference directing the user to the
|
97
|
+
copies of the GNU GPL and this license document.
|
98
|
+
|
99
|
+
d) Do one of the following:
|
100
|
+
|
101
|
+
0) Convey the Minimal Corresponding Source under the terms of this
|
102
|
+
License, and the Corresponding Application Code in a form
|
103
|
+
suitable for, and under terms that permit, the user to
|
104
|
+
recombine or relink the Application with a modified version of
|
105
|
+
the Linked Version to produce a modified Combined Work, in the
|
106
|
+
manner specified by section 6 of the GNU GPL for conveying
|
107
|
+
Corresponding Source.
|
108
|
+
|
109
|
+
1) Use a suitable shared library mechanism for linking with the
|
110
|
+
Library. A suitable mechanism is one that (a) uses at run time
|
111
|
+
a copy of the Library already present on the user's computer
|
112
|
+
system, and (b) will operate properly with a modified version
|
113
|
+
of the Library that is interface-compatible with the Linked
|
114
|
+
Version.
|
115
|
+
|
116
|
+
e) Provide Installation Information, but only if you would otherwise
|
117
|
+
be required to provide such information under section 6 of the
|
118
|
+
GNU GPL, and only to the extent that such information is
|
119
|
+
necessary to install and execute a modified version of the
|
120
|
+
Combined Work produced by recombining or relinking the
|
121
|
+
Application with a modified version of the Linked Version. (If
|
122
|
+
you use option 4d0, the Installation Information must accompany
|
123
|
+
the Minimal Corresponding Source and Corresponding Application
|
124
|
+
Code. If you use option 4d1, you must provide the Installation
|
125
|
+
Information in the manner specified by section 6 of the GNU GPL
|
126
|
+
for conveying Corresponding Source.)
|
127
|
+
|
128
|
+
5. Combined Libraries.
|
129
|
+
|
130
|
+
You may place library facilities that are a work based on the
|
131
|
+
Library side by side in a single library together with other library
|
132
|
+
facilities that are not Applications and are not covered by this
|
133
|
+
License, and convey such a combined library under terms of your
|
134
|
+
choice, if you do both of the following:
|
135
|
+
|
136
|
+
a) Accompany the combined library with a copy of the same work based
|
137
|
+
on the Library, uncombined with any other library facilities,
|
138
|
+
conveyed under the terms of this License.
|
139
|
+
|
140
|
+
b) Give prominent notice with the combined library that part of it
|
141
|
+
is a work based on the Library, and explaining where to find the
|
142
|
+
accompanying uncombined form of the same work.
|
143
|
+
|
144
|
+
6. Revised Versions of the GNU Lesser General Public License.
|
145
|
+
|
146
|
+
The Free Software Foundation may publish revised and/or new versions
|
147
|
+
of the GNU Lesser General Public License from time to time. Such new
|
148
|
+
versions will be similar in spirit to the present version, but may
|
149
|
+
differ in detail to address new problems or concerns.
|
150
|
+
|
151
|
+
Each version is given a distinguishing version number. If the
|
152
|
+
Library as you received it specifies that a certain numbered version
|
153
|
+
of the GNU Lesser General Public License "or any later version"
|
154
|
+
applies to it, you have the option of following the terms and
|
155
|
+
conditions either of that published version or of any later version
|
156
|
+
published by the Free Software Foundation. If the Library as you
|
157
|
+
received it does not specify a version number of the GNU Lesser
|
158
|
+
General Public License, you may choose any version of the GNU Lesser
|
159
|
+
General Public License ever published by the Free Software Foundation.
|
160
|
+
|
161
|
+
If the Library as you received it specifies that a proxy can decide
|
162
|
+
whether future versions of the GNU Lesser General Public License shall
|
163
|
+
apply, that proxy's public statement of acceptance of any version is
|
164
|
+
permanent authorization for you to choose that version for the
|
165
|
+
Library.
|
data/README
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
Malline 1.0.2
|
2
|
+
=============
|
3
|
+
|
4
|
+
See documentation on http://www.malline.org/
|
5
|
+
|
6
|
+
Copyright © 2007,2008 Riku Palomäki, riku@palomaki.fi
|
7
|
+
Malline is released under GNU Lesser General Public License.
|
8
|
+
|
9
|
+
|
10
|
+
Example template file images.mn:
|
11
|
+
|
12
|
+
html do
|
13
|
+
_render :partial => 'head'
|
14
|
+
body do
|
15
|
+
div.images! "There are some images:" do
|
16
|
+
images.each do |im|
|
17
|
+
a(:href => img_path(im)) { img :src => im.url }
|
18
|
+
span.caption im.caption
|
19
|
+
end
|
20
|
+
_"No more images"
|
21
|
+
end
|
22
|
+
div.footer! { _render :partial => 'footer' }
|
23
|
+
end
|
24
|
+
end
|
data/bin/malline
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$: << File.join(File.dirname(__FILE__), '..', 'lib')
|
4
|
+
require 'malline.rb'
|
5
|
+
|
6
|
+
nonstrict = ARGV.delete('-ns')
|
7
|
+
|
8
|
+
input = File.open(ARGV.shift, 'r') rescue $stdin
|
9
|
+
output = File.open(ARGV.shift, 'w') rescue $stdout
|
10
|
+
|
11
|
+
if File.exists?('malline_helper.rb')
|
12
|
+
require 'malline_helper.rb'
|
13
|
+
view = MallineHelper.new
|
14
|
+
else
|
15
|
+
view = nil
|
16
|
+
end
|
17
|
+
|
18
|
+
mn = Malline::Base.new(view, :strict => (nonstrict ? false : true))
|
19
|
+
|
20
|
+
output.puts mn.render(input.read)
|
data/lib/malline.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
# Copyright © 2007,2008 Riku Palomäki
|
2
|
+
#
|
3
|
+
# This file is part of Malline.
|
4
|
+
#
|
5
|
+
# Malline is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Malline is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU Lesser General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU Lesser General Public License
|
16
|
+
# along with Malline. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
require 'malline/view_proxy.rb'
|
19
|
+
require 'malline/view_wrapper.rb'
|
20
|
+
require 'malline/view_xhtml.rb'
|
21
|
+
require 'malline/erb_out.rb'
|
22
|
+
require 'malline/form_builder.rb'
|
23
|
+
require 'malline/template.rb'
|
24
|
+
|
25
|
+
module Malline
|
26
|
+
VERSION = '1.0.2'
|
27
|
+
|
28
|
+
# Template-handler class that is registered to ActionView and initialized by it.
|
29
|
+
class Base
|
30
|
+
# Default options for new instances, can be changed with setopt
|
31
|
+
@@options = { :strict => true, :xhtml => true, :encoding => 'UTF-8', :lang => 'en', :form_for_proxy => true }
|
32
|
+
attr_reader :view
|
33
|
+
|
34
|
+
# First parameter is the view object (if any)
|
35
|
+
# Last parameter is optional options hash
|
36
|
+
def initialize(*opts)
|
37
|
+
@options = @@options.dup
|
38
|
+
@options.merge! opts.pop if opts.last.is_a?(Hash)
|
39
|
+
|
40
|
+
@view = opts.shift || Class.new
|
41
|
+
unless @view.is_a?(ViewWrapper)
|
42
|
+
@view.extend ViewWrapper
|
43
|
+
@view.malline @options
|
44
|
+
Malline::XHTML.load_plugin self if @options[:xhtml]
|
45
|
+
else
|
46
|
+
@view.malline @options
|
47
|
+
end
|
48
|
+
|
49
|
+
if @options[:form_for_proxy]
|
50
|
+
begin
|
51
|
+
ActionView::Base.default_form_builder = ::Malline::FormBuilder
|
52
|
+
rescue NameError
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def set_path path
|
58
|
+
@view.malline.path = path
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.setopt hash
|
62
|
+
output = nil
|
63
|
+
if block_given?
|
64
|
+
o = @@options.dup
|
65
|
+
@@options.merge!(hash) if hash
|
66
|
+
begin
|
67
|
+
output = yield
|
68
|
+
ensure
|
69
|
+
@@options = o
|
70
|
+
end
|
71
|
+
else
|
72
|
+
@@options.merge!(hash)
|
73
|
+
end
|
74
|
+
output
|
75
|
+
end
|
76
|
+
|
77
|
+
# n is there to keep things compatible with Markaby
|
78
|
+
def render tpl = nil, local_assigns = {}, n = nil, &block
|
79
|
+
add_local_assigns local_assigns
|
80
|
+
@view.malline_is_active = true
|
81
|
+
@view.malline.run tpl, &block
|
82
|
+
end
|
83
|
+
|
84
|
+
def self.render tpl = nil, local_assigns = {}, &block
|
85
|
+
self.new.render(tpl, local_assigns, &block)
|
86
|
+
end
|
87
|
+
|
88
|
+
# TODO: These should also be able to disable
|
89
|
+
def definetags *tags
|
90
|
+
tags.each do |tag|
|
91
|
+
eval %{
|
92
|
+
def @view.#{tag}(*args, &block)
|
93
|
+
tag!('#{tag}', *args, &block)
|
94
|
+
end
|
95
|
+
}
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
def add_local_assigns l
|
101
|
+
@view.instance_eval do
|
102
|
+
l.each { |key, value| instance_variable_set "@#{key}", value }
|
103
|
+
evaluate_assigns if respond_to?(:evaluate_assigns, true)
|
104
|
+
class << self; self; end.send(:attr_accessor, *(l.keys))
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# Copyright © 2007,2008 Riku Palomäki
|
2
|
+
#
|
3
|
+
# This file is part of Malline.
|
4
|
+
#
|
5
|
+
# Malline is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Malline is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU Lesser General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU Lesser General Public License
|
16
|
+
# along with Malline. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module Malline
|
19
|
+
# ERB emulator
|
20
|
+
class ErbOut
|
21
|
+
def initialize view
|
22
|
+
@view = view
|
23
|
+
end
|
24
|
+
def concat value
|
25
|
+
@view << value
|
26
|
+
end
|
27
|
+
alias_method :<<, :concat
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# Copyright © 2007,2008 Riku Palomäki
|
2
|
+
#
|
3
|
+
# This file is part of Malline.
|
4
|
+
#
|
5
|
+
# Malline is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Malline is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU Lesser General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU Lesser General Public License
|
16
|
+
# along with Malline. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module Malline
|
19
|
+
class FormBuilder
|
20
|
+
def initialize *args
|
21
|
+
@view = eval('self', args.last)
|
22
|
+
@view = nil unless @view.respond_to?(:is_malline?) && @view.is_malline?
|
23
|
+
@builder = ::ActionView::Helpers::FormBuilder.new(*args)
|
24
|
+
end
|
25
|
+
def method_missing *args, &block
|
26
|
+
if @view
|
27
|
+
@view << @builder.send(*args, &block)
|
28
|
+
else
|
29
|
+
@builder.send(*args, &block)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# Copyright © 2007,2008 Riku Palomäki
|
2
|
+
#
|
3
|
+
# This file is part of Malline.
|
4
|
+
#
|
5
|
+
# Malline is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Malline is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU Lesser General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU Lesser General Public License
|
16
|
+
# along with Malline. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
require 'malline' unless Kernel.const_defined?('Malline')
|
19
|
+
|
20
|
+
ActionView::Base.register_template_handler 'mn', Malline::Base
|
21
|
+
|
22
|
+
module ActionView
|
23
|
+
class Base
|
24
|
+
alias_method :orig_render_template, :render_template
|
25
|
+
def render_template template_extension, template, file_path = nil, *rest
|
26
|
+
@current_tpl_path = file_path
|
27
|
+
orig_render_template(template_extension, template, file_path, *rest)
|
28
|
+
end
|
29
|
+
|
30
|
+
alias_method :orig_delegate_render, :delegate_render
|
31
|
+
def delegate_render(handler, template, local_assigns)
|
32
|
+
old = @malline_is_active
|
33
|
+
tmp = if handler == Malline::Base
|
34
|
+
h = handler.new(self)
|
35
|
+
h.set_path(@current_tpl_path) if @current_tpl_path
|
36
|
+
h.render(template, local_assigns)
|
37
|
+
else
|
38
|
+
@malline_is_active = false
|
39
|
+
orig_delegate_render(handler, template, local_assigns)
|
40
|
+
end
|
41
|
+
@malline_is_active = old
|
42
|
+
tmp
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
# Copyright © 2007,2008 Riku Palomäki
|
2
|
+
#
|
3
|
+
# This file is part of Malline.
|
4
|
+
#
|
5
|
+
# Malline is free software: you can redistribute it and/or modify
|
6
|
+
# it under the terms of the GNU Lesser General Public License as published by
|
7
|
+
# the Free Software Foundation, either version 3 of the License, or
|
8
|
+
# (at your option) any later version.
|
9
|
+
#
|
10
|
+
# Malline is distributed in the hope that it will be useful,
|
11
|
+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12
|
+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13
|
+
# GNU Lesser General Public License for more details.
|
14
|
+
#
|
15
|
+
# You should have received a copy of the GNU Lesser General Public License
|
16
|
+
# along with Malline. If not, see <http://www.gnu.org/licenses/>.
|
17
|
+
|
18
|
+
module Malline
|
19
|
+
class Template
|
20
|
+
attr_accessor :options
|
21
|
+
attr_accessor :short_tag_excludes
|
22
|
+
attr_accessor :whitespace
|
23
|
+
attr_accessor :path
|
24
|
+
|
25
|
+
def initialize view, opts
|
26
|
+
@view = view
|
27
|
+
@whitespace = false
|
28
|
+
@path = 'Malline template'
|
29
|
+
@options = opts
|
30
|
+
@short_tag_excludes = []
|
31
|
+
end
|
32
|
+
|
33
|
+
# These two are stolen from ERB
|
34
|
+
# © 1999-2000,2002,2003 Masatoshi SEKI
|
35
|
+
def self.html_escape(s)
|
36
|
+
s.to_s.gsub(/&/, "&").gsub(/\"/, """).gsub(/>/, ">").gsub(/</, "<")
|
37
|
+
end
|
38
|
+
def self.url_encode(s)
|
39
|
+
s.to_s.gsub(/[^a-zA-Z0-9_\-.]/n){ sprintf("%%%02X", $&.unpack("C")[0]) }
|
40
|
+
end
|
41
|
+
|
42
|
+
# Changes dom to active @dom, and executes tpl / block
|
43
|
+
def execute dom, tpl = nil, &block
|
44
|
+
tmp = @dom
|
45
|
+
@dom = dom
|
46
|
+
if block_given?
|
47
|
+
@view.instance_eval &block
|
48
|
+
else
|
49
|
+
@view.instance_eval tpl, @path
|
50
|
+
end
|
51
|
+
@dom = tmp
|
52
|
+
end
|
53
|
+
|
54
|
+
def add_text *values
|
55
|
+
@dom << ' ' if @whitespace
|
56
|
+
@dom << Template.html_escape(values.join(' '))
|
57
|
+
end
|
58
|
+
|
59
|
+
def add_unescaped_text value
|
60
|
+
@dom << ' ' if @whitespace
|
61
|
+
@dom << value.to_s unless value.nil?
|
62
|
+
end
|
63
|
+
|
64
|
+
def helper helper, *args, &block
|
65
|
+
tmp = @view.send(helper, *args, &block)
|
66
|
+
@dom << ' ' if @whitespace
|
67
|
+
@dom << tmp.to_s
|
68
|
+
tmp
|
69
|
+
end
|
70
|
+
|
71
|
+
def tag s, *args, &block
|
72
|
+
tag = { :name => s.to_s, :attrs => {}, :children => [] }
|
73
|
+
|
74
|
+
tag[:whitespace] = true if @whitespace
|
75
|
+
whitespace = @whitespace
|
76
|
+
@whitespace = true if args.delete(:whitespace)
|
77
|
+
|
78
|
+
if args.last.is_a?(Hash)
|
79
|
+
tag[:attrs].merge!(args.pop)
|
80
|
+
end
|
81
|
+
|
82
|
+
txt = args.flatten.join('')
|
83
|
+
tag[:children] << Template.html_escape(txt) unless txt.empty?
|
84
|
+
|
85
|
+
@dom << tag
|
86
|
+
execute tag[:children], &block if block_given?
|
87
|
+
@whitespace = whitespace
|
88
|
+
|
89
|
+
ViewProxy.new self, tag
|
90
|
+
end
|
91
|
+
|
92
|
+
# Render the xml tree at dom or root
|
93
|
+
def render dom = nil
|
94
|
+
(dom || @dom).inject('') do |out, tag|
|
95
|
+
if tag.is_a?(String)
|
96
|
+
out << tag
|
97
|
+
else
|
98
|
+
out << ' ' if tag[:whitespace]
|
99
|
+
out << "<#{tag[:name]}"
|
100
|
+
out << tag[:attrs].inject(''){|s, a| s += " #{a.first}=\"#{Template.html_escape(a.last)}\""}
|
101
|
+
|
102
|
+
if tag[:children].empty?
|
103
|
+
if @short_tag_excludes.include?(tag[:name])
|
104
|
+
out << "></#{tag[:name]}>"
|
105
|
+
else
|
106
|
+
out << '/>'
|
107
|
+
end
|
108
|
+
else
|
109
|
+
out << '>'
|
110
|
+
out << render(tag[:children])
|
111
|
+
out << "</#{tag[:name]}>"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
# Execute and render a text or block
|
118
|
+
def run tpl = nil, &block
|
119
|
+
tmp = []
|
120
|
+
execute tmp, tpl, &block
|
121
|
+
render tmp
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|