summaryrefslogtreecommitdiff
path: root/codegen/vulkan/scripts/htmldiff/htmldiff.pl
blob: af2a6cd290e2b1bbda3849ea9688e23c8452e652 (plain)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#!/usr/bin/perl
#
# htmldiff - present a diff marked version of two html documents
#
# Copyright (c) 1998-2006 MACS, Inc.
#
# Copyright (c) 2007 SiSco, Inc.
#
# SPDX-License-Identifier: MIT
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# See http://www.themacs.com for more information.
#
# usage: htmldiff [[-c] [-l] [-o] oldversion newversion [output]]
#
# -c - disable metahtml comment processing
# -o - disable outputting of old text
# -l - use navindex to create sequence of diffs
# oldversion - the previous version of the document
# newversion - the newer version of the document
# output - a filename to place the output in. If omitted, the output goes to
#          standard output.
#
# if invoked with no options or arguments, operates as a CGI script. It then
# takes the following parameters:
#
# oldfile - the URL of the original file
# newfile - the URL of the new file
# mhtml - a flag to indicate whether it should be aware of MetaHTML comments.
#
# requires GNU diff utility
# also requires the perl modules Getopt::Std
#
# NOTE: The markup created by htmldiff may not validate against the HTML 4.0
# DTD. This is because the algorithm is realtively simple, and there are
# places in the markup content model where the span element is not allowed.
# Htmldiff is NOT aware of these places.
#
# $Source: /u/sources/public/2009/htmldiff/htmldiff.pl,v $
# $Revision: 1.3 $
#
# $Log: htmldiff.pl,v $
# Revision 1.3  2016/10/24 15:06:51  dom
# Summary: Use nav script always
#
# Revision 1.2  2016/10/24 15:04:28  dom
# Add navigation script
#
# Revision 1.1  2014-01-06 08:04:51  dom
# added copy of htmldiff perl script since aptest.com repo no longer available
#
# Revision 1.5  2008/03/05 13:23:16  ahby
# Fixed a problem with leading whitespace before markup.
#
# Revision 1.4  2007/12/13 13:09:16  ahby
# Updated copyright and license.
#
# Revision 1.3  2007/12/13 12:53:34  ahby
# Changed use of span to ins and del
#
# Revision 1.2  2002/02/13 16:27:23  ahby
# Changed processing model.
# Improved handling of old text and changed styles.
#
# Revision 1.1  2000/07/12 12:20:04  ahby
# Updated to remove empty spans - this fixes validation problems under
# strict.
#
# Revision 1.11  1999/12/08 19:46:45  ahby
# Fixed validation errors introduced by placing markup where it didn't
# belong.
#
# Revision 1.10  1999/10/18 13:42:58  ahby
# Added -o to the usage message.
#
# Revision 1.9  1999/05/04 12:29:11  ahby
# Added an option to turn off the display of old text.
#
# Revision 1.8  1999/04/09 14:37:27  ahby
# Fixed a perl syntax error.
#
# Revision 1.7  1999/04/09 14:35:49  ahby
# Added reference to MACS homepage.
#
# Revision 1.6  1999/04/09 14:35:09  ahby
# Added comment about validity of generated markup.
#
# Revision 1.5  1999/02/22 22:17:54  ahby
# Changed to use stylesheets.
# Changed to rely upon span.
# Changed to work around content model problems.
#
# Revision 1.4  1999/02/08 02:32:22  ahby
# Added a copyright statement.
#
# Revision 1.3  1999/02/08 02:30:40  ahby
# Added header processing.
#
# Revision 1.2  1998/12/10 17:31:31  ahby
# Fixed to escape less-thans in change blocks and to not permit change
# markup within specific elements (like TITLE).
#
# Revision 1.1  1998/11/26 00:09:22  ahby
# Initial revision
#
#

use Getopt::Std;

sub usage {
	print STDERR "htmldiff [-c] [-o] oldversion newversion [output]\n";
	exit;
}

sub url_encode {
    my $str = shift;
    $str =~ s/([\x00-\x1f\x7F-\xFF])/
                 sprintf ('%%%02x', ord ($1))/eg;
    return $str;
}

# markit - diff-mark the streams
#
# markit(file1, file2)
#
# markit relies upon GNUdiff to mark up the text.
#
# The markup is encoded using special control sequences:
#
#   a block wrapped in control-a is deleted text
#   a block wrapped in control-b is old text
#   a block wrapped in control-c is new text
#
# The main processing loop attempts to wrap the text blocks in appropriate
# SPANs based upon the type of text that it is.
#
# When the loop encounters a < in the text, it stops the span. Then it outputs
# the element that is defined, then it restarts the span.

sub markit {
	my $retval = "";
	my($file1) = shift;
	my($file2) = shift;
#	my $old="<span class=\\\"diff-old-a\\\">deleted text: </span>%c'\012'%c'\001'%c'\012'%<%c'\012'%c'\001'%c'\012'";
	my $old="%c'\012'%c'\001'%c'\012'%<%c'\012'%c'\001'%c'\012'";
	my $new="%c'\012'%c'\003'%c'\012'%>%c'\012'%c'\003'%c'\012'";
	my $unchanged="%=";
	my $changed="%c'\012'%c'\001'%c'\012'%<%c'\012'%c'\001'%c'\012'%c'\004'%c'\012'%>%c'\012'%c'\004'%c'\012'";
	if ($opt_o) {
		$old = "";
		$changed = "%c'\012'%c'\004'%c'\012'%>%c'\012'%c'\004'%c'\012'";
	}
#	my $old="%c'\002'<font color=\\\"purple\\\" size=\\\"-2\\\">deleted text:</font><s>%c'\012'%c'\001'%c'\012'%<%c'\012'%c'\001'%c'\012'</s>%c'\012'%c'\002'";
#	my $new="%c'\002'<font color=\\\"purple\\\"><u>%c'\012'%c'\002'%>%c'\002'</u></font>%c'\002'%c'\012'";
#	my $unchanged="%=";
#	my $changed="%c'\002'<s>%c'\012'%c'\001'%c'\012'%<%c'\012'%c'\001'%c'\012'</s><font color=\\\"purple\\\"><u>%c'\002'%c'\012'%>%c'\012'%c'\002'</u></font>%c'\002'%c'\012'";

	my @span;
	$span[0]="</span>";
	$span[1]="<del class=\"diff-old\">";
	$span[2]="<del class=\"diff-old\">";
	$span[3]="<ins class=\"diff-new\">";
	$span[4]="<ins class=\"diff-chg\">";

	my @diffEnd ;
	$diffEnd[1] = '</del>';
	$diffEnd[2] = '</del>';
	$diffEnd[3] = '</ins>';
	$diffEnd[4] = '</ins>';

	my $diffcounter = 0;

	open(FILE, qq(diff -d --old-group-format="$old" --new-group-format="$new" --changed-group-format="$changed" --unchanged-group-format="$unchanged" $file1 $file2 |)) || die("Diff failed: $!");
#	system (qq(diff --old-group-format="$old" --new-group-format="$new" --changed-group-format="$changed" --unchanged-group-format="$unchanged" $file1 $file2 > /tmp/output));

	my $state = 0;
	my $inblock = 0;
	my $temp = "";
	my $lineCount = 0;

# strategy:
#
# process the output of diff...
#
# a link with control A-D means the start/end of the corresponding ordinal
# state (1-4). Resting state is state 0.
#
# While in a state, accumulate the contents for that state. When exiting the
# state, determine if it is appropriate to emit the contents with markup or
# not (basically, if the accumulated buffer contains only empty lines or lines
# with markup, then we don't want to emit the wrappers.  We don't need them.
#
# Note that if there is markup in the "old" block, that markup is silently
# removed.  It isn't really that interesting, and it messes up the output
# something fierce.

	while (<FILE>) {
		my $nextCounter = $diffcounter + 1;
		my $anchor = $opt_l ? qq[<a tabindex="$diffcounter" id="diff-$diffcounter" href="#diff-$nextCounter">] : "" ;
		my $anchorEnd = $opt_l ? q[</a>] : "" ;
		$lineCount ++;
		if ($state == 0) {	# if we are resting and we find a marker,
							# then we must be entering a block
			if (m/^([\001-\004])/) {
				$state = ord($1);
				$_ = "";
			}
#			if (m/^\001/) {
#				$state = 1;
#				s/^/$span[1]/;
#			} elsif (m/^\002/) {
#				$state = 2;
#				s/^/$span[2]/;
#			} elsif (m/^\003/) {
#				$state = 3;
#				s/^/$span[3]/;
#			} elsif (m/^\004/) {
#				$state = 4;
#				s/^/$span[4]/;
#			}
		} else {
			# if we are in "old" state, remove markup
			if (($state == 1) || ($state == 2)) {
				s/\<.*\>//;	# get rid of any old markup
				s/\</&lt;/g; # escape any remaining STAG or ETAGs
				s/\>/&gt;/g;
			}
			# if we found another marker, we must be exiting the state
			if (m/^([\001-\004])/) {
				if ($temp ne "") {
					$_ = $span[$state] . $anchor . $temp . $anchorEnd . $diffEnd[$state] . "\n";
					$temp = "";
					$diffcounter++;
				} else {
					$_ = "" ;
				}
				$state = 0;
			} elsif (m/^\s*\</) { # otherwise, is this line markup?
				# if it is markup AND we haven't seen anything else yet,
				# then we will emit the markup
				if ($temp eq "") {
					$retval .= $_;
					$_ = "";
				} else {	# we wrap it with the state switches and hold it
					s/^/$anchorEnd$diffEnd[$state]/;
					s/$/$span[$state]$anchor/;
					$temp .= $_;
					$_ = "";
					$diffcounter++;
				}
			} else {
				if (m/.+/) {
					$temp .= $_;
					$_ = "";
				}
			}
		}

		s/\001//g;
		s/\002//g;
		s/\003//g;
		s/\004//g;
		if ($_ !~ m/^$/) {
			$retval .= $_;
		}
	}
	close FILE;
	$retval =~ s/$span[1]\n+$diffEnd[1]//g;
	$retval =~ s/$span[2]\n+$diffEnd[2]//g;
	$retval =~ s/$span[3]\n+$diffEnd[3]//g;
	$retval =~ s/$span[4]\n+$diffEnd[4]//g;
	$retval =~ s/$span[1]\n*$//g;
	$retval =~ s/$span[2]\n*$//g;
	$retval =~ s/$span[3]\n*$//g;
	$retval =~ s/$span[4]\n*$//g;
	return $retval;
}

sub splitit {
	my $filename = shift;
	my $headertmp = shift;
	my $inheader=0;
	my $preformatted=0;
	my $inelement=0;
	my $retval = "";
	my $styles = q(<style type='text/css'>
.diff-old-a {
  font-size: smaller;
  color: red;
}
.diff-new a { text-decoration: none; }
.diff-new { background-color: yellow; }
.diff-chg { background-color: lime; }
.diff-chg a { text-decoration: none; }
.diff-new:before,
.diff-new:after
    { content: "\2191" }
.diff-chg:before, .diff-chg:after
    { content: "\2195" }
.diff-old { text-decoration: line-through; background-color: #FBB; }
.diff-old:before,
.diff-old:after
    { content: "\2193" }
.diff-old a { text-decoration: none; }
:focus { border: thin red solid}
</style>
<script src="https://www.w3.org/2016/10/htmldiff-nav.js"></script>);
	if ($opt_t) {
		$styles .= q(
<script type="text/javascript">
<!--
function setOldDisplay() {
	for ( var s = 0; s < document.styleSheets.length; s++ ) {
		var css = document.styleSheets[s];
		var mydata ;
		try { mydata = css.cssRules ;
		if ( ! mydata ) mydata = css.rules;
		for ( var r = 0; r < mydata.length; r++ ) {
			if ( mydata[r].selectorText == '.diff-old' ) {
				mydata[r].style.display = ( mydata[r].style.display == '' ) ? 'none'
: '';
				return;
			}
		}
		} catch(e) {} ;
	}
}
-->
</script>
);

	}

	if ($stripheader) {
		open(HEADER, ">$headertmp");
	}

	my $incomment = 0;
	my $inhead = 1;
	open(FILE, $filename) || die("File $filename cannot be opened: $!");
	while (<FILE>) {
		if ($inhead == 1) {
			if (m/\<\/head/i) {
				print HEADER $styles;
			}
			if (m/\<body/i) {
				$inhead = 0;
				print HEADER;
				if ($opt_t) {
					print HEADER q(
<form action=""><input type="button" onclick="setOldDisplay()" value="Show/Hide Old Content" /></form>
);
				}
				if ($opt_l) {
					print HEADER q(
						<p><em>NOTE: Click highlighted diff text to jump to the following difference.</em></p>
					);
				}
				close HEADER;
			} else {
				print HEADER;
			}
		} else {
			if ($incomment) {
				if (m;-->;) {
					$incomment = 0;
					s/.*-->//;
				} else {
					next;
				}
			}
			if (m;<!--;) {
				while (m;<!--.*-->;) {
					s/<!--.*?-->//;
				}
				if (m;<!--; ) {
					$incomment = 1;
					s/<!--.*//;
				}
			}
			if (m/\<pre/i) {
				$preformatted = 1;
			}
			if (m/\<\/pre\>/i) {
				$preformatted = 0;
			}
			if ($preformatted) {
				$retval .= $_;
			} elsif ($mhtmlcomments && /^;;;/) {
				$retval .= $_;
			} else {
				my @list = split(' ');
				foreach $element (@list) {
					if ($element =~ m/\<H[1-6]/i) {
#						$inheader = 1;
					}
					if ($inheader == 0) {
						$element =~ s/</\n</g;
						$element =~ s/^\n//;
						$element =~ s/>/>\n/g;
						$element =~ s/\n$//;
						$element =~ s/>\n([.,:!]+)/>$1/g;
					}
					if ($element =~ m/\<\/H[1-6]\>/i) {
						$inheader = 0;
					}
					$retval .= "$element";
					$inelement += ($element =~ s/</&lt;/g);
					$inelement -= ($element =~ s/>/&gt;/g);
					if ($inelement < 0) {
						$inelement = 0;
					}
					if (($inelement == 0) && ($inheader == 0)) {
						$retval .= "\n";
					} else {
						$retval .= " ";
					}
				}
			undef @list;
			}
		}
	}
	$retval .= "\n";
	close FILE;
	return $retval;
}

$mhtmlcomments = 1;

sub cli {
	getopts("clto") || usage();

	if ($opt_c) {$mhtmlcomments = 0;}

	if (@ARGV < 2) { usage(); }

	$file1 = $ARGV[0];
	$file2 = $ARGV[1];
	$file3 = $ARGV[2];

	$tmp = splitit($file1, $headertmp1);
	open (FILE, ">$tmp1");
	print FILE $tmp;
	close FILE;

	$tmp = splitit($file2, $headertmp2);
	open (FILE, ">$tmp2");
	print FILE $tmp;
	close FILE;

	$output = "";

	if ($stripheader) {
		open(FILE, $headertmp2);
		while (<FILE>) {
			$output .= $_;
		}
		close(FILE);
	}

	$output .= markit($tmp1, $tmp2);

	if ($file3) {
		open(FILE, ">$file3");
		print FILE $output;
		close FILE;
	} else {
		print $output;
	}
}

sub cgi {
#	use LWP::UserAgent;
#	use CGI;

	my $query = new CGI;
	my $url1 = $query->param("oldfile");
	my $url2 = $query->param("newfile");
	my $mhtml = $query->param("mhtml");

	my $file1 = "/tmp/htdcgi1.$$";
	my $file2 = "/tmp/htdcgi2.$$";

	my $ua = new LWP::UserAgent;
	$ua->agent("MACS, Inc. HTMLdiff/0.9 " . $ua->agent);

	# Create a request

	my $req1 = new HTTP::Request GET => $url1;

	my $res1 = $ua->request($req1, $file1);
	if ($res1->is_error) {
		print $res1->error_as_HTML();
		print "<p>The URL $url1 could not be found.  Please check it and try again.</p>";
		return;
	}

	my $req2 = new HTTP::Request GET => $url2;

	my $res2 = $ua->request($req2, $file2);
	if ($res2->is_error) {
		print $res2->error_as_HTML();
		print "<p>The URL $url2 could not be found.  Please check it and try again.</p>";
		return;
	}

	$split1 = splitit($file1, $headertmp1);
	open (FILE, ">$tmp1");
	print FILE $split1;
	close FILE;

	$split2 = splitit($file2, $headertmp2);
	open (FILE, ">$tmp2");
	print FILE $split2;
	close FILE;

	$output = "";

	if ($stripheader) {
		open(FILE, $headertmp2);
		while (<FILE>) {
			$output .= $_;
		}
		close(FILE);
	}

	$output .= markit($tmp1, $tmp2);

	my $base=$res2->base;

	if ($base !~ /\/$/) {
		$base =~ s/[^\/]*$//;
	}

	if ( $output !~ /<base/i ) {
		$output =~ s/<head>/<head>\n<base href="$base">/i ||
	  	$output =~ s/<html>/<html>\n<base href="$base">/i ;
	}

	print $query->header(-type=>'text/html',-nph=>1);
	print $output;

	unlink $file1;
	unlink $file2;

}

$tmp1="/tmp/htdtmp1.$$";
$headertmp1="/tmp/htdhtmp1.$$";
$tmp2="/tmp/htdtmp2.$$";
$headertmp2="/tmp/htdhtmp2.$$";
$stripheader = 1;

if (@ARGV == 0) {
	cgi();		# if no arguments, we must be operating as a cgi script
} else {
	cli();		# if there are arguments, then we are operating as a CLI
}

unlink $tmp1;
unlink $headertmp1;
unlink $tmp2;
unlink $headertmp2;