Skip to content

Commit

Permalink
Merge pull request #179 from metricfu/separate_syntax_highlighting
Browse files Browse the repository at this point in the history
[Misc] Separate syntax highlighting, Extract Metric Report Template
  • Loading branch information
bf4 committed Nov 26, 2013
2 parents 3aa4cb3 + 839b075 commit 9115fb0
Show file tree
Hide file tree
Showing 14 changed files with 133 additions and 65 deletions.
47 changes: 47 additions & 0 deletions lib/metric_fu/formatter/syntax.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require 'coderay'
MetricFu.lib_require { 'utility' }
# CodeRay options
# used to analyze source code, because object Tokens is a list of tokens with specified types.
# :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
module MetricFu
module Formatter
class Syntax

def initialize
@options = { :css => :class, :style => :alpha }
@line_number_options = {:line_numbers => :inline, :line_number_start => 0 }
end

def highlight(ruby_text, line_number)
tokens = tokenize(ruby_text)
tokens.div(highlight_options(line_number))
end

def highlight_options(line_number)
line_number = line_number.to_i
if line_number > 0
@options.merge(@line_number_options.merge(:line_number_start => line_number))
else
@options
end
end

private

def tokenize(ruby_text)
ascii_text = MetricFu::Utility.clean_ascii_text(ruby_text)
tokens = CodeRay.scan(ascii_text, :ruby)
end

end
end
end
4 changes: 2 additions & 2 deletions lib/metric_fu/metrics/base_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ def output_filename(section)
# @return String
# The contents of the css file
def inline_css(css)
css_file = File.join(template_directory, css)
open(css_file) {|f| f.read }
css_file = File.join(MetricFu.lib_dir,'templates', css)
MetricFu::Utility.binread(css_file)
end

# Provides a link to open a file through the textmate protocol
Expand Down
67 changes: 8 additions & 59 deletions lib/metric_fu/reporting/templates/awesome/awesome_template.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
require 'fileutils'
require 'coderay'
MetricFu.metrics_require { 'base_template' }
MetricFu.lib_require { 'utility' }
MetricFu.lib_require { 'templates/report' }

class AwesomeTemplate < MetricFu::Template

Expand Down Expand Up @@ -44,70 +43,20 @@ def write
write_file_data
end

def convert_ruby_to_html(ruby_text, line_number)
tokens = CodeRay.scan(MetricFu::Utility.clean_ascii_text(ruby_text), :ruby)
options = { :css => :class, :style => :alpha }
if line_number.to_i > 0
options = options.merge({:line_numbers => :inline, :line_number_start => line_number.to_i })
end
tokens.div(options)
# CodeRay options
# used to analyze source code, because object Tokens is a list of tokens with specified types.
# :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
end
def write_file_data

per_file_data.each_pair do |file, lines|
next if file.to_s.empty?
next unless File.file?(file)
report = MetricFu::Templates::Report.new(file, lines).render(@metrics)

data = File.readlines(file)
fn = "#{file.gsub(%r{/}, '_')}.html"

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

formatter.write_template(out, fn)
formatter.write_template(report, html_filename(file))
end
end

def html_filename(file)
"#{file.gsub(%r{/}, '_')}.html"
end

def template_directory
File.dirname(__FILE__)
end
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
32 changes: 32 additions & 0 deletions lib/metric_fu/templates/report.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<html>
<head>
<style>
<%= inline_css('css/syntax.css') %>
<%= inline_css('css/bluff.css') if MetricFu.configuration.graph_engine == :bluff %>
<%= inline_css('css/rcov.css') if @metrics.has_key?(:rcov) %>
</style>
</head>
<body>
<table cellpadding='0' cellspacing='0' class='ruby'>
<% @data.each_with_index do |line, idx| %>
<% line_number = (idx + 1).to_s %>
<tr>
<td valign='top'>
<% if @lines.has_key?(line_number) %>
<ul>
<% @lines[line_number].each do |problem| %>
<li><%= "#{problem[:description]} &raquo; #{problem[:type]}" %></li>
<% end %>
</ul>
<% else %>
&nbsp;
<% end %>
</td>
<td valign='top'>
<%= line_for_display(line, line_number) %>
</td>
</tr>
<% end %>
<table>
</body>
</html>
36 changes: 36 additions & 0 deletions lib/metric_fu/templates/report.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
MetricFu.lib_require { 'formatter/syntax' }

module MetricFu
module Templates
class Report < MetricFu::Template

def initialize(file, lines)
@file = file
@lines = lines
@data = File.readlines(file)
end

def render(metrics)
@metrics = metrics
erbify('report')
end

def convert_ruby_to_html(ruby_text, line_number)
MetricFu::Formatter::Syntax.new.highlight(ruby_text, line_number)
end

def line_for_display(line, line_number)
if MetricFu::Formatter::Templates.option('syntax_highlighting')
line_for_display = convert_ruby_to_html(line, line_number)
else
"<a name='n#{line_number}' href='n#{line_number}'>#{line_number}</a>#{line}"
end
end

def template_directory
File.dirname(__FILE__)
end

end
end
end
4 changes: 4 additions & 0 deletions lib/metric_fu/utility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,9 @@ def load_yaml(file)
YAML.load_file(file)
end

def binread(file)
File.binread(file)
end

end
end
8 changes: 4 additions & 4 deletions spec/metric_fu/metrics/base_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@
describe "#inline_css" do
it 'should return the contents of a css file' do
css = 'mycss.css'
@template.should_receive(:template_directory).and_return('dir')
io = double('io', :read => "css contents")
@template.should_receive(:open).and_yield(io)
dir = File.join(MetricFu.lib_dir, 'templates', css)
contents = "css contents"
MetricFu::Utility.should_receive(:binread).with(dir).and_return(contents)
result = @template.send(:inline_css, css)
result.should == 'css contents'
expect(result).to eq(contents)
end
end

Expand Down

0 comments on commit 9115fb0

Please sign in to comment.