1package Text::WordDiff::HTMLTwoLines;
2
3use strict;
4use HTML::Entities qw(encode_entities);
5use vars qw($VERSION @ISA);
6
7$VERSION = '0.08';
8@ISA = qw(Text::WordDiff::Base);
9
10sub file_header {
11	my $self = shift;
12	my $fn1 = $self->filename_a;
13	my $fn2 = $self->filename_b;
14
15	if (defined $fn1 && defined $fn2)
16	{	my $p1 = $self->filename_prefix_a;
17		my $t1 = $self->mtime_a;
18		my $p2 = $self->filename_prefix_b;
19		my $t2 = $self->mtime_b;
20
21		$self->{__str1} = '<div class="file"><span class="fileheader">'
22		. "$p1 $fn1" . (defined $t1 ? " " . localtime $t1 : '') . '</span>';
23
24		$self->{__str2} = '<div class="file"><span class="fileheader">'
25		. "$p2 $fn2" . (defined $t2 ? " " . localtime $t2 : '') . '</span>';
26	}
27	else
28	{	$self->{__str1} = $self->{__str2} = '<div class="file">';
29	}
30	return '';
31}
32
33sub hunk_header {
34	my $self = shift;
35	$self->{__str1} .= '<span class="hunk">';
36	$self->{__str2} .= '<span class="hunk">';
37	return '';
38}
39sub hunk_footer {
40	my $self = shift;
41	$self->{__str1} .= '</span>';
42	$self->{__str2} .= '</span>';
43	return '';
44}
45
46sub file_footer {
47	my $self = shift;
48	return $self->{__str1} . "</div>\n" . $self->{__str2} . "</div>\n";
49}
50
51sub same_items {
52	my $self = shift;
53	$self->{__str1} .= encode_entities( join '', @_ );
54	$self->{__str2} .= encode_entities( join '', @_ );
55	return '';
56}
57
58sub delete_items {
59	my $self = shift;
60	$self->{__str1} .= '<del>' . encode_entities( join '', @_ ) . '</del>';
61	return '';
62}
63
64sub insert_items {
65	my $self = shift;
66	$self->{__str2} .= '<ins>' . encode_entities( join '', @_ ) . '</ins>';
67	return '';
68}
69
701;
71
72__END__
73
74=head1 Name
75
76Text::WordDiff::HTMLTwoLines - XHTML formatting for Text::WordDiff with content on two lines
77
78=head1 Synopsis
79
80    use Text::WordDiff;
81
82    my $diff = word_diff 'file1.txt', 'file2.txt';  { STYLE => 'HTMLTwoLines' };
83    my $diff = word_diff \$string1,   \$string2,    { STYLE => 'HTMLTwoLines' };
84    my $diff = word_diff \*FH1,       \*FH2,        { STYLE => 'HTMLTwoLines' };
85    my $diff = word_diff \&reader1,   \&reader2,    { STYLE => 'HTMLTwoLines' };
86    my $diff = word_diff \@records1,  \@records2,   { STYLE => 'HTMLTwoLines' };
87
88    # May also mix input types:
89    my $diff = word_diff \@records1,  'file_B.txt', { STYLE => 'HTMLTwoLines' };
90
91=head1 Description
92
93This class subclasses Text::WordDiff::Base to provide a XHTML formatting for
94Text::WordDiff. See L<Term::WordDiff|Term::WordDiff> for usage details. This
95class should never be used directly.
96
97Text::WordDiff::HTMLTwoLines formats word diffs for viewing in a Web browser.
98The output is similar to that produced by
99L<Term::WordDiff::HTML|Term::WordDiff::HTML> but the two lines (or files,
100records, etc.) are shown separately, with deleted items highlighted in the
101first line and inserted items highlighted in the second. HTMLTwoLines puts a
102span tag around each word or set of words in the diff.
103
104The diff content is highlighted as follows:
105
106=over
107
108=item * C<< <div class="file"> >>
109
110The inputs to C<word_diff()> are each contained in a div element of class
111"file". All the following results are subsumed by these elements.
112
113=over
114
115=item * C<< <span class="fileheader"> >>
116
117The header section for the files being C<diff>ed, usually something like:
118
119  --- in.txt	Thu Sep  1 12:51:03 2005
120
121for the first file, and
122
123  +++ out.txt	Thu Sep  1 12:52:12 2005
124
125for the second.
126
127This element immediately follows the opening "file" C<< <div> >> element, but
128will not be present if Text::WordDiff cannot determine the file names for both
129files being compared.
130
131=item * C<< <span class="hunk"> >>
132
133This element contains a single diff "hunk". Each hunk may contain the
134following elements:
135
136=over
137
138=item * C<< <ins> >>
139
140Inserted content.
141
142=item * C<< <del> >>
143
144Deleted content.
145
146=back
147
148=back
149
150=back
151
152You may do whatever you like with these elements and classes; I highly
153recommend that you style them using CSS. You'll find an example CSS file in
154the F<eg> directory in the Text-WordDiff distribution.
155
156=head1 See Also
157
158=over
159
160=item L<Text::WordDiff|Text::WordDiff>
161
162=item L<Text::WordDiff::ANSIColor|Text::WordDiff::HTML>
163
164=item L<Text::WordDiff::ANSIColor|Text::WordDiff::ANSIColor>
165
166=back
167
168=head1 Author
169
170Amelia Ireland <join(".", $firstname, $lastname) . "@gmail.com">
171
172=head1 Copyright and License
173
174Copyright (c) 2011 Amelia Ireland. Some Rights Reserved.
175
176This module is free software; you can redistribute it and/or modify it under the
177same terms as Perl itself.
178
179=cut
180