From 1082ac7a6576ace5cf43669183439f1f9be47568 Mon Sep 17 00:00:00 2001 From: nex3 Date: Tue, 21 Nov 2006 06:29:39 +0000 Subject: [PATCH] Making ActionView's capture work properly. git-svn-id: svn://hamptoncatlin.com/haml/branches/edge@167 7063305b-7217-0410-af8c-cdc13e5119b9 --- lib/haml/helpers.rb | 49 ++++++++++++++++------------ lib/haml/helpers/action_view_mods.rb | 5 ++- test/results/helpers.xhtml | 4 +++ test/templates/helpers.haml | 4 +++ 4 files changed, 40 insertions(+), 22 deletions(-) diff --git a/lib/haml/helpers.rb b/lib/haml/helpers.rb index 58ad5a64..8a20274d 100644 --- a/lib/haml/helpers.rb +++ b/lib/haml/helpers.rb @@ -15,11 +15,6 @@ module Haml def self.action_view? @@action_view end - - # Sets whether or not ActionView is installed on the system. - def self.action_view(value) # :nodoc: - @@action_view = value - end # Takes any string, finds all the endlines and converts them to # HTML entities for endlines so they'll render correctly in @@ -145,12 +140,37 @@ module Haml # the local variable foo would be assigned to "

13

\n". # def capture_haml(*args, &block) - buffer_buffer = buffer.buffer - position = buffer_buffer.length + capture_haml_with_buffer(buffer.buffer, *args, &block) + end + + private + + # Sets whether or not ActionView is installed on the system. + def self.action_view(value) # :nodoc: + @@action_view = value + end + + # Gets a reference to the current Haml::Buffer object. + def buffer + @haml_stack[-1] + end + + # Gives a proc the same local "_hamlout" and "_erbout" variables + # that the current template has. + def bind_proc(&proc) + _hamlout = buffer + _erbout = _hamlout.buffer + proc { |*args| proc.call(*args) } + end + + # Performs the function of capture_haml, assuming local_buffer + # is where the output of block goes. + def capture_haml_with_buffer(local_buffer, *args, &block) + position = local_buffer.length block.call(*args) - captured = buffer_buffer.slice!(position..-1) + captured = local_buffer.slice!(position..-1) min_tabs = nil captured.each do |line| @@ -164,19 +184,6 @@ module Haml end result.to_s end - - # Gets a reference to the current Haml::Buffer object. - def buffer # :nodoc: - @haml_stack[-1] - end - - # Gives a proc the same local "_hamlout" and "_erbout" variables - # that the current template has. - def bind_proc(&proc) # :nodoc: - _hamlout = buffer - _erbout = _hamlout.buffer - proc { |*args| proc.call(*args) } - end include ActionViewMods if self.const_defined? "ActionViewMods" end diff --git a/lib/haml/helpers/action_view_mods.rb b/lib/haml/helpers/action_view_mods.rb index 98ebcd93..4d93bba3 100644 --- a/lib/haml/helpers/action_view_mods.rb +++ b/lib/haml/helpers/action_view_mods.rb @@ -24,7 +24,10 @@ if action_view_included #++ module ActionViewMods def self.included(othermod) # :nodoc: - othermod.action_view(true) + othermod.class_eval do + action_view(true) + alias_method :capture_erb_with_buffer, :capture_haml_with_buffer + end end def concat(string, binding = nil) # :nodoc: diff --git a/test/results/helpers.xhtml b/test/results/helpers.xhtml index 4e2d8633..c40ef56c 100644 --- a/test/results/helpers.xhtml +++ b/test/results/helpers.xhtml @@ -34,6 +34,10 @@

Small

+
+

foo

+

bar

+
(parentheses!) diff --git a/test/templates/helpers.haml b/test/templates/helpers.haml index 52810206..a97164c7 100644 --- a/test/templates/helpers.haml +++ b/test/templates/helpers.haml @@ -19,6 +19,10 @@ %h1 Big! %p Small / Invisible + = capture do + .dilly + %p foo + %h1 bar = surround '(', ')' do %strong parentheses! = precede '*' do