From 899c06c1c0bcc08c2247f7bba048be001d764b50 Mon Sep 17 00:00:00 2001 From: nex3 Date: Mon, 20 Nov 2006 06:46:44 +0000 Subject: [PATCH] A slight tweak to the behavior of capture_haml. git-svn-id: svn://hamptoncatlin.com/haml/branches/edge@162 7063305b-7217-0410-af8c-cdc13e5119b9 --- lib/haml/helpers.rb | 25 ++++++++++++++++++++----- test/results/helpers.xhtml | 12 ++++++++---- test/templates/helpers.haml | 12 +++++++----- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/lib/haml/helpers.rb b/lib/haml/helpers.rb index 06cd4c38..82f95db3 100644 --- a/lib/haml/helpers.rb +++ b/lib/haml/helpers.rb @@ -60,11 +60,14 @@ module Haml buffer.tabulation -= i end - # Captures the result of the given block of Haml code and returns - # them as a string. For example, after the following, + # Captures the result of the given block of Haml code, + # gets rid of the excess indentation, + # and returns it as a string. + # For example, after the following, # - # - foo = capture_haml(13) do |a| - # %p= a + # .foo + # - foo = capture_haml(13) do |a| + # %p= a # # the local variable foo would be assigned to "

13

\n". def capture_haml(*args, &block) @@ -73,7 +76,19 @@ module Haml block.call(*args) - buffer_buffer.slice!(position..-1) + captured = buffer_buffer.slice!(position..-1) + + min_tabs = nil + captured.each do |line| + tabs = line.index(/[^ ]/) + min_tabs ||= tabs + min_tabs = min_tabs > tabs ? tabs : min_tabs + end + + result = captured.map do |line| + line[min_tabs..-1] + end + result.to_s end # Gets a reference to the current Haml::Buffer object. diff --git a/test/results/helpers.xhtml b/test/results/helpers.xhtml index 2538b396..b40e3627 100644 --- a/test/results/helpers.xhtml +++ b/test/results/helpers.xhtml @@ -27,10 +27,14 @@

reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong

-
-

Big!

-

Small

- +
+
+
+

Big!

+

Small

+ +
+

baz

boom

diff --git a/test/templates/helpers.haml b/test/templates/helpers.haml index eb8e2af9..3d66acaa 100644 --- a/test/templates/helpers.haml +++ b/test/templates/helpers.haml @@ -12,11 +12,13 @@ - tab_up %p reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong - tab_down -= capture_haml do - %div - %h1 Big! - %p Small - / Invisible +.woah + #funky + = capture_haml do + %div + %h1 Big! + %p Small + / Invisible %p baz - buffer.tabulation = 10 %p boom