source: other-projects/diffcol/trunk/diffcol/Algorithm/htmldiff.pl@ 21711

Last change on this file since 21711 was 21711, checked in by oranfry, 14 years ago

bringing across the diffcol project

File size: 1.8 KB
Line 
1#!/usr/bin/perl -w
2# diffs two files and writes an HTML output file.
3use strict;
4use CGI qw(:standard :html3);
5use Algorithm::Diff 'traverse_sequences';
6use Text::Tabs;
7
8my ( @a, @b );
9
10# Take care of whitespace.
11sub 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
19sub 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
26sub 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
33sub 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.
42print "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.
47open FH, $ARGV[0];
48@a = <FH>;
49close FH;
50
51open FH, $ARGV[1];
52@b = <FH>;
53close FH;
54
55# Expand whitespace
56preprocess( \@a );
57preprocess( \@b );
58
59# inline style
60my $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 }
69EOS
70
71# Print out the starting HTML
72print
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
90traverse_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
100print end_html();
Note: See TracBrowser for help on using the repository browser.