1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

A slight tweak to the behavior of capture_haml.

git-svn-id: svn://hamptoncatlin.com/haml/branches/edge@162 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
nex3 2006-11-20 06:46:44 +00:00
parent 53b68c335f
commit 899c06c1c0
3 changed files with 35 additions and 14 deletions

View file

@ -60,11 +60,14 @@ module Haml
buffer.tabulation -= i buffer.tabulation -= i
end end
# Captures the result of the given block of Haml code and returns # Captures the result of the given block of Haml code,
# them as a string. For example, after the following, # gets rid of the excess indentation,
# and returns it as a string.
# For example, after the following,
# #
# - foo = capture_haml(13) do |a| # .foo
# %p= a # - foo = capture_haml(13) do |a|
# %p= a
# #
# the local variable <tt>foo</tt> would be assigned to "<p>13</p>\n". # the local variable <tt>foo</tt> would be assigned to "<p>13</p>\n".
def capture_haml(*args, &block) def capture_haml(*args, &block)
@ -73,7 +76,19 @@ module Haml
block.call(*args) 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 end
# Gets a reference to the current Haml::Buffer object. # Gets a reference to the current Haml::Buffer object.

View file

@ -27,10 +27,14 @@
<p> <p>
reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong
</p> </p>
<div> <div class='woah'>
<h1>Big!</h1> <div id='funky'>
<p>Small</p> <div>
<!-- Invisible --> <h1>Big!</h1>
<p>Small</p>
<!-- Invisible -->
</div>
</div>
</div> </div>
<p>baz</p> <p>baz</p>
<p>boom</p> <p>boom</p>

View file

@ -12,11 +12,13 @@
- tab_up - tab_up
%p reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong %p reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong
- tab_down - tab_down
= capture_haml do .woah
%div #funky
%h1 Big! = capture_haml do
%p Small %div
/ Invisible %h1 Big!
%p Small
/ Invisible
%p baz %p baz
- buffer.tabulation = 10 - buffer.tabulation = 10
%p boom %p boom