Class: AwesomeTemplate

Inherits:
MetricFu::Template show all
Defined in:
lib/templates/awesome/awesome_template.rb

Instance Attribute Summary

Attributes inherited from MetricFu::Template

#per_file_data, #report

Instance Method Summary collapse

Instance Method Details

#convert_ruby_to_html(ruby_text) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/templates/awesome/awesome_template.rb', line 40

def convert_ruby_to_html(ruby_text)
  # convertor = Syntax::Convertors::HTML.for_syntax('ruby')
  # convertor.convert(ruby_text)
  tokens = CodeRay.scan(ruby_text, :ruby)
  tokens.div( :line_numbers => :table, :css => :class, :style => :alpha )
  # :tab_width – tabulation width in spaces. Default: 8
# :css – how to include the styles (:class и :style). Default: :class)
# 
# :wrap – wrap result in html tag :page, :div, :span or not to wrap (nil)
# 
# :line_numbers – how render line numbers (:table, :inline, :list or nil)
# 
# :line_number_start – first line number
# 
# :bold_every – make every n-th line number bold. Default: 10
# CodeRay, as Syntax may be used to analyze source code, because object Tokens is a list of tokens with specified types.
end

#this_directoryObject



88
89
90
# File 'lib/templates/awesome/awesome_template.rb', line 88

def this_directory
  File.dirname(__FILE__)
end

#writeObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/templates/awesome/awesome_template.rb', line 8

def write
  # Getting rid of the crap before and after the project name from integrity
  # @name = File.basename(Dir.pwd).gsub(/^\w+-|-\w+$/, "")
  @name = Pathname.new(Dir.pwd).basename

  # Copy Bluff javascripts to output directory
  Dir[File.join(this_directory, '..', 'javascripts', '*')].each do |f|
    FileUtils.copy(f, File.join(MetricFu.output_directory, File.basename(f)))
  end

  report.each_pair do |section, contents|
    if template_exists?(section)
      create_instance_var(section, contents)
      create_instance_var(:per_file_data, per_file_data)
      @html = erbify(section)
      html = erbify('layout')
      fn = output_filename(section)
      MetricFu.report.save_output(html, MetricFu.output_directory, fn)
    end
  end

  # Instance variables we need should already be created from above
  if template_exists?('index')
    @html = erbify('index')
    html = erbify('layout')
    fn = output_filename('index')
    MetricFu.report.save_output(html, MetricFu.output_directory, fn)
  end

  write_file_data
end

#write_file_dataObject

CodeRay, as Syntax may be used to analyze source code, because object Tokens is a list of tokens with specified types.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/templates/awesome/awesome_template.rb', line 57

def write_file_data

  per_file_data.each_pair do |file, lines|
    data = File.open(file, 'r').readlines
    fn = "#{file.gsub(%r{/}, '_')}.html"

    out = "<html><head><style>#{inline_css('css/syntax.css')}</style></head><body>"
    out << "<table cellpadding='0' cellspacing='0' class='ruby'>"
    data.each_with_index do |line, idx|
      out << "<tr><td valign='top'><small>#{idx + 1}</small></td>"
      out << "<td valign='top'>"
      if lines.has_key?((idx + 1).to_s)
        out << "<ul>"
        lines[(idx + 1).to_s].each do |problem|
          out << "<li>#{problem[:description]} &raquo; #{problem[:type]}</li>"
        end
        out << "</ul>"
      else
        out << "&nbsp;"
      end
      out << "</td>"
      line_for_display = MetricFu.configuration.syntax_highlighting ? convert_ruby_to_html(line) : line
      out << "<td valign='top'><a name='line#{idx + 1}'>#{line_for_display}</a></td>"
      out << "</tr>"
    end
    out << "<table></body></html>"

    MetricFu.report.save_output(out, MetricFu.output_directory, fn)
  end
end