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 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
- tmp1, tmp2 = tmp_var('capture'), tmp_var('capture')
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, "#{tmp1} = #{code}"],
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
- [:capture, tmp2,
50
- compile!(content)],
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, tmp1, [:multi])]
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
@@ -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, $1]
18
- when /^\#\{([^\}]*)\}/
17
+ block << [:static, '#{']
18
+ string = $'
19
+ when /^\#\{/
19
20
  # Interpolation
20
- block << [:slim, :output, true, $1, [:multi]]
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
- DELIMITERS = {
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
@@ -1,6 +1,7 @@
1
1
  require 'slim'
2
2
 
3
3
  Slim::Engine.default_options[:generator] = Temple::Generators::RailsOutputBuffer
4
+ Slim::Engine.default_options[:disable_capture] = true
4
5
 
5
6
  module ActionView
6
7
  module TemplateHandlers
@@ -1,5 +1,5 @@
1
1
  module Slim
2
- class Validator
2
+ module Validator
3
3
  class << self
4
4
  def validate!(source)
5
5
  Slim::Engine.new.compile(source.to_s)
data/lib/slim/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Slim
2
- VERSION = '0.8.1'
2
+ VERSION = '0.8.2'
3
3
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 8
8
- - 1
9
- version: 0.8.1
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-17 00:00:00 +11:00
19
+ date: 2010-12-22 00:00:00 -05:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency