PrettyDiff.rb fails to render image diffs with Ruby 1.9.3p194
[WebKit-https.git] / Websites / bugs.webkit.org / PrettyPatch / PrettyPatch.rb
1 require 'cgi'
2 require 'diff'
3 require 'open3'
4 require 'open-uri'
5 require 'pp'
6 require 'set'
7 require 'tempfile'
8
9 module PrettyPatch
10
11 public
12
13     GIT_PATH = "git"
14
15     def self.prettify(string)
16         $last_prettify_file_count = -1
17         $last_prettify_part_count = { "remove" => 0, "add" => 0, "shared" => 0, "binary" => 0, "extract-error" => 0 }
18         string = normalize_line_ending(string)
19         str = "#{HEADER}<body>\n"
20
21         # Just look at the first line to see if it is an SVN revision number as added
22         # by webkit-patch for git checkouts.
23         $svn_revision = 0
24         string.each_line do |line|
25             match = /^Subversion\ Revision: (\d*)$/.match(line)
26             unless match.nil?
27                 str << "<span class='revision'>#{match[1]}</span>\n"
28                 $svn_revision = match[1].to_i;
29             end
30             break
31         end
32
33         fileDiffs = FileDiff.parse(string)
34
35         $last_prettify_file_count = fileDiffs.length
36         str << fileDiffs.collect{ |diff| diff.to_html }.join
37         str << "</body></html>"
38     end
39
40     def self.filename_from_diff_header(line)
41         DIFF_HEADER_FORMATS.each do |format|
42             match = format.match(line)
43             return match[1] unless match.nil?
44         end
45         nil
46     end
47
48     def self.diff_header?(line)
49         RELAXED_DIFF_HEADER_FORMATS.any? { |format| line =~ format }
50     end
51
52 private
53     DIFF_HEADER_FORMATS = [
54         /^Index: (.*)\r?$/,
55         /^diff --git "?a\/.+"? "?b\/(.+)"?\r?$/,
56         /^\+\+\+ ([^\t]+)(\t.*)?\r?$/
57     ]
58
59     RELAXED_DIFF_HEADER_FORMATS = [
60         /^Index:/,
61         /^diff/
62     ]
63
64     BINARY_FILE_MARKER_FORMAT = /^Cannot display: file marked as a binary type.$/
65
66     IMAGE_FILE_MARKER_FORMAT = /^svn:mime-type = image\/png$/
67
68     GIT_INDEX_MARKER_FORMAT = /^index ([0-9a-f]{40})\.\.([0-9a-f]{40})/
69
70     GIT_BINARY_FILE_MARKER_FORMAT = /^GIT binary patch$/
71
72     GIT_BINARY_PATCH_FORMAT = /^(literal|delta) \d+$/
73
74     GIT_LITERAL_FORMAT = /^literal \d+$/
75
76     GIT_DELTA_FORMAT = /^delta \d+$/
77
78     START_OF_BINARY_DATA_FORMAT = /^[0-9a-zA-Z\+\/=]{20,}/ # Assume 20 chars without a space is base64 binary data.
79
80     START_OF_SECTION_FORMAT = /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@\s*(.*)/
81
82     START_OF_EXTENT_STRING = "%c" % 0
83     END_OF_EXTENT_STRING = "%c" % 1
84
85     # We won't search for intra-line diffs in lines longer than this length, to avoid hangs. See <http://webkit.org/b/56109>.
86     MAXIMUM_INTRALINE_DIFF_LINE_LENGTH = 10000
87
88     SMALLEST_EQUAL_OPERATION = 3
89
90     OPENSOURCE_TRAC_URL = "http://trac.webkit.org/"
91
92     OPENSOURCE_DIRS = Set.new %w[
93         Examples
94         LayoutTests
95         PerformanceTests
96         Source
97         Tools
98         WebKitLibraries
99         Websites
100     ]
101
102     IMAGE_CHECKSUM_ERROR = "INVALID: Image lacks a checksum. This will fail with a MISSING error in run-webkit-tests. Always generate new png files using run-webkit-tests."
103
104     def self.normalize_line_ending(s)
105         if RUBY_VERSION >= "1.9"
106             # Transliteration table from http://stackoverflow.com/a/6609998
107             transliteration_table = { '\xc2\x82' => ',',        # High code comma
108                                       '\xc2\x84' => ',,',       # High code double comma
109                                       '\xc2\x85' => '...',      # Tripple dot
110                                       '\xc2\x88' => '^',        # High carat
111                                       '\xc2\x91' => '\x27',     # Forward single quote
112                                       '\xc2\x92' => '\x27',     # Reverse single quote
113                                       '\xc2\x93' => '\x22',     # Forward double quote
114                                       '\xc2\x94' => '\x22',     # Reverse double quote
115                                       '\xc2\x95' => ' ',
116                                       '\xc2\x96' => '-',        # High hyphen
117                                       '\xc2\x97' => '--',       # Double hyphen
118                                       '\xc2\x99' => ' ',
119                                       '\xc2\xa0' => ' ',
120                                       '\xc2\xa6' => '|',        # Split vertical bar
121                                       '\xc2\xab' => '<<',       # Double less than
122                                       '\xc2\xbb' => '>>',       # Double greater than
123                                       '\xc2\xbc' => '1/4',      # one quarter
124                                       '\xc2\xbd' => '1/2',      # one half
125                                       '\xc2\xbe' => '3/4',      # three quarters
126                                       '\xca\xbf' => '\x27',     # c-single quote
127                                       '\xcc\xa8' => '',         # modifier - under curve
128                                       '\xcc\xb1' => ''          # modifier - under line
129                                    }
130             encoded_string = s.force_encoding('UTF-8').encode('UTF-16', :invalid => :replace, :replace => '', :fallback => transliteration_table).encode('UTF-8')
131             encoded_string.gsub /\r\n?/, "\n"
132         else
133             s.gsub /\r\n?/, "\n"
134         end
135     end
136
137     def self.find_url_and_path(file_path)
138         # Search file_path from the bottom up, at each level checking whether
139         # we've found a directory we know exists in the source tree.
140
141         dirname, basename = File.split(file_path)
142         dirname.split(/\//).reverse.inject(basename) do |path, directory|
143             path = directory + "/" + path
144
145             return [OPENSOURCE_TRAC_URL, path] if OPENSOURCE_DIRS.include?(directory)
146
147             path
148         end
149
150         [nil, file_path]
151     end
152
153     def self.linkifyFilename(filename)
154         url, pathBeneathTrunk = find_url_and_path(filename)
155
156         url.nil? ? filename : "<a href='#{url}browser/trunk/#{pathBeneathTrunk}'>#{filename}</a>"
157     end
158
159
160     HEADER =<<EOF
161 <html>
162 <head>
163 <style>
164 :link, :visited {
165     text-decoration: none;
166     border-bottom: 1px dotted;
167 }
168
169 :link {
170     color: #039;
171 }
172
173 .FileDiff {
174     background-color: #f8f8f8;
175     border: 1px solid #ddd;
176     font-family: monospace;
177     margin: 1em 0;
178     position: relative;
179 }
180
181 h1 {
182     color: #333;
183     font-family: sans-serif;
184     font-size: 1em;
185     margin-left: 0.5em;
186     display: table-cell;
187     width: 100%;
188     padding: 0.5em;
189 }
190
191 h1 :link, h1 :visited {
192     color: inherit;
193 }
194
195 h1 :hover {
196     color: #555;
197     background-color: #eee;
198 }
199
200 .DiffLinks {
201     float: right;
202 }
203
204 .FileDiffLinkContainer {
205     opacity: 0;
206     display: table-cell;
207     padding-right: 0.5em;
208     white-space: nowrap;
209 }
210
211 .DiffSection {
212     background-color: white;
213     border: solid #ddd;
214     border-width: 1px 0px;
215 }
216
217 .ExpansionLine, .LineContainer {
218     white-space: nowrap;
219 }
220
221 .sidebyside .DiffBlockPart.add:first-child {
222     float: right;
223 }
224
225 .LineSide,
226 .sidebyside .DiffBlockPart.remove,
227 .sidebyside .DiffBlockPart.add {
228     display:inline-block;
229     width: 50%;
230     vertical-align: top;
231 }
232
233 .sidebyside .resizeHandle {
234     width: 5px;
235     height: 100%;
236     cursor: move;
237     position: absolute;
238     top: 0;
239     left: 50%;
240 }
241
242 .sidebyside .resizeHandle:hover {
243     background-color: grey;
244     opacity: 0.5;
245 }
246
247 .sidebyside .DiffBlockPart.remove .to,
248 .sidebyside .DiffBlockPart.add .from {
249     display: none;
250 }
251
252 .lineNumber, .expansionLineNumber {
253     border-bottom: 1px solid #998;
254     border-right: 1px solid #ddd;
255     color: #444;
256     display: inline-block;
257     padding: 1px 5px 0px 0px;
258     text-align: right;
259     vertical-align: bottom;
260     width: 3em;
261 }
262
263 .lineNumber {
264   background-color: #eed;
265 }
266
267 .expansionLineNumber {
268   background-color: #eee;
269 }
270
271 .text {
272     padding-left: 5px;
273     white-space: pre-wrap;
274     word-wrap: break-word;
275 }
276
277 .image {
278     border: 2px solid black;
279 }
280
281 .context, .context .lineNumber {
282     color: #849;
283     background-color: #fef;
284 }
285
286 .Line.add, .FileDiff .add {
287     background-color: #dfd;
288 }
289
290 .Line.add ins {
291     background-color: #9e9;
292     text-decoration: none;
293 }
294
295 .Line.remove, .FileDiff .remove {
296     background-color: #fdd;
297 }
298
299 .Line.remove del {
300     background-color: #e99;
301     text-decoration: none;
302 }
303
304 /* Support for inline comments */
305
306 .author {
307   font-style: italic;
308 }
309
310 .comment {
311   position: relative;
312 }
313
314 .comment textarea {
315   height: 6em;
316 }
317
318 .overallComments textarea {
319   height: 2em;
320   max-width: 100%;
321   min-width: 200px;
322 }
323
324 .comment textarea, .overallComments textarea {
325   display: block;
326   width: 100%;
327 }
328
329 .overallComments .open {
330   -webkit-transition: height .2s;
331   height: 4em;
332 }
333
334 #statusBubbleContainer.wrap {
335   display: block;
336 }
337
338 #toolbar {
339   display: -webkit-flex;
340   display: -moz-flex;
341   padding: 3px;
342   left: 0;
343   right: 0;
344   border: 1px solid #ddd;
345   background-color: #eee;
346   font-family: sans-serif;
347   position: fixed;
348   bottom: 0;
349 }
350
351 #toolbar .actions {
352   float: right;
353 }
354
355 .winter {
356   position: fixed;
357   z-index: 5;
358   left: 0;
359   right: 0;
360   top: 0;
361   bottom: 0;
362   background-color: black;
363   opacity: 0.8;
364 }
365
366 .inactive {
367   display: none;
368 }
369
370 .lightbox {
371   position: fixed;
372   z-index: 6;
373   left: 10%;
374   right: 10%;
375   top: 10%;
376   bottom: 10%;
377   background: white;
378 }
379
380 .lightbox iframe {
381   width: 100%;
382   height: 100%;
383 }
384
385 .commentContext .lineNumber {
386   background-color: yellow;
387 }
388
389 .selected .lineNumber {
390   background-color: #69F;
391   border-bottom-color: #69F;
392   border-right-color: #69F;
393 }
394
395 .ExpandLinkContainer {
396   opacity: 0;
397   border-top: 1px solid #ddd;
398   border-bottom: 1px solid #ddd;
399 }
400
401 .ExpandArea {
402   margin: 0;
403 }
404
405 .ExpandText {
406   margin-left: 0.67em;
407 }
408
409 .LinkContainer {
410   font-family: sans-serif;
411   font-size: small;
412   font-style: normal;
413   -webkit-transition: opacity 0.5s;
414 }
415
416 .LinkContainer a {
417   border: 0;
418 }
419
420 .LinkContainer label:after,
421 .LinkContainer a:after {
422   content: " | ";
423   color: black;
424 }
425
426 .LinkContainer a:last-of-type:after {
427   content: "";
428 }
429
430 .LinkContainer label {
431   color: #039;
432 }
433
434 .help {
435  color: gray;
436  font-style: italic;
437 }
438
439 #message {
440   font-size: small;
441   font-family: sans-serif;
442 }
443
444 .commentStatus {
445   font-style: italic;
446 }
447
448 .comment, .previousComment, .frozenComment {
449   background-color: #ffd;
450 }
451
452 .overallComments {
453   -webkit-flex: 1;
454   -moz-flex: 1;
455   margin-right: 3px;
456 }
457
458 .previousComment, .frozenComment {
459   border: inset 1px;
460   padding: 5px;
461   white-space: pre-wrap;
462 }
463
464 .comment button {
465   width: 6em;
466 }
467
468 div:focus {
469   outline: 1px solid blue;
470   outline-offset: -1px;
471 }
472
473 .statusBubble {
474   /* The width/height get set to the bubble contents via postMessage on browsers that support it. */
475   width: 450px;
476   height: 20px;
477   margin: 2px 2px 0 0;
478   border: none;
479   vertical-align: middle;
480 }
481
482 .revision {
483   display: none;
484 }
485
486 .autosave-state {
487   position: absolute;
488   right: 0;
489   top: -1.3em;
490   padding: 0 3px;
491   outline: 1px solid #DDD;
492   color: #8FDF5F;
493   font-size: small;   
494   background-color: #EEE;
495 }
496
497 .autosave-state:empty {
498   outline: 0px;
499 }
500 .autosave-state.saving {
501   color: #E98080;
502 }
503
504 .clear_float {
505     clear: both;
506 }
507 </style>
508 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
509 <script src="code-review.js?version=46"></script>
510 </head>
511 EOF
512
513     def self.revisionOrDescription(string)
514         case string
515         when /\(revision \d+\)/
516             /\(revision (\d+)\)/.match(string)[1]
517         when /\(.*\)/
518             /\((.*)\)/.match(string)[1]
519         end
520     end
521
522     def self.has_image_suffix(filename)
523         filename =~ /\.(png|jpg|gif)$/
524     end
525
526     class FileDiff
527         def initialize(lines)
528             @filename = PrettyPatch.filename_from_diff_header(lines[0].chomp)
529             startOfSections = 1
530             for i in 0...lines.length
531                 case lines[i]
532                 when /^--- /
533                     @from = PrettyPatch.revisionOrDescription(lines[i])
534                 when /^\+\+\+ /
535                     @filename = PrettyPatch.filename_from_diff_header(lines[i].chomp) if @filename.nil?
536                     @to = PrettyPatch.revisionOrDescription(lines[i])
537                     startOfSections = i + 1
538                     break
539                 when BINARY_FILE_MARKER_FORMAT
540                     @binary = true
541                     if (IMAGE_FILE_MARKER_FORMAT.match(lines[i + 1]) or PrettyPatch.has_image_suffix(@filename)) then
542                         @image = true
543                         startOfSections = i + 2
544                         for x in startOfSections...lines.length
545                             # Binary diffs often have property changes listed before the actual binary data.  Skip them.
546                             if START_OF_BINARY_DATA_FORMAT.match(lines[x]) then
547                                 startOfSections = x
548                                 break
549                             end
550                         end
551                     end
552                     break
553                 when GIT_INDEX_MARKER_FORMAT
554                     @git_indexes = [$1, $2]
555                 when GIT_BINARY_FILE_MARKER_FORMAT
556                     @binary = true
557                     if (GIT_BINARY_PATCH_FORMAT.match(lines[i + 1]) and PrettyPatch.has_image_suffix(@filename)) then
558                         @git_image = true
559                         startOfSections = i + 1
560                     end
561                     break
562                 end
563             end
564             lines_with_contents = lines[startOfSections...lines.length]
565             @sections = DiffSection.parse(lines_with_contents) unless @binary
566             if @image and not lines_with_contents.empty?
567                 @image_url = "data:image/png;base64," + lines_with_contents.join
568                 @image_checksum = FileDiff.read_checksum_from_png(lines_with_contents.join.unpack("m").join)
569             elsif @git_image
570                 begin
571                     raise "index line is missing" unless @git_indexes
572
573                     chunks = nil
574                     for i in 0...lines_with_contents.length
575                         if lines_with_contents[i] =~ /^$/
576                             chunks = [lines_with_contents[i + 1 .. -1], lines_with_contents[0 .. i]]
577                             break
578                         end
579                     end
580
581                     raise "no binary chunks" unless chunks
582
583                     from_filepath = FileDiff.extract_contents_of_from_revision(@filename, chunks[0], @git_indexes[0])
584                     to_filepath = FileDiff.extract_contents_of_to_revision(@filename, chunks[1], @git_indexes[1], from_filepath, @git_indexes[0])
585                     filepaths = from_filepath, to_filepath
586
587                     binary_contents = filepaths.collect { |filepath| File.exists?(filepath) ? File.read(filepath) : nil }
588                     @image_urls = binary_contents.collect { |content| (content and not content.empty?) ? "data:image/png;base64," + [content].pack("m") : nil }
589                     @image_checksums = binary_contents.collect { |content| FileDiff.read_checksum_from_png(content) }
590                 rescue
591                     $last_prettify_part_count["extract-error"] += 1
592                     @image_error = "Exception raised during decoding git binary patch:<pre>#{CGI.escapeHTML($!.to_s + "\n" + $!.backtrace.join("\n"))}</pre>"
593                 ensure
594                     File.unlink(from_filepath) if (from_filepath and File.exists?(from_filepath))
595                     File.unlink(to_filepath) if (to_filepath and File.exists?(to_filepath))
596                 end
597             end
598             nil
599         end
600
601         def image_to_html
602             if not @image_url then
603                 return "<span class='text'>Image file removed</span>"
604             end
605
606             image_checksum = ""
607             if @image_checksum
608                 image_checksum = @image_checksum
609             elsif @filename.include? "-expected.png" and @image_url
610                 image_checksum = IMAGE_CHECKSUM_ERROR
611             end
612
613             return "<p>" + image_checksum + "</p><img class='image' src='" + @image_url + "' />"
614         end
615
616         def to_html
617             str = "<div class='FileDiff'>\n"
618             str += "<h1>#{PrettyPatch.linkifyFilename(@filename)}</h1>\n"
619             if @image then
620                 str += self.image_to_html
621             elsif @git_image then
622                 if @image_error
623                     str += @image_error
624                 else
625                     for i in (0...2)
626                         image_url = @image_urls[i]
627                         image_checksum = @image_checksums[i]
628
629                         style = ["remove", "add"][i]
630                         str += "<p class=\"#{style}\">"
631
632                         if image_checksum
633                             str += image_checksum
634                         elsif @filename.include? "-expected.png" and image_url
635                             str += IMAGE_CHECKSUM_ERROR
636                         end
637
638                         str += "<br>"
639
640                         if image_url
641                             str += "<img class='image' src='" + image_url + "' />"
642                         else
643                             str += ["</p>Added", "</p>Removed"][i]
644                         end
645                     end
646                 end
647             elsif @binary then
648                 $last_prettify_part_count["binary"] += 1
649                 str += "<span class='text'>Binary file, nothing to see here</span>"
650             else
651                 str += @sections.collect{ |section| section.to_html }.join("<br>\n") unless @sections.nil?
652             end
653
654             if @from then
655                 str += "<span class='revision'>" + @from + "</span>"
656             end
657
658             str += "</div>\n"
659         end
660
661         def self.parse(string)
662             haveSeenDiffHeader = false
663             linesForDiffs = []
664             string.each_line do |line|
665                 if (PrettyPatch.diff_header?(line))
666                     linesForDiffs << []
667                     haveSeenDiffHeader = true
668                 elsif (!haveSeenDiffHeader && line =~ /^--- /)
669                     linesForDiffs << []
670                     haveSeenDiffHeader = false
671                 end
672                 linesForDiffs.last << line unless linesForDiffs.last.nil?
673             end
674
675             linesForDiffs.collect { |lines| FileDiff.new(lines) }
676         end
677
678         def self.read_checksum_from_png(png_bytes)
679             # Ruby 1.9 added the concept of string encodings, so to avoid treating binary data as UTF-8,
680             # we can force the encoding to binary at this point.
681             if RUBY_VERSION >= "1.9"
682                 png_bytes.force_encoding('binary')
683             end
684             match = png_bytes && png_bytes.match(/tEXtchecksum\0([a-fA-F0-9]{32})/)
685             match ? match[1] : nil
686         end
687
688         def self.git_new_file_binary_patch(filename, encoded_chunk, git_index)
689             return <<END
690 diff --git a/#{filename} b/#{filename}
691 new file mode 100644
692 index 0000000000000000000000000000000000000000..#{git_index}
693 GIT binary patch
694 #{encoded_chunk.join("")}literal 0
695 HcmV?d00001
696
697 END
698         end
699
700         def self.git_changed_file_binary_patch(to_filename, from_filename, encoded_chunk, to_git_index, from_git_index)
701             return <<END
702 diff --git a/#{from_filename} b/#{to_filename}
703 copy from #{from_filename}
704 +++ b/#{to_filename}
705 index #{from_git_index}..#{to_git_index}
706 GIT binary patch
707 #{encoded_chunk.join("")}literal 0
708 HcmV?d00001
709
710 END
711         end
712
713         def self.get_svn_uri(repository_path)
714             "http://svn.webkit.org/repository/webkit/!svn/bc/" + $svn_revision.to_s + "/trunk/" + (repository_path)
715         end
716
717         def self.get_new_temp_filepath_and_name
718             tempfile = Tempfile.new("PrettyPatch")
719             filepath = tempfile.path + '.bin'
720             filename = File.basename(filepath)
721             return filepath, filename
722         end
723
724         def self.download_from_revision_from_svn(repository_path)
725             filepath, filename = get_new_temp_filepath_and_name
726             svn_uri = get_svn_uri(repository_path)
727             open(filepath, 'wb') do |to_file|
728                 to_file << open(svn_uri) { |from_file| from_file.read }
729             end
730             return filepath
731         end
732
733         def self.run_git_apply_on_patch(output_filepath, patch)
734             # Apply the git binary patch using git-apply.
735             cmd = GIT_PATH + " apply --directory=" + File.dirname(output_filepath)
736             stdin, stdout, stderr = *Open3.popen3(cmd)
737             begin
738                 stdin.puts(patch)
739                 stdin.close
740
741                 error = stderr.read
742                 if error != ""
743                     error = "Error running " + cmd + "\n" + "with patch:\n" + patch[0..500] + "...\n" + error
744                 end
745                 raise error if error != ""
746             ensure
747                 stdin.close unless stdin.closed?
748                 stdout.close
749                 stderr.close
750             end
751         end
752
753         def self.extract_contents_from_git_binary_literal_chunk(encoded_chunk, git_index)
754             filepath, filename = get_new_temp_filepath_and_name
755             patch = FileDiff.git_new_file_binary_patch(filename, encoded_chunk, git_index)
756             run_git_apply_on_patch(filepath, patch)
757             return filepath
758         end
759
760         def self.extract_contents_from_git_binary_delta_chunk(from_filepath, from_git_index, encoded_chunk, to_git_index)
761             to_filepath, to_filename = get_new_temp_filepath_and_name
762             from_filename = File.basename(from_filepath)
763             patch = FileDiff.git_changed_file_binary_patch(to_filename, from_filename, encoded_chunk, to_git_index, from_git_index)
764             run_git_apply_on_patch(to_filepath, patch)
765             return to_filepath
766         end
767
768         def self.extract_contents_of_from_revision(repository_path, encoded_chunk, git_index)
769             # For literal encoded, simply reconstruct.
770             if GIT_LITERAL_FORMAT.match(encoded_chunk[0])
771                 return extract_contents_from_git_binary_literal_chunk(encoded_chunk, git_index)
772             end
773             #  For delta encoded, download from svn.
774             if GIT_DELTA_FORMAT.match(encoded_chunk[0])
775                 return download_from_revision_from_svn(repository_path)
776             end
777             raise "Error: unknown git patch encoding"
778         end
779
780         def self.extract_contents_of_to_revision(repository_path, encoded_chunk, git_index, from_filepath, from_git_index)
781             # For literal encoded, simply reconstruct.
782             if GIT_LITERAL_FORMAT.match(encoded_chunk[0])
783                 return extract_contents_from_git_binary_literal_chunk(encoded_chunk, git_index)
784             end
785             # For delta encoded, reconstruct using delta and previously constructed 'from' revision.
786             if GIT_DELTA_FORMAT.match(encoded_chunk[0])
787                 return extract_contents_from_git_binary_delta_chunk(from_filepath, from_git_index, encoded_chunk, git_index)
788             end
789             raise "Error: unknown git patch encoding"
790         end
791     end
792
793     class DiffBlock
794         attr_accessor :parts
795
796         def initialize(container)
797             @parts = []
798             container << self
799         end
800
801         def to_html
802             str = "<div class='DiffBlock'>\n"
803             str += @parts.collect{ |part| part.to_html }.join
804             str += "<div class='clear_float'></div></div>\n"
805         end
806     end
807
808     class DiffBlockPart
809         attr_reader :className
810         attr :lines
811
812         def initialize(className, container)
813             $last_prettify_part_count[className] += 1
814             @className = className
815             @lines = []
816             container.parts << self
817         end
818
819         def to_html
820             str = "<div class='DiffBlockPart %s'>\n" % @className
821             str += @lines.collect{ |line| line.to_html }.join
822             # Don't put white-space after this so adjacent inline-block DiffBlockParts will not wrap.
823             str += "</div>"
824         end
825     end
826
827     class DiffSection
828         def initialize(lines)
829             lines.length >= 1 or raise "DiffSection.parse only received %d lines" % lines.length
830
831             matches = START_OF_SECTION_FORMAT.match(lines[0])
832
833             if matches
834                 from, to = [matches[1].to_i, matches[3].to_i]
835                 if matches[2] and matches[4]
836                     from_end = from + matches[2].to_i
837                     to_end = to + matches[4].to_i
838                 end
839             end
840
841             @blocks = []
842             diff_block = nil
843             diff_block_part = nil
844
845             for line in lines[1...lines.length]
846                 startOfLine = line =~ /^[-\+ ]/ ? 1 : 0
847                 text = line[startOfLine...line.length].chomp
848                 case line[0]
849                 when ?-
850                     if (diff_block_part.nil? or diff_block_part.className != 'remove')
851                         diff_block = DiffBlock.new(@blocks)
852                         diff_block_part = DiffBlockPart.new('remove', diff_block)
853                     end
854
855                     diff_block_part.lines << CodeLine.new(from, nil, text)
856                     from += 1 unless from.nil?
857                 when ?+
858                     if (diff_block_part.nil? or diff_block_part.className != 'add')
859                         # Put add lines that immediately follow remove lines into the same DiffBlock.
860                         if (diff_block.nil? or diff_block_part.className != 'remove')
861                             diff_block = DiffBlock.new(@blocks)
862                         end
863
864                         diff_block_part = DiffBlockPart.new('add', diff_block)
865                     end
866
867                     diff_block_part.lines << CodeLine.new(nil, to, text)
868                     to += 1 unless to.nil?
869                 else
870                     if (diff_block_part.nil? or diff_block_part.className != 'shared')
871                         diff_block = DiffBlock.new(@blocks)
872                         diff_block_part = DiffBlockPart.new('shared', diff_block)
873                     end
874
875                     diff_block_part.lines << CodeLine.new(from, to, text)
876                     from += 1 unless from.nil?
877                     to += 1 unless to.nil?
878                 end
879
880                 break if from_end and to_end and from == from_end and to == to_end
881             end
882
883             changes = [ [ [], [] ] ]
884             for block in @blocks
885                 for block_part in block.parts
886                     for line in block_part.lines
887                         if (!line.fromLineNumber.nil? and !line.toLineNumber.nil?) then
888                             changes << [ [], [] ]
889                             next
890                         end
891                         changes.last.first << line if line.toLineNumber.nil?
892                         changes.last.last << line if line.fromLineNumber.nil?
893                     end
894                 end
895             end
896
897             for change in changes
898                 next unless change.first.length == change.last.length
899                 for i in (0...change.first.length)
900                     from_text = change.first[i].text
901                     to_text = change.last[i].text
902                     next if from_text.length > MAXIMUM_INTRALINE_DIFF_LINE_LENGTH or to_text.length > MAXIMUM_INTRALINE_DIFF_LINE_LENGTH
903                     raw_operations = HTMLDiff::DiffBuilder.new(from_text, to_text).operations
904                     operations = []
905                     back = 0
906                     raw_operations.each_with_index do |operation, j|
907                         if operation.action == :equal and j < raw_operations.length - 1
908                            length = operation.end_in_new - operation.start_in_new
909                            if length < SMALLEST_EQUAL_OPERATION
910                                back = length
911                                next
912                            end
913                         end
914                         operation.start_in_old -= back
915                         operation.start_in_new -= back
916                         back = 0
917                         operations << operation
918                     end
919                     change.first[i].operations = operations
920                     change.last[i].operations = operations
921                 end
922             end
923
924             @blocks.unshift(ContextLine.new(matches[5])) unless matches.nil? || matches[5].empty?
925         end
926
927         def to_html
928             str = "<div class='DiffSection'>\n"
929             str += @blocks.collect{ |block| block.to_html }.join
930             str += "</div>\n"
931         end
932
933         def self.parse(lines)
934             linesForSections = lines.inject([[]]) do |sections, line|
935                 sections << [] if line =~ /^@@/
936                 sections.last << line
937                 sections
938             end
939
940             linesForSections.delete_if { |lines| lines.nil? or lines.empty? }
941             linesForSections.collect { |lines| DiffSection.new(lines) }
942         end
943     end
944
945     class Line
946         attr_reader :fromLineNumber
947         attr_reader :toLineNumber
948         attr_reader :text
949
950         def initialize(from, to, text)
951             @fromLineNumber = from
952             @toLineNumber = to
953             @text = text
954         end
955
956         def text_as_html
957             CGI.escapeHTML(text)
958         end
959
960         def classes
961             lineClasses = ["Line", "LineContainer"]
962             lineClasses << ["add"] unless @toLineNumber.nil? or !@fromLineNumber.nil?
963             lineClasses << ["remove"] unless @fromLineNumber.nil? or !@toLineNumber.nil?
964             lineClasses
965         end
966
967         def to_html
968             markedUpText = self.text_as_html
969             str = "<div class='%s'>\n" % self.classes.join(' ')
970             str += "<span class='from lineNumber'>%s</span><span class='to lineNumber'>%s</span>" %
971                    [@fromLineNumber.nil? ? '&nbsp;' : @fromLineNumber,
972                     @toLineNumber.nil? ? '&nbsp;' : @toLineNumber] unless @fromLineNumber.nil? and @toLineNumber.nil?
973             str += "<span class='text'>%s</span>\n" % markedUpText
974             str += "</div>\n"
975         end
976     end
977
978     class CodeLine < Line
979         attr :operations, true
980
981         def text_as_html
982             html = []
983             tag = @fromLineNumber.nil? ? "ins" : "del"
984             if @operations.nil? or @operations.empty?
985                 return CGI.escapeHTML(@text)
986             end
987             @operations.each do |operation|
988                 start = @fromLineNumber.nil? ? operation.start_in_new : operation.start_in_old
989                 eend = @fromLineNumber.nil? ? operation.end_in_new : operation.end_in_old
990                 escaped_text = CGI.escapeHTML(@text[start...eend])
991                 if eend - start === 0 or operation.action === :equal
992                     html << escaped_text
993                 else
994                     html << "<#{tag}>#{escaped_text}</#{tag}>"
995                 end
996             end
997             html.join
998         end
999     end
1000
1001     class ContextLine < Line
1002         def initialize(context)
1003             super("@", "@", context)
1004         end
1005
1006         def classes
1007             super << "context"
1008         end
1009     end
1010 end