slim 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +19 -19
- data/lib/slim.rb +1 -1
- data/lib/slim/compiler.rb +15 -13
- data/lib/slim/optimizer.rb +2 -2
- data/slim.gemspec +4 -4
- data/test/slim/test_compiler.rb +25 -9
- data/test/slim/test_engine.rb +14 -0
- metadata +5 -5
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
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 reduce the syntax to the essential parts without becoming cryptic.
|
4
4
|
|
5
5
|
## What?
|
6
6
|
|
@@ -28,10 +28,10 @@ I actually like the indentation and tag closing nature of Haml. I don't like th
|
|
28
28
|
|
29
29
|
|
30
30
|
So here's what I came up with:
|
31
|
-
|
31
|
+
|
32
32
|
! doctype html
|
33
|
-
html
|
34
|
-
head
|
33
|
+
html
|
34
|
+
head
|
35
35
|
title Slim Examples
|
36
36
|
meta name="keywords" content="template language"
|
37
37
|
body
|
@@ -44,10 +44,10 @@ So here's what I came up with:
|
|
44
44
|
- unless items.empty?
|
45
45
|
table
|
46
46
|
- for item in items do
|
47
|
-
tr
|
48
|
-
td
|
47
|
+
tr
|
48
|
+
td
|
49
49
|
= item.name
|
50
|
-
td
|
50
|
+
td
|
51
51
|
= item.price
|
52
52
|
- else
|
53
53
|
p No items found
|
@@ -55,7 +55,7 @@ So here's what I came up with:
|
|
55
55
|
div id="footer"
|
56
56
|
| Copyright © 2010 Andrew Stone
|
57
57
|
|
58
|
-
= render partial: 'tracking_code'
|
58
|
+
= render partial: 'tracking_code'
|
59
59
|
|
60
60
|
script
|
61
61
|
| $(content).do_something();
|
@@ -73,7 +73,7 @@ So here's what I came up with:
|
|
73
73
|
# Or nest it. __Note:__ Must use a pipe or a backtick (followed by a space) to escape processing
|
74
74
|
|
75
75
|
body
|
76
|
-
h1 id="headline"
|
76
|
+
h1 id="headline"
|
77
77
|
| Welcome to my site.
|
78
78
|
|
79
79
|
#### Add content to a tag with code
|
@@ -83,7 +83,7 @@ So here's what I came up with:
|
|
83
83
|
body
|
84
84
|
h1 id="headline" = page_headline
|
85
85
|
|
86
|
-
# Or nest it.
|
86
|
+
# Or nest it.
|
87
87
|
|
88
88
|
body
|
89
89
|
h1 id="headline"
|
@@ -118,26 +118,26 @@ So here's what I came up with:
|
|
118
118
|
# Just use standard Ruby interpolation.
|
119
119
|
|
120
120
|
body
|
121
|
-
table
|
122
|
-
- for user in users do
|
121
|
+
table
|
122
|
+
- for user in users do
|
123
123
|
tr id="user_#{user.id}"
|
124
|
-
|
124
|
+
|
125
125
|
|
126
126
|
#### Treat multiple lines of code as text that should bypass parsing.
|
127
127
|
|
128
|
-
# Use a backtick to start the escape. Each following line that is
|
128
|
+
# Use a backtick to start the escape. Each following line that is
|
129
129
|
# indented greater than the backtick is copied over.
|
130
130
|
|
131
131
|
body
|
132
132
|
p
|
133
133
|
|
|
134
134
|
This is a test of the text block.
|
135
|
-
|
135
|
+
|
136
136
|
# The parsed result of the above:
|
137
137
|
|
138
138
|
<body><p>This is a test of the text block.</p></body>
|
139
|
-
|
140
|
-
# The left margin is set at the indent of the backtick + one space.
|
139
|
+
|
140
|
+
# The left margin is set at the indent of the backtick + one space.
|
141
141
|
# Any additional spaces will be copied over.
|
142
142
|
|
143
143
|
body
|
@@ -168,7 +168,7 @@ So here's what I came up with:
|
|
168
168
|
* -
|
169
169
|
* The dash denotes control code (similar to Haml). Examples of control code are loops and conditionals.
|
170
170
|
* =
|
171
|
-
* The equal sign tells Slim it's a Ruby call that produces output to add to the buffer (similar to Erb and Haml).
|
171
|
+
* The equal sign tells Slim it's a Ruby call that produces output to add to the buffer (similar to Erb and Haml).
|
172
172
|
* !
|
173
173
|
* This is a directive. Most common example:
|
174
174
|
` ! doctype html renders <!doctype html> `
|
data/lib/slim.rb
CHANGED
data/lib/slim/compiler.rb
CHANGED
@@ -7,15 +7,17 @@ module Slim
|
|
7
7
|
include Optimizer
|
8
8
|
AUTOCLOSED = %w(meta img link br hr input area param col base)
|
9
9
|
|
10
|
-
CONTROL_WORDS
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
10
|
+
CONTROL_WORDS = %w{if unless do}
|
11
|
+
ELSE_CONTROL_WORDS = %w{else elsif}
|
12
|
+
|
13
|
+
REGEX_LINE_PARSER = /^(\s*)(!?`?\|?-?=?\w*)((?:\s*(?:\w|-)*="[^=]+")+|(\s*[#.]\S+))?(.*)/
|
14
|
+
REGEX_LINE_CONTAINS_OUTPUT_CODE = /^=(.*)/
|
15
|
+
REGEX_METHOD_HAS_NO_PARENTHESES = /^\w+( )/
|
16
|
+
REGEX_CODE_BLOCK_DETECTED = / do ?(.*)$/
|
17
|
+
REGEX_CODE_CONTROL_WORD_DETECTED = /(?:( )|(\())(#{CONTROL_WORDS * '|'})\b ?(.*)$/
|
18
|
+
REGEX_CODE_ELSE_CONTROL_WORD_DETECTED = /^(#{ELSE_CONTROL_WORDS * '|'})\b/
|
19
|
+
REGEX_FIND_ATTR_ID = /#([^.\s]+)/
|
20
|
+
REGEX_FIND_ATTR_CLASS = /\.([^#\s]+)/
|
19
21
|
|
20
22
|
def compile
|
21
23
|
@_buffer = ["_buf = [];"]
|
@@ -28,7 +30,7 @@ module Slim
|
|
28
30
|
|
29
31
|
if line.length == 0
|
30
32
|
@_buffer << "_buf << \"<br/>\";" if @in_text
|
31
|
-
next
|
33
|
+
next
|
32
34
|
end
|
33
35
|
|
34
36
|
line =~ REGEX_LINE_PARSER
|
@@ -81,11 +83,11 @@ module Slim
|
|
81
83
|
ender, ender_indent = enders.pop
|
82
84
|
|
83
85
|
if ender_indent >= indent
|
84
|
-
unless ender == 'end;' && line_type == :control_code && ender_indent == indent
|
86
|
+
unless ender == 'end;' && line_type == :control_code && ender_indent == indent && string =~ REGEX_CODE_ELSE_CONTROL_WORD_DETECTED
|
85
87
|
@_buffer << ender
|
86
88
|
end
|
87
89
|
else
|
88
|
-
enders << [ender, ender_indent]
|
90
|
+
enders << [ender, ender_indent]
|
89
91
|
continue_closing = false
|
90
92
|
end
|
91
93
|
end while continue_closing == true
|
@@ -158,4 +160,4 @@ module Slim
|
|
158
160
|
string
|
159
161
|
end
|
160
162
|
end
|
161
|
-
end
|
163
|
+
end
|
data/lib/slim/optimizer.rb
CHANGED
@@ -6,13 +6,13 @@ module Slim
|
|
6
6
|
# title Simple Test Title
|
7
7
|
# body
|
8
8
|
# - if logged_in?
|
9
|
-
# p
|
9
|
+
# p
|
10
10
|
# ` Welcome!
|
11
11
|
# - else
|
12
12
|
# p
|
13
13
|
# ` Please sign in.
|
14
14
|
#
|
15
|
-
# When compiling the above code to be eval'd, Slim produces a
|
15
|
+
# When compiling the above code to be eval'd, Slim produces a
|
16
16
|
# compiled string that looks like:
|
17
17
|
#
|
18
18
|
# buf = [];
|
data/slim.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{slim}
|
8
|
-
s.version = "0.5.
|
8
|
+
s.version = "0.5.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Andrew Stone"]
|
12
|
-
s.date = %q{2010-10-
|
12
|
+
s.date = %q{2010-10-08}
|
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 = %q{andy@stonean.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -40,9 +40,9 @@ Gem::Specification.new do |s|
|
|
40
40
|
s.summary = %q{Slim is a template language.}
|
41
41
|
s.test_files = [
|
42
42
|
"test/helper.rb",
|
43
|
-
"test/
|
43
|
+
"test/slim/test_compiler.rb",
|
44
44
|
"test/slim/test_engine.rb",
|
45
|
-
"test/
|
45
|
+
"test/test_slim.rb"
|
46
46
|
]
|
47
47
|
|
48
48
|
if s.respond_to? :specification_version then
|
data/test/slim/test_compiler.rb
CHANGED
@@ -99,7 +99,7 @@ HTML
|
|
99
99
|
def test_nested_content
|
100
100
|
string = <<HTML
|
101
101
|
body
|
102
|
-
p
|
102
|
+
p
|
103
103
|
` Hello World, meet Slim.
|
104
104
|
p
|
105
105
|
| Hello World, meet Slim again.
|
@@ -113,7 +113,7 @@ HTML
|
|
113
113
|
def test_closing_tag_without_content_or_attributes
|
114
114
|
string = <<HTML
|
115
115
|
hr
|
116
|
-
p
|
116
|
+
p
|
117
117
|
` Hello World, meet Slim.
|
118
118
|
HTML
|
119
119
|
|
@@ -126,7 +126,7 @@ HTML
|
|
126
126
|
def test_closing_tag_without_content
|
127
127
|
string = <<HTML
|
128
128
|
img width="100" height="50" src="/images/test.jpg"
|
129
|
-
p
|
129
|
+
p
|
130
130
|
` Hello World, meet Slim.
|
131
131
|
HTML
|
132
132
|
|
@@ -138,7 +138,7 @@ HTML
|
|
138
138
|
|
139
139
|
def test_text_that_starts_with_tag_name
|
140
140
|
string = <<HTML
|
141
|
-
p
|
141
|
+
p
|
142
142
|
` another one bites the dust
|
143
143
|
p
|
144
144
|
` i am iron man
|
@@ -154,7 +154,7 @@ HTML
|
|
154
154
|
string = <<HTML
|
155
155
|
body
|
156
156
|
- if something
|
157
|
-
p
|
157
|
+
p
|
158
158
|
` another one bites the dust
|
159
159
|
- else
|
160
160
|
p
|
@@ -166,9 +166,25 @@ HTML
|
|
166
166
|
assert_equal expected, TestEngine.new(string).compiled
|
167
167
|
end
|
168
168
|
|
169
|
+
def test_consecutive_if_code_blocks
|
170
|
+
string = <<HTML
|
171
|
+
body
|
172
|
+
- if something
|
173
|
+
p
|
174
|
+
` another one bites the dust
|
175
|
+
- if something_else
|
176
|
+
p
|
177
|
+
` i am iron man
|
178
|
+
HTML
|
179
|
+
|
180
|
+
expected = %q|_buf = [];_buf << "<body>";if something;_buf << "<p>";_buf << "another one bites the dust";_buf << "</p>";end;if something_else;_buf << "<p>";_buf << "i am iron man";_buf << "</p>";end;_buf << "</body>";_buf.join;|
|
181
|
+
|
182
|
+
assert_equal expected, TestEngine.new(string).compiled
|
183
|
+
end
|
184
|
+
|
169
185
|
def test_simple_output_code
|
170
186
|
string = <<HTML
|
171
|
-
p
|
187
|
+
p
|
172
188
|
= hello_world
|
173
189
|
HTML
|
174
190
|
|
@@ -179,7 +195,7 @@ HTML
|
|
179
195
|
|
180
196
|
def test_simple_output_code_with_params
|
181
197
|
string = <<HTML
|
182
|
-
p
|
198
|
+
p
|
183
199
|
= hello_world(params[:key])
|
184
200
|
HTML
|
185
201
|
|
@@ -245,8 +261,8 @@ TEMPLATE
|
|
245
261
|
|
246
262
|
def test_simple_html_with_multiple_wraparound_text
|
247
263
|
string = <<HTML
|
248
|
-
p
|
249
|
-
`
|
264
|
+
p
|
265
|
+
`
|
250
266
|
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam malesuada, dui at condimentum dapibus, purus sapien fringilla est, vel eleifend massa purus ut lectus. Praesent aliquam, ipsum eu ornare porta, tellus lacus viverra diam, nec scelerisque ipsum nunc et lacus. Nam adipiscing velit eget dolor ultricies in dignissim augue ultricies. Sed sed leo in sapien pretium dictum. Fusce sem quam, tincidunt vel lobortis sed, tincidunt vulputate est. Phasellus et ipsum quam, ac fringilla orci. In facilisis est et ante tempus suscipit. Morbi elementum quam ut urna laoreet vel malesuada justo adipiscing. Suspendisse eu lorem id lacus molestie dapibus fringilla ut orci. Cras pellentesque auctor leo, nec bibendum est suscipit id. Mauris dignissim aliquet libero vitae vulputate. Maecenas viverra sodales suscipit. Aenean eu nisi velit.
|
251
267
|
|
252
268
|
Phasellus ultricies vulputate lacus, eget pretium orci tincidunt tristique. Duis vitae luctus risus. Aliquam turpis massa, adipiscing in adipiscing ut, congue sed justo. Sed egestas ullamcorper nisl placerat dictum. Sed a leo lectus, sit amet vehicula nisl. Duis adipiscing congue tortor ut vulputate. Phasellus ligula lectus, congue non lobortis sed, dictum sed tellus. Vestibulum viverra vestibulum felis convallis pharetra. Phasellus a dignissim tellus. Proin dapibus malesuada lorem, et porttitor diam bibendum a. Donec et dui mauris, et tempus metus. Etiam pharetra varius dignissim. Maecenas lacinia, ligula ut tincidunt porttitor, sapien nisi pulvinar magna, nec sollicitudin libero odio bibendum nisi. Aenean ipsum eros, convallis id consequat nec, commodo eget diam. Integer malesuada, libero non dignissim varius, velit metus malesuada lectus, a consequat turpis purus ut elit.
|
data/test/slim/test_engine.rb
CHANGED
@@ -34,6 +34,20 @@ HTML
|
|
34
34
|
assert_equal expected, Slim::Engine.new(string).render(@env)
|
35
35
|
end
|
36
36
|
|
37
|
+
def test_render_with_consecutive_conditionals
|
38
|
+
string = <<HTML
|
39
|
+
div
|
40
|
+
- if show_first? true
|
41
|
+
p The first paragraph
|
42
|
+
- if show_first? true
|
43
|
+
p The second paragraph
|
44
|
+
HTML
|
45
|
+
|
46
|
+
expected = "<div><p>The first paragraph</p><p>The second paragraph</p></div>"
|
47
|
+
|
48
|
+
assert_equal expected, Slim::Engine.new(string).render(@env)
|
49
|
+
end
|
50
|
+
|
37
51
|
def test_render_with_parameterized_conditional
|
38
52
|
string = <<HTML
|
39
53
|
div
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 5
|
8
|
-
-
|
9
|
-
version: 0.5.
|
8
|
+
- 1
|
9
|
+
version: 0.5.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Andrew Stone
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-10-
|
17
|
+
date: 2010-10-08 00:00:00 +11:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
@@ -76,6 +76,6 @@ specification_version: 3
|
|
76
76
|
summary: Slim is a template language.
|
77
77
|
test_files:
|
78
78
|
- test/helper.rb
|
79
|
-
- test/test_slim.rb
|
80
|
-
- test/slim/test_engine.rb
|
81
79
|
- test/slim/test_compiler.rb
|
80
|
+
- test/slim/test_engine.rb
|
81
|
+
- test/test_slim.rb
|