trenni 3.9.0 → 3.13.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/bake/trenni/entities.rb +57 -0
  3. data/bake/trenni/parsers.rb +66 -0
  4. data/ext/tmp/x86_64-linux/lib/trenni/trenni.so +0 -0
  5. data/ext/tmp/x86_64-linux/trenni/2.4.0/Makefile +263 -0
  6. data/ext/tmp/x86_64-linux/trenni/2.4.0/escape.o +0 -0
  7. data/ext/tmp/x86_64-linux/trenni/2.4.0/markup.o +0 -0
  8. data/ext/tmp/x86_64-linux/trenni/2.4.0/mkmf.log +108 -0
  9. data/ext/tmp/x86_64-linux/trenni/2.4.0/tag.o +0 -0
  10. data/ext/tmp/x86_64-linux/trenni/2.4.0/template.o +0 -0
  11. data/ext/tmp/x86_64-linux/trenni/2.4.0/trenni.o +0 -0
  12. data/ext/tmp/x86_64-linux/trenni/2.4.0/trenni.so +0 -0
  13. data/ext/tmp/x86_64-linux/trenni/2.7.0/Makefile +266 -0
  14. data/ext/tmp/x86_64-linux/trenni/2.7.0/escape.o +0 -0
  15. data/ext/tmp/x86_64-linux/trenni/2.7.0/markup.o +0 -0
  16. data/ext/tmp/x86_64-linux/trenni/2.7.0/mkmf.log +113 -0
  17. data/ext/tmp/x86_64-linux/trenni/2.7.0/query.o +0 -0
  18. data/ext/tmp/x86_64-linux/trenni/2.7.0/tag.o +0 -0
  19. data/ext/tmp/x86_64-linux/trenni/2.7.0/template.o +0 -0
  20. data/ext/tmp/x86_64-linux/trenni/2.7.0/trenni.o +0 -0
  21. data/ext/tmp/x86_64-linux/trenni/2.7.0/trenni.so +0 -0
  22. data/ext/tmp/x86_64-linux/trenni/2.7.1/Makefile +266 -0
  23. data/ext/tmp/x86_64-linux/trenni/2.7.1/escape.o +0 -0
  24. data/ext/tmp/x86_64-linux/trenni/2.7.1/markup.o +0 -0
  25. data/ext/tmp/x86_64-linux/trenni/2.7.1/mkmf.log +113 -0
  26. data/ext/tmp/x86_64-linux/trenni/2.7.1/query.o +0 -0
  27. data/ext/tmp/x86_64-linux/trenni/2.7.1/tag.o +0 -0
  28. data/ext/tmp/x86_64-linux/trenni/2.7.1/template.o +0 -0
  29. data/ext/tmp/x86_64-linux/trenni/2.7.1/trenni.o +0 -0
  30. data/ext/tmp/x86_64-linux/trenni/2.7.1/trenni.so +0 -0
  31. data/lib/trenni/builder.rb +79 -15
  32. data/lib/trenni/entities.rb +2082 -2084
  33. data/lib/trenni/entities.trenni +1 -3
  34. data/lib/trenni/template.rb +23 -22
  35. data/lib/trenni/version.rb +1 -1
  36. data/spec/trenni/builder_spec.rb +103 -60
  37. data/spec/trenni/template_spec/builder.trenni +2 -2
  38. metadata +60 -62
  39. data/.editorconfig +0 -6
  40. data/.gitignore +0 -19
  41. data/.rspec +0 -5
  42. data/.travis.yml +0 -23
  43. data/Gemfile +0 -21
  44. data/README.md +0 -312
  45. data/Rakefile +0 -21
  46. data/benchmark/call_vs_yield.rb +0 -52
  47. data/benchmark/interpolation_vs_concat.rb +0 -30
  48. data/benchmark/io_vs_string.rb +0 -91
  49. data/entities.json +0 -2233
  50. data/tasks/entities.rake +0 -34
  51. data/tasks/parsers.rake +0 -44
  52. data/trenni.gemspec +0 -38
@@ -1,6 +0,0 @@
1
- root = true
2
-
3
- [*]
4
- indent_style = tab
5
- indent_size = 2
6
-
data/.gitignore DELETED
@@ -1,19 +0,0 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
18
-
19
- lib/trenni/trenni.bundle
data/.rspec DELETED
@@ -1,5 +0,0 @@
1
- --color
2
- --format documentation
3
- --backtrace
4
- --warnings
5
- --require spec_helper
@@ -1,23 +0,0 @@
1
- language: ruby
2
- cache: bundler
3
-
4
- before_script:
5
- - sh -c "cd ext && bundle exec rake compile"
6
-
7
- matrix:
8
- include:
9
- - rvm: 2.4
10
- - rvm: 2.5
11
- - rvm: 2.6
12
- - rvm: 2.6
13
- env: TRENNI_PREFER_FALLBACK=y
14
- - rvm: 2.6
15
- env: COVERAGE=Summary
16
- - rvm: 2.7
17
- - rvm: ruby-head
18
- - rvm: jruby-head
19
- - rvm: truffleruby
20
- allow_failures:
21
- - rvm: truffleruby
22
- - rvm: ruby-head
23
- - rvm: jruby-head
data/Gemfile DELETED
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source 'https://rubygems.org'
4
-
5
- # Specify your gem's dependencies in trenni.gemspec
6
- gemspec
7
-
8
- group :development do
9
- gem 'pry'
10
- gem "rake-compiler"
11
- end
12
-
13
- group :test do
14
- gem 'ruby-prof', platforms: [:mri]
15
- gem "benchmark-ips"
16
-
17
- gem 'covered'
18
-
19
- # For comparisons:
20
- gem "nokogiri"
21
- end
data/README.md DELETED
@@ -1,312 +0,0 @@
1
- # Trenni
2
-
3
- Trenni is a templating system built loosely on top of XHTML markup. It uses efficient native parsers where possible and compiles templates into efficient Ruby. It also includes a markup builder to assist with the generation of pleasantly formatted markup which is compatible with the included parsers.
4
-
5
- [![Build Status](https://travis-ci.com/ioquatix/trenni.svg?branch=master)](https://travis-ci.com/ioquatix/trenni)
6
- [![Code Climate](https://codeclimate.com/github/ioquatix/trenni.svg)](https://codeclimate.com/github/ioquatix/trenni)
7
- [![Coverage Status](https://coveralls.io/repos/ioquatix/trenni/badge.svg)](https://coveralls.io/r/ioquatix/trenni)
8
-
9
- ## Motivation
10
-
11
- Trenni was designed for [Utopia](https://github.com/ioquatix/utopia). When I originally looked at template engines, I was surprised by the level of complexity and the effort involved in processing a template to produce useful output. In particular, many template engines generate an AST and walk over it to generate output (e.g. ERB, at least at the time I last checked). This is exceedingly slow in Ruby.
12
-
13
- At the time (around 2008?) I was playing around with [ramaze](https://github.com/Ramaze/ramaze) and found a template engine I really liked the design of, called [ezamar](https://github.com/manveru/ezamar). The template compilation process actually generates Ruby code which can then be compiled and executed efficiently. Another engine, by the same author, [nagoro](https://github.com/manveru/nagoro), also provided some inspiration.
14
-
15
- More recently I was doing some investigation regarding using `eval` for executing the code. The problem is that it's [not possible to replace the binding](http://stackoverflow.com/questions/27391909/replace-evalcode-string-binding-with-lambda/27392437) of a `Proc` once it's created, so template engines that evaluate code in a given binding cannot use a compiled proc, they must parse the code every time. By using a `Proc` we can generate a Ruby function which *can* be compiled to a faster representation by the VM.
16
-
17
- In addition, I wanted a simple markup parser and builder for HTML style markup. These are used heavily by Utopia for implementing it's tag based evaluation. `Trenni::Builder` is a simple and efficient way to generate markup, it's not particularly notable, except that it doesn't use `method_missing` to [implement normal behaviour](https://github.com/sparklemotion/nokogiri/blob/b6679e928924529b56dcc0f3164224c040d14555/lib/nokogiri/xml/builder.rb#L355) which is [sort of slow](http://franck.verrot.fr/blog/2015/07/12/benchmarking-ruby-method-missing-and-define-method/).
18
-
19
- The 2nd release of Trenni in 2016 saw an overhaul of the internal parsers. I used [Ragel](http://www.colm.net/open-source/ragel/) to implement efficient event-based markup and template parsers, which can be compiled to both C and Ruby. This provides a native code path where possible giving speed-ups between 10x - 20x. In addition, the formal grammar is more robust.
20
-
21
- The 3rd release of Trenni in 2017 was primarily focused on performance, by moving more of the critical parsing, escaping and tag generation functions to C. In practical usage, this gave about a 40-50% improvement in performance overall.
22
-
23
- ## Is it fast?
24
-
25
- It's faster than Nokogiri for parsing markup:
26
-
27
- Trenni::Native
28
- Warming up --------------------------------------
29
- Large (Trenni) 71.000 i/100ms
30
- Large (Nokogiri) 28.000 i/100ms
31
- Calculating -------------------------------------
32
- Large (Trenni) 662.050 (± 3.9%) i/s - 3.337k in 5.048115s
33
- Large (Nokogiri) 266.878 (±10.9%) i/s - 1.316k in 5.008464s
34
-
35
- Comparison:
36
- Large (Trenni): 662.1 i/s
37
- Large (Nokogiri): 266.9 i/s - 2.48x slower
38
-
39
- It's significantly faster than ERB:
40
-
41
- Trenni::Template
42
- Warming up --------------------------------------
43
- Trenni (object) 75.667k i/100ms
44
- ERB (binding) 6.940k i/100ms
45
- Calculating -------------------------------------
46
- Trenni (object) 1.095M (± 7.9%) i/s - 5.448M in 5.007244s
47
- ERB (binding) 69.381k (± 7.1%) i/s - 347.000k in 5.027333s
48
-
49
- Comparison:
50
- Trenni (object): 1094979.9 i/s
51
- ERB (binding): 69381.1 i/s - 15.78x slower
52
-
53
- ## Installation
54
-
55
- Add this line to your application's Gemfile:
56
-
57
- gem 'trenni'
58
-
59
- And then execute:
60
-
61
- $ bundle
62
-
63
- Or install it yourself as:
64
-
65
- $ gem install trenni
66
-
67
- ## Usage
68
-
69
- ### Markup Parser
70
-
71
- The markup parser parses a loose super-set of HTML in a way that's useful for content processing, similar to an XSLT processor. It's designed to be faster and easier to use, and integrate directly into an output pipeline.
72
-
73
- To invoke the markup parser:
74
-
75
- ```ruby
76
- require 'trenni'
77
-
78
- buffer = Trenni::Buffer(string)
79
-
80
- # Custom entities, or could use Trenni::Entities::HTML5
81
- entities = {'amp' => '&', 'lt', => '<', 'gt' => '>', 'quot' => '"'}
82
-
83
- # Modify this class to accumulate events or pass them on somewhere else.
84
- class Delegate
85
- # Called when encountering an open tag: `<` name
86
- def open_tag_begin(name, offset)
87
- end
88
-
89
- # Called when encountering an attribute after open_tag_begin
90
- def attribute(key, value)
91
- end
92
-
93
- # Called when encountering the end of the opening tag.
94
- def open_tag_end(self_closing)
95
- end
96
-
97
- # Called when encountering the closing tag: '</' name '>'
98
- def close_tag(name, offset)
99
- end
100
-
101
- # Called with the full doctype: '<!DOCTYPE html>'
102
- def doctype(string)
103
- end
104
-
105
- # Called with the full comment: '<!-- comment -->'
106
- def comment(string)
107
- end
108
-
109
- # Called with the parsed instruction: '<?' identifier space+ body '?>'
110
- def instruction(string)
111
- end
112
-
113
- # Called with a cdata block: '<![CDATA[text]]>'
114
- def cdata(string)
115
- end
116
-
117
- # Called with any arbitrary pcdata text (e.g. between tags).
118
- def text(string)
119
- end
120
- end
121
-
122
- # Do the actual work:
123
- Trenni::Parsers.parse_markup(buffer, Delegate.new, entities)
124
- ```
125
-
126
- ### Templates
127
-
128
- Trenni templates work essentially the same way as all other templating systems:
129
-
130
- ```ruby
131
- buffer = Trenni::Buffer('<?r self.each do |item| ?>#{item}<?r end ?>')
132
- template = Trenni::Template.new(buffer)
133
-
134
- items = 1..4
135
-
136
- template.to_string(items) # => "1234"
137
- ```
138
-
139
- The code above demonstrate the only two constructs, `<?r expression ?>` and `#{output}`.
140
-
141
- Trenni doesn't support using `binding` for evaluation, as this is a slow code path. It uses `instance_exec`
142
-
143
- ### Builder
144
-
145
- Trenni can help construct XML/HTML using a simple DSL:
146
-
147
- ```ruby
148
- Trenni::Builder.fragment do |builder|
149
- builder.inline 'p' do
150
- builder.tag 'strong' do
151
- builder.text 'Hello'
152
- end
153
- builder.text ' World'
154
- end
155
- end.to_s
156
- # => "<p><strong>Hello</strong> World</p>"
157
- ```
158
-
159
- ### Testing
160
-
161
- To test the Ruby parsers:
162
-
163
- rake generate_fallback_parsers && TRENNI_PREFER_FALLBACK=y rspec
164
-
165
- To test the native C parsers:
166
-
167
- rake generate_native_parsers && rake compile && rspec
168
-
169
- ### Benchmarks
170
-
171
- Trenni has a pure Ruby implemenation, with performance critical operations implemented natively. All critical code paths have benchmark specs.
172
-
173
- #### Parser Performance
174
-
175
- You can evaluate and compare template performance with ERB:
176
-
177
- ```
178
- rspec spec/trenni/parsers_performance_spec.rb
179
-
180
- Trenni::Native
181
- #parse_markup
182
- Warming up --------------------------------------
183
- Large (Trenni) 64.000 i/100ms
184
- Large (Nokogiri) 30.000 i/100ms
185
- Calculating -------------------------------------
186
- Large (Trenni) 637.720 (± 6.4%) i/s - 3.200k in 5.038187s
187
- Large (Nokogiri) 294.762 (± 5.8%) i/s - 1.470k in 5.004284s
188
-
189
- Comparison:
190
- Large (Trenni): 637.7 i/s
191
- Large (Nokogiri): 294.8 i/s - 2.16x slower
192
-
193
- should be fast to parse large documents
194
- #parse_template
195
- Warming up --------------------------------------
196
- Large (Trenni) 7.791k i/100ms
197
- Large (ERB) 488.000 i/100ms
198
- Calculating -------------------------------------
199
- Large (Trenni) 87.889k (± 9.5%) i/s - 436.296k in 5.024283s
200
- Large (ERB) 4.844k (± 5.6%) i/s - 24.400k in 5.053247s
201
-
202
- Comparison:
203
- Large (Trenni): 87889.4 i/s
204
- Large (ERB): 4844.5 i/s - 18.14x slower
205
-
206
- should have better performance using instance
207
-
208
- Finished in 28.2 seconds (files took 0.14204 seconds to load)
209
- 2 examples, 0 failures
210
- ```
211
-
212
- To run this with the pure ruby implementation, use `TRENNI_PREFER_FALLBACK=y rspec spec/trenni/parsers_performance_spec.rb`.
213
-
214
- #### Markup String Performance
215
-
216
- Markup safe strings require escaping characters. Doing this natively makes sense, and in MRI, `CGI.escape_html` is implemented in C. Strings that include characters that need to be escaped are a bit slower because a new string must be allocated and modified. So, we test these two cases.
217
-
218
- ```
219
- rspec spec/trenni/markup_performance_spec.rb
220
-
221
- Trenni::Markup
222
- Warming up --------------------------------------
223
- General String 179.396k i/100ms
224
- Code String 85.050k i/100ms
225
- Calculating -------------------------------------
226
- General String 4.773M (±10.0%) i/s - 23.680M in 5.027576s
227
- Code String 1.469M (± 5.7%) i/s - 7.399M in 5.052467s
228
-
229
- Comparison:
230
- General String: 4773201.3 i/s
231
- Code String: 1469345.5 i/s - 3.25x slower
232
-
233
- should be fast to parse large documents
234
-
235
- Finished in 14.11 seconds (files took 0.09696 seconds to load)
236
- 1 example, 0 failures
237
- ```
238
-
239
- #### Template Evaluation Performance
240
-
241
- Evaluating templates and generating output is critical to performance. You can compare Trenni with ERB. The primary factor affecting performance, is the number of interpolations, because each interpolation requires evaluation and concatenation.
242
-
243
- ```
244
- rspec spec/trenni/template_performance_spec.rb
245
-
246
- Trenni::Template
247
- Warming up --------------------------------------
248
- Trenni 79.000 i/100ms
249
- Calculating -------------------------------------
250
- Trenni 817.703 (± 7.7%) i/s - 4.108k in 5.071586s
251
- should be fast for lots of interpolations
252
- Warming up --------------------------------------
253
- Trenni (object) 79.149k i/100ms
254
- ERB (binding) 5.416k i/100ms
255
- Calculating -------------------------------------
256
- Trenni (object) 1.081M (± 3.7%) i/s - 5.461M in 5.059151s
257
- ERB (binding) 59.016k (± 4.7%) i/s - 297.880k in 5.058614s
258
-
259
- Comparison:
260
- Trenni (object): 1080909.2 i/s
261
- ERB (binding): 59016.3 i/s - 18.32x slower
262
-
263
- should be fast for basic templates
264
- Warming up --------------------------------------
265
- Trenni 34.204k i/100ms
266
- Calculating -------------------------------------
267
- Trenni 407.905k (± 9.0%) i/s - 2.018M in 5.001248s
268
- should be fast with capture
269
-
270
- Finished in 28.25 seconds (files took 0.09765 seconds to load)
271
- 3 examples, 0 failures
272
- ```
273
-
274
- ## See Also
275
-
276
- - [language-trenni](https://atom.io/packages/language-trenni) package for the [Atom text editor](https://atom.io). It provides syntax highlighting and integration when Trenni is used with the [utopia web framework](https://github.com/ioquatix/utopia).
277
-
278
- - [vim-trenni](https://github.com/huba/vim-trenni) package for Vim.
279
-
280
- - [Trenni Formatters](https://github.com/ioquatix/trenni-formatters) is a separate gem that uses `Trenni::Builder` to generate HTML forms easily.
281
-
282
- ## Contributing
283
-
284
- 1. Fork it
285
- 2. Create your feature branch (`git checkout -b my-new-feature`)
286
- 3. Commit your changes (`git commit -am 'Add some feature'`)
287
- 4. Push to the branch (`git push origin my-new-feature`)
288
- 5. Create new Pull Request
289
-
290
- ## License
291
-
292
- Released under the MIT license.
293
-
294
- Copyright, 2017, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
295
-
296
- Permission is hereby granted, free of charge, to any person obtaining a copy
297
- of this software and associated documentation files (the "Software"), to deal
298
- in the Software without restriction, including without limitation the rights
299
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
300
- copies of the Software, and to permit persons to whom the Software is
301
- furnished to do so, subject to the following conditions:
302
-
303
- The above copyright notice and this permission notice shall be included in
304
- all copies or substantial portions of the Software.
305
-
306
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
307
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
308
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
309
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
310
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
311
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
312
- THE SOFTWARE.
data/Rakefile DELETED
@@ -1,21 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler/gem_tasks"
4
- require "rspec/core/rake_task"
5
-
6
- # Load all rake tasks:
7
- import(*Dir.glob('tasks/**/*.rake'))
8
-
9
- RSpec::Core::RakeTask.new(:test)
10
-
11
- task :environment do
12
- $LOAD_PATH.unshift File.expand_path('lib', __dir__)
13
- end
14
-
15
- task :console => :environment do
16
- require 'pry'
17
-
18
- Pry.start
19
- end
20
-
21
- task :default => :test
@@ -1,52 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'benchmark/ips'
4
-
5
- puts "Ruby #{RUBY_VERSION} at #{Time.now}"
6
- puts
7
-
8
- firstname = 'soundarapandian'
9
- middlename = 'rathinasamy'
10
- lastname = 'arumugam'
11
-
12
- def do_call(&block)
13
- block.call
14
- end
15
-
16
- def do_yield(&block)
17
- yield
18
- end
19
-
20
- def do_yield_without_block
21
- yield
22
- end
23
-
24
- existing_block = proc{}
25
-
26
- Benchmark.ips do |x|
27
- x.report("block.call") do |i|
28
- buffer = String.new
29
-
30
- while (i -= 1) > 0
31
- do_call(&existing_block)
32
- end
33
- end
34
-
35
- x.report("yield with block") do |i|
36
- buffer = String.new
37
-
38
- while (i -= 1) > 0
39
- do_yield(&existing_block)
40
- end
41
- end
42
-
43
- x.report("yield") do |i|
44
- buffer = String.new
45
-
46
- while (i -= 1) > 0
47
- do_yield_without_block(&existing_block)
48
- end
49
- end
50
-
51
- x.compare!
52
- end