# File lib/haml/helpers.rb, line 440
    def haml_tag(name, *rest, &block)
      ret = ErrorReturn.new("haml_tag")

      text = rest.shift.to_s unless [Symbol, Hash, NilClass].any? {|t| rest.first.is_a? t}
      flags = []
      flags << rest.shift while rest.first.is_a? Symbol
      attrs = Haml::Util.map_keys(rest.shift || {}) {|key| key.to_s}
      name, attrs = merge_name_and_attributes(name.to_s, attrs)

      attributes = Haml::Precompiler.build_attributes(haml_buffer.html?,
                                                      haml_buffer.options[:attr_wrapper],
                                                      attrs)

      if text.nil? && block.nil? && (haml_buffer.options[:autoclose].include?(name) || flags.include?(:/))
        haml_concat "<#{name}#{attributes} />"
        return ret
      end

      if flags.include?(:/)
        raise Error.new("Self-closing tags can't have content.") if text
        raise Error.new("Illegal nesting: nesting within a self-closing tag is illegal.") if block
      end

      tag = "<#{name}#{attributes}>"
      if block.nil?
        text = text.to_s
        if text.include?("\n")
          haml_concat tag
          tab_up
          haml_concat text
          tab_down
          haml_concat "</#{name}>"
        else
          tag << text << "</#{name}>"
          haml_concat tag
        end
        return ret
      end

      if text
        raise Error.new("Illegal nesting: content can't be both given to haml_tag :#{name} and nested within it.")
      end

      if flags.include?(:<)
        tag << capture_haml(&block).strip << "</#{name}>"
        haml_concat tag
        return ret
      end

      haml_concat tag
      tab_up
      block.call
      tab_down
      haml_concat "</#{name}>"

      ret
    end