module HTML::Mixin::HtmlHandler

The HtmlHandler module is the library for generating html output.

Public Instance Methods

html(formatting = true) click to toggle source

Returns the HTML text for the current object. Indentation and end tag options are optional, based on the settings of the classes themselves.

If formatting is false, then formatting and whitespace is not applied and you will get a single, very long string. Note that case is still honored.

# File lib/html/mixin/html_handler.rb, line 42
def html(formatting = true)
  if HTML::Table.html_case == 'upper'
    @html_begin.upcase!
    @html_end.upcase!
  end

  ilevel = 0

  if formatting && self.class.respond_to?(:indent_level)
    ilevel = self.class.indent_level
  end

  html          = (' ' * ilevel) + @html_begin[0..-1]
  len           = html.length
  html[len, len] = '>'

  if is_a?(Array)
    if formatting
      html << map { |e| "\n#{e.html(formatting)}" }.join
    else
      html << map { |e| e.html(formatting).to_s }.join
    end
  else
    html << @html_body
  end

  #####################################################################
  # Add end tags, or not, depending on whether the class supports the
  # end_tags class method.  Those that don't have an end_tags class
  # method necessarily means that the end tag must be included.
  #
  # The Table.global_end_tags method overrides the individual class
  # preferences with regards to end tags.
  #####################################################################
  if is_a?(Array)
    if HTML::Table.global_end_tags?
      if self.class.respond_to?(:end_tags?)
        if formatting
          if self.class.end_tags?
            html << ("\n" << (' ' * ilevel) << @html_end)
          end
        else
          html << ((' ' * ilevel) << @html_end) if self.class.end_tags?
        end
      else
        if formatting
          html << ("\n" << (' ' * ilevel) << @html_end)
        else
          html << ((' ' * ilevel) << @html_end)
        end
      end
    else
      unless self.class.respond_to?(:end_tags?)
        if formatting
          html << ("\n" << (' ' * ilevel) << @html_end)
        else
          html << ((' ' * ilevel) << @html_end)
        end
      end
    end
  else
    if HTML::Table.global_end_tags?
      if self.class.respond_to?(:end_tags?)
        html << @html_end if self.class.end_tags?
      else
        html << @html_end
      end
    else
      unless self.class.respond_to?(:end_tags?)
        html << @html_end
      end
    end
  end

  html
end