renshi 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README +9 -22
- data/lib/renshi/parser.rb +6 -2
- data/lib/renshi.rb +1 -1
- data/spec/each_spec.rb +5 -4
- data/spec/unless_spec.rb +3 -1
- data/spec/while_spec.rb +2 -0
- metadata +1 -1
data/README
CHANGED
@@ -2,21 +2,19 @@ Renshi is a templating language for Ruby which is unobtrusive in HTML and XHTML
|
|
2
2
|
|
3
3
|
$ Code
|
4
4
|
======
|
5
|
-
To output data on the page stick a $ in front of your expression
|
5
|
+
To output data on the page stick a $ in front of your var. or expression
|
6
6
|
|
7
7
|
$foo
|
8
8
|
$Time.now
|
9
9
|
|
10
|
-
If there's a space in your expression or you need formatting
|
10
|
+
If there's a space in your expression or you need formatting it use ${}
|
11
11
|
|
12
12
|
${foo},$Time.now
|
13
13
|
|
14
|
-
To insert
|
14
|
+
To insert statements in your template use $[]
|
15
15
|
|
16
16
|
$[foo = "hello world"]
|
17
17
|
|
18
|
-
$foo
|
19
|
-
|
20
18
|
Attribute Expressions
|
21
19
|
===================
|
22
20
|
Renshi has a small library of expressions which can be inserted into HTML or XML elements as attributes.
|
@@ -26,7 +24,7 @@ r:expression_name="expression" - the expression value is treated as a Ruby expre
|
|
26
24
|
|
27
25
|
Some expressions, such as r:each, use the quoted value to also hold parameter information.
|
28
26
|
|
29
|
-
|
27
|
+
Current Attribute Expressions
|
30
28
|
* If
|
31
29
|
* Unless
|
32
30
|
* Elsif
|
@@ -80,18 +78,6 @@ but this is not:
|
|
80
78
|
<div id="user_menu"> ... </div>
|
81
79
|
</div>
|
82
80
|
|
83
|
-
but this is:
|
84
|
-
|
85
|
-
<div id="menu" r:if="user.is_administrator?">
|
86
|
-
<div id="admin_menu"> ... </div>
|
87
|
-
<div id="menu" r:else>
|
88
|
-
<div id="user_menu"> ... </div>
|
89
|
-
</div>
|
90
|
-
<div id="register">
|
91
|
-
<div id="register"> ... </div>
|
92
|
-
</div>
|
93
|
-
|
94
|
-
|
95
81
|
* While
|
96
82
|
|
97
83
|
<div r:while="foo != 2" id="content$foo">
|
@@ -126,6 +112,10 @@ hello99
|
|
126
112
|
</div>
|
127
113
|
|
128
114
|
* Each
|
115
|
+
The expression takes the object and then any arguments for the each block after a comma; so you can do anything with it that each supports.
|
116
|
+
|
117
|
+
r:each="foos, |k,v|", r:each="foos, |foo|"..
|
118
|
+
|
129
119
|
<div id="content$foo" r:each="foos, |foo|">
|
130
120
|
hello$foo
|
131
121
|
</div>
|
@@ -138,13 +128,10 @@ hello0
|
|
138
128
|
hello99
|
139
129
|
</div>
|
140
130
|
|
141
|
-
|
142
131
|
Further elements will appear as I have time to make them or others want to contribute them. There isn't much to making them.
|
143
132
|
|
144
133
|
See renshi/lib/renshi/attribute_expressions for how they work.
|
145
134
|
|
146
|
-
Planned element expressions:
|
147
|
-
|
148
135
|
Other Rules
|
149
136
|
===========
|
150
137
|
$ values inside of attributes are only interpreted as Renshi variables for regular attributes (not renshi attributes).
|
@@ -190,7 +177,7 @@ Firstly, it doesn't need a reason. It's a fun project.
|
|
190
177
|
But ...
|
191
178
|
|
192
179
|
I've always found ERB to be a bit cumbersome - <%= is quite tiring to type out when you realise it could be much shorter. I used to think that Velocity, in Java,
|
193
|
-
was the
|
180
|
+
was the most fun templating language I'd used, and I had wanted something equally as concise for Ruby.
|
194
181
|
|
195
182
|
A real need for it emerged in a project which relied upon an external design house handing us HTML. Converting it incessantly into HAML was nightmarish. A colleague
|
196
183
|
mentioned Genshi in Python as ideal, which was when the idea of Renshi was conceived.
|
data/lib/renshi/parser.rb
CHANGED
@@ -26,7 +26,7 @@ module Renshi
|
|
26
26
|
|
27
27
|
inner_html = doc.inner_html
|
28
28
|
compiled = compile_to_buffer(inner_html) if inner_html
|
29
|
-
|
29
|
+
# puts "\n", compiled, "\n"
|
30
30
|
return compiled
|
31
31
|
end
|
32
32
|
|
@@ -67,6 +67,8 @@ module Renshi
|
|
67
67
|
if text[(idx + 1)..(idx + 1)] == "("
|
68
68
|
#this may be jquery, etc. $(...)
|
69
69
|
return text
|
70
|
+
|
71
|
+
#${...}
|
70
72
|
elsif text[(idx + 1)..(idx + 1)] == "{"
|
71
73
|
begin
|
72
74
|
closing_brace_idx = text.rindex("}")
|
@@ -77,6 +79,8 @@ module Renshi
|
|
77
79
|
rescue Renshi::StandardError
|
78
80
|
raise SyntaxError, "No closing brace: #{text[(idx +1)..-1]}", caller
|
79
81
|
end
|
82
|
+
|
83
|
+
#$[...]
|
80
84
|
elsif text[(idx + 1)..(idx + 1)] == "["
|
81
85
|
begin
|
82
86
|
closing_brace_idx = text.rindex("]")
|
@@ -106,7 +110,7 @@ module Renshi
|
|
106
110
|
end
|
107
111
|
idx = next_statement_idx
|
108
112
|
end
|
109
|
-
|
113
|
+
|
110
114
|
return bits.join
|
111
115
|
end
|
112
116
|
|
data/lib/renshi.rb
CHANGED
data/spec/each_spec.rb
CHANGED
@@ -2,11 +2,12 @@ require File.dirname(__FILE__) + '/spec_helper'
|
|
2
2
|
require 'nokogiri'
|
3
3
|
|
4
4
|
describe Renshi::Parser do
|
5
|
-
it "should evaluate r:
|
5
|
+
it "should evaluate r:each" do
|
6
6
|
foos = [0,1,2]
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
doc = Nokogiri::HTML("<span id='red$foo' r:each='foos, |foo|'>hello$foo</span>")
|
8
|
+
compiled = Renshi::Parser.parse(doc.root.to_s)
|
9
|
+
doc = eval(compiled, binding)
|
10
|
+
doc = N(doc)
|
10
11
|
(doc/"span[@id='red0']").text.strip.should eql "hello0"
|
11
12
|
(doc/"span[@id='red1']").text.strip.should eql "hello1"
|
12
13
|
(doc/"span[@id='red2']").text.strip.should eql "hello2"
|
data/spec/unless_spec.rb
CHANGED
@@ -5,7 +5,9 @@ describe Renshi::Parser do
|
|
5
5
|
it "should evaluate r:unless(false)" do
|
6
6
|
doc = Nokogiri::HTML("<span id='red' r:unless='false'>hello</span>")
|
7
7
|
compiled = Renshi::Parser.parse(doc.root.to_s)
|
8
|
+
puts compiled
|
8
9
|
out = eval(compiled, binding)
|
9
|
-
|
10
|
+
out = N(out)
|
11
|
+
(out/"span[@id='red']").text.strip.should eql "hello"
|
10
12
|
end
|
11
13
|
end
|
data/spec/while_spec.rb
CHANGED