Added "puts" helper to output properly-indented text.
git-svn-id: svn://hamptoncatlin.com/haml/trunk@408 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
parent
202a96df96
commit
aeade6a657
|
@ -15,9 +15,16 @@ module Haml
|
|||
# _erbout for compatibility with ERB-specific code.
|
||||
attr_accessor :buffer
|
||||
|
||||
# The number of tabs that are added or subtracted from the
|
||||
# tabulation proscribed by the precompiled template.
|
||||
attr_accessor :tabulation
|
||||
# Gets the current tabulation of the document.
|
||||
def tabulation
|
||||
@real_tabs + @tabulation
|
||||
end
|
||||
|
||||
# Sets the current tabulation of the document.
|
||||
def tabulation=(val)
|
||||
val = val - @real_tabs
|
||||
@tabulation = val > -1 ? val : 0
|
||||
end
|
||||
|
||||
# Creates a new buffer.
|
||||
def initialize(options = {})
|
||||
|
@ -27,6 +34,10 @@ module Haml
|
|||
@buffer = ""
|
||||
@one_liner_pending = false
|
||||
@tabulation = 0
|
||||
|
||||
# The number of tabs that Engine thinks we should have
|
||||
# @real_tabs + @tabulation is the number of tabs actually output
|
||||
@real_tabs = 0
|
||||
end
|
||||
|
||||
# Renders +text+ with the proper tabulation. This also deals with
|
||||
|
@ -87,6 +98,7 @@ module Haml
|
|||
str = ">\n"
|
||||
end
|
||||
@buffer << "#{tabs(tabulation)}<#{name}#{build_attributes(attributes)}#{str}"
|
||||
@real_tabs += 1
|
||||
end
|
||||
|
||||
# Creates a closing tag with the given name.
|
||||
|
@ -107,6 +119,7 @@ module Haml
|
|||
@one_liner_pending = true
|
||||
else
|
||||
@buffer << "\n"
|
||||
@real_tabs += 1
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -127,10 +140,34 @@ module Haml
|
|||
@one_liner_pending = false
|
||||
end
|
||||
|
||||
# Some of these methods are exposed as public class methods
|
||||
# so they can be re-used in helpers.
|
||||
|
||||
# Takes a hash and builds a list of XHTML attributes from it, returning
|
||||
# the result.
|
||||
def self.build_attributes(attributes = {})
|
||||
result = attributes.collect do |a,v|
|
||||
v = v.to_s
|
||||
unless v.nil? || v.empty?
|
||||
attr_wrapper = @options[:attr_wrapper]
|
||||
if v.include? attr_wrapper
|
||||
if v.include? @other_quote_char
|
||||
v = v.gsub(attr_wrapper, @quote_escape)
|
||||
else
|
||||
attr_wrapper = @other_quote_char
|
||||
end
|
||||
end
|
||||
" #{a}=#{attr_wrapper}#{v}#{attr_wrapper}"
|
||||
end
|
||||
end
|
||||
result.sort.join
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# Gets <tt>count</tt> tabs. Mostly for internal use.
|
||||
def tabs(count)
|
||||
@real_tabs = count
|
||||
' ' * (count + @tabulation)
|
||||
end
|
||||
|
||||
|
@ -175,26 +212,6 @@ module Haml
|
|||
{:id => id, :class => class_name}
|
||||
end
|
||||
|
||||
# Takes a hash and builds a list of XHTML attributes from it, returning
|
||||
# the result.
|
||||
def build_attributes(attributes = {})
|
||||
result = attributes.collect do |a,v|
|
||||
v = v.to_s
|
||||
unless v.nil? || v.empty?
|
||||
attr_wrapper = @options[:attr_wrapper]
|
||||
if v.include? attr_wrapper
|
||||
if v.include? @other_quote_char
|
||||
v = v.gsub(attr_wrapper, @quote_escape)
|
||||
else
|
||||
attr_wrapper = @other_quote_char
|
||||
end
|
||||
end
|
||||
" #{a}=#{attr_wrapper}#{v}#{attr_wrapper}"
|
||||
end
|
||||
end
|
||||
result.sort.join
|
||||
end
|
||||
|
||||
# Returns whether or not the given value is short enough to be rendered
|
||||
# on one line.
|
||||
def one_liner?(value)
|
||||
|
|
|
@ -184,6 +184,42 @@ module Haml
|
|||
def capture_haml(*args, &block)
|
||||
capture_haml_with_buffer(buffer.buffer, *args, &block)
|
||||
end
|
||||
|
||||
# Outputs text directly to the Haml buffer, with the proper tabulation
|
||||
def puts(text)
|
||||
buffer.buffer << (' ' * buffer.tabulation) << text << "\n"
|
||||
nil
|
||||
end
|
||||
|
||||
# open_tag helps you construct HTML in your helpers.
|
||||
# It can be used this way
|
||||
#
|
||||
# open_tag :table do
|
||||
# open_tag :tr do
|
||||
# open_tag :td do
|
||||
# puts "data"
|
||||
# end
|
||||
# open_tag :td do
|
||||
# puts "more_data"
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# TODO: Make it output with better tabulation
|
||||
# TODO: TEST!!!!
|
||||
def open(name, text = nil, attributes = {}, &block)
|
||||
puts "<#{name}#{Haml::Buffer.build_attributes(attributes)}>"
|
||||
tab_up
|
||||
# Print out either the text (using push_text) or call the block and add an endline
|
||||
if text
|
||||
puts(text)
|
||||
else
|
||||
lock.call
|
||||
end
|
||||
tab_down
|
||||
puts "</#{name}>"
|
||||
nil
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
|
|
|
@ -59,40 +59,6 @@ if action_view_included
|
|||
res
|
||||
end
|
||||
|
||||
# View accessor for the push_text helper
|
||||
def push_text(text, tabulation = 0)
|
||||
buffer.push_text(text, tabulation)
|
||||
end
|
||||
|
||||
# open_tag helps you construct HTML in your helpers.
|
||||
# It can be used this way
|
||||
#
|
||||
# open_tag :table do
|
||||
# open_tag :tr do
|
||||
# open_tag :td do
|
||||
# push_text "data"
|
||||
# end
|
||||
# open_tag :td do
|
||||
# push_text "more_data"
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# TODO: Make it output with better tabulation
|
||||
# TODO: TEST!!!!
|
||||
def open_tag(named, text = nil, options = {}, &block)
|
||||
# TODO: I'm sure re-coding this is bad. I know we do this elsewhere, obviously.
|
||||
concat "\n"
|
||||
buffer.open_tag(named, 0, false, true, nil, options, nil, false)
|
||||
concat "\n"
|
||||
tab_up
|
||||
# Print out either the text (using push_text) or call the block and add an endline
|
||||
text ? buffer.push_text(text, 1) : (block.call && concat("\n"))
|
||||
tab_down
|
||||
concat "\n"
|
||||
buffer.close_tag(named, 0)
|
||||
end
|
||||
|
||||
def form_for(object_name, *args, &proc) # :nodoc:
|
||||
if block_given? && is_haml?
|
||||
oldproc = proc
|
||||
|
|
|
@ -45,9 +45,11 @@
|
|||
click
|
||||
<a href='thing'>here</a>.
|
||||
<p>baz</p>
|
||||
<p>boom</p>
|
||||
<p>boom</p>
|
||||
foo
|
||||
<form action="hello/world" method="post">
|
||||
<p>
|
||||
<form action="hello/world" method="post">
|
||||
</p>
|
||||
<form action="heeheeaform" method="post">
|
||||
<div><input name="commit" type="submit" value="save" /></div>
|
||||
</form>
|
||||
|
@ -58,3 +60,12 @@ foo
|
|||
<input id="article_body" name="article[body]" size="30" type="text" value="World" />
|
||||
</form>
|
||||
<li><a href='http://www.google.com'>google</a></li>
|
||||
<p>
|
||||
foo
|
||||
<div>
|
||||
bar
|
||||
</div>
|
||||
boom
|
||||
baz
|
||||
boom, again
|
||||
</p>
|
||||
|
|
|
@ -31,12 +31,13 @@ click
|
|||
= succeed '.' do
|
||||
%a{:href=>"thing"} here
|
||||
%p baz
|
||||
- buffer.tabulation = 10
|
||||
- tab_up
|
||||
%p boom
|
||||
- concat "foo\n"
|
||||
- buffer.tabulation = 0
|
||||
- tab_down
|
||||
- def url_for(*stuff); stuff.join(' '); end
|
||||
= form_tag 'hello/world'
|
||||
%p
|
||||
= form_tag 'hello/world'
|
||||
- form_tag 'heeheeaform' do
|
||||
%div= submit_tag 'save'
|
||||
- form_for :article, @article, :url => 'article_url' do |f|
|
||||
|
@ -46,3 +47,10 @@ click
|
|||
= f.text_field :body
|
||||
= list_of({:google => 'http://www.google.com'}) do |name, link|
|
||||
%a{ :href => link }= name
|
||||
%p
|
||||
- puts "foo"
|
||||
%div
|
||||
- puts "bar"
|
||||
- puts "boom"
|
||||
baz
|
||||
- puts "boom, again"
|
||||
|
|
Loading…
Reference in New Issue