1 | #!/usr/bin/perl -w
|
---|
2 | # diffs two files and writes an HTML output file.
|
---|
3 | use strict;
|
---|
4 | use CGI qw(:standard :html3);
|
---|
5 | use Algorithm::Diff 'traverse_sequences';
|
---|
6 | use Text::Tabs;
|
---|
7 |
|
---|
8 | my ( @a, @b );
|
---|
9 |
|
---|
10 | # Take care of whitespace.
|
---|
11 | sub preprocess
|
---|
12 | {
|
---|
13 | my $arrayRef = shift;
|
---|
14 | chomp(@$arrayRef);
|
---|
15 | @$arrayRef = expand(@$arrayRef);
|
---|
16 | }
|
---|
17 |
|
---|
18 | # This will be called with both lines are the same
|
---|
19 | sub match
|
---|
20 | {
|
---|
21 | my ( $ia, $ib ) = @_;
|
---|
22 | print pre( $a[$ia] ), "\n";
|
---|
23 | }
|
---|
24 |
|
---|
25 | # This will be called when there is a line in A that isn't in B
|
---|
26 | sub only_a
|
---|
27 | {
|
---|
28 | my ( $ia, $ib ) = @_;
|
---|
29 | print pre( { -class => 'onlyA' }, $a[$ia] ), "\n";
|
---|
30 | }
|
---|
31 |
|
---|
32 | # This will be called when there is a line in B that isn't in A
|
---|
33 | sub only_b
|
---|
34 | {
|
---|
35 | my ( $ia, $ib ) = @_;
|
---|
36 | print pre( { -class => 'onlyB' }, $b[$ib] ), "\n";
|
---|
37 | }
|
---|
38 |
|
---|
39 | # MAIN PROGRAM
|
---|
40 |
|
---|
41 | # Check for two arguments.
|
---|
42 | print "usage: $0 file1 file2 > diff.html\n" if @ARGV != 2;
|
---|
43 |
|
---|
44 | $tabstop = 4; # For Text::Tabs
|
---|
45 |
|
---|
46 | # Read each file into an array.
|
---|
47 | open FH, $ARGV[0];
|
---|
48 | @a = <FH>;
|
---|
49 | close FH;
|
---|
50 |
|
---|
51 | open FH, $ARGV[1];
|
---|
52 | @b = <FH>;
|
---|
53 | close FH;
|
---|
54 |
|
---|
55 | # Expand whitespace
|
---|
56 | preprocess( \@a );
|
---|
57 | preprocess( \@b );
|
---|
58 |
|
---|
59 | # inline style
|
---|
60 | my $style = <<EOS;
|
---|
61 | PRE {
|
---|
62 | margin-left: 24pt;
|
---|
63 | font-size: 12pt;
|
---|
64 | font-family: Courier, monospaced;
|
---|
65 | white-space: pre
|
---|
66 | }
|
---|
67 | PRE.onlyA { color: red }
|
---|
68 | PRE.onlyB { color: blue }
|
---|
69 | EOS
|
---|
70 |
|
---|
71 | # Print out the starting HTML
|
---|
72 | print
|
---|
73 |
|
---|
74 | # header(),
|
---|
75 | start_html(
|
---|
76 | {
|
---|
77 | -title => "$ARGV[0] vs. $ARGV[1]",
|
---|
78 | -style => { -code => $style }
|
---|
79 | }
|
---|
80 | ),
|
---|
81 | h1(
|
---|
82 | { -style => 'margin-left: 24pt' },
|
---|
83 | span( { -style => 'color: red' }, $ARGV[0] ),
|
---|
84 | span(" <i>vs.</i> "),
|
---|
85 | span( { -style => 'color: blue' }, $ARGV[1] )
|
---|
86 | ),
|
---|
87 | "\n";
|
---|
88 |
|
---|
89 | # And compare the arrays
|
---|
90 | traverse_sequences(
|
---|
91 | \@a, # first sequence
|
---|
92 | \@b, # second sequence
|
---|
93 | {
|
---|
94 | MATCH => \&match, # callback on identical lines
|
---|
95 | DISCARD_A => \&only_a, # callback on A-only
|
---|
96 | DISCARD_B => \&only_b, # callback on B-only
|
---|
97 | }
|
---|
98 | );
|
---|
99 |
|
---|
100 | print end_html();
|
---|