mote 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +36 -20
- data/lib/mote.rb +5 -2
- data/test/mote_test.rb +23 -0
- metadata +5 -5
data/README.md
CHANGED
@@ -13,18 +13,21 @@ Usage
|
|
13
13
|
|
14
14
|
Usage is very similar to that of ERB:
|
15
15
|
|
16
|
-
|
17
|
-
|
16
|
+
```ruby
|
17
|
+
template = Mote.parse("This is a template")
|
18
|
+
template.call #=> "This is a template"
|
19
|
+
```
|
18
20
|
|
19
21
|
Silly example, you may say, and I would agree. What follows is a short list of
|
20
22
|
the different use cases you may face:
|
21
23
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
```
|
25
|
+
% if user == "Bruno"
|
26
|
+
{{user}} rhymes with Piano
|
27
|
+
% elsif user == "Brutus"
|
28
|
+
{{user}} rhymes with Opus
|
29
|
+
% end
|
30
|
+
```
|
28
31
|
|
29
32
|
## Control flow
|
30
33
|
|
@@ -38,40 +41,53 @@ Whatever it is between `{{` and `}}` gets printed in the template.
|
|
38
41
|
|
39
42
|
There's nothing special about comments, it's just a `#` inside your Ruby code:
|
40
43
|
|
41
|
-
|
44
|
+
```
|
45
|
+
% # This is a comment.
|
46
|
+
```
|
42
47
|
|
43
48
|
|
44
49
|
## Block evaluation
|
45
50
|
|
46
51
|
As with control instructions, it happens naturally:
|
47
52
|
|
48
|
-
|
49
|
-
|
50
|
-
|
53
|
+
```
|
54
|
+
% 3.times do |i|
|
55
|
+
{{i}}
|
56
|
+
% end
|
57
|
+
```
|
51
58
|
|
52
59
|
## Parameters
|
53
60
|
|
54
61
|
The values passed to the template are available as local variables:
|
55
62
|
|
56
|
-
|
57
|
-
|
58
|
-
|
63
|
+
```ruby
|
64
|
+
example = Mote.parse("Hello {{name}}")
|
65
|
+
assert_equal "Hello world", example.call(name: "world")
|
66
|
+
assert_equal "Hello Bruno", example.call(name: "Bruno")
|
67
|
+
```
|
59
68
|
|
60
69
|
# Helpers
|
61
70
|
|
62
71
|
There's a helper available in the `Mote::Helpers` module, and you are
|
63
72
|
free to include it in your code. To do it, just type:
|
64
73
|
|
65
|
-
|
74
|
+
```ruby
|
75
|
+
include Mote::Helpers
|
76
|
+
```
|
66
77
|
|
67
78
|
## mote
|
68
79
|
|
69
80
|
The `mote` helper receives a file name and a hash and returns the rendered
|
70
81
|
version of its content. The compiled template is cached for subsequent calls.
|
71
82
|
|
72
|
-
|
83
|
+
```ruby
|
84
|
+
assert_equal "***\n", mote("test/basic.mote", n: 3)
|
85
|
+
```
|
86
|
+
|
87
|
+
## Installation
|
73
88
|
|
74
|
-
|
75
|
-
------------
|
89
|
+
As usual, you can install it using rubygems.
|
76
90
|
|
77
|
-
|
91
|
+
```
|
92
|
+
$ gem install mote
|
93
|
+
```
|
data/lib/mote.rb
CHANGED
@@ -18,10 +18,12 @@
|
|
18
18
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
19
|
# THE SOFTWARE.
|
20
20
|
class Mote
|
21
|
-
VERSION = "0.2.
|
21
|
+
VERSION = "0.2.3"
|
22
|
+
|
23
|
+
PATTERN = /^\s*(%)(.*?)$|(<\?)\s+(.*?)\s+\?>|(\{\{)(.*?)\}\}/m
|
22
24
|
|
23
25
|
def self.parse(template, context = self, vars = [])
|
24
|
-
terms = template.split(
|
26
|
+
terms = template.split(PATTERN)
|
25
27
|
|
26
28
|
parts = "Proc.new do |params, __o|\n params ||= {}; __o ||= ''\n"
|
27
29
|
|
@@ -31,6 +33,7 @@ class Mote
|
|
31
33
|
|
32
34
|
while term = terms.shift
|
33
35
|
case term
|
36
|
+
when "<?" then parts << "#{terms.shift}\n"
|
34
37
|
when "%" then parts << "#{terms.shift}\n"
|
35
38
|
when "{{" then parts << "__o << (#{terms.shift}).to_s\n"
|
36
39
|
else parts << "__o << #{term.dump}\n"
|
data/test/mote_test.rb
CHANGED
@@ -82,6 +82,29 @@ scope do
|
|
82
82
|
example = Mote.parse("{{ [1, 2, 3].map { |i| i * i }.join(',') }}")
|
83
83
|
assert_equal "1,4,9", example.call
|
84
84
|
end
|
85
|
+
|
86
|
+
test "multi-line XML-style directives" do
|
87
|
+
template = (<<-EOT).gsub(/^ /, "")
|
88
|
+
<? res = ""
|
89
|
+
[1, 2, 3].each_with_index do |item, idx|
|
90
|
+
res << "%d. %d\n" % [idx + 1, item * item]
|
91
|
+
end
|
92
|
+
?>
|
93
|
+
{{ res }}
|
94
|
+
EOT
|
95
|
+
|
96
|
+
example = Mote.parse(template)
|
97
|
+
assert_equal "\n1. 1\n2. 4\n3. 9\n\n", example.call
|
98
|
+
end
|
99
|
+
|
100
|
+
test "preserve XML directives" do
|
101
|
+
template = (<<-EOT).gsub(/^ /, "")
|
102
|
+
<?xml "hello" ?>
|
103
|
+
EOT
|
104
|
+
|
105
|
+
example = Mote.parse(template)
|
106
|
+
assert_equal "<?xml \"hello\" ?>\n", example.call
|
107
|
+
end
|
85
108
|
end
|
86
109
|
|
87
110
|
include Mote::Helpers
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mote
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-04-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: cutest
|
16
|
-
requirement: &
|
16
|
+
requirement: &2152129460 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *2152129460
|
25
25
|
description: Mote is a very simple and fast template engine.
|
26
26
|
email:
|
27
27
|
- michel@soveran.com
|
@@ -59,7 +59,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
59
|
version: '0'
|
60
60
|
requirements: []
|
61
61
|
rubyforge_project:
|
62
|
-
rubygems_version: 1.8.
|
62
|
+
rubygems_version: 1.8.11
|
63
63
|
signing_key:
|
64
64
|
specification_version: 3
|
65
65
|
summary: Minimum Operational Template.
|