Skip to content

Commit 4f57e0a

Browse files
committed
Mote is shared with the world.
0 parents  commit 4f57e0a

File tree

8 files changed

+259
-0
lines changed

8 files changed

+259
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/pkg

LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2010 Michel Martens
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in
11+
all copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

README.markdown

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
Mote
2+
====
3+
4+
Minimum Operational Template.
5+
6+
Description
7+
-----------
8+
9+
Mote is the little brother of ERB. It only provides a subset of ERB's
10+
features, but praises itself of being simple--both internally and externally--
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.
14+
15+
Usage
16+
-----
17+
18+
Usage is very similar to that of ERB:
19+
20+
template = Mote.parse("This is a template")
21+
template.call #=> "This is a template"
22+
23+
Silly example, you may say, and I would agree. What follows is a short list of
24+
the different use cases you may face:
25+
26+
## Assignment
27+
28+
example = Mote.parse("<%= \"***\" %>")
29+
assert_equal "***", example.call
30+
31+
## Comment
32+
33+
example = Mote.parse("*<%# \"*\" %>*")
34+
assert_equal "**", example.call
35+
36+
## Control flow
37+
38+
example = Mote.parse("<% if false %>*<% else %>***<% end %>")
39+
assert_equal "***", example.call
40+
41+
## Block evaluation
42+
43+
example = Mote.parse("<% 3.times { %>*<% } %>")
44+
assert_equal "***", example.call
45+
46+
## Parameters
47+
48+
example = Mote.parse("<% params[:n].times { %>*<% } %>")
49+
assert_equal "***", example[n: 3]
50+
assert_equal "****", example[n: 4]
51+
52+
Two things are worth noting in the last example: the first is that as the
53+
returned value is a `Proc`, you can call it with square brackets and anything
54+
you put inside becomes a parameter. Second, that within the template you have
55+
access to those parameters through the `params` hash, instead of the usual
56+
approach of converting each key to a local variable.
57+
58+
# Helpers
59+
60+
There are a couple helpers available in the `Mote::Helpers` module, and you are
61+
free to include it in your code. To do it, just type:
62+
63+
include Mote::Helpers
64+
65+
Here are the available helpers:
66+
67+
## `mote`
68+
69+
The `mote` helper receives a template string and returns the rendered version
70+
of it:
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)
77+
78+
## `mote_file`
79+
80+
The `mote_file` helper receives a file name without the `.erb` extension and
81+
returns the rendered version of its content. The compiled template is cached
82+
for subsequent calls.
83+
84+
assert_equal "***\n", mote_file("test/basic", :n => 3)
85+
86+
Installation
87+
------------
88+
89+
$ gem install mote
90+
91+
License
92+
-------
93+
94+
Copyright (c) 2011 Michel Martens
95+
96+
Permission is hereby granted, free of charge, to any person
97+
obtaining a copy of this software and associated documentation
98+
files (the "Software"), to deal in the Software without
99+
restriction, including without limitation the rights to use,
100+
copy, modify, merge, publish, distribute, sublicense, and/or sell
101+
copies of the Software, and to permit persons to whom the
102+
Software is furnished to do so, subject to the following
103+
conditions:
104+
105+
The above copyright notice and this permission notice shall be
106+
included in all copies or substantial portions of the Software.
107+
108+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
109+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
110+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
111+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
112+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
113+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
114+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
115+
OTHER DEALINGS IN THE SOFTWARE.

Rakefile

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
task :test do
2+
require "cutest"
3+
Cutest.run(Dir["test/*.rb"])
4+
end
5+
6+
task :default => :test

lib/mote.rb

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Copyright (c) 2010 Michel Martens
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
# THE SOFTWARE.
20+
class Mote
21+
VERSION = "0.0.1"
22+
23+
def self.parse(template, context = self)
24+
terms = template.split(/(<%[=#]?)\s*(.*?)\s*%>/)
25+
26+
parts = "Proc.new do |params, __o|\n params ||= {}; __o ||= ''\n"
27+
28+
while term = terms.shift
29+
case term
30+
when "<%#" then terms.shift # skip
31+
when "<%" then parts << "#{terms.shift}\n"
32+
when "<%=" then parts << "__o << (#{terms.shift}).to_s\n"
33+
else parts << "__o << #{term.inspect}\n"
34+
end
35+
end
36+
37+
parts << "__o; end"
38+
39+
context.instance_eval(parts)
40+
end
41+
42+
module Helpers
43+
def mote(template, params = {})
44+
Mote.parse(template).call(params)
45+
end
46+
47+
def mote_file(file_name, params = {})
48+
@_mote ||= {}
49+
@_mote[file_name] ||= Mote.parse(File.read("#{file_name}.erb"))
50+
@_mote[file_name][params]
51+
end
52+
end
53+
end

mote.gemspec.erb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<% require "./lib/mote" -%>
2+
Gem::Specification.new do |s|
3+
s.name = "mote"
4+
s.version = "<%= Mote::VERSION %>"
5+
s.summary = "Minimum Operational Template."
6+
s.description = "Mote is the little brother of ERB. It only provides a subset of ERB's features, but praises itself of being simple--both internally and externally--and super fast."
7+
s.authors = ["Michel Martens"]
8+
s.email = ["[email protected]"]
9+
s.homepage = "https://github.com/soveran/mote"
10+
s.files = <%= Dir[
11+
"LICENSE",
12+
"README.markdown",
13+
"Rakefile",
14+
"lib/**/*.rb",
15+
"*.gemspec",
16+
"test/**/*.rb"
17+
].inspect %>
18+
s.add_development_dependency "cutest", "~> 0.1"
19+
end

test/basic.erb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<% 3.times do %>*<% end %>

test/mote_test.rb

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require File.expand_path("../lib/mote", File.dirname(__FILE__))
2+
3+
scope do
4+
test "assignment" do
5+
example = Mote.parse("<%= \"***\" %>")
6+
assert_equal "***", example.call
7+
end
8+
9+
test "comment" do
10+
example = Mote.parse("*<%# \"*\" %>*")
11+
assert_equal "**", example.call
12+
end
13+
14+
test "control flow" do
15+
example = Mote.parse("<% if false %>*<% else %>***<% end %>")
16+
assert_equal "***", example.call
17+
end
18+
19+
test "block evaluation" do
20+
example = Mote.parse("<% 3.times { %>*<% } %>")
21+
assert_equal "***", example.call
22+
end
23+
24+
test "parameters" do
25+
example = Mote.parse("<% params[:n].times { %>*<% } %>")
26+
assert_equal "***", example[:n => 3]
27+
assert_equal "****", example[:n => 4]
28+
end
29+
end
30+
31+
include Mote::Helpers
32+
33+
scope do
34+
test do
35+
assert_equal "1 2 3", mote("1 <%= 2 %> 3")
36+
end
37+
38+
test do
39+
assert_equal "1 2 3", mote("1 <%= params[:n] %> 3", :n => 2)
40+
end
41+
42+
test do
43+
assert_equal "***\n", mote_file("test/basic", :n => 3)
44+
end
45+
end

0 commit comments

Comments
 (0)