slim 0.6.0.beta.3 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,42 +1,54 @@
1
1
  # Slim
2
2
 
3
- Slim is a template language whose goal is reduce the syntax to the essential parts without becoming cryptic.
3
+ Slim is a template language whose goal is to reduce the view syntax to the essential parts without becoming cryptic.
4
+
4
5
 
5
6
  ## What?
6
7
 
7
- Slim is a Rails 3, Ruby 1.9.2 templating option. I do not intend on making a Rails 2.x compatible version. I don't think it would be difficult, so if you want it, I will happily accept contributions with tests.
8
+ Slim is a fast, lightweight templating engine for __Rails 3__. It has been tested on Ruby 1.9.2 and Ruby/REE 1.8.7. Slim is heavily influenced by [Haml](http://github.com/nex3/haml) and [Jade](http://github.com/visionmedia/jade).
9
+
8
10
 
9
11
  ## Why?
10
12
 
11
- Simply put, I wanted to see if I could pull of a template language that required minimum use of special characters and at least matched Erb's speed. Yes, Slim is speedy.
13
+ 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. Slim was born to bring the minimalist syntax approach of _Haml_ and the higher performance of Erb into once solution.
14
+
15
+ ___Yes, Slim is speedy!___ Benchmarks are provided at the end of this README file. Alternatively, a benchmark script is provided so you could test it yourself (`./benchmarks/run.rb`).
16
+
12
17
 
13
18
  ## How?
14
19
 
20
+ Install Slim as a gem:
21
+
22
+ gem install slim
23
+
15
24
  Include Slim in your Gemfile:
16
25
 
17
26
  gem 'slim'
18
27
 
19
- In `config/application.rb`, add the following line near the top:
28
+ In `config/application.rb`, add the following line near the top (i.e. just below `require 'rails/all'`):
20
29
 
21
30
  require 'slim/rails'
22
31
 
23
32
  That's it!
24
33
 
25
- ### The syntax
26
34
 
27
- I actually like the indentation and tag closing nature of Haml. I don't like the overall result of the markup though, it's a little cryptic. I'm sure, with practice, people read it like the Matrix, but it's never suited me. So why not try to improve it for me? There may be one or two other people with the same thoughts.
35
+ ## The syntax
36
+
37
+ As a Rails developer, you might already be very familiar with _Haml_'s syntax and you think it is fantastic - until you entered the magic kingdom of _node.js_ and got introduced to _Jade_.
28
38
 
39
+ Slim's syntax is inspired by both _Haml_ and _Jade_.
29
40
 
30
- So here's what I came up with:
41
+ Here's a quick example to demonstrate what a Slim template looks like:
31
42
 
32
43
  ! doctype html
33
44
  html
34
45
  head
35
46
  title Slim Examples
36
47
  meta name="keywords" content="template language"
48
+
37
49
  body
38
50
  h1 Markup examples
39
- div id="content" class="example1"
51
+ #content.example1
40
52
  p Nest by indentation
41
53
 
42
54
  = yield
@@ -45,14 +57,12 @@ So here's what I came up with:
45
57
  table
46
58
  - for item in items do
47
59
  tr
48
- td
49
- = item.name
50
- td
51
- = item.price
60
+ td = item.name
61
+ td = item.price
52
62
  - else
53
63
  p No items found
54
64
 
55
- div id="footer"
65
+ #footer
56
66
  | Copyright © 2010 Andrew Stone
57
67
 
58
68
  = render partial: 'tracking_code'
@@ -61,9 +71,37 @@ So here's what I came up with:
61
71
  | $(content).do_something();
62
72
 
63
73
 
64
- ### How do I?
74
+ ## Language features
75
+
76
+ ### Line indicators
77
+
78
+ __Please note that all line indicators must be followed by a space__
79
+
80
+ * |
81
+ * The pipe tells Slim to just copy the line. It essentially escapes any processing.
82
+ * `
83
+ * _Same as the pipe ('|')._
84
+ * -
85
+ * The dash denotes control code (similar to Haml). Examples of control code are loops and conditionals.
86
+ * =
87
+ * The equal sign tells Slim it's a Ruby call that produces output to add to the buffer (similar to Erb and Haml).
88
+ * ==
89
+ * Same as the single equal sign, but does not go through the escape_html method.
90
+ * !
91
+ * This is a directive. Most common example: `! doctype html # renders <!doctype html>`
92
+ * /
93
+ * Use the forward slash for ruby code comments - anything after it won't get displayed in the final render.
94
+
95
+ ### Things to know
96
+
97
+ * Standard Ruby syntax after '-' and '='
98
+ * __end__ is not required
99
+ * Can put content on same line or nest it.
100
+ * If you nest content (e.g. put it on the next line), start the line with a pipe ('|') or a backtick ('`').
101
+ * Indentation matters, but it's not as strict as Haml.
102
+ * If you want to first indent 2 spaces, then 5 spaces, it's your choice. To nest markup you only need to indent by one space, the rest is gravy.
65
103
 
66
- #### Add content to a tag
104
+ ### Add content to a tag
67
105
 
68
106
  # Either start on the same line as the tag
69
107
 
@@ -76,7 +114,7 @@ So here's what I came up with:
76
114
  h1 id="headline"
77
115
  | Welcome to my site.
78
116
 
79
- #### Add content to a tag with code
117
+ ### Add content to a tag with code
80
118
 
81
119
  # Can make the call on the same line
82
120
 
@@ -89,7 +127,7 @@ So here's what I came up with:
89
127
  h1 id="headline"
90
128
  = page_headline
91
129
 
92
- #### Shortcut form for `id` and `class` attributes
130
+ ### Shortcut form for `id` and `class` attributes
93
131
 
94
132
  # Similarly to Haml, you can specify the `id` and `class`
95
133
  # attributes in the following shortcut form
@@ -113,7 +151,7 @@ So here's what I came up with:
113
151
  div class="content"
114
152
  = show_content
115
153
 
116
- #### Set an attribute's value with a method?
154
+ ### Set an attribute's value with a method
117
155
 
118
156
  # Use standard Ruby interpolation.
119
157
 
@@ -122,7 +160,7 @@ So here's what I came up with:
122
160
  - for user in users do
123
161
  tr id="user_#{user.id}"
124
162
 
125
- #### Call a method in content
163
+ ### Call a method in content
126
164
 
127
165
  # Use standard Ruby interpolation.
128
166
 
@@ -134,7 +172,7 @@ So here's what I came up with:
134
172
  body
135
173
  h1 Welcome \#{current_user.name} to the show.
136
174
 
137
- #### Escape the escaping?
175
+ ### Skip the escaping
138
176
 
139
177
  # Use a double equal sign
140
178
 
@@ -142,7 +180,7 @@ So here's what I came up with:
142
180
  h1 id="headline"
143
181
  == page_headline
144
182
 
145
- #### Treat multiple lines of code as text that should bypass parsing.
183
+ ### Treat multiple lines of code as text that should bypass parsing
146
184
 
147
185
  # Use a pipe ('|') or backtick ('`') to start the escape.
148
186
  # Each following line that is indented greater than
@@ -168,7 +206,7 @@ So here's what I came up with:
168
206
  This line will have two spaces in front of it.
169
207
  And so on...
170
208
 
171
- #### Add ruby code comments?
209
+ ### Add code comments
172
210
 
173
211
  # Use a forward slash for ruby code comments
174
212
 
@@ -181,34 +219,34 @@ So here's what I came up with:
181
219
 
182
220
  <body><p></p></body>
183
221
 
184
- ### Things to know:
185
222
 
186
- * Standard Ruby syntax after '-' and '='
187
- * __end__ is not required
188
- * Can put content on same line or nest it.
189
- * If you nest content (e.g. put it on the next line), start the line with a pipe ('|') or a backtick ('`').
190
- * Indentation matters, but it's not as strict as Haml.
191
- * If you want to indent 2 spaces, then 5. It's your choice. To nest markup you only need to indent by one space, the rest is gravy.
223
+ ## Benchmarks
192
224
 
225
+ # OS X 10.6 + REE 1.8.7
193
226
 
194
- ### Line indicators:
195
- __Please note that all line indicators must be followed by a space__
227
+ user system total real
228
+ erb 0.500000 0.000000 0.500000 ( 0.516644)
229
+ slim 0.550000 0.000000 0.550000 ( 0.579362)
230
+ haml 3.390000 0.050000 3.440000 ( 3.669325)
231
+ mustache 1.130000 0.040000 1.170000 ( 1.213134)
232
+ erb (cached) 0.090000 0.000000 0.090000 ( 0.099274)
233
+ slim (cached) 0.080000 0.000000 0.080000 ( 0.079823)
234
+ haml (cached) 0.290000 0.000000 0.290000 ( 0.312542)
235
+ mustache (cached) 0.080000 0.000000 0.080000 ( 0.079184)
196
236
 
197
- * |
198
- * The pipe tells Slim to just copy the line. It essentially escapes any processing.
199
- * `
200
- * _Same as the pipe ('|')._
201
- * -
202
- * The dash denotes control code (similar to Haml). Examples of control code are loops and conditionals.
203
- * =
204
- * The equal sign tells Slim it's a Ruby call that produces output to add to the buffer (similar to Erb and Haml).
205
- * ==
206
- * Same as the single equal sign, but does not go through the escape_html method.
207
- * !
208
- * This is a directive. Most common example:
209
- ` ! doctype html renders <!doctype html> `
210
- * /
211
- * Use the forward slash for ruby code comments - anything after it won't get displayed in the final render.
237
+ # OS X 10.6 + Ruby 1.9.2
238
+
239
+ user system total real
240
+ erb 0.400000 0.000000 0.400000 ( 0.431967)
241
+ slim 0.410000 0.010000 0.420000 ( 0.425059)
242
+ haml 3.030000 0.030000 3.060000 ( 3.199970)
243
+ mustache 1.360000 0.030000 1.390000 ( 1.435129)
244
+ erb (cached) 0.150000 0.000000 0.150000 ( 0.157284)
245
+ slim (cached) 0.130000 0.010000 0.140000 ( 0.129576)
246
+ haml (cached) 0.320000 0.000000 0.320000 ( 0.344316)
247
+ mustache (cached) 0.040000 0.000000 0.040000 ( 0.049058)
212
248
 
249
+ ## Authors
213
250
 
214
- ### Please add feature requests and bugs to the Github issue tracker.
251
+ * [Andrew Stone](http://github.com/stonean)
252
+ * [Fred Wu](http://github.com/fredwu)
@@ -10,7 +10,7 @@ require 'slim/engine'
10
10
  module Slim
11
11
  class << self
12
12
  def version
13
- '0.6.0.beta.3'
13
+ '0.6.0'
14
14
  end
15
15
 
16
16
  def escape_html(html)
@@ -10,7 +10,7 @@ module Slim
10
10
  CONTROL_WORDS = %w{if unless do}
11
11
  ELSE_CONTROL_WORDS = %w{else elsif}
12
12
 
13
- REGEX_LINE_PARSER = /^(\s*)(!?`?\|?-?=?\/?\w*)((?:\s*(?:\w|-)*="[^=]+")+|(\S*[#.]\S+))?(.*)/
13
+ REGEX_LINE_PARSER = /^(\s*)(!?`?\|?-?=?\/?\w*)((\S*[#.]\S+)?(?:\s*(?:\w|-)*="[^=]+")*)?(.*)/
14
14
 
15
15
  REGEX_LINE_CONTAINS_OUTPUT_CODE = /^\s*=(.*)/
16
16
  REGEX_LINE_CONTAINS_METHOD_DETECTED = /^((?:(?!#{CONTROL_WORDS * '\b|'}\b).)*)/
@@ -31,7 +31,7 @@ module Slim
31
31
  line.chomp!
32
32
  line.rstrip!
33
33
 
34
- if line.length == 0
34
+ if line.empty?
35
35
  @_buffer << '_buf << "<br/>";' if in_text
36
36
  next
37
37
  end
@@ -97,12 +97,10 @@ module Slim
97
97
  @_buffer << "_buf << \"<#{marker}#{attrs}>\";"
98
98
  end
99
99
 
100
- unless string.empty?
101
- if string =~ REGEX_LINE_CONTAINS_OUTPUT_CODE
102
- @_buffer << "_buf << #{parse_string($1.strip)};"
103
- else
104
- @_buffer << "_buf << \"#{string}\";"
105
- end
100
+ if string =~ REGEX_LINE_CONTAINS_OUTPUT_CODE
101
+ @_buffer << "_buf << #{parse_string($1.strip)};"
102
+ else
103
+ @_buffer << "_buf << \"#{string}\";" unless string.empty?
106
104
  end
107
105
  when :text
108
106
  in_text = true
@@ -0,0 +1,159 @@
1
+ <h2>Slim</h2>
2
+
3
+ <p>Slim is a template language whose goal is reduce the syntax to the essential parts without becoming cryptic. </p>
4
+
5
+ <h2>What?</h2>
6
+
7
+ <p>Slim is a Rails 3, Ruby 1.9.2 templating option. I do not intend on making a Rails 2.x compatible version. I don't think it would be difficult, so if you want it, I will happily accept contributions with tests.</p>
8
+
9
+ <h2>Why?</h2>
10
+
11
+ <p>Simply put, I wanted to see if I could pull of a template language that required minimum use of special characters and at least matched Erb's speed. Yes, Slim is speedy.</p>
12
+
13
+ <h3>The syntax</h3>
14
+
15
+ <p>I actually like the indentation and tag closing nature of Haml. I don't like the overall result of the markup though, it's a little cryptic. I'm sure, with practice, people read it like the Matrix, but it's never suited me. So why not try to improve it for me? There may be one or two other people with the same thoughts.</p>
16
+
17
+ <p>So here's what I came up with:</p>
18
+
19
+ <pre><code>! doctype html
20
+ html
21
+ head
22
+ title Slim Examples
23
+ meta name="keywords" content="template language"
24
+ body
25
+ h1 Markup examples
26
+ div id="content" class="example1"
27
+ p Nest by indentation
28
+
29
+ = yield
30
+
31
+ - unless items.empty?
32
+ table
33
+ - for item in items do
34
+ tr
35
+ td
36
+ = item.name
37
+ td
38
+ = item.price
39
+ - else
40
+ p No items found
41
+
42
+ div id="footer"
43
+ ` Copyright &amp;copy; 2010 Andrew Stone
44
+
45
+ = render partial: 'tracking_code'
46
+
47
+ script
48
+ ` $(content).do_something();
49
+ </code></pre>
50
+
51
+ <h3>How do I?</h3>
52
+
53
+ <h4>Add content to a tag</h4>
54
+
55
+ <pre><code> # Either start on the same line as the tag
56
+
57
+ body
58
+ h1 id="headline" Welcome to my site.
59
+
60
+ # Or nest it. __Note:__ Must use backtick (with following space) to escape processing
61
+
62
+ body
63
+ h1 id="headline"
64
+ ` Welcome to my site.
65
+ </code></pre>
66
+
67
+ <h4>Add content to a tag with code</h4>
68
+
69
+ <pre><code> # Can make the call on the same line
70
+
71
+ body
72
+ h1 id="headline" = page_headline
73
+
74
+ # Or nest it.
75
+
76
+ body
77
+ h1 id="headline"
78
+ = page_headline
79
+ </code></pre>
80
+
81
+ <h4>Set an attribute's value with a method?</h4>
82
+
83
+ <pre><code> # Just use standard Ruby interpolation.
84
+
85
+ body
86
+ table
87
+ - for user in users do
88
+ tr id="user_#{user.id}"
89
+ </code></pre>
90
+
91
+ <h4>Treat multiple lines of code as text that should bypass parsing.</h4>
92
+
93
+ <pre><code> # Use a backtick to start the escape. Each following line that is
94
+ # indented greater than the backtick is copied over.
95
+
96
+ body
97
+ p
98
+ '
99
+ This is a test of the text block.
100
+
101
+ # The parsed result of the above:
102
+
103
+ &lt;body&gt;&lt;p&gt;This is a test of the text block.&lt;/p&gt;&lt;/body&gt;
104
+
105
+ # The left margin is set at the indent of the backtick + one space.
106
+ # Any additional spaces will be copied over.
107
+
108
+ body
109
+ p
110
+ '
111
+ This line is on the left margin.
112
+ This line will have one space in front of it.
113
+ This line will have two spaces in front of it.
114
+ And so on...
115
+ </code></pre>
116
+
117
+ <h3>Things to know:</h3>
118
+
119
+ <ul>
120
+ <li>Standard Ruby syntax after '-' and '='
121
+ <ul>
122
+ <li><strong>end</strong> is not required</li>
123
+ <li>If you're making a method call, wrap the arguments in parenthesis (<strong>TODO:</strong> make this a non requirement)</li>
124
+ </ul></li>
125
+ <li>Can put content on same line or nest it.
126
+ <ul>
127
+ <li>If you nest content (e.g. put it on the next line), start the line with a backtick ('`')</li>
128
+ </ul></li>
129
+ <li>Indentation matters, but it's not as strict as Haml.
130
+ <ul>
131
+ <li>If you want to indent 2 spaces, then 5. It's your choice. To nest markup you only need to indent by one space, the rest is gravy.</li>
132
+ </ul></li>
133
+ </ul>
134
+
135
+ <h3>Line indicators:</h3>
136
+
137
+ <p><strong>Please note that all line indicators must be followed by a space</strong></p>
138
+
139
+ <ul>
140
+ <li>`
141
+ <ul>
142
+ <li>The backtick tells Slim to just copy the line. It essentially escapes any processing.</li>
143
+ </ul></li>
144
+ <li>-
145
+ <ul>
146
+ <li>The dash denotes control code (similar to Haml). Examples of control code are loops and conditionals.</li>
147
+ </ul></li>
148
+ <li>=
149
+ <ul>
150
+ <li>The equal sign tells Slim it's a Ruby call that produces output to add to the buffer (similar to Erb and Haml). </li>
151
+ </ul></li>
152
+ <li>!
153
+ <ul>
154
+ <li>This is a directive. Most common example:
155
+ <code>! doctype html renders &lt;!doctype html&gt;</code></li>
156
+ </ul></li>
157
+ </ul>
158
+
159
+ <h3>Please add feature requests and bugs to the Github issue tracker.</h3>
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{slim}
8
- s.version = "0.6.0.beta.3"
8
+ s.version = "0.6.0"
9
9
 
10
- s.required_rubygems_version = Gem::Requirement.new("> 1.3.1") if s.respond_to? :required_rubygems_version=
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andrew Stone", "Fred Wu"]
12
- s.date = %q{2010-10-16}
12
+ s.date = %q{2010-10-17}
13
13
  s.description = %q{Slim is a template language whose goal is reduce the syntax to the essential parts without becoming cryptic.}
14
14
  s.email = ["andy@stonean.com", "ifredwu@gmail.com"]
15
15
  s.extra_rdoc_files = [
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
25
25
  "lib/slim/engine.rb",
26
26
  "lib/slim/optimizer.rb",
27
27
  "lib/slim/rails.rb",
28
+ "readme.html",
28
29
  "slim.gemspec",
29
30
  "test/helper.rb",
30
31
  "test/slim/test_compiler.rb",
@@ -39,9 +40,9 @@ Gem::Specification.new do |s|
39
40
  s.summary = %q{Slim is a template language.}
40
41
  s.test_files = [
41
42
  "test/helper.rb",
42
- "test/slim/test_compiler.rb",
43
+ "test/test_slim.rb",
43
44
  "test/slim/test_engine.rb",
44
- "test/test_slim.rb"
45
+ "test/slim/test_compiler.rb"
45
46
  ]
46
47
 
47
48
  if s.respond_to? :specification_version then
@@ -131,8 +131,7 @@ HTML
131
131
 
132
132
  def test_render_with_call_to_set_attributes
133
133
  string = <<HTML
134
- p id="#\{id_helper}" class="hello world"
135
- = hello_world
134
+ p id="#\{id_helper}" class="hello world" = hello_world
136
135
  HTML
137
136
 
138
137
  expected = "<p id=\"notice\" class=\"hello world\">Hello World from @env</p>"
@@ -294,6 +293,17 @@ HTML
294
293
  assert_equal expected, Slim::Engine.new(string).render(@env)
295
294
  end
296
295
 
296
+ def test_hash_call_in_attribute
297
+ string = <<HTML
298
+ p id="#\{hash[:a]}" Test it
299
+ HTML
300
+
301
+ expected = "<p id=\"The letter a\">Test it</p>"
302
+
303
+ assert_equal expected, Slim::Engine.new(string).render(@env)
304
+ end
305
+
306
+
297
307
  def test_escaping_evil_method
298
308
  string = <<HTML
299
309
  p = evil_method
@@ -405,4 +415,24 @@ HTML
405
415
 
406
416
  assert_equal expected, Slim::Engine.new(string).render(@env)
407
417
  end
418
+
419
+ def test_dashed_attributes
420
+ string = <<HTML
421
+ p data-info="Illudium Q-36" = output_number
422
+ HTML
423
+
424
+ expected = %(<p data-info="Illudium Q-36">1337</p>)
425
+
426
+ assert_equal expected, Slim::Engine.new(string).render(@env)
427
+ end
428
+
429
+ def test_dashed_attributes_with_shortcuts
430
+ string = <<HTML
431
+ p#marvin.martian data-info="Illudium Q-36" = output_number
432
+ HTML
433
+
434
+ expected = %(<p id="marvin" class="martian" data-info="Illudium Q-36">1337</p>)
435
+
436
+ assert_equal expected, Slim::Engine.new(string).render(@env)
437
+ end
408
438
  end
metadata CHANGED
@@ -1,14 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slim
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: true
4
+ prerelease: false
5
5
  segments:
6
6
  - 0
7
7
  - 6
8
8
  - 0
9
- - beta
10
- - 3
11
- version: 0.6.0.beta.3
9
+ version: 0.6.0
12
10
  platform: ruby
13
11
  authors:
14
12
  - Andrew Stone
@@ -17,7 +15,7 @@ autorequire:
17
15
  bindir: bin
18
16
  cert_chain: []
19
17
 
20
- date: 2010-10-16 00:00:00 +11:00
18
+ date: 2010-10-17 00:00:00 -04:00
21
19
  default_executable:
22
20
  dependencies:
23
21
  - !ruby/object:Gem::Dependency
@@ -105,6 +103,7 @@ files:
105
103
  - lib/slim/engine.rb
106
104
  - lib/slim/optimizer.rb
107
105
  - lib/slim/rails.rb
106
+ - readme.html
108
107
  - slim.gemspec
109
108
  - test/helper.rb
110
109
  - test/slim/test_compiler.rb
@@ -124,20 +123,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
124
123
  requirements:
125
124
  - - ">="
126
125
  - !ruby/object:Gem::Version
127
- hash: 2018264533147870573
126
+ hash: 686036806339254494
128
127
  segments:
129
128
  - 0
130
129
  version: "0"
131
130
  required_rubygems_version: !ruby/object:Gem::Requirement
132
131
  none: false
133
132
  requirements:
134
- - - ">"
133
+ - - ">="
135
134
  - !ruby/object:Gem::Version
136
135
  segments:
137
- - 1
138
- - 3
139
- - 1
140
- version: 1.3.1
136
+ - 0
137
+ version: "0"
141
138
  requirements: []
142
139
 
143
140
  rubyforge_project: slim
@@ -147,6 +144,6 @@ specification_version: 3
147
144
  summary: Slim is a template language.
148
145
  test_files:
149
146
  - test/helper.rb
150
- - test/slim/test_compiler.rb
151
- - test/slim/test_engine.rb
152
147
  - test/test_slim.rb
148
+ - test/slim/test_engine.rb
149
+ - test/slim/test_compiler.rb