mote 0.0.2 → 0.1.0.rc1
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/AUTHORS +3 -0
- data/README.markdown +30 -66
- data/lib/mote.rb +14 -20
- data/mote.gemspec +1 -0
- data/test/mote_test.rb +53 -20
- metadata +35 -38
data/AUTHORS
ADDED
data/README.markdown
CHANGED
@@ -6,11 +6,8 @@ Minimum Operational Template.
|
|
6
6
|
Description
|
7
7
|
-----------
|
8
8
|
|
9
|
-
Mote is
|
10
|
-
|
11
|
-
and super fast. It was born out of experimentations while discussing
|
12
|
-
[NOLATE](https://github.com/antirez/nolate), another small library with the
|
13
|
-
same goals and more features.
|
9
|
+
Mote is very simple and fast template engine that praises itself of being
|
10
|
+
simple--both internally and externally-- and super fast.
|
14
11
|
|
15
12
|
Usage
|
16
13
|
-----
|
@@ -23,92 +20,59 @@ Usage is very similar to that of ERB:
|
|
23
20
|
Silly example, you may say, and I would agree. What follows is a short list of
|
24
21
|
the different use cases you may face:
|
25
22
|
|
23
|
+
|
24
|
+
% if user == "Bruno"
|
25
|
+
${user} rhymes with Piano
|
26
|
+
% elsif user == "Brutus"
|
27
|
+
${user} rhymes with Opus
|
28
|
+
% end
|
29
|
+
|
30
|
+
## Control flow
|
31
|
+
|
32
|
+
Lines that start with `%` are evaluated as Ruby code.
|
33
|
+
|
26
34
|
## Assignment
|
27
35
|
|
28
|
-
|
29
|
-
assert_equal "***", example.call
|
36
|
+
Whatever it is between `${` and `}` gets printed in the template.
|
30
37
|
|
31
|
-
##
|
38
|
+
## Comments
|
32
39
|
|
33
|
-
|
34
|
-
assert_equal "**", example.call
|
40
|
+
There's nothing special about comments, it's just a `#` inside your Ruby code:
|
35
41
|
|
36
|
-
|
42
|
+
% # This is a comment.
|
37
43
|
|
38
|
-
example = Mote.parse("<% if false %>*<% else %>***<% end %>")
|
39
|
-
assert_equal "***", example.call
|
40
44
|
|
41
45
|
## Block evaluation
|
42
46
|
|
43
|
-
|
44
|
-
|
47
|
+
As with control instructions, it happens naturally:
|
48
|
+
|
49
|
+
% 3.times do |i|
|
50
|
+
${i}
|
51
|
+
% end
|
45
52
|
|
46
53
|
## Parameters
|
47
54
|
|
48
|
-
|
49
|
-
assert_equal "***", example[n: 3]
|
50
|
-
assert_equal "****", example[n: 4]
|
55
|
+
The values passed to the template are available as local variables:
|
51
56
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
access to those parameters through the `params` hash, instead of the usual
|
56
|
-
approach of converting each key to a local variable.
|
57
|
+
example = Mote.parse("Hello ${name}")
|
58
|
+
assert_equal "Hello world", example.call(name: "world")
|
59
|
+
assert_equal "Hello Bruno", example.call(name: "Bruno")
|
57
60
|
|
58
61
|
# Helpers
|
59
62
|
|
60
|
-
There
|
63
|
+
There's a helper available in the `Mote::Helpers` module, and you are
|
61
64
|
free to include it in your code. To do it, just type:
|
62
65
|
|
63
66
|
include Mote::Helpers
|
64
67
|
|
65
|
-
Here are the available helpers:
|
66
|
-
|
67
68
|
## mote
|
68
69
|
|
69
|
-
The `mote` helper receives a
|
70
|
-
of
|
71
|
-
|
72
|
-
assert_equal "1 2 3", mote("1 <%= 2 %> 3")
|
73
|
-
|
74
|
-
It works with parameters too:
|
75
|
-
|
76
|
-
assert_equal "1 2 3", mote("1 <%= params[:n] %> 3", :n => 2)
|
70
|
+
The `mote` helper receives a file name and a hash and returns the rendered
|
71
|
+
version of its content. The compiled template is cached for subsequent calls.
|
77
72
|
|
78
|
-
|
79
|
-
|
80
|
-
The `mote_file` helper receives a file name and returns the rendered version of
|
81
|
-
its content. The compiled template is cached for subsequent calls.
|
82
|
-
|
83
|
-
assert_equal "***\n", mote_file("test/basic.erb", :n => 3)
|
73
|
+
assert_equal "***\n", mote("test/basic.erb", n: 3)
|
84
74
|
|
85
75
|
Installation
|
86
76
|
------------
|
87
77
|
|
88
78
|
$ gem install mote
|
89
|
-
|
90
|
-
License
|
91
|
-
-------
|
92
|
-
|
93
|
-
Copyright (c) 2011 Michel Martens
|
94
|
-
|
95
|
-
Permission is hereby granted, free of charge, to any person
|
96
|
-
obtaining a copy of this software and associated documentation
|
97
|
-
files (the "Software"), to deal in the Software without
|
98
|
-
restriction, including without limitation the rights to use,
|
99
|
-
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
100
|
-
copies of the Software, and to permit persons to whom the
|
101
|
-
Software is furnished to do so, subject to the following
|
102
|
-
conditions:
|
103
|
-
|
104
|
-
The above copyright notice and this permission notice shall be
|
105
|
-
included in all copies or substantial portions of the Software.
|
106
|
-
|
107
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
108
|
-
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
109
|
-
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
110
|
-
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
111
|
-
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
112
|
-
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
113
|
-
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
114
|
-
OTHER DEALINGS IN THE SOFTWARE.
|
data/lib/mote.rb
CHANGED
@@ -18,19 +18,22 @@
|
|
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.0.
|
21
|
+
VERSION = "0.1.0.rc1"
|
22
22
|
|
23
|
-
def self.parse(template, context = self)
|
24
|
-
terms = template.split(
|
23
|
+
def self.parse(template, context = self, vars = [])
|
24
|
+
terms = template.split(/^\s*(%)(.*?)$|(\$\{)(.*?)\}/)
|
25
25
|
|
26
26
|
parts = "Proc.new do |params, __o|\n params ||= {}; __o ||= ''\n"
|
27
27
|
|
28
|
+
vars.each do |var|
|
29
|
+
parts << "%s = params[:%s]\n" % [var, var]
|
30
|
+
end
|
31
|
+
|
28
32
|
while term = terms.shift
|
29
33
|
case term
|
30
|
-
when "
|
31
|
-
when "
|
32
|
-
|
33
|
-
else parts << "__o << #{term.inspect}\n"
|
34
|
+
when "%" then parts << "#{terms.shift}\n"
|
35
|
+
when "${" then parts << "__o << (#{terms.shift}).to_s\n"
|
36
|
+
else parts << "__o << #{term.inspect}\n"
|
34
37
|
end
|
35
38
|
end
|
36
39
|
|
@@ -40,22 +43,13 @@ class Mote
|
|
40
43
|
end
|
41
44
|
|
42
45
|
module Helpers
|
43
|
-
def mote(
|
44
|
-
mote_cache[
|
45
|
-
mote_cache[
|
46
|
-
end
|
47
|
-
|
48
|
-
def mote_file(filename, params = {})
|
49
|
-
mote_files[filename] ||= Mote.parse(File.read(filename))
|
50
|
-
mote_files[filename][params]
|
46
|
+
def mote(filename, params = {})
|
47
|
+
mote_cache[filename] ||= Mote.parse(File.read(filename), self, params.keys)
|
48
|
+
mote_cache[filename][params]
|
51
49
|
end
|
52
50
|
|
53
51
|
def mote_cache
|
54
|
-
|
55
|
-
end
|
56
|
-
|
57
|
-
def mote_files
|
58
|
-
@_mote_files ||= {}
|
52
|
+
Thread.current[:_mote_cache] ||= {}
|
59
53
|
end
|
60
54
|
end
|
61
55
|
end
|
data/mote.gemspec
CHANGED
data/test/mote_test.rb
CHANGED
@@ -2,33 +2,59 @@ require File.expand_path("../lib/mote", File.dirname(__FILE__))
|
|
2
2
|
|
3
3
|
scope do
|
4
4
|
test "assignment" do
|
5
|
-
example = Mote.parse("
|
5
|
+
example = Mote.parse("${ \"***\" }")
|
6
6
|
assert_equal "***", example.call
|
7
7
|
end
|
8
8
|
|
9
9
|
test "comment" do
|
10
|
-
|
11
|
-
|
10
|
+
template = (<<-EOT).gsub(/ {4}/, "")
|
11
|
+
*
|
12
|
+
% # "*"
|
13
|
+
*
|
14
|
+
EOT
|
15
|
+
|
16
|
+
example = Mote.parse(template)
|
17
|
+
assert_equal "*\n*\n", example.call.squeeze("\n")
|
12
18
|
end
|
13
19
|
|
14
20
|
test "control flow" do
|
15
|
-
|
16
|
-
|
21
|
+
template = (<<-EOT).gsub(/ {4}/, "")
|
22
|
+
% if false
|
23
|
+
*
|
24
|
+
% else
|
25
|
+
***
|
26
|
+
% end
|
27
|
+
EOT
|
28
|
+
|
29
|
+
example = Mote.parse(template)
|
30
|
+
assert_equal "\n ***\n\n", example.call
|
17
31
|
end
|
18
32
|
|
19
33
|
test "block evaluation" do
|
20
|
-
|
21
|
-
|
34
|
+
template = (<<-EOT).gsub(/ {4}/, "")
|
35
|
+
% 3.times {
|
36
|
+
*
|
37
|
+
% }
|
38
|
+
EOT
|
39
|
+
|
40
|
+
example = Mote.parse(template)
|
41
|
+
assert_equal "\n*\n\n*\n\n*\n\n", example.call
|
22
42
|
end
|
23
43
|
|
24
44
|
test "parameters" do
|
25
|
-
|
26
|
-
|
27
|
-
|
45
|
+
template = (<<-EOT).gsub(/ {4}/, "")
|
46
|
+
% params[:n].times {
|
47
|
+
*
|
48
|
+
% }
|
49
|
+
EOT
|
50
|
+
|
51
|
+
example = Mote.parse(template)
|
52
|
+
assert_equal "\n*\n\n*\n\n*\n\n", example[:n => 3]
|
53
|
+
assert_equal "\n*\n\n*\n\n*\n\n*\n\n", example[:n => 4]
|
28
54
|
end
|
29
55
|
|
30
56
|
test "multiline" do
|
31
|
-
example = Mote.parse("The\nMan\nAnd\n
|
57
|
+
example = Mote.parse("The\nMan\nAnd\n${\"The\"}\nSea")
|
32
58
|
assert_equal "The\nMan\nAnd\nThe\nSea", example[:n => 3]
|
33
59
|
end
|
34
60
|
|
@@ -36,20 +62,27 @@ scope do
|
|
36
62
|
example = Mote.parse("'foo' 'bar' 'baz'")
|
37
63
|
assert_equal "'foo' 'bar' 'baz'", example.call
|
38
64
|
end
|
39
|
-
end
|
40
65
|
|
41
|
-
|
66
|
+
test "context" do
|
67
|
+
context = Object.new
|
68
|
+
context.instance_variable_set(:@user, "Bruno")
|
42
69
|
|
43
|
-
|
44
|
-
|
45
|
-
assert_equal "1 2 3", mote("1 <%= 2 %> 3")
|
70
|
+
example = Mote.parse("${ @user }", context)
|
71
|
+
assert_equal "Bruno", example.call
|
46
72
|
end
|
47
73
|
|
48
|
-
test do
|
49
|
-
|
74
|
+
test "locals" do
|
75
|
+
context = Object.new
|
76
|
+
|
77
|
+
example = Mote.parse("${ user }", context, [:user])
|
78
|
+
assert_equal "Bruno", example.call(user: "Bruno")
|
50
79
|
end
|
80
|
+
end
|
51
81
|
|
52
|
-
|
53
|
-
|
82
|
+
include Mote::Helpers
|
83
|
+
|
84
|
+
scope do
|
85
|
+
test "helpers" do
|
86
|
+
assert_equal "\n *\n\n *\n\n *\n\n", mote("test/basic.erb", :n => 3)
|
54
87
|
end
|
55
88
|
end
|
metadata
CHANGED
@@ -1,70 +1,67 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: mote
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0.rc1
|
5
|
+
prerelease: 6
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Michel Martens
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
dependencies:
|
15
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2011-08-21 00:00:00.000000000 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
16
|
name: cutest
|
17
|
-
|
18
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
17
|
+
requirement: &2157905920 !ruby/object:Gem::Requirement
|
19
18
|
none: false
|
20
|
-
requirements:
|
21
|
-
- -
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version:
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
24
23
|
type: :development
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *2157905920
|
26
|
+
description: Mote is the little brother of ERB. It only provides a subset of ERB's
|
27
|
+
features, but praises itself of being simple--both internally and externally--and
|
28
|
+
super fast.
|
29
|
+
email:
|
28
30
|
- michel@soveran.com
|
29
31
|
executables: []
|
30
|
-
|
31
32
|
extensions: []
|
32
|
-
|
33
33
|
extra_rdoc_files: []
|
34
|
-
|
35
|
-
files:
|
34
|
+
files:
|
36
35
|
- LICENSE
|
36
|
+
- AUTHORS
|
37
37
|
- README.markdown
|
38
38
|
- Rakefile
|
39
39
|
- lib/mote.rb
|
40
40
|
- mote.gemspec
|
41
41
|
- test/mote_test.rb
|
42
|
+
has_rdoc: true
|
42
43
|
homepage: http://github.com/soveran/mote
|
43
44
|
licenses: []
|
44
|
-
|
45
45
|
post_install_message:
|
46
46
|
rdoc_options: []
|
47
|
-
|
48
|
-
require_paths:
|
47
|
+
require_paths:
|
49
48
|
- lib
|
50
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
50
|
none: false
|
52
|
-
requirements:
|
53
|
-
- -
|
54
|
-
- !ruby/object:Gem::Version
|
55
|
-
version:
|
56
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
57
56
|
none: false
|
58
|
-
requirements:
|
59
|
-
- -
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version:
|
57
|
+
requirements:
|
58
|
+
- - ! '>'
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 1.3.1
|
62
61
|
requirements: []
|
63
|
-
|
64
62
|
rubyforge_project:
|
65
|
-
rubygems_version: 1.
|
63
|
+
rubygems_version: 1.6.2
|
66
64
|
signing_key:
|
67
65
|
specification_version: 3
|
68
66
|
summary: Minimum Operational Template.
|
69
67
|
test_files: []
|
70
|
-
|