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:
parent
53b68c335f
commit
899c06c1c0
3 changed files with 35 additions and 14 deletions
|
@ -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 <tt>foo</tt> would be assigned to "<p>13</p>\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.
|
||||
|
|
|
@ -27,10 +27,14 @@
|
|||
<p>
|
||||
reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally loooooooooooooooooong
|
||||
</p>
|
||||
<div>
|
||||
<h1>Big!</h1>
|
||||
<p>Small</p>
|
||||
<!-- Invisible -->
|
||||
<div class='woah'>
|
||||
<div id='funky'>
|
||||
<div>
|
||||
<h1>Big!</h1>
|
||||
<p>Small</p>
|
||||
<!-- Invisible -->
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<p>baz</p>
|
||||
<p>boom</p>
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue