artemv-diff_to_html 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
File without changes
data/diff_to_html.rb ADDED
@@ -0,0 +1,162 @@
1
+ require 'cgi'
2
+
3
+ class DiffToHtml
4
+
5
+ attr_accessor :file_prefix
6
+
7
+ #
8
+ # globals
9
+ #
10
+
11
+ def ln_cell(ln, side = nil)
12
+ anchor = "f#{@filenum}#{side}#{ln}"
13
+ result = "<td class = 'ln'>"
14
+ result += "<a name='#{anchor}' href='##{anchor}'>" if ln
15
+ result += "#{ln}"
16
+ result += "</a>" if ln
17
+ result += "</td>"
18
+ result
19
+ end
20
+ #
21
+ # helper for building the next row in the diff
22
+ #
23
+
24
+ def get_diff_row(left_ln, right_ln)
25
+ result = []
26
+ if @left.length > 0 or @right.length > 0
27
+ modified = (@last_op != ' ')
28
+ if modified
29
+ left_class = " class='r'"
30
+ right_class = " class='a'"
31
+ result << "<tbody class='mod'>"
32
+ else
33
+ left_class = right_class = ''
34
+ end
35
+ result << @left.map do |line|
36
+ x = "<tr#{left_class}>#{ln_cell(left_ln, 'l')}"
37
+ if modified
38
+ x += ln_cell(nil)
39
+ else
40
+ x += ln_cell(right_ln, 'r')
41
+ right_ln += 1
42
+ end
43
+ x += "<td>#{line}</td></tr>"
44
+ left_ln += 1
45
+ x
46
+ end
47
+ if modified
48
+ result << @right.map do |line|
49
+ x = "<tr#{right_class}>#{ln_cell(nil)}#{ln_cell(right_ln, 'r')}<td>#{line}</td></tr>"
50
+ right_ln += 1
51
+ x
52
+ end
53
+ result << "</tbody>"
54
+ end
55
+ end
56
+ return result.join("\n"), left_ln, right_ln
57
+ end
58
+
59
+ #
60
+ # build the diff
61
+ #
62
+
63
+ def range_row(range)
64
+ "<tr class='range'><td>...</td<td>...</td><td>#{range}</td></tr>"
65
+ end
66
+
67
+ def range_info(range)
68
+ range.match(/^@@ \-(\d+),\d+ \+(\d+),\d+ @@/)
69
+ left_ln = Integer($1)
70
+ right_ln = Integer($2)
71
+ return left_ln, right_ln
72
+ end
73
+
74
+ def begin_file(file)
75
+ @filenum ||=0
76
+ result = <<EOF
77
+ <li><h2><a name="F#{@filenum}" href="#F#{@filenum}">#{@file_prefix}#{file}</a></h2><table class='diff'>
78
+ <colgroup>
79
+ <col class="lineno"/>
80
+ <col class="lineno"/>
81
+ <col class="content"/>
82
+ </colgroup>
83
+ EOF
84
+ @filenum += 1
85
+ result
86
+ end
87
+
88
+ def flush_changes(result, left_ln, right_ln)
89
+ x, left_ln, right_ln = get_diff_row(left_ln, right_ln)
90
+ result << x
91
+ @left.clear
92
+ @right.clear
93
+ return left_ln, right_ln
94
+ end
95
+
96
+ def get_diff(diff_file)
97
+ @last_op = ' '
98
+ @left = []
99
+ @right = []
100
+
101
+ result = ""
102
+
103
+ diff = diff_file.split("\n")
104
+
105
+ index = diff.shift
106
+ file = index[7..-1]
107
+ diff.shift #equals
108
+ header1 = diff.shift
109
+ if header1 =~ /^---/
110
+ diff.shift
111
+
112
+ result << "<ul class='diff'>#{begin_file(file)}"
113
+ range = diff.shift
114
+ left_ln, right_ln = range_info(range)
115
+ result << range_row(range)
116
+
117
+ skip = 0
118
+ diff.each do |line|
119
+ if skip > 0
120
+ skip -= 1
121
+ else
122
+ op = line[0,1]
123
+ line = line[1..-1]
124
+
125
+ if ((@last_op != ' ' and op == ' ') or (@last_op == ' ' and op != ' '))
126
+ flush_changes(result, left_ln, right_ln)
127
+ end
128
+
129
+ # truncate and escape
130
+ line = CGI.escapeHTML(line)
131
+
132
+ case op
133
+ when ' '
134
+ @left.push(line)
135
+ @right.push(line)
136
+ when '-' then @left.push(line)
137
+ when '+' then @right.push(line)
138
+ when '@'
139
+ range = '@' + line
140
+ flush_changes(result, left_ln, right_ln)
141
+ left_ln, right_ln = range_info(range)
142
+ result << range_row(range)
143
+ when 'I'
144
+ file = line[6..-1]
145
+ flush_changes(result, left_ln, right_ln)
146
+ result << "</table></li>#{begin_file(file)}"
147
+ skip = 3
148
+ end
149
+ @last_op = op
150
+ end
151
+ end
152
+
153
+ flush_changes(result, left_ln, right_ln)
154
+ result << "</table></li></ul>"
155
+ else
156
+ result = "<div class='error'>#{header1}</div>"
157
+ end
158
+
159
+ result
160
+ end
161
+
162
+ end
data/examples/diff.css ADDED
@@ -0,0 +1,68 @@
1
+ ul.diff {
2
+ padding:0;
3
+ }
4
+
5
+ .diff table col.lineno {
6
+ width:4em;
7
+ }
8
+
9
+ .diff h2, .diff h2 a {
10
+ color:#333333;
11
+ font-size:14px;
12
+ letter-spacing:normal;
13
+ margin:0pt auto;
14
+ }
15
+
16
+ .diff h2 {
17
+ padding:0.1em 0pt 0.25em 0.5em;
18
+ }
19
+
20
+ table.diff {
21
+ font-size : 9pt;
22
+ font-family : "lucida console", "courier new", monospace;
23
+ white-space : pre;
24
+ border : 1px solid #D7D7D7;
25
+ border-collapse : collapse;
26
+ line-height : 110%;
27
+ width: 100%;
28
+ }
29
+
30
+ .diff tr {
31
+ background: white;
32
+ }
33
+
34
+ .diff td {
35
+ border : none;
36
+ padding : 0px 10px;
37
+ margin : 0px;
38
+ }
39
+
40
+ .diff td a {
41
+ text-decoration: none;
42
+ }
43
+
44
+ tr.a { background : #ddffdd; }
45
+
46
+ tr.r { background : #ffdddd; }
47
+
48
+ tr.range { background : #EAF2F5; color : #999; }
49
+
50
+ td.ln {
51
+ background : #ECECEC;
52
+ color : #aaa;
53
+ border-top:1px solid #999988;
54
+ border-bottom:1px solid #999988;
55
+ border-right:1px solid #D7D7D7;
56
+ }
57
+
58
+ .diff li {
59
+ background:#F7F7F7 none repeat scroll 0%;
60
+ border:1px solid #D7D7D7;
61
+ list-style-type:none;
62
+ margin:0pt 0pt 2em;
63
+ padding:2px;
64
+ }
65
+
66
+ .ln a {
67
+ color: #aaa;
68
+ }
data/examples/test.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'diff_to_html'
2
+ diff = `svn diff -r 46:47 svn://hamptoncatlin.com/haml --diff-cmd diff -x "-w -U 2"`
3
+
4
+ puts <<EOF
5
+ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
6
+ <html xmlns="http://www.w3.org/1999/xhtml">
7
+ <head>
8
+ <link type="text/css" rel="stylesheet" href="diff.css">
9
+ <title>diff of HAML revision 47</title>
10
+ </head>
11
+ <body>
12
+ #{DiffToHtml.new.get_diff(diff)}
13
+ </body>
14
+ </html>
15
+ EOF
data/examples/test.sh ADDED
@@ -0,0 +1 @@
1
+ ruby test.rb >out.html
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'diff_to_html'
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: artemv-diff_to_html
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Artem Vasiliev
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-23 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Generates HTML view of given unified diff
17
+ email: abublic@gmail.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - diff_to_html.rb
26
+ - examples/diff.css
27
+ - examples/test.rb
28
+ - examples/test.sh
29
+ - init.rb
30
+ - lib
31
+ - README
32
+ has_rdoc: false
33
+ homepage: http://github.com/artemv/diff_to_html.rb
34
+ post_install_message:
35
+ rdoc_options: []
36
+
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ requirements: []
52
+
53
+ rubyforge_project:
54
+ rubygems_version: 1.0.1
55
+ signing_key:
56
+ specification_version: 2
57
+ summary: Unified diff to HTML converter
58
+ test_files: []
59
+