Sha256: eda8ea585999f7d894711b117aaa0d377a56a5cb82d914fb3bcb523a25605ddc

Contents?: true

Size: 1.88 KB

Versions: 9

Compression:

Stored size: 1.88 KB

Contents

# monkey patching HTML formatter to be safe

module Ruport
  class Formatter::HTML < Formatter    
    
    def safe(val)
      out = ERB::Util.html_escape(val.to_s)
      out.gsub("&amp;nbsp;", "&nbsp;")
    end
    
    def safe_join(array, sep)
      safe_array = array.collect{ |a| safe(a) }
      safe_array.join(sep)
    end

    # Generates table headers based on the column names of your Data::Table.  
    #
    # This method does not do anything if options.show_table_headers is false
    # or the Data::Table has no column names.
    def build_table_header
      output << "\t<table>\n"
      unless data.column_names.empty? || !options.show_table_headers
        output << "\t\t<tr>\n\t\t\t<th>" + 
          safe_join(data.column_names, "</th>\n\t\t\t<th>") + 
          "</th>\n\t\t</tr>\n"
      end
    end
    
    # Renders individual rows for the table.
    def build_row(data = self.data)
      output <<
        "\t\t<tr>\n\t\t\t<td>" +
        safe_join(data.to_a, "</td>\n\t\t\t<td>") +
        "</td>\n\t\t</tr>\n"
    end

    # Renders the header for a group using the group name.
    #
    def build_group_header
      output << "\t<p>#{safe(data.name)}</p>\n"
    end
    
    private
    
    def render_justified_grouping
      output << "\t<table>\n\t\t<tr>\n\t\t\t<th>" +
        "#{safe(data.grouped_by)}</th>\n\t\t\t<th>" +
        safe_join(grouping_columns, "</th>\n\t\t\t<th>") + 
        "</th>\n\t\t</tr>\n"
      data.each do |name, group|                     
        group.each_with_index do |row, i|
          output << "\t\t<tr>\n\t\t\t"
          if i == 0
            output << "<td class=\"groupName\">#{safe(name)}</td>\n\t\t\t<td>"
          else
            output << "<td>&nbsp;</td>\n\t\t\t<td>"
          end
          output << safe_join(row.to_a, "</td>\n\t\t\t<td>") +
            "</td>\n\t\t</tr>\n"
        end
      end
      output << "\t</table>\n"
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
daily-0.0.10 app/formatters/html_formatter.rb
daily-0.0.9 app/formatters/html_formatter.rb
daily-0.0.8 app/formatters/html_formatter.rb
daily-0.0.7 app/formatters/html_formatter.rb
daily-0.0.6 app/formatters/html_formatter.rb
daily-0.0.5 app/formatters/html_formatter.rb
daily-0.0.4 app/formatters/html_formatter.rb
daily-0.0.3 app/formatters/html_formatter.rb
daily-0.0.2 app/formatters/html_formatter.rb