From 6c0473e38e21a00af9b9454f9a33597f3d640465 Mon Sep 17 00:00:00 2001 From: Nathan Weizenbaum Date: Thu, 24 Apr 2008 09:52:26 -0700 Subject: [PATCH] Replace @haml_stack with a linked list of buffers. This is done via the new Buffer#upper property. --- lib/haml/buffer.rb | 14 +++++++++++++- lib/haml/engine.rb | 7 +++---- lib/haml/helpers.rb | 4 ++-- lib/haml/precompiler.rb | 7 +++---- 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/haml/buffer.rb b/lib/haml/buffer.rb index e13f8ffe..03b3b75b 100644 --- a/lib/haml/buffer.rb +++ b/lib/haml/buffer.rb @@ -18,6 +18,11 @@ module Haml # The options hash passed in from Haml::Engine. 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 def xhtml? not html? @@ -38,6 +43,12 @@ module Haml @options[:format] == :html5 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. def tabulation @real_tabs + @tabulation @@ -50,7 +61,8 @@ module Haml end # Creates a new buffer. - def initialize(options = {}) + def initialize(upper = nil, options = {}) + @upper = upper @options = { :attr_wrapper => "'", :ugly => false, diff --git a/lib/haml/engine.rb b/lib/haml/engine.rb index 9d44143e..00c21ca6 100644 --- a/lib/haml/engine.rb +++ b/lib/haml/engine.rb @@ -147,7 +147,7 @@ END # they won't work. def render(scope = Object.new, locals = {}, &block) 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) scope_object = eval("self", scope) @@ -161,8 +161,7 @@ END scope_object.instance_eval do extend Haml::Helpers - @haml_stack ||= Array.new - @haml_stack.push(buffer) + @haml_buffer = buffer @haml_is_haml = true end @@ -170,7 +169,7 @@ END # Get rid of the current buffer scope_object.instance_eval do - @haml_stack.pop + @haml_buffer = buffer.upper @haml_is_haml = false end diff --git a/lib/haml/helpers.rb b/lib/haml/helpers.rb index 1086c8c9..b0eb04c9 100644 --- a/lib/haml/helpers.rb +++ b/lib/haml/helpers.rb @@ -38,7 +38,7 @@ module Haml # def init_haml_helpers @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 end @@ -345,7 +345,7 @@ END # Gets a reference to the current Haml::Buffer object. def haml_buffer - @haml_stack[-1] + @haml_buffer end # Gives a proc the same local "_hamlout" and "_erbout" variables diff --git a/lib/haml/precompiler.rb b/lib/haml/precompiler.rb index 7e31d527..9d1a24a6 100644 --- a/lib/haml/precompiler.rb +++ b/lib/haml/precompiler.rb @@ -97,15 +97,14 @@ module Haml def precompiled_with_ambles(local_names) preamble = <