mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
Replace @haml_stack with a linked list of buffers.
This is done via the new Buffer#upper property.
This commit is contained in:
parent
40a18ff882
commit
6c0473e38e
4 changed files with 21 additions and 11 deletions
|
@ -18,6 +18,11 @@ module Haml
|
||||||
# The options hash passed in from Haml::Engine.
|
# The options hash passed in from Haml::Engine.
|
||||||
attr_accessor :options
|
attr_accessor :options
|
||||||
|
|
||||||
|
# The Buffer for the enclosing Haml document.
|
||||||
|
# This is set for partials and similar sorts of nested templates.
|
||||||
|
# It's nil at the top level (see #toplevel?).
|
||||||
|
attr_accessor :upper
|
||||||
|
|
||||||
# True if the format is XHTML
|
# True if the format is XHTML
|
||||||
def xhtml?
|
def xhtml?
|
||||||
not html?
|
not html?
|
||||||
|
@ -38,6 +43,12 @@ module Haml
|
||||||
@options[:format] == :html5
|
@options[:format] == :html5
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# True if this buffer is a top-level template,
|
||||||
|
# as opposed to a nested partial.
|
||||||
|
def toplevel?
|
||||||
|
upper.nil?
|
||||||
|
end
|
||||||
|
|
||||||
# Gets the current tabulation of the document.
|
# Gets the current tabulation of the document.
|
||||||
def tabulation
|
def tabulation
|
||||||
@real_tabs + @tabulation
|
@real_tabs + @tabulation
|
||||||
|
@ -50,7 +61,8 @@ module Haml
|
||||||
end
|
end
|
||||||
|
|
||||||
# Creates a new buffer.
|
# Creates a new buffer.
|
||||||
def initialize(options = {})
|
def initialize(upper = nil, options = {})
|
||||||
|
@upper = upper
|
||||||
@options = {
|
@options = {
|
||||||
:attr_wrapper => "'",
|
:attr_wrapper => "'",
|
||||||
:ugly => false,
|
:ugly => false,
|
||||||
|
|
|
@ -147,7 +147,7 @@ END
|
||||||
# they won't work.
|
# they won't work.
|
||||||
def render(scope = Object.new, locals = {}, &block)
|
def render(scope = Object.new, locals = {}, &block)
|
||||||
locals = (@options[:locals] || {}).merge(locals)
|
locals = (@options[:locals] || {}).merge(locals)
|
||||||
buffer = Haml::Buffer.new(options_for_buffer)
|
buffer = Haml::Buffer.new(scope.instance_variable_get('@haml_buffer'), options_for_buffer)
|
||||||
|
|
||||||
if scope.is_a?(Binding) || scope.is_a?(Proc)
|
if scope.is_a?(Binding) || scope.is_a?(Proc)
|
||||||
scope_object = eval("self", scope)
|
scope_object = eval("self", scope)
|
||||||
|
@ -161,8 +161,7 @@ END
|
||||||
|
|
||||||
scope_object.instance_eval do
|
scope_object.instance_eval do
|
||||||
extend Haml::Helpers
|
extend Haml::Helpers
|
||||||
@haml_stack ||= Array.new
|
@haml_buffer = buffer
|
||||||
@haml_stack.push(buffer)
|
|
||||||
@haml_is_haml = true
|
@haml_is_haml = true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -170,7 +169,7 @@ END
|
||||||
|
|
||||||
# Get rid of the current buffer
|
# Get rid of the current buffer
|
||||||
scope_object.instance_eval do
|
scope_object.instance_eval do
|
||||||
@haml_stack.pop
|
@haml_buffer = buffer.upper
|
||||||
@haml_is_haml = false
|
@haml_is_haml = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -38,7 +38,7 @@ module Haml
|
||||||
#
|
#
|
||||||
def init_haml_helpers
|
def init_haml_helpers
|
||||||
@haml_is_haml = true
|
@haml_is_haml = true
|
||||||
@haml_stack = [Haml::Buffer.new(Haml::Engine.new('').send(:options_for_buffer))]
|
@haml_buffer = Haml::Buffer.new(@haml_buffer, Haml::Engine.new('').send(:options_for_buffer))
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -345,7 +345,7 @@ END
|
||||||
|
|
||||||
# Gets a reference to the current Haml::Buffer object.
|
# Gets a reference to the current Haml::Buffer object.
|
||||||
def haml_buffer
|
def haml_buffer
|
||||||
@haml_stack[-1]
|
@haml_buffer
|
||||||
end
|
end
|
||||||
|
|
||||||
# Gives a proc the same local "_hamlout" and "_erbout" variables
|
# Gives a proc the same local "_hamlout" and "_erbout" variables
|
||||||
|
|
|
@ -97,15 +97,14 @@ module Haml
|
||||||
def precompiled_with_ambles(local_names)
|
def precompiled_with_ambles(local_names)
|
||||||
preamble = <<END.gsub("\n", ";")
|
preamble = <<END.gsub("\n", ";")
|
||||||
extend Haml::Helpers
|
extend Haml::Helpers
|
||||||
@haml_stack ||= Array.new
|
_hamlout = @haml_buffer = Haml::Buffer.new(@haml_buffer, #{options_for_buffer.inspect})
|
||||||
@haml_stack.push(Haml::Buffer.new(#{options_for_buffer.inspect}))
|
|
||||||
@haml_is_haml = true
|
@haml_is_haml = true
|
||||||
_hamlout = @haml_stack[-1]
|
|
||||||
_erbout = _hamlout.buffer
|
_erbout = _hamlout.buffer
|
||||||
END
|
END
|
||||||
postamble = <<END.gsub("\n", ";")
|
postamble = <<END.gsub("\n", ";")
|
||||||
@haml_is_haml = false
|
@haml_is_haml = false
|
||||||
@haml_stack.pop.buffer
|
@haml_buffer = @haml_buffer.upper
|
||||||
|
_erbout
|
||||||
END
|
END
|
||||||
preamble + locals_code(local_names) + @precompiled + postamble
|
preamble + locals_code(local_names) + @precompiled + postamble
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue