slim 4.1.0 → 5.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +51 -0
  3. data/.yardopts +1 -1
  4. data/CHANGES +27 -7
  5. data/Gemfile +15 -41
  6. data/LICENSE +1 -1
  7. data/README.jp.md +17 -20
  8. data/README.md +32 -24
  9. data/Rakefile +1 -8
  10. data/doc/jp/translator.md +1 -1
  11. data/doc/logic_less.md +1 -1
  12. data/doc/translator.md +1 -1
  13. data/lib/slim/do_inserter.rb +1 -1
  14. data/lib/slim/embedded.rb +12 -11
  15. data/lib/slim/end_inserter.rb +2 -2
  16. data/lib/slim/engine.rb +2 -0
  17. data/lib/slim/parser.rb +9 -29
  18. data/lib/slim/railtie.rb +17 -0
  19. data/lib/slim/splat/builder.rb +8 -2
  20. data/lib/slim/splat/filter.rb +2 -1
  21. data/lib/slim/template.rb +0 -14
  22. data/lib/slim/version.rb +1 -1
  23. data/lib/slim.rb +1 -0
  24. data/slim.gemspec +4 -5
  25. data/test/core/test_code_evaluation.rb +1 -0
  26. data/test/core/test_code_structure.rb +17 -0
  27. data/test/core/test_commands.rb +8 -7
  28. data/test/core/test_embedded_engines.rb +18 -14
  29. data/test/core/test_erb_converter.rb +4 -6
  30. data/test/core/test_html_attributes.rb +8 -0
  31. data/test/core/test_html_structure.rb +6 -0
  32. data/test/core/test_pretty.rb +4 -7
  33. data/test/literate/TESTS.md +72 -22
  34. data/test/literate/run.rb +2 -2
  35. data/test/rails/config/application.rb +0 -17
  36. data/test/rails/test/test_slim.rb +6 -12
  37. data/test/sinatra/helper.rb +0 -2
  38. metadata +13 -27
  39. data/.travis.yml +0 -38
  40. data/benchmarks/context.rb +0 -11
  41. data/benchmarks/profile-parser.rb +0 -10
  42. data/benchmarks/profile-render.rb +0 -12
  43. data/benchmarks/run-benchmarks.rb +0 -120
  44. data/benchmarks/run-diffbench.rb +0 -21
  45. data/benchmarks/view.erb +0 -25
  46. data/benchmarks/view.haml +0 -20
  47. data/benchmarks/view.slim +0 -19
  48. data/test/rails/config/initializers/secret_token.rb +0 -7
@@ -27,6 +27,22 @@ renders as
27
27
  Text block
28
28
  ~~~
29
29
 
30
+ You can add leading or trailing white space with the `<` and `>` markers:
31
+
32
+ ~~~ slim
33
+ |< Text with leading whitespace.
34
+ | Text with leading whitespace.
35
+ |> Text with trailing whitespace.
36
+ |<> Text with both leading and trailing whitespace.
37
+ ~~~
38
+
39
+ renders as
40
+
41
+ ~~~ html
42
+ Text with leading whitespace. Text with leading whitespace.Text with trailing whitespace. Text with both leading and trailing whitespace.
43
+ ~~~
44
+
45
+
30
46
  Multiple lines can be indented beneath the first text line.
31
47
 
32
48
  ~~~ slim
@@ -77,6 +93,30 @@ Text
77
93
  lines
78
94
  ~~~
79
95
 
96
+ ~~~ slim
97
+ |><
98
+
99
+ Text
100
+ block
101
+
102
+ with
103
+
104
+ multiple
105
+ lines
106
+ ~~~
107
+
108
+ renders as
109
+
110
+ ~~~ html
111
+ Text
112
+ block
113
+
114
+ with
115
+
116
+ multiple
117
+ lines
118
+ ~~~
119
+
80
120
  You can nest text blocks beneath tags.
81
121
 
82
122
  ~~~ slim
@@ -95,13 +135,23 @@ renders as
95
135
  You can embed html code in the text which is not escaped.
96
136
 
97
137
  ~~~ slim
98
- | <a href="http://slim-lang.com">slim-lang.com</a>
138
+ | <a href="http://github.com/slim-template/slim">github.com/slim-template/slim</a>
139
+ ~~~
140
+
141
+ renders as
142
+
143
+ ~~~ html
144
+ <a href="http://github.com/slim-template/slim">github.com/slim-template/slim</a>
145
+ ~~~
146
+
147
+ ~~~ slim
148
+ |<a href="http://github.com/slim-template/slim">github.com/slim-template/slim</a>
99
149
  ~~~
100
150
 
101
151
  renders as
102
152
 
103
153
  ~~~ html
104
- <a href="http://slim-lang.com">slim-lang.com</a>
154
+ <a href="http://github.com/slim-template/slim">github.com/slim-template/slim</a>
105
155
  ~~~
106
156
 
107
157
  ### Text with trailing white space `'`
@@ -122,13 +172,13 @@ This is especially useful if you use tags behind a text block.
122
172
 
123
173
  ~~~ slim
124
174
  ' Link to
125
- a href="http://slim-lang.com" slim-lang.com
175
+ a href="http://github.com/slim-template/slim" github.com/slim-template/slim
126
176
  ~~~
127
177
 
128
178
  renders as
129
179
 
130
180
  ~~~ html
131
- Link to <a href="http://slim-lang.com">slim-lang.com</a>
181
+ Link to <a href="http://github.com/slim-template/slim">github.com/slim-template/slim</a>
132
182
  ~~~
133
183
 
134
184
  Multiple lines can be indented beneath the first text line.
@@ -186,13 +236,13 @@ lines
186
236
  HTML can be written directly.
187
237
 
188
238
  ~~~ slim
189
- <a href="http://slim-lang.com">slim-lang.com</a>
239
+ <a href="http://github.com/slim-template/slim">github.com/slim-template/slim</a>
190
240
  ~~~
191
241
 
192
242
  renders as
193
243
 
194
244
  ~~~ html
195
- <a href="http://slim-lang.com">slim-lang.com</a>
245
+ <a href="http://github.com/slim-template/slim">github.com/slim-template/slim</a>
196
246
  ~~~
197
247
 
198
248
  HTML tags allow nested blocks inside.
@@ -800,31 +850,31 @@ If a delimiter makes the syntax more readable for you, you can use the character
800
850
 
801
851
  ~~~ slim
802
852
  li
803
- a(href="http://slim-lang.com" class="important") Link
853
+ a(href="http://github.com/slim-template/slim" class="important") Link
804
854
  li
805
- a[href="http://slim-lang.com" class="important"] Link
855
+ a[href="http://github.com/slim-template/slim" class="important"] Link
806
856
  li
807
- a{href="http://slim-lang.com" class="important"} Link
857
+ a{href="http://github.com/slim-template/slim" class="important"} Link
808
858
  ~~~
809
859
 
810
860
  renders as
811
861
 
812
862
  ~~~ html
813
863
  <li>
814
- <a class="important" href="http://slim-lang.com">Link</a>
864
+ <a class="important" href="http://github.com/slim-template/slim">Link</a>
815
865
  </li>
816
866
  <li>
817
- <a class="important" href="http://slim-lang.com">Link</a>
867
+ <a class="important" href="http://github.com/slim-template/slim">Link</a>
818
868
  </li>
819
869
  <li>
820
- <a class="important" href="http://slim-lang.com">Link</a>
870
+ <a class="important" href="http://github.com/slim-template/slim">Link</a>
821
871
  </li>
822
872
  ~~~
823
873
 
824
874
  If you wrap the attributes, you can spread them across multiple lines:
825
875
 
826
876
  ~~~ slim
827
- a(href="http://slim-lang.com"
877
+ a(href="http://github.com/slim-template/slim"
828
878
 
829
879
  class="important") Link
830
880
  ~~~
@@ -832,7 +882,7 @@ a(href="http://slim-lang.com"
832
882
  renders as
833
883
 
834
884
  ~~~ html
835
- <a class="important" href="http://slim-lang.com">Link</a>
885
+ <a class="important" href="http://github.com/slim-template/slim">Link</a>
836
886
  ~~~
837
887
 
838
888
  ~~~ slim
@@ -872,19 +922,19 @@ renders as
872
922
  You can use single or double quotes for simple text attributes.
873
923
 
874
924
  ~~~ slim
875
- a href="http://slim-lang.com" title='Slim Homepage' Goto the Slim homepage
925
+ a href="http://github.com/slim-template/slim" title='Slim Homepage' Goto the Slim homepage
876
926
  ~~~
877
927
 
878
928
  renders as
879
929
 
880
930
  ~~~ html
881
- <a href="http://slim-lang.com" title="Slim Homepage">Goto the Slim homepage</a>
931
+ <a href="http://github.com/slim-template/slim" title="Slim Homepage">Goto the Slim homepage</a>
882
932
  ~~~
883
933
 
884
934
  You can use text interpolation in the quoted attributes:
885
935
 
886
936
  ~~~ slim
887
- - url='slim-lang.com'
937
+ - url='github.com/slim-template/slim'
888
938
  a href="http://#{url}" Goto the #{url}
889
939
  a href="{"test"}" Test of quoted text in braces
890
940
  ~~~
@@ -892,7 +942,7 @@ a href="{"test"}" Test of quoted text in braces
892
942
  renders as
893
943
 
894
944
  ~~~ html
895
- <a href="http://slim-lang.com">Goto the slim-lang.com</a><a href="{&quot;test&quot;}">Test of quoted text in braces</a>
945
+ <a href="http://github.com/slim-template/slim">Goto the github.com/slim-template/slim</a><a href="{&quot;test&quot;}">Test of quoted text in braces</a>
896
946
  ~~~
897
947
 
898
948
  The attribute value will be escaped by default. Use == if you want to disable escaping in the attribute.
@@ -1040,13 +1090,13 @@ You can configure attributes to be merged if multiple are given (See option `:me
1040
1090
  this is done for class attributes with the white space as delimiter.
1041
1091
 
1042
1092
  ~~~ slim
1043
- a.menu class="highlight" href="http://slim-lang.com/" Slim-lang.com
1093
+ a.menu class="highlight" href="http://github.com/slim-template/slim/" github.com/slim-template/slim
1044
1094
  ~~~
1045
1095
 
1046
1096
  renders as
1047
1097
 
1048
1098
  ~~~ html
1049
- <a class="menu highlight" href="http://slim-lang.com/">Slim-lang.com</a>
1099
+ <a class="menu highlight" href="http://github.com/slim-template/slim/">github.com/slim-template/slim</a>
1050
1100
  ~~~
1051
1101
 
1052
1102
  You can also use an `Array` as attribute value and the array elements will be merged using the delimiter.
@@ -1074,7 +1124,7 @@ with the :tag key.
1074
1124
  ~~~ slim
1075
1125
  ruby:
1076
1126
  def self.a_unless_current
1077
- @page_current ? {tag: 'span'} : {tag: 'a', href: 'http://slim-lang.com/'}
1127
+ @page_current ? {tag: 'span'} : {tag: 'a', href: 'http://github.com/slim-template/slim/'}
1078
1128
  end
1079
1129
  - @page_current = true
1080
1130
  *a_unless_current Link
@@ -1085,7 +1135,7 @@ ruby:
1085
1135
  renders as
1086
1136
 
1087
1137
  ~~~ html
1088
- <span>Link</span><a href="http://slim-lang.com/">Link</a>
1138
+ <span>Link</span><a href="http://github.com/slim-template/slim/">Link</a>
1089
1139
  ~~~
1090
1140
 
1091
1141
  ### Shortcuts
data/test/literate/run.rb CHANGED
@@ -65,9 +65,9 @@ class LiterateTest < Temple::Engine
65
65
  @in_testcase = false
66
66
  result = " html = #{code.inspect}\n"
67
67
  if @opts.empty?
68
- result << " render(slim).must_equal html\nend\n"
68
+ result << " _(render(slim)).must_equal html\nend\n"
69
69
  else
70
- result << " options = #{@opts.inspect}\n render(slim, options).must_equal html\nend\n"
70
+ result << " options = #{@opts.inspect}\n _(render(slim, options)).must_equal html\nend\n"
71
71
  end
72
72
  end
73
73
 
@@ -6,9 +6,7 @@ require 'action_view/railtie'
6
6
  #require 'active_record/railtie'
7
7
  #require 'action_mailer/railtie'
8
8
  require 'sprockets/railtie'
9
-
10
9
  require 'slim'
11
- require 'slim-rails/register_engine'
12
10
 
13
11
  module Dummy
14
12
  class Application < Rails::Application
@@ -42,20 +40,5 @@ module Dummy
42
40
 
43
41
  # Configure sensitive parameters which will be filtered from the log file.
44
42
  config.filter_parameters += [:password]
45
-
46
- # From slim-rails fix for "ActionView::Template::Error: Unknown line indicator"
47
- # https://github.com/slim-template/slim-rails/blob/991589ea5648e5e896781e68912bc51beaf4102a/lib/slim-rails/register_engine.rb
48
- if config.respond_to?(:assets)
49
- config.assets.configure do |env|
50
- if env.respond_to?(:register_transformer) && Sprockets::VERSION.to_i > 3
51
- env.register_mime_type 'text/slim', extensions: ['.slim', '.slim.html']
52
- env.register_transformer 'text/slim', 'text/html', Slim::Rails::RegisterEngine::Transformer
53
- elsif env.respond_to?(:register_engine)
54
- args = ['.slim', Slim::Template]
55
- args << { silence_deprecation: true } if Sprockets::VERSION.start_with?('3')
56
- env.register_engine(*args)
57
- end
58
- end
59
- end
60
43
  end
61
44
  end
@@ -46,18 +46,12 @@ class TestSlim < ActionDispatch::IntegrationTest
46
46
  assert_html "<h1>Hello Slim!</h1><p>With a partial!</p>"
47
47
  end
48
48
 
49
- if RUBY_ENGINE == 'jruby' && RUBY_ENGINE < '2.2.0'
50
- puts 'Streaming test disabled for JRuby < 9000.',
51
- 'See https://github.com/jruby/jruby/issues/1243',
52
- 'and https://github.com/jruby/jruby/issues/1789'
53
- else
54
- puts 'Streaming test enabled.'
55
- test "streaming" do
56
- get "/slim/streaming"
57
- output = "2f\r\n<!DOCTYPE html><html><head><title>Dummy</title>\r\nd\r\n</head><body>\r\n17\r\nHeading set from a view\r\n15\r\n<div class=\"content\">\r\n53\r\n<p>Page content</p><h1><p>Hello Streaming!</p></h1><h2><p>Hello Streaming!</p></h2>\r\n14\r\n</div></body></html>\r\n0\r\n\r\n"
58
- assert_equal output, @response.body
59
- end
60
- end
49
+ # TODO Reenable streaming test
50
+ # test "streaming" do
51
+ # get "/slim/streaming"
52
+ # output = "2f\r\n<!DOCTYPE html><html><head><title>Dummy</title>\r\nd\r\n</head><body>\r\n17\r\nHeading set from a view\r\n15\r\n<div class=\"content\">\r\n53\r\n<p>Page content</p><h1><p>Hello Streaming!</p></h1><h2><p>Hello Streaming!</p></h2>\r\n14\r\n</div></body></html>\r\n0\r\n\r\n"
53
+ # assert_equal output, @response.body
54
+ # end
61
55
 
62
56
  test "render integers" do
63
57
  get "/slim/integers"
@@ -8,8 +8,6 @@ if ENV['COVERAGE']
8
8
  end
9
9
 
10
10
  ENV['APP_ENV'] = 'test'
11
- Encoding.default_external = "UTF-8" if defined? Encoding
12
-
13
11
  RUBY_ENGINE = 'ruby' unless defined? RUBY_ENGINE
14
12
 
15
13
  require 'rack'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slim
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.0
4
+ version: 5.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Mendler
@@ -10,28 +10,22 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2020-05-07 00:00:00.000000000 Z
13
+ date: 2023-03-06 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: temple
17
17
  requirement: !ruby/object:Gem::Requirement
18
18
  requirements:
19
- - - ">="
20
- - !ruby/object:Gem::Version
21
- version: 0.7.6
22
- - - "<"
19
+ - - "~>"
23
20
  - !ruby/object:Gem::Version
24
- version: '0.9'
21
+ version: 0.10.0
25
22
  type: :runtime
26
23
  prerelease: false
27
24
  version_requirements: !ruby/object:Gem::Requirement
28
25
  requirements:
29
- - - ">="
30
- - !ruby/object:Gem::Version
31
- version: 0.7.6
32
- - - "<"
26
+ - - "~>"
33
27
  - !ruby/object:Gem::Version
34
- version: '0.9'
28
+ version: 0.10.0
35
29
  - !ruby/object:Gem::Dependency
36
30
  name: tilt
37
31
  requirement: !ruby/object:Gem::Requirement
@@ -41,7 +35,7 @@ dependencies:
41
35
  version: 2.0.6
42
36
  - - "<"
43
37
  - !ruby/object:Gem::Version
44
- version: '2.1'
38
+ version: '2.2'
45
39
  type: :runtime
46
40
  prerelease: false
47
41
  version_requirements: !ruby/object:Gem::Requirement
@@ -51,7 +45,7 @@ dependencies:
51
45
  version: 2.0.6
52
46
  - - "<"
53
47
  - !ruby/object:Gem::Version
54
- version: '2.1'
48
+ version: '2.2'
55
49
  description: Slim is a template language whose goal is reduce the syntax to the essential
56
50
  parts without becoming cryptic.
57
51
  email:
@@ -63,8 +57,8 @@ executables:
63
57
  extensions: []
64
58
  extra_rdoc_files: []
65
59
  files:
60
+ - ".github/workflows/test.yml"
66
61
  - ".gitignore"
67
- - ".travis.yml"
68
62
  - ".yardopts"
69
63
  - CHANGES
70
64
  - Gemfile
@@ -72,14 +66,6 @@ files:
72
66
  - README.jp.md
73
67
  - README.md
74
68
  - Rakefile
75
- - benchmarks/context.rb
76
- - benchmarks/profile-parser.rb
77
- - benchmarks/profile-render.rb
78
- - benchmarks/run-benchmarks.rb
79
- - benchmarks/run-diffbench.rb
80
- - benchmarks/view.erb
81
- - benchmarks/view.haml
82
- - benchmarks/view.slim
83
69
  - bin/slimrb
84
70
  - doc/include.md
85
71
  - doc/jp/include.md
@@ -106,6 +92,7 @@ files:
106
92
  - lib/slim/logic_less/context.rb
107
93
  - lib/slim/logic_less/filter.rb
108
94
  - lib/slim/parser.rb
95
+ - lib/slim/railtie.rb
109
96
  - lib/slim/smart.rb
110
97
  - lib/slim/smart/escaper.rb
111
98
  - lib/slim/smart/filter.rb
@@ -177,7 +164,6 @@ files:
177
164
  - test/rails/config/initializers/backtrace_silencers.rb
178
165
  - test/rails/config/initializers/inflections.rb
179
166
  - test/rails/config/initializers/mime_types.rb
180
- - test/rails/config/initializers/secret_token.rb
181
167
  - test/rails/config/initializers/session_store.rb
182
168
  - test/rails/config/locales/en.yml
183
169
  - test/rails/config/routes.rb
@@ -195,7 +181,7 @@ files:
195
181
  - test/sinatra/views/layout2.slim
196
182
  - test/smart/test_smart_text.rb
197
183
  - test/translator/test_translator.rb
198
- homepage: http://slim-lang.com/
184
+ homepage: http://github.com/slim-template/slim/
199
185
  licenses:
200
186
  - MIT
201
187
  metadata: {}
@@ -207,14 +193,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
207
193
  requirements:
208
194
  - - ">="
209
195
  - !ruby/object:Gem::Version
210
- version: 2.0.0
196
+ version: 2.5.0
211
197
  required_rubygems_version: !ruby/object:Gem::Requirement
212
198
  requirements:
213
199
  - - ">="
214
200
  - !ruby/object:Gem::Version
215
201
  version: '0'
216
202
  requirements: []
217
- rubygems_version: 3.1.2
203
+ rubygems_version: 3.2.5
218
204
  signing_key:
219
205
  specification_version: 4
220
206
  summary: Slim is a template language.
data/.travis.yml DELETED
@@ -1,38 +0,0 @@
1
- # https://www.ruby-lang.org/en/downloads/releases/
2
- language: ruby
3
- rvm:
4
- - 2.6.1
5
- - 2.5.3
6
- - 2.4.5
7
- - jruby-9.2.6.0
8
- - jruby-9.1.17.0
9
-
10
- # Recommend sudo required when using trusty dist
11
- # https://docs.travis-ci.com/user/reference/trusty/
12
- sudo: required
13
- dist: trusty
14
-
15
- script: "bundle exec rake $TASK"
16
-
17
- after_success:
18
- - bundle exec codeclimate-test-reporter
19
-
20
- env:
21
- global:
22
- - secure: "a7sD9iwPJJn3Fj+mn62GAmy/PEguh3elrilsp1KS+WfDiCiIKD8Q5KG2Jv67DGcQAGI3dPWeh7+ZhZ/W7nEipwWUBmSvGYVeoF63y8j6mNRLeekqspj94l47hXyFePj9bCadY1b1/xY4lE1pMEU8eA8AOUHUqCSuH+Kk/MuvyLM="
23
- matrix:
24
- - "TASK=test:core_and_plugins"
25
- - "TASK=test:rails RAILS=6.0.0.beta2"
26
- - "TASK=test:rails RAILS=5.2.2"
27
- - "TASK=test:rails RAILS=5.2.1.1"
28
- - "TASK=test:rails RAILS=5.2.1"
29
- - "TASK=test:sinatra SINATRA=2.0.5"
30
- - "TASK=test:sinatra SINATRA=2.0.4"
31
- - "TASK=test:sinatra SINATRA=2.0.3"
32
-
33
- matrix:
34
- exclude:
35
- - rvm: 2.4.5
36
- env: "TASK=test:rails RAILS=6.0.0.beta2"
37
- - rvm: jruby-9.1.17.0
38
- env: "TASK=test:rails RAILS=6.0.0.beta2"
@@ -1,11 +0,0 @@
1
- class Context
2
- def header
3
- 'Colors'
4
- end
5
-
6
- def item
7
- [ { name: 'red', current: true, url: '#red' },
8
- { name: 'green', current: false, url: '#green' },
9
- { name: 'blue', current: false, url: '#blue' } ]
10
- end
11
- end
@@ -1,10 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'), File.dirname(__FILE__))
4
-
5
- require 'slim'
6
-
7
- content = File.read(File.dirname(__FILE__) + '/view.slim')
8
- engine = Slim::Engine.new
9
-
10
- 1000.times { engine.call(content) }
@@ -1,12 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'), File.dirname(__FILE__))
4
-
5
- require 'slim'
6
- require 'context'
7
-
8
- content = File.read(File.dirname(__FILE__) + '/view.slim')
9
- slim = Slim::Template.new { content }
10
- context = Context.new
11
-
12
- 10000.times { slim.render(context) }
@@ -1,120 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'), File.dirname(__FILE__))
4
-
5
- require 'slim'
6
- require 'context'
7
-
8
- require 'benchmark/ips'
9
- require 'tilt'
10
- require 'erubis'
11
- require 'erb'
12
- require 'haml'
13
-
14
- class SlimBenchmarks
15
- def initialize(slow)
16
- @benches = Hash.new { |h, k| h[k] = [] }
17
-
18
- @erb_code = File.read(File.dirname(__FILE__) + '/view.erb')
19
- @haml_code = File.read(File.dirname(__FILE__) + '/view.haml')
20
- @slim_code = File.read(File.dirname(__FILE__) + '/view.slim')
21
-
22
- init_compiled_benches
23
- init_tilt_benches
24
- init_parsing_benches if slow
25
- end
26
-
27
- def init_compiled_benches
28
- haml = Haml::Engine.new(@haml_code, format: :html5, escape_attrs: false)
29
-
30
- context = Context.new
31
-
32
- haml.def_method(context, :run_haml)
33
- context.instance_eval %{
34
- def run_erb; #{ERB.new(@erb_code).src}; end
35
- def run_erubis; #{Erubis::Eruby.new(@erb_code).src}; end
36
- def run_temple_erb; #{Temple::ERB::Engine.new.call @erb_code}; end
37
- def run_fast_erubis; #{Erubis::FastEruby.new(@erb_code).src}; end
38
- def run_slim_pretty; #{Slim::Engine.new(pretty: true).call @slim_code}; end
39
- def run_slim_ugly; #{Slim::Engine.new.call @slim_code}; end
40
- }
41
-
42
- bench(:compiled, 'erb') { context.run_erb }
43
- bench(:compiled, 'erubis') { context.run_erubis }
44
- bench(:compiled, 'fast erubis') { context.run_fast_erubis }
45
- bench(:compiled, 'temple erb') { context.run_temple_erb }
46
- bench(:compiled, 'slim pretty') { context.run_slim_pretty }
47
- bench(:compiled, 'slim ugly') { context.run_slim_ugly }
48
- bench(:compiled, 'haml') { context.run_haml }
49
- end
50
-
51
- def init_tilt_benches
52
- tilt_erb = Tilt::ERBTemplate.new { @erb_code }
53
- tilt_erubis = Tilt::ErubisTemplate.new { @erb_code }
54
- tilt_temple_erb = Temple::ERB::Template.new { @erb_code }
55
- tilt_haml = Tilt::HamlTemplate.new(format: :html5) { @haml_code }
56
- tilt_slim_pretty = Slim::Template.new(pretty: true) { @slim_code }
57
- tilt_slim_ugly = Slim::Template.new { @slim_code }
58
-
59
- context = Context.new
60
-
61
- bench(:tilt, 'erb') { tilt_erb.render(context) }
62
- bench(:tilt, 'erubis') { tilt_erubis.render(context) }
63
- bench(:tilt, 'temple erb') { tilt_temple_erb.render(context) }
64
- bench(:tilt, 'slim pretty') { tilt_slim_pretty.render(context) }
65
- bench(:tilt, 'slim ugly') { tilt_slim_ugly.render(context) }
66
- bench(:tilt, 'haml') { tilt_haml.render(context) }
67
- end
68
-
69
- def init_parsing_benches
70
- context = Context.new
71
- context_binding = context.instance_eval { binding }
72
-
73
- bench(:parsing, 'erb') { ERB.new(@erb_code).result(context_binding) }
74
- bench(:parsing, 'erubis') { Erubis::Eruby.new(@erb_code).result(context_binding) }
75
- bench(:parsing, 'fast erubis') { Erubis::FastEruby.new(@erb_code).result(context_binding) }
76
- bench(:parsing, 'temple erb') { Temple::ERB::Template.new { @erb_code }.render(context) }
77
- bench(:parsing, 'slim pretty') { Slim::Template.new(pretty: true) { @slim_code }.render(context) }
78
- bench(:parsing, 'slim ugly') { Slim::Template.new { @slim_code }.render(context) }
79
- bench(:parsing, 'haml') { Haml::Engine.new(@haml_code, format: :html5).render(context) }
80
- end
81
-
82
- def run
83
- @benches.each do |group_name, group_benches|
84
- puts "Running #{group_name} benchmarks:"
85
-
86
- Benchmark.ips do |x|
87
- group_benches.each do |name, block|
88
- x.report("#{group_name} #{name}", &block)
89
- end
90
-
91
- x.compare!
92
- end
93
- end
94
-
95
- puts "
96
- Compiled benchmark: Template is parsed before the benchmark and
97
- generated ruby code is compiled into a method.
98
- This is the fastest evaluation strategy because it benchmarks
99
- pure execution speed of the generated ruby code.
100
-
101
- Compiled Tilt benchmark: Template is compiled with Tilt, which gives a more
102
- accurate result of the performance in production mode in frameworks like
103
- Sinatra, Ramaze and Camping. (Rails still uses its own template
104
- compilation.)
105
-
106
- Parsing benchmark: Template is parsed every time.
107
- This is not the recommended way to use the template engine
108
- and Slim is not optimized for it. Activate this benchmark with 'rake bench slow=1'.
109
-
110
- Temple ERB is the ERB implementation using the Temple framework. It shows the
111
- overhead added by the Temple framework compared to ERB.
112
- "
113
- end
114
-
115
- def bench(group, name, &block)
116
- @benches[group].push([name, block])
117
- end
118
- end
119
-
120
- SlimBenchmarks.new(ENV['slow']).run
@@ -1,21 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- $:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'), File.dirname(__FILE__))
4
-
5
- require 'slim'
6
- require 'context'
7
- require 'diffbench'
8
-
9
- content = File.read(File.dirname(__FILE__) + '/view.slim')
10
- engine = Slim::Engine.new
11
- template = Slim::Template.new { content }
12
- context = Context.new
13
-
14
- DiffBench.bm do
15
- report("Parse") do
16
- 2000.times { engine.call(content) }
17
- end
18
- report("Render") do
19
- 100000.times { template.render(context) }
20
- end
21
- end
data/benchmarks/view.erb DELETED
@@ -1,25 +0,0 @@
1
- <!DOCTYPE HTML>
2
-
3
- <html lang="en">
4
- <head>
5
- <title>Simple Benchmark</title>
6
- <meta charset="utf-8">
7
- <meta name="description" content="This is an example of a meta description.">
8
- </head>
9
- <body>
10
- <h1><%= header %></h1>
11
- <% unless item.empty? %>
12
- <ul>
13
- <% for i in item %>
14
- <% if i[:current] %>
15
- <li><strong><%= i[:name] %></strong></li>
16
- <% else %>
17
- <li><a href="<%= i[:url] %>"><%= i[:name] %></a></li>
18
- <% end %>
19
- <% end %>
20
- </ul>
21
- <% else %>
22
- <p>The list is empty.</p>
23
- <% end %>
24
- </body>
25
- </html>