/
formatter.rb
92 lines (78 loc) · 3.1 KB
/
formatter.rb
1
2
3
4
5
6
7
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
87
88
89
90
91
92
require 'erb'
require 'syntax'
require 'syntax/convertors/html'
module Insurance
class Formatter
include ERB::Util
def self.svn_blame_for(file)
if File.exist?(File.dirname(File.expand_path(file)) + '/.svn')
`svn blame #{file}`.split("\n").map {|l| l.split[1]}.map { |l| l.split('@')[0] }
else
[]
end
end
def self.run(dbfile, outputdir)
raw = Marshal.load(open(dbfile))
unless File.exist?(outputdir)
Dir.mkdir(outputdir)
end
asset_dir = File.dirname(__FILE__) + "/templates/assets"
files = raw.keys.inject([]) { |arr, k| arr += raw[k].keys; arr }.uniq.sort
project_name = File.basename Dir.pwd
File.open("#{outputdir}/index.html", 'w') do |f|
f.write ERB.new(File.read("#{File.dirname(__FILE__)}/templates/index.rhtml")).result(binding)
puts "Wrote #{outputdir}/index.html"
end
files.each do |file, lines|
contents = File.open(file, 'r').readlines
lines = []
contents.each_with_index do |line, num|
sline = line.strip
lines << num + 1 if sline.empty?
lines << num + 1 if sline =~ /^#/
lines << num + 1 if sline =~ /^\s*(?:end|\})\s*(?:#.*)?$/
lines << num + 1 if sline =~ /^(public|private|protected)/
lines << num + 1 if sline =~ /^(?:begin\s*(?:#.*)?|ensure\s*(?:#.*)?|else\s*(?:#.*)?)$/
lines << num + 1 if sline =~ /^(?:rescue)/
lines << num + 1 if sline =~ /^case\s*(?:#.*)?$/
lines << num + 1 if sline =~ /^(\)|\]|\})(?:#.*)?$/
end
[:unit, :functional, :integration].each do |suite|
if raw[suite][file]
raw[suite][file] += lines
end
end
end
# Create html output
files.each do |file, lines|
blame = svn_blame_for(file)
file_under_test = file
percentage = 0 # XXX
File.open("#{outputdir}/#{file.gsub('/', '-')}.html", 'w') do |f|
contents = File.open(file, 'r').readlines
body = "<table class=\"ruby\">\n"
contents.each_with_index do |line, num|
classes = []
[:unit, :functional, :integration].each do |suite|
if raw[suite][file] && raw[suite][file].include?(num+1)
classes << suite.to_s
end
end
lineno = (num + 1).to_s
body << "<tr><td class=\"lineno\">"
body << "<a href=\"txmt://open?url=file://#{File.expand_path(file)}&line=#{lineno}\">#{lineno}</a>"
body << "</td>"
unless blame.empty?
body << "<td class=\"blame\">#{blame[num]}</td>"
end
body << "<td><pre> <span class=\"codeline #{classes * ' '}\">#{Syntax::Convertors::HTML.for_syntax('ruby').convert(line, false)}</span>"
body << "</td></tr>"
end
body << "</table>"
f.write ERB.new(File.read("#{File.dirname(__FILE__)}/templates/code-page.rhtml")).result(binding)
puts "Wrote #{outputdir}/#{file.gsub('/', '-')}.html"
end
end
end
end
end