-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Emmanuel Oga
authored and
Emmanuel Oga
committed
Apr 4, 2011
1 parent
895909b
commit abd902a
Showing
3 changed files
with
138 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,28 @@ | ||
load 'nolate.rb' | ||
load 'nolatep.rb' | ||
|
||
def bench(descr,times) | ||
def bench(descr, times) | ||
start = Time.now.to_f | ||
times.times { yield } | ||
elapsed = Time.now.to_f - start | ||
reqsec = times / elapsed | ||
puts "#{descr}: #{reqsec} requests/second" | ||
puts "#{descr.ljust(25)}: #{(reqsec/1000).to_i.to_s.rjust(6)}K requests/second" | ||
$template = "" | ||
end | ||
|
||
bench("empty template",30000) { | ||
nolate("") | ||
} | ||
TIMES = 100_000 | ||
|
||
bench("small constant template",30000) { | ||
nolate("nosub") | ||
} | ||
puts("nolate") | ||
bench("empty template" , TIMES) { nolate("") } | ||
bench("small constant template" , TIMES) { nolate("nosub") } | ||
bench("simple substitution" , TIMES) { nolate("simple <%= 'sub' %>") } | ||
bench("hash substitution" , TIMES) { nolate("hash sub <%#x%>") } | ||
bench("testview2 file template" , TIMES) { nlt(:testview2) } | ||
|
||
bench("simple substitution",30000) { | ||
nolate("simple <%= 'sub' %>") | ||
} | ||
|
||
bench("hash substitution",30000) { | ||
nolate("hash sub <%#x%>") | ||
} | ||
|
||
bench("testview2 file template",30000) { | ||
nlt(:testview2) | ||
} | ||
include Nolatep | ||
puts("\nnolatep") | ||
t = nlt_parse("") ; bench("empty template" , TIMES) { nlt_eval(t) } | ||
t = nlt_parse("nosub") ; bench("small constant template" , TIMES) { nlt_eval(t) } | ||
t = nlt_parse("simple <%= 'sub' %>") ; bench("simple substitution" , TIMES) { nlt_eval(t) } | ||
t = nlt_parse("hash sub <%#x%>") ; bench("hash substitution" , TIMES) { nlt_eval(t) } | ||
bench("testview2 file template" , TIMES) { Nolatep.nlt(:testview2) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# NOLATE, A NO LAme TEmplate system | ||
# | ||
# Please read the README file for more information | ||
# | ||
# Copyright (c) 2011, Salvatore Sanfilippo <antirez at gmail dot com> | ||
# | ||
# All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are | ||
# met: | ||
# | ||
# * Redistributions of source code must retain the above copyright | ||
# notice, this list of conditions and the following disclaimer. | ||
# | ||
# * Redistributions in binary form must reproduce the above copyright | ||
# notice, this list of conditions and the following disclaimer in the | ||
# documentation and/or other materials provided with the distribution. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
# | ||
module Nolatep | ||
extend self | ||
|
||
def nlt_empty_binding | ||
return binding() | ||
end | ||
|
||
def nlt_templates | ||
@templates ||= {} | ||
end | ||
|
||
def nlt_flush_templates | ||
@templates = {} | ||
end | ||
|
||
def nlt_parse(str) | ||
i = -1 # I wish I had map.with_index in 1.8 :( | ||
str.split(/<%([=#%].*?)%>/m).map do |s| | ||
i, first_char = i + 1, s[0..0] | ||
if i % 2 == 0 then [s] | ||
elsif first_char == "=" then [:evalo, s[1..-1]] | ||
elsif first_char == "%" then [:eval, s[1..-1]] | ||
elsif first_char == "#" then [:sub, s[1..-1].to_sym] | ||
else | ||
raise "Invalid template #{str.inspect}" | ||
end | ||
end | ||
end | ||
|
||
def nlt_eval(template, sub = {}, b = nlt_empty_binding) | ||
template.map do |action, param| | ||
case action | ||
when :evalo then eval(param, b, __FILE__, __LINE__) | ||
when :eval then eval(param, b, __FILE__, __LINE__); "" | ||
when :sub then sub[param] | ||
else action | ||
end | ||
end.join | ||
end | ||
|
||
def nlt(viewname, sub={}, b = nlt_empty_binding) | ||
viewname = "#{viewname}.nlt" if viewname.is_a?(Symbol) | ||
unless nlt_templates[viewname] | ||
filename = "views/"+viewname | ||
raise "NOLATE error: no template at #{filename}" unless File.exists?(filename) | ||
nlt_templates[viewname] = nlt_parse(File.open(filename).read) | ||
end | ||
nlt_eval(nlt_templates[viewname], sub, b) | ||
end | ||
|
||
def nolate(str, sub={}) | ||
nlt_eval(nlt_parse(str), sub, binding) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
require 'test/unit' | ||
load 'nolatep.rb' | ||
|
||
class Object | ||
include Nolatep | ||
end | ||
|
||
class MyExampleClass | ||
def method_one | ||
@x = "Hello" | ||
nolate "<%= @x %>" | ||
end | ||
|
||
def method_two | ||
@x = "World" | ||
nlt :testview3 | ||
end | ||
end | ||
|
||
class NolateTest < Test::Unit::TestCase | ||
def test_basic | ||
assert_equal(nolate(""),"") | ||
assert_equal(nolate("nosub"),"nosub") | ||
assert_equal(nolate("simple <%= 'sub' %>"),"simple sub") | ||
assert_equal(nolate("hash sub <%#x%>",{:x => 1}),"hash sub 1") | ||
assert_equal(nolate("just ev<%% 'sub' %>al"),"just eval") | ||
assert_equal(nlt(:testview),"test 4 view\n") | ||
assert_equal(nlt("testview.nlt"),"test 4 view\n") | ||
assert_equal(nlt(:testview2),"<html>\n<body>\n4\n</body>\n</html>\n") | ||
assert_equal(nolate("<% foo %>"),"<% foo %>") | ||
assert_equal(nolate("<%%x=2%><%=x+1%>"),"3") | ||
assert_equal(MyExampleClass.new.method_one,"Hello") | ||
assert_equal(MyExampleClass.new.method_two,"World\n") | ||
end | ||
end |