trenni 1.4.0 → 1.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +0 -1
- data/Gemfile +4 -0
- data/README.md +4 -2
- data/lib/trenni/template.rb +27 -8
- data/lib/trenni/version.rb +1 -1
- data/spec/trenni/escaped.trenni +2 -0
- data/spec/trenni/large.trenni +16 -0
- data/spec/trenni/template_spec.rb +32 -2
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3c2a33d255bc9d2ed30d0e54ee6b0b95f3800ba8
|
4
|
+
data.tar.gz: 49d50142db0d3621ca2b95c81c475dea3616d89d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f788a9552f2ef0590292acf3fee2104db05958cd166d44c761051b23ffd8d915b10159f4ab0254a1ae776e52be607a95c9d40a339e711abaadf9ff9fb67216da
|
7
|
+
data.tar.gz: 7f96731a0c1cc4594a6398ca6d8c8bb0437710085d0ed4da29d1f12be5615a3512bce3da75215a9e294dce1a45f447f8cbafd55704bcebe196b9bf4bffe02aa6
|
data/.travis.yml
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -31,9 +31,11 @@ Trenni templates work essentially the same way as all other templating systems:
|
|
31
31
|
|
32
32
|
items = 1..4
|
33
33
|
|
34
|
-
|
34
|
+
template.to_string(binding) # => "1234"
|
35
35
|
|
36
|
-
The code above
|
36
|
+
The code above demonstrate the only two constructs, `<?r expression ?>` and `#{output}`.
|
37
|
+
|
38
|
+
Trenni provides a slightly higher performance API using objects rather than bindings. If you provide an object instance, `instance_eval` would be used instead.
|
37
39
|
|
38
40
|
## Contributing
|
39
41
|
|
data/lib/trenni/template.rb
CHANGED
@@ -19,6 +19,7 @@
|
|
19
19
|
# THE SOFTWARE.
|
20
20
|
|
21
21
|
require 'strscan'
|
22
|
+
require 'stringio'
|
22
23
|
|
23
24
|
module Trenni
|
24
25
|
# The output variable that will be used in templates:
|
@@ -62,12 +63,12 @@ module Trenni
|
|
62
63
|
end
|
63
64
|
|
64
65
|
def code
|
65
|
-
parts = ["#{OUT} = [] ; "] + @parts + ["#{OUT}
|
66
|
+
parts = ["#{OUT} = [] ; "] + @parts + ["#{OUT}"]
|
66
67
|
|
67
68
|
code = parts.join
|
68
69
|
end
|
69
70
|
end
|
70
|
-
|
71
|
+
|
71
72
|
class Scanner < StringScanner
|
72
73
|
TEXT = /([^<#]|<(?!\?r)|#(?!\{)){1,1024}/m
|
73
74
|
|
@@ -146,24 +147,42 @@ module Trenni
|
|
146
147
|
def initialize(template, filename = '<Trenni>')
|
147
148
|
@template = template
|
148
149
|
@filename = filename
|
149
|
-
compile!
|
150
150
|
end
|
151
151
|
|
152
|
-
def
|
153
|
-
|
152
|
+
def to_string(scope = nil)
|
153
|
+
to_array(scope).join
|
154
|
+
end
|
155
|
+
|
156
|
+
# Legacy functions:
|
157
|
+
alias evaluate to_string
|
158
|
+
alias result to_string
|
159
|
+
|
160
|
+
def to_array(scope)
|
161
|
+
if Binding === scope
|
162
|
+
eval(code, scope, @filename)
|
163
|
+
else
|
164
|
+
# This can sometimes be a bit faster:
|
165
|
+
scope.instance_eval(&to_proc)
|
166
|
+
end
|
154
167
|
end
|
155
168
|
|
156
|
-
|
169
|
+
def to_proc
|
170
|
+
@compiled_proc ||= eval("proc{\n#{code}\n}", binding, @filename, 0)
|
171
|
+
end
|
157
172
|
|
158
173
|
protected
|
159
174
|
|
160
|
-
def
|
175
|
+
def code
|
176
|
+
@code ||= compile!
|
177
|
+
end
|
178
|
+
|
179
|
+
def compile!
|
161
180
|
buffer = Buffer.new
|
162
181
|
scanner = Scanner.new(buffer, @template)
|
163
182
|
|
164
183
|
scanner.parse
|
165
184
|
|
166
|
-
|
185
|
+
buffer.code
|
167
186
|
end
|
168
187
|
end
|
169
188
|
end
|
data/lib/trenni/version.rb
CHANGED
@@ -0,0 +1,16 @@
|
|
1
|
+
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent gravida, turpis id bibendum bibendum, ipsum diam vestibulum arcu, sit amet tempor ipsum lorem non urna. Suspendisse nec nisl a risus laoreet lacinia ac sit amet neque. Mauris vel turpis semper massa bibendum viverra. Curabitur vel felis est. Donec sed faucibus augue, ac molestie purus. Nulla a lectus dui. Quisque id scelerisque urna. Aenean in vehicula nibh, in aliquam turpis. #{'Suspendisse'} iaculis suscipit lectus in efficitur. Nulla facilisi. Fusce facilisis, justo ac eleifend convallis, ligula est bibendum risus, a consequat metus sapien eget mauris. Sed quis mollis tellus, sed tempor lacus. Proin et efficitur metus. Vestibulum blandit tellus sed turpis tincidunt, in aliquet augue bibendum. Suspendisse venenatis faucibus lectus sed convallis.
|
2
|
+
|
3
|
+
#{'Curabitur'} aliquet ligula et volutpat efficitur. Duis sed tortor elit. Quisque justo eros, maximus quis elit sed, accumsan faucibus lectus. Nulla blandit quam urna, ac bibendum turpis semper quis. Proin a ligula eu eros semper condimentum. Ut pellentesque, justo sed malesuada condimentum, est massa tempor turpis, id facilisis neque odio eu neque. Morbi convallis laoreet tortor, ut dapibus mi accumsan a. Proin convallis neque sed leo blandit lacinia. Phasellus porta odio nunc, quis placerat sem facilisis quis. Quisque et #{'ornare'} elit. Duis efficitur cursus lectus vitae placerat. In lacinia eros nec feugiat finibus.
|
4
|
+
|
5
|
+
Morbi orci magna, tempor at convallis sed, porta elementum nulla. Aliquam sit amet fringilla felis, eget volutpat lorem. Pellentesque laoreet, elit quis fringilla ultricies, leo nisl semper mauris, ac feugiat libero tortor id urna. Praesent ligula augue, mollis nec justo vitae, faucibus posuere risus. Praesent blandit molestie ex eu fringilla. Ut non vehicula mauris. Aliquam gravida rutrum laoreet. Pellentesque facilisis mi ipsum, ac dapibus mi iaculis nec.
|
6
|
+
|
7
|
+
Vestibulum sit amet nibh velit. Sed rhoncus odio ut nisi blandit vestibulum. Aliquam erat volutpat. Suspendisse sed posuere ligula. Proin ex ex, pharetra vel dui vulputate, pellentesque ullamcorper metus. Cras imperdiet dictum lorem. Nullam eleifend nunc eu metus ullamcorper dignissim. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean sit amet sem sed enim rutrum volutpat a non libero.
|
8
|
+
|
9
|
+
<?r 10.downto(1) do |i| ?>
|
10
|
+
#{i} bottle#{i == 1 ? '' : 's'} of beer on the wall,
|
11
|
+
#{i} bottle#{i == 1 ? '' : 's'} of beer,
|
12
|
+
Take one down and pass it around,
|
13
|
+
#{i-1} bottle#{i == 1 ? '' : 's'} of beer on the wall.
|
14
|
+
<?r end ?>
|
15
|
+
|
16
|
+
Integer in ligula at ex gravida pellentesque in et felis. Nulla dolor sapien, pretium a odio a, consectetur ultrices justo. Suspendisse libero quam, pulvinar eu est non, fermentum volutpat nisl. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Cras at dapibus lorem, nec imperdiet quam. Proin ut tellus sagittis, sollicitudin elit ac, porttitor diam. Quisque at hendrerit lacus, sit amet mollis lacus.
|
@@ -1,4 +1,4 @@
|
|
1
|
-
#!/usr/bin/env
|
1
|
+
#!/usr/bin/env rspec
|
2
2
|
|
3
3
|
# Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
4
|
#
|
@@ -21,6 +21,9 @@
|
|
21
21
|
# THE SOFTWARE.
|
22
22
|
|
23
23
|
require 'trenni'
|
24
|
+
require 'benchmark'
|
25
|
+
|
26
|
+
# require 'ruby-prof'
|
24
27
|
|
25
28
|
module Trenni::TemplateSpec
|
26
29
|
describe Trenni::Template do
|
@@ -28,7 +31,34 @@ module Trenni::TemplateSpec
|
|
28
31
|
template = Trenni::Template.new('<?r items.each do |item| ?>#{item}<?r end ?>')
|
29
32
|
|
30
33
|
items = 1..4
|
31
|
-
expect(template.
|
34
|
+
expect(template.to_string(binding)).to be == "1234"
|
35
|
+
end
|
36
|
+
|
37
|
+
let(:large_template) {Trenni::Template.load File.join(__dir__, "large.trenni")}
|
38
|
+
|
39
|
+
it "should have better performance using instance" do
|
40
|
+
n = 1_000
|
41
|
+
|
42
|
+
#RubyProf.start
|
43
|
+
|
44
|
+
object_time = Benchmark.realtime{n.times{large_template.to_string(self)}}
|
45
|
+
binding_time = Benchmark.realtime{n.times{large_template.to_string(binding)}}
|
46
|
+
|
47
|
+
#result = RubyProf.stop
|
48
|
+
|
49
|
+
# Print a flat profile to text
|
50
|
+
#printer = RubyProf::FlatPrinter.new(result)
|
51
|
+
#printer.print(STDOUT)
|
52
|
+
|
53
|
+
expect(object_time).to be < binding_time
|
54
|
+
end
|
55
|
+
|
56
|
+
let(:escaped_template) {Trenni::Template.load File.join(__dir__, "escaped.trenni")}
|
57
|
+
|
58
|
+
it "should process escaped characters" do
|
59
|
+
expect(escaped_template.to_string).to be ==
|
60
|
+
"This\\nisn't one line.\n" +
|
61
|
+
"\\tIndentation is the best."
|
32
62
|
end
|
33
63
|
end
|
34
64
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trenni
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.4.
|
4
|
+
version: 1.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-12-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -74,6 +74,8 @@ files:
|
|
74
74
|
- lib/trenni/template.rb
|
75
75
|
- lib/trenni/version.rb
|
76
76
|
- spec/trenni/builder_spec.rb
|
77
|
+
- spec/trenni/escaped.trenni
|
78
|
+
- spec/trenni/large.trenni
|
77
79
|
- spec/trenni/parser_spec.rb
|
78
80
|
- spec/trenni/strings_spec.rb
|
79
81
|
- spec/trenni/template_spec.rb
|
@@ -103,6 +105,8 @@ specification_version: 4
|
|
103
105
|
summary: A fast native templating system that compiles directly to Ruby code.
|
104
106
|
test_files:
|
105
107
|
- spec/trenni/builder_spec.rb
|
108
|
+
- spec/trenni/escaped.trenni
|
109
|
+
- spec/trenni/large.trenni
|
106
110
|
- spec/trenni/parser_spec.rb
|
107
111
|
- spec/trenni/strings_spec.rb
|
108
112
|
- spec/trenni/template_spec.rb
|