slim 0.8.1 → 0.8.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/slim/compiler.rb +10 -5
- data/lib/slim/engine.rb +5 -2
- data/lib/slim/interpolation.rb +34 -5
- data/lib/slim/parser.rb +9 -7
- data/lib/slim/rails.rb +1 -0
- data/lib/slim/validator.rb +1 -1
- data/lib/slim/version.rb +1 -1
- metadata +3 -3
data/lib/slim/compiler.rb
CHANGED
@@ -2,6 +2,8 @@ module Slim
|
|
2
2
|
# Compiles Slim expressions into Temple::HTML expressions.
|
3
3
|
# @api private
|
4
4
|
class Compiler < Filter
|
5
|
+
set_default_options :disable_capture => false
|
6
|
+
|
5
7
|
# Handle control expression `[:slim, :control, code, content]`
|
6
8
|
#
|
7
9
|
# @param [String] ruby code
|
@@ -35,25 +37,28 @@ module Slim
|
|
35
37
|
# @param [Array] content Temple expression
|
36
38
|
# @return [Array] Compiled temple expression
|
37
39
|
def on_slim_output_block(escape, code, content)
|
38
|
-
|
40
|
+
tmp = tmp_var('capture')
|
39
41
|
|
40
42
|
[:multi,
|
41
43
|
# Capture the result of the code in a variable. We can't do
|
42
44
|
# `[:dynamic, code]` because it's probably not a complete
|
43
45
|
# expression (which is a requirement for Temple).
|
44
|
-
[:block, "#{
|
46
|
+
[:block, "#{tmp} = #{code}"],
|
45
47
|
|
46
48
|
# Capture the content of a block in a separate buffer. This means
|
47
49
|
# that `yield` will not output the content to the current buffer,
|
48
50
|
# but rather return the output.
|
49
|
-
|
50
|
-
|
51
|
+
#
|
52
|
+
# The capturing can be disabled with the option :disable_capture.
|
53
|
+
# Output code in the block writes directly to the output buffer then.
|
54
|
+
# Rails handles this by replacing the output buffer for helpers (with_output_buffer - braindead!).
|
55
|
+
options[:disable_capture] ? compile!(content) : [:capture, tmp_var('capture'), compile!(content)],
|
51
56
|
|
52
57
|
# Close the block.
|
53
58
|
[:block, 'end'],
|
54
59
|
|
55
60
|
# Output the content.
|
56
|
-
on_slim_output(escape,
|
61
|
+
on_slim_output(escape, tmp, [:multi])]
|
57
62
|
end
|
58
63
|
|
59
64
|
# Handle directive expression `[:slim, :directive, type]`
|
data/lib/slim/engine.rb
CHANGED
@@ -11,19 +11,22 @@ module Slim
|
|
11
11
|
:attr_wrapper => '"',
|
12
12
|
:format => :html5,
|
13
13
|
:id_delimiter => nil,
|
14
|
-
:generator => Temple::Generators::ArrayBuffer
|
14
|
+
:generator => Temple::Generators::ArrayBuffer,
|
15
|
+
:disable_capture => false,
|
16
|
+
:debug => false
|
15
17
|
|
16
18
|
use Slim::Parser, :file, :tabsize
|
17
19
|
use Slim::EmbeddedEngine, :enable_engines, :disable_engines
|
18
20
|
use Slim::Interpolation
|
19
21
|
use Slim::Sections, :sections, :dictionary, :dictionary_access
|
20
22
|
use Slim::EndInserter
|
21
|
-
use Slim::Compiler
|
23
|
+
use Slim::Compiler, :disable_capture
|
22
24
|
filter :EscapeHTML, :use_html_safe
|
23
25
|
use Temple::HTML::Pretty, :format, :attr_wrapper, :id_delimiter, :pretty
|
24
26
|
filter :MultiFlattener
|
25
27
|
filter :StaticMerger
|
26
28
|
filter :DynamicInliner
|
29
|
+
filter :Debugger, :debug, :debug_prefix, :debug_pretty
|
27
30
|
chain << proc {|options| options[:generator].new }
|
28
31
|
end
|
29
32
|
end
|
data/lib/slim/interpolation.rb
CHANGED
@@ -12,19 +12,48 @@ module Slim
|
|
12
12
|
block = [:multi]
|
13
13
|
until string.empty?
|
14
14
|
case string
|
15
|
-
when
|
15
|
+
when /^\\\#\{/
|
16
16
|
# Escaped interpolation
|
17
|
-
block << [:static,
|
18
|
-
|
17
|
+
block << [:static, '#{']
|
18
|
+
string = $'
|
19
|
+
when /^\#\{/
|
19
20
|
# Interpolation
|
20
|
-
|
21
|
+
string, code = parse_expression($')
|
22
|
+
block << [:slim, :output, true, code, [:multi]]
|
21
23
|
when /^([^\#]+|\#)/
|
22
24
|
# Static text
|
23
25
|
block << [:static, $&]
|
26
|
+
string = $'
|
24
27
|
end
|
25
|
-
string = $'
|
26
28
|
end
|
27
29
|
block
|
28
30
|
end
|
31
|
+
|
32
|
+
def parse_expression(string)
|
33
|
+
stack, code = [], ''
|
34
|
+
|
35
|
+
until string.empty?
|
36
|
+
if stack.empty? && string =~ /^\}/
|
37
|
+
# Stack is empty, this means we are finished
|
38
|
+
# if the next character is a closing bracket
|
39
|
+
string.slice!(0)
|
40
|
+
break
|
41
|
+
elsif string =~ Parser::DELIMITER_REGEX
|
42
|
+
# Delimiter found, push it on the stack
|
43
|
+
stack << Parser::DELIMITERS[$1]
|
44
|
+
code << string.slice!(0)
|
45
|
+
elsif string =~ Parser::CLOSE_DELIMITER_REGEX
|
46
|
+
# Closing delimiter found, pop it from the stack if everything is ok
|
47
|
+
raise "Text interpolation: Unexpected closing #{$1}" if stack.empty?
|
48
|
+
raise "Text interpolation: Expected closing #{stack.last}" if stack.last != $1
|
49
|
+
code << string.slice!(0)
|
50
|
+
stack.pop
|
51
|
+
else
|
52
|
+
code << string.slice!(0)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
return string, code
|
57
|
+
end
|
29
58
|
end
|
30
59
|
end
|
data/lib/slim/parser.rb
CHANGED
@@ -222,6 +222,14 @@ module Slim
|
|
222
222
|
result
|
223
223
|
end
|
224
224
|
|
225
|
+
DELIMITERS = {
|
226
|
+
'(' => ')',
|
227
|
+
'[' => ']',
|
228
|
+
'{' => '}',
|
229
|
+
}.freeze
|
230
|
+
DELIMITER_REGEX = /^([\(\[\{])/
|
231
|
+
CLOSE_DELIMITER_REGEX = /^([\)\]\}])/
|
232
|
+
|
225
233
|
private
|
226
234
|
|
227
235
|
ATTR_REGEX = /^ (\w[:\w-]*)=/
|
@@ -230,13 +238,7 @@ module Slim
|
|
230
238
|
'#' => 'id',
|
231
239
|
'.' => 'class',
|
232
240
|
}.freeze
|
233
|
-
|
234
|
-
'(' => ')',
|
235
|
-
'[' => ']',
|
236
|
-
'{' => '}',
|
237
|
-
}.freeze
|
238
|
-
DELIMITER_REGEX = /^([\(\[\{])/
|
239
|
-
CLOSE_DELIMITER_REGEX = /^([\)\]\}])/
|
241
|
+
|
240
242
|
if RUBY_VERSION > '1.9'
|
241
243
|
CLASS_ID_REGEX = /^(#|\.)([\w\u00c0-\uFFFF][\w:\u00c0-\uFFFF-]*)/
|
242
244
|
else
|
data/lib/slim/rails.rb
CHANGED
data/lib/slim/validator.rb
CHANGED
data/lib/slim/version.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 8
|
8
|
-
-
|
9
|
-
version: 0.8.
|
8
|
+
- 2
|
9
|
+
version: 0.8.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Andrew Stone
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2010-12-
|
19
|
+
date: 2010-12-22 00:00:00 -05:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|