diff --git a/lib/haml/buffer.rb b/lib/haml/buffer.rb index 787392df..df3330c6 100644 --- a/lib/haml/buffer.rb +++ b/lib/haml/buffer.rb @@ -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 count 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) diff --git a/lib/haml/helpers.rb b/lib/haml/helpers.rb index ba5decc9..9d679196 100644 --- a/lib/haml/helpers.rb +++ b/lib/haml/helpers.rb @@ -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 "" + nil + end private diff --git a/lib/haml/helpers/action_view_mods.rb b/lib/haml/helpers/action_view_mods.rb index 09a99803..eec897eb 100644 --- a/lib/haml/helpers/action_view_mods.rb +++ b/lib/haml/helpers/action_view_mods.rb @@ -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 diff --git a/test/haml/results/helpers.xhtml b/test/haml/results/helpers.xhtml index ccedf4ed..230c4ee4 100644 --- a/test/haml/results/helpers.xhtml +++ b/test/haml/results/helpers.xhtml @@ -45,9 +45,11 @@ click here.

baz

-

boom

+

boom

foo -
+

+ +

@@ -58,3 +60,12 @@ foo
  • google
  • +

    + foo +

    + bar +
    + boom + baz + boom, again +

    diff --git a/test/haml/templates/helpers.haml b/test/haml/templates/helpers.haml index bf99c520..fce2282b 100644 --- a/test/haml/templates/helpers.haml +++ b/test/haml/templates/helpers.haml @@ -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"