forked from jscruggs/metric_fu
-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #179 from metricfu/separate_syntax_highlighting
[Misc] Separate syntax highlighting, Extract Metric Report Template
- Loading branch information
Showing
14 changed files
with
133 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]} » #{problem[:type]}" %></li> | ||
<% end %> | ||
</ul> | ||
<% else %> | ||
| ||
<% end %> | ||
</td> | ||
<td valign='top'> | ||
<%= line_for_display(line, line_number) %> | ||
</td> | ||
</tr> | ||
<% end %> | ||
<table> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,5 +33,9 @@ def load_yaml(file) | |
YAML.load_file(file) | ||
end | ||
|
||
def binread(file) | ||
File.binread(file) | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters