Sha256: c360a64050b731ec3ffc69b0df48286ac2f9e9706a29a5f9bcfa70bc9b269d17

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

require 'rouge'

module Fukuzatsu

  module Formatters

    class Html

      include Formatters::Base

      def self.index(summaries)
        Fukuzatsu::Formatters::HtmlIndex.new(summaries).export
      end

      def columns
        ["class", "method", "complexity"]
      end

      def content
        Haml::Engine.new(output_template).render(
          Object.new, {
            header: header,
            rows: rows,
            source_lines: preprocessed,
            class_name: summary.container_name,
            average_complexity: sprintf("%0.2f", summary.average_complexity),
            path_to_file: summary.source_file,
            date: Time.now.strftime("%Y/%m/%d"),
            time: Time.now.strftime("%l:%M %P")
          }
        )
      end

      def export
        begin
          File.open(path_to_results, 'w') {|outfile| outfile.write(content)}
        rescue Exception => e
          puts "Unable to write output: #{e} #{e.backtrace}"
        end
      end

      def file_extension
        ".htm"
      end

      def formatter
        Rouge::Formatters::HTML.new(line_numbers: true)
      end

      def header
        columns.map{|col| "<th>#{col.titleize}</th>"}.join("\r\n")
      end

      def lexer
        lexer = Rouge::Lexers::Ruby.new
      end

      def output_template
        File.read(File.dirname(__FILE__) + "/templates/output.html.haml")
      end

      def preprocessed
        formatter.format(lexer.lex(summary.raw_source))
      end

      def rows
        i = 0
        summary.summaries.inject([]) do |a, summary|
          i += 1
          a << "<tr class='#{i % 2 == 1 ? 'even' : 'odd'}'>"
          a << "  <td>#{summary.container_name}</td>"
          a << "  <td>#{summary.entity_name}</td>"
          a << "  <td>#{summary.complexity}</td>"
          a << "</tr>"
          a
        end.join("\r\n")
      end

    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fukuzatsu-2.1.1 lib/fukuzatsu/formatters/html.rb