slim 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,8 +2,7 @@ PATH
2
2
  remote: .
3
3
  specs:
4
4
  slim (0.7.0)
5
- escape_utils (>= 0.1.9)
6
- temple (~> 0.1.3)
5
+ temple (~> 0.1.4)
7
6
  tilt (~> 1.1)
8
7
 
9
8
  GEM
@@ -12,14 +11,13 @@ GEM
12
11
  abstract (1.0.0)
13
12
  erubis (2.6.6)
14
13
  abstract (>= 1.0.0)
15
- escape_utils (0.1.9)
16
14
  haml (3.0.22)
17
15
  liquid (2.2.2)
18
16
  minitest (1.7.2)
19
17
  rake (0.8.7)
20
18
  rcov (0.9.9)
21
19
  rdiscount (1.6.5)
22
- temple (0.1.3)
20
+ temple (0.1.4)
23
21
  tilt (1.1)
24
22
  yard (0.6.1)
25
23
 
@@ -28,7 +26,6 @@ PLATFORMS
28
26
 
29
27
  DEPENDENCIES
30
28
  erubis
31
- escape_utils (>= 0.1.9)
32
29
  haml
33
30
  liquid
34
31
  minitest
@@ -36,6 +33,6 @@ DEPENDENCIES
36
33
  rcov
37
34
  rdiscount
38
35
  slim!
39
- temple (~> 0.1.3)
36
+ temple (~> 0.1.4)
40
37
  tilt (~> 1.1)
41
38
  yard
data/README.md CHANGED
@@ -5,7 +5,7 @@ Slim is a template language whose goal is to reduce the view syntax to the essen
5
5
 
6
6
  ## What?
7
7
 
8
- Slim is a fast, lightweight templating engine with support for __Rails 3__. It has been tested on Ruby 1.9.2 and Ruby/REE 1.8.7.
8
+ Slim is a fast, lightweight templating engine with support for __Rails 3__. It has been tested on Ruby 1.9.2 and Ruby/REE 1.8.7.
9
9
 
10
10
  Slim's core syntax is guided by one thought: "What's the minimum required to make this work".
11
11
 
@@ -15,10 +15,11 @@ Slim uses [Temple](http://github.com/judofyr/temple) for parsing/compilation and
15
15
 
16
16
  ## Why?
17
17
 
18
- Within the Rails community, _Erb_ and _Haml_ are without doubt the two most popular templating engines. However, _Erb_'s syntax is cumbersome and Haml's performance isn't exactly the best.
18
+ Within the Rails community, _Erb_ and _Haml_ are without doubt the two most popular templating engines. However, _Erb_'s syntax is cumbersome and _Haml_'s performance isn't exactly the best.
19
19
 
20
- Slim was born to bring a minimalist syntax approach with speed. If people chose not to use Slim, it would not be because of speed. ___Yes, Slim is speedy!___ Benchmarks are provided at the end of this README file. Alternatively, a benchmark rake task is provided so you could test it yourself (`rake bench`).
20
+ Slim was born to bring a minimalist syntax approach with speed. If people chose not to use Slim, it would not be because of speed.
21
21
 
22
+ ___Yes, Slim is speedy!___ Benchmarks are provided at the end of this README file. Alternatively, a benchmark rake task is provided so you could test it yourself (`rake bench`).
22
23
 
23
24
  ## How?
24
25
 
@@ -28,13 +29,9 @@ Install Slim as a gem:
28
29
 
29
30
  Include Slim in your Gemfile:
30
31
 
31
- gem 'slim'
32
+ gem 'slim', require: 'slim/rails'
32
33
 
33
- In `config/application.rb`, add the following line near the top (i.e. just below `require 'rails/all'`):
34
-
35
- require 'slim/rails'
36
-
37
- That's it!
34
+ That's it! Now, just use the .slim extension and you're good to go.
38
35
 
39
36
  If you want to use the Slim template directly, you can use the Tilt interface:
40
37
 
@@ -122,7 +119,7 @@ __Please note that all line indicators must be followed by a space__
122
119
  ### Things to know
123
120
 
124
121
  #### Standard Ruby syntax after `-` and `=`
125
- `end` is not required but can be used if you don't want to omit it
122
+ `end` is forbidden behind `-`. Blocks are defined only by indentation.
126
123
 
127
124
  #### Can put content on same line or nest it.
128
125
  If you nest content (e.g. put it on the next line), start the line with a pipe (`|`) or a backtick (`` ` ``).
@@ -267,56 +264,29 @@ __Please note that all line indicators must be followed by a space__
267
264
 
268
265
  <body><p></p></body>
269
266
 
270
-
271
267
  ## Benchmarks
272
268
 
273
269
  # OS X 10.6 + Ruby 1.9.2
274
270
 
275
- user system total real
276
- erb 0.410000 0.010000 0.420000 ( 0.421608)
277
- erubis 0.350000 0.000000 0.350000 ( 0.357187)
278
- fast erubis 0.340000 0.000000 0.340000 ( 0.351943)
279
- slim 2.360000 0.020000 2.380000 ( 2.495331)
280
- haml 2.970000 0.010000 2.980000 ( 3.023121)
281
- haml ugly 2.870000 0.010000 2.880000 ( 2.968662)
282
- erb (cached) 0.150000 0.000000 0.150000 ( 0.149980)
283
- erubis (cached) 0.120000 0.000000 0.120000 ( 0.122935)
284
- fast erubis (cached) 0.100000 0.000000 0.100000 ( 0.105832)
285
- slim (cached) 0.020000 0.000000 0.020000 ( 0.020290)
286
- haml (cached) 0.330000 0.000000 0.330000 ( 0.335519)
287
- haml ugly (cached) 0.280000 0.000000 0.280000 ( 0.286695)
288
-
289
- # OS X 10.6 + REE 1.8.7
290
-
291
- user system total real
292
- erb 0.440000 0.010000 0.450000 ( 0.463941)
293
- erubis 0.310000 0.000000 0.310000 ( 0.322083)
294
- fast erubis 0.310000 0.000000 0.310000 ( 0.309852)
295
- slim 2.420000 0.020000 2.440000 ( 2.470208)
296
- haml 2.990000 0.020000 3.010000 ( 3.040942)
297
- haml ugly 2.900000 0.020000 2.920000 ( 3.101786)
298
- erb (cached) 0.080000 0.000000 0.080000 ( 0.079252)
299
- erubis (cached) 0.070000 0.000000 0.070000 ( 0.066370)
300
- fast erubis (cached) 0.060000 0.000000 0.060000 ( 0.062001)
301
- slim (cached) 0.030000 0.000000 0.030000 ( 0.023835)
302
- haml (cached) 0.270000 0.010000 0.280000 ( 0.279409)
303
- haml ugly (cached) 0.210000 0.000000 0.210000 ( 0.221059)
304
-
305
- # OSX 10.6 + JRuby 1.5.3
306
-
307
- user system total real
308
- erb 0.970000 0.000000 0.970000 ( 0.970000)
309
- erubis 0.672000 0.000000 0.672000 ( 0.672000)
310
- fast erubis 0.624000 0.000000 0.624000 ( 0.624000)
311
- slim 2.694000 0.000000 2.694000 ( 2.694000)
312
- haml 3.368000 0.000000 3.368000 ( 3.368000)
313
- haml ugly 3.462000 0.000000 3.462000 ( 3.462000)
314
- erb (cached) 0.736000 0.000000 0.736000 ( 0.736000)
315
- erubis (cached) 0.413000 0.000000 0.413000 ( 0.413000)
316
- fast erubis (cached) 0.340000 0.000000 0.340000 ( 0.340000)
317
- slim (cached) 0.069000 0.000000 0.069000 ( 0.069000)
318
- haml (cached) 1.001000 0.000000 1.001000 ( 1.001000)
319
- haml ugly (cached) 0.763000 0.000000 0.763000 ( 0.763000)
271
+ user system total real
272
+ erb 2.200000 0.020000 2.220000 ( 2.259262)
273
+ erubis 1.870000 0.010000 1.880000 ( 1.895547)
274
+ fast erubis 1.870000 0.010000 1.880000 ( 1.887996)
275
+ slim 10.380000 0.080000 10.460000 ( 10.543296)
276
+ haml 16.250000 0.070000 16.320000 ( 16.376137)
277
+ haml ugly 15.700000 0.080000 15.780000 ( 15.869233)
278
+ erb (compiled) 0.820000 0.010000 0.830000 ( 0.821538)
279
+ erubis (compiled) 0.680000 0.000000 0.680000 ( 0.680444)
280
+ fast erubis (compiled) 0.600000 0.010000 0.610000 ( 0.605370)
281
+ slim (compiled) 0.180000 0.000000 0.180000 ( 0.182536)
282
+ haml (compiled) 1.800000 0.020000 1.820000 ( 1.863224)
283
+ haml ugly (compiled) 1.560000 0.020000 1.580000 ( 1.602106)
284
+ erb (cached) 0.120000 0.000000 0.120000 ( 0.127988)
285
+ erubis (cached) 0.110000 0.000000 0.110000 ( 0.115064)
286
+ fast erubis (cached) 0.120000 0.010000 0.130000 ( 0.122645)
287
+ slim (cached) 0.140000 0.000000 0.140000 ( 0.134598)
288
+ haml (cached) 0.660000 0.000000 0.660000 ( 0.661025)
289
+ haml ugly (cached) 0.590000 0.010000 0.600000 ( 0.602522)
320
290
 
321
291
  ## Authors
322
292
 
@@ -327,3 +297,9 @@ __Please note that all line indicators must be followed by a space__
327
297
  ## Discuss
328
298
 
329
299
  [Google Group](http://groups.google.com/group/slim-template)
300
+
301
+ ## Slim related projects
302
+
303
+ * [Textmate bundle](http://github.com/fredwu/ruby-slim-textmate-bundle)
304
+ * [Rails 3 Generators](http://github.com/leogalmeida/slim-rails)
305
+ * [Slim for Clojure](http://github.com/chaslemley/slim.clj)
data/Rakefile CHANGED
@@ -7,8 +7,8 @@ end
7
7
  require 'rake/testtask'
8
8
 
9
9
  desc 'Run Slim benchmarks! (Default :iterations is 1000)'
10
- task :bench, :iterations do |t, args|
11
- ruby("benchmarks/run.rb #{args[:iterations]}")
10
+ task :bench, :iterations, :slow do |t, args|
11
+ ruby("benchmarks/run.rb #{args[:slow]} #{args[:iterations]}")
12
12
  end
13
13
 
14
14
  Rake::TestTask.new(:test) do |t|
@@ -12,7 +12,7 @@ require 'erb'
12
12
  require 'haml'
13
13
 
14
14
  class SlimBenchmarks
15
- def initialize(iterations)
15
+ def initialize(slow, iterations)
16
16
  @iterations = (iterations || 1000).to_i
17
17
  @benches = []
18
18
 
@@ -26,22 +26,41 @@ class SlimBenchmarks
26
26
  erb = ERB.new(tpl_erb)
27
27
  erubis = Erubis::Eruby.new(tpl_erb)
28
28
  fast_erubis = Erubis::FastEruby.new(tpl_erb)
29
- haml = Haml::Engine.new(tpl_haml)
30
- haml_ugly = Haml::Engine.new(tpl_haml, :ugly => true)
29
+ haml = Haml::Engine.new(tpl_haml, :format => :html5)
30
+ haml_ugly = Haml::Engine.new(tpl_haml, :format => :html5, :ugly => true)
31
31
  slim = Slim::Template.new { tpl_slim }
32
32
 
33
- bench('erb') { ERB.new(tpl_erb).result(eview) }
34
- bench('erubis') { Erubis::Eruby.new(tpl_erb).result(eview) }
35
- bench('fast erubis') { Erubis::Eruby.new(tpl_erb).result(eview) }
36
- bench('slim') { Slim::Template.new { tpl_slim }.render(view) }
37
- bench('haml') { Haml::Engine.new(tpl_haml).render(view) }
38
- bench('haml ugly') { Haml::Engine.new(tpl_haml, :ugly => true).render(view) }
39
- bench('erb (cached)') { erb.result(eview) }
40
- bench('erubis (cached)') { erubis.result(eview) }
41
- bench('fast erubis (cached)') { fast_erubis.result(eview) }
42
- bench('slim (cached)') { slim.render(view) }
43
- bench('haml (cached)') { haml.render(view) }
44
- bench('haml ugly (cached)') { haml_ugly.render(view) }
33
+ haml.def_method(view, :run_haml)
34
+ haml_ugly.def_method(view, :run_haml_ugly)
35
+ view.instance_eval %{
36
+ def run_erb; #{erb.src}; end
37
+ def run_erubis; #{erubis.src}; end
38
+ def run_fast_erubis; #{fast_erubis.src}; end
39
+ def run_slim; #{slim.precompiled_template}; end
40
+ }
41
+
42
+ if slow
43
+ bench('erb (1)') { ERB.new(tpl_erb).result(eview) }
44
+ bench('erubis (1)') { Erubis::Eruby.new(tpl_erb).result(eview) }
45
+ bench('fast erubis (1)') { Erubis::Eruby.new(tpl_erb).result(eview) }
46
+ bench('slim (1)') { Slim::Template.new { tpl_slim }.render(view) }
47
+ bench('haml (1)') { Haml::Engine.new(tpl_haml, :format => :html5).render(view) }
48
+ bench('haml ugly (1)') { Haml::Engine.new(tpl_haml, :format => :html5, :ugly => true).render(view) }
49
+ end
50
+
51
+ bench('erb (2)') { erb.result(eview) }
52
+ bench('erubis (2)') { erubis.result(eview) }
53
+ bench('fast (2)') { fast_erubis.result(eview) }
54
+ bench('slim (2)') { slim.render(view) }
55
+ bench('haml (2)') { haml.render(view) }
56
+ bench('haml (2)') { haml_ugly.render(view) }
57
+
58
+ bench('erb (3)') { view.run_erb }
59
+ bench('erubis (3)') { view.run_erubis }
60
+ bench('fast erubis (3)') { view.run_fast_erubis }
61
+ bench('slim (3)') { view.run_slim }
62
+ bench('haml (3)') { view.run_haml }
63
+ bench('haml ugly (3)') { view.run_haml_ugly }
45
64
  end
46
65
 
47
66
  def run
@@ -53,6 +72,20 @@ class SlimBenchmarks
53
72
  end
54
73
  end
55
74
  end
75
+ puts "
76
+ 1. Uncached benchmark. Template is parsed every time.
77
+ Activate this benchmark with slow=1.
78
+
79
+ 2. Cached benchmark. Template is parsed before the benchmark.
80
+ The ruby code generated by the template engine might be evaluated every time.
81
+ This benchmark uses the standard API of the template engine.
82
+
83
+ 3. Compiled benchmark. Template is parsed before the benchmark and
84
+ generated ruby code is compiled into a method.
85
+ This is the fastest evaluation strategy because it benchmarks
86
+ pure execution speed of the generated ruby code.
87
+
88
+ "
56
89
  end
57
90
 
58
91
  def bench(name, &block)
@@ -60,4 +93,4 @@ class SlimBenchmarks
60
93
  end
61
94
  end
62
95
 
63
- SlimBenchmarks.new(ARGV[0]).run
96
+ SlimBenchmarks.new(ARGV[0], ARGV[1]).run
@@ -1,9 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  require 'temple'
4
- require 'tilt'
5
-
6
- require 'slim/helpers'
7
4
  require 'slim/parser'
8
5
  require 'slim/filter'
9
6
  require 'slim/end_inserter'
@@ -13,11 +10,6 @@ require 'slim/engine'
13
10
  require 'slim/template'
14
11
  require 'slim/version'
15
12
 
16
- begin
17
- require 'escape_utils'
18
- rescue LoadError
19
- end
20
-
21
13
  module Slim
22
14
  def self.version
23
15
  VERSION
@@ -6,7 +6,7 @@ module Slim
6
6
  #
7
7
  # @param [String] string Static text
8
8
  # @return [Array] Compiled temple expression
9
- def on_text(string)
9
+ def on_slim_text(string)
10
10
  # Interpolate variables in text (#{variable}).
11
11
  # Split the text into multiple dynamic and static parts.
12
12
  block = [:multi]
@@ -17,7 +17,7 @@ module Slim
17
17
  block << [:static, $1]
18
18
  when /^\#\{([^\}]*)\}/
19
19
  # Interpolation
20
- block << [:dynamic, escape_code($1)]
20
+ block << [:escape, :dynamic, $1]
21
21
  when /^([^\#]+|\#)/
22
22
  # Static text
23
23
  block << [:static, $&]
@@ -32,10 +32,10 @@ module Slim
32
32
  # @param [String] ruby code
33
33
  # @param [Array] content Temple expression
34
34
  # @return [Array] Compiled temple expression
35
- def on_control(code, content)
35
+ def on_slim_control(code, content)
36
36
  [:multi,
37
37
  [:block, code],
38
- compile(content)]
38
+ compile!(content)]
39
39
  end
40
40
 
41
41
  # Handle output expression `[:slim, :output, escape, code, content]`
@@ -44,11 +44,11 @@ module Slim
44
44
  # @param [String] code Ruby code
45
45
  # @param [Array] content Temple expression
46
46
  # @return [Array] Compiled temple expression
47
- def on_output(escape, code, content)
47
+ def on_slim_output(escape, code, content)
48
48
  if empty_exp?(content)
49
- [:multi, [:dynamic, escape ? escape_code(code) : code], content]
49
+ [:multi, escape ? [:escape, :dynamic, code] : [:dynamic, code], content]
50
50
  else
51
- on_output_block(escape, code, content)
51
+ on_slim_output_block(escape, code, content)
52
52
  end
53
53
  end
54
54
 
@@ -59,7 +59,7 @@ module Slim
59
59
  # @param [String] code Ruby code
60
60
  # @param [Array] content Temple expression
61
61
  # @return [Array] Compiled temple expression
62
- def on_output_block(escape, code, content)
62
+ def on_slim_output_block(escape, code, content)
63
63
  tmp1, tmp2 = tmp_var, tmp_var
64
64
 
65
65
  [:multi,
@@ -72,7 +72,7 @@ module Slim
72
72
  # that `yield` will not output the content to the current buffer,
73
73
  # but rather return the output.
74
74
  [:capture, tmp2,
75
- compile(content)],
75
+ compile!(content)],
76
76
 
77
77
  # Make sure that `yield` returns the output.
78
78
  [:block, tmp2],
@@ -81,14 +81,14 @@ module Slim
81
81
  [:block, 'end'],
82
82
 
83
83
  # Output the content.
84
- on_output(escape, tmp1, [:multi])]
84
+ on_slim_output(escape, tmp1, [:multi])]
85
85
  end
86
86
 
87
87
  # Handle directive expression `[:slim, :directive, type]`
88
88
  #
89
89
  # @param [String] type Directive type
90
90
  # @return [Array] Compiled temple expression
91
- def on_directive(type)
91
+ def on_slim_directive(type)
92
92
  if type =~ /^doctype/
93
93
  [:html, :doctype, $'.strip]
94
94
  end
@@ -100,29 +100,20 @@ module Slim
100
100
  # @param [Array] attrs Attributes
101
101
  # @param [Array] content Temple expression
102
102
  # @return [Array] Compiled temple expression
103
- def on_tag(name, attrs, content)
104
- attrs = attrs.inject([:html, :attrs]) do |m, (key, dynamic, value)|
103
+ def on_slim_tag(name, attrs, closed, content)
104
+ attrs = attrs.inject([:html, :staticattrs]) do |m, (key, dynamic, value)|
105
105
  value = if dynamic
106
- [:dynamic, escape_code(value)]
106
+ [:escape, :dynamic, value]
107
107
  else
108
- on_text(value)
108
+ on_slim_text(value)
109
109
  end
110
- m << [:html, :basicattr, [:static, key.to_s], value]
110
+ m << [key.to_s, value]
111
111
  end
112
-
113
- [:html, :tag, name, attrs, compile(content)]
112
+ [:html, :tag, name, attrs, closed, compile!(content)]
114
113
  end
115
114
 
116
115
  private
117
116
 
118
- # Generate code to escape html
119
- #
120
- # @param [String] param Ruby code
121
- # @return [String] Ruby code
122
- def escape_code(param)
123
- "Slim::Helpers.escape_html#{@options[:use_html_safe] ? '_safe' : ''}((#{param}))"
124
- end
125
-
126
117
  # Generate unique temporary variable name
127
118
  #
128
119
  # @return [String] Variable name
@@ -14,8 +14,8 @@ module Slim
14
14
  engine.dup
15
15
  end
16
16
 
17
- def on_embedded(engine, *body)
18
- EmbeddedEngine[engine].on_embedded(engine, *body)
17
+ def on_slim_embedded(engine, *body)
18
+ EmbeddedEngine[engine].on_slim_embedded(engine, *body)
19
19
  end
20
20
 
21
21
  def collect_text(body)
@@ -33,18 +33,18 @@ module Slim
33
33
  # Code to collect local variables
34
34
  COLLECT_LOCALS = %q{eval('{' + local_variables.select {|v| v[0] != ?_ }.map {|v| ":#{v}=>#{v}" }.join(',') + '}')}
35
35
 
36
- def on_embedded(engine, *body)
36
+ def on_slim_embedded(engine, *body)
37
37
  text = collect_text(body)
38
38
  engine = Tilt[engine]
39
- if @options[:precompiled]
39
+ if options[:precompiled]
40
40
  # Wrap precompiled code in proc, local variables from out the proc are accessible
41
41
  # WARNING: This is a bit of a hack. Tilt::Engine#precompiled is protected
42
42
  precompiled = engine.new { text }.send(:precompiled, {}).first
43
43
  [:dynamic, "proc { #{precompiled} }.call"]
44
- elsif @options[:dynamic]
44
+ elsif options[:dynamic]
45
45
  # Fully dynamic evaluation of the template during runtime (Slow and uncached)
46
46
  [:dynamic, "#{engine.name}.new { #{text.inspect} }.render(self, #{COLLECT_LOCALS})"]
47
- elsif @options[:interpolate]
47
+ elsif options[:interpolate]
48
48
  # Static template with interpolated ruby code
49
49
  [:slim, :text, engine.new { text }.render]
50
50
  else
@@ -54,15 +54,22 @@ module Slim
54
54
  end
55
55
  end
56
56
 
57
+ class ERBEngine < EmbeddedEngine
58
+ def on_slim_embedded(engine, *body)
59
+ text = collect_text(body)
60
+ Temple::ERB::Parser.new(:auto_escape => true).compile(text)
61
+ end
62
+ end
63
+
57
64
  class TagEngine < EmbeddedEngine
58
- def on_embedded(engine, *body)
59
- content = @options[:engine] ? @options[:engine].new.on_embedded(engine, *body) : [:multi, *body]
60
- [:slim, :tag, @options[:tag], @options[:attributes].map {|k, v| [k, false, v ] }, content]
65
+ def on_slim_embedded(engine, *body)
66
+ content = options[:engine] ? options[:engine].new.on_slim_embedded(engine, *body) : [:multi, *body]
67
+ [:slim, :tag, options[:tag], options[:attributes].map {|k, v| [k, false, v] }, false, content]
61
68
  end
62
69
  end
63
70
 
64
71
  class RubyEngine < EmbeddedEngine
65
- def on_embedded(engine, *body)
72
+ def on_slim_embedded(engine, *body)
66
73
  [:block, collect_text(body)]
67
74
  end
68
75
  end
@@ -79,7 +86,7 @@ module Slim
79
86
  register :less, TagEngine, :tag => 'style', :attributes => { :type => 'text/css' }, :engine => TiltEngine
80
87
 
81
88
  # These engines are precompiled, code is embedded
82
- register :erb, TiltEngine, :precompiled => true
89
+ register :erb, ERBEngine
83
90
  register :haml, TiltEngine, :precompiled => true
84
91
  register :nokogiri, TiltEngine, :precompiled => true
85
92
  register :builder, TiltEngine, :precompiled => true