Class: Fukuzatsu::Formatters::Html

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/fukuzatsu/formatters/html.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

#filename, included, #initialize, #output_directory, #output_path, #path_to_results

Class Method Details

.index(summaries, base_output_path) ⇒ Object



11
12
13
# File 'lib/fukuzatsu/formatters/html.rb', line 11

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

Instance Method Details

#columnsObject



15
16
17
# File 'lib/fukuzatsu/formatters/html.rb', line 15

def columns
  ["Class", "Method", "Complexity"]
end

#contentObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fukuzatsu/formatters/html.rb', line 19

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

#exportObject



34
35
36
37
38
39
40
# File 'lib/fukuzatsu/formatters/html.rb', line 34

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

#file_extensionObject



42
43
44
# File 'lib/fukuzatsu/formatters/html.rb', line 42

def file_extension
  ".htm"
end

#formatterObject



46
47
48
# File 'lib/fukuzatsu/formatters/html.rb', line 46

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

#headerObject



50
51
52
# File 'lib/fukuzatsu/formatters/html.rb', line 50

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

#lexerObject



54
55
56
# File 'lib/fukuzatsu/formatters/html.rb', line 54

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

#output_templateObject



58
59
60
# File 'lib/fukuzatsu/formatters/html.rb', line 58

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

#preprocessedObject



62
63
64
# File 'lib/fukuzatsu/formatters/html.rb', line 62

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

#rowsObject



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/fukuzatsu/formatters/html.rb', line 66

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