blib/lib/Text/Diff/HTML.pm | |||
---|---|---|---|
Criterion | Covered | Total | % |
statement | 40 | 40 | 100.0 |
branch | 6 | 8 | 75.0 |
condition | n/a | ||
subroutine | 12 | 12 | 100.0 |
pod | 0 | 5 | 0.0 |
total | 58 | 65 | 89.2 |
line | stmt | bran | cond | sub | pod | time | code |
---|---|---|---|---|---|---|---|
1 | package Text::Diff::HTML; | ||||||
2 | |||||||
3 | 1 | 1 | 73106 | use strict; | |||
1 | 2 | ||||||
1 | 46 | ||||||
4 | 1 | 1 | 5 | use vars qw(@ISA $VERSION); | |||
1 | 1 | ||||||
1 | 257 | ||||||
5 | 1 | 1 | 1292 | use HTML::Entities; | |||
1 | 21694 | ||||||
1 | 115 | ||||||
6 | 1 | 1 | 1876 | use Text::Diff (); # Just to be safe. | |||
1 | 18319 | ||||||
1 | 15058 | ||||||
7 | |||||||
8 | $VERSION = '0.07'; | ||||||
9 | @ISA = qw(Text::Diff::Unified); | ||||||
10 | |||||||
11 | sub file_header { | ||||||
12 | 1 | 1 | 0 | 690 | return ' ' |
||
13 | . encode_entities(shift->SUPER::file_header(@_)) | ||||||
14 | . ''; | ||||||
15 | } | ||||||
16 | |||||||
17 | sub hunk_header { | ||||||
18 | 1 | 1 | 0 | 452 | return ' ' |
||
19 | . encode_entities(shift->SUPER::hunk_header(@_)) | ||||||
20 | . ''; | ||||||
21 | } | ||||||
22 | |||||||
23 | sub hunk_footer { | ||||||
24 | 1 | 1 | 0 | 372 | return ' | ||
25 | . encode_entities(shift->SUPER::hunk_footer(@_)) | ||||||
26 | . ''; | ||||||
27 | } | ||||||
28 | |||||||
29 | sub file_footer { | ||||||
30 | 1 | 1 | 0 | 427 | return ' | ||
31 | . encode_entities(shift->SUPER::file_footer(@_)) | ||||||
32 | . ''; | ||||||
33 | } | ||||||
34 | |||||||
35 | # Each of the items in $seqs is an array reference. The first one has the | ||||||
36 | # contents of the first file and the second has the contents of the second | ||||||
37 | # file, all broken into hunks. $ops is an array reference of array references, | ||||||
38 | # one corresponding to each of the hunks in the sequences. | ||||||
39 | # | ||||||
40 | # The contents of each op in $ops tell us what to do with each hunk. Each op | ||||||
41 | # can have up to four items: | ||||||
42 | # | ||||||
43 | # 0: The index of the relevant hunk in the first file sequence. | ||||||
44 | # 1: The index of the relevant hunk in the second file sequence. | ||||||
45 | # 2: The opcode for the hunk, either '+', '-', or ' '. | ||||||
46 | # 3: A flag; not sure what this is, doesn't seem to apply to unified diffs. | ||||||
47 | # | ||||||
48 | # So what we do is figure out which op we have and output the relevant span | ||||||
49 | # element if it is different from the last op. Then we select the hunk from | ||||||
50 | # second sequence (SEQ_B_IDX) if it's '+' and the first sequence (SEQ_A_IDX) | ||||||
51 | # otherwise, and then output the opcode and the hunk. | ||||||
52 | |||||||
53 | 1 | 1 | 43 | use constant OPCODE => 2; # "-", " ", "+" | |||
1 | 4 | ||||||
1 | 184 | ||||||
54 | 1 | 1 | 7 | use constant SEQ_A_IDX => 0; | |||
1 | 2 | ||||||
1 | 54 | ||||||
55 | 1 | 1 | 10 | use constant SEQ_B_IDX => 1; | |||
1 | 2 | ||||||
1 | 807 | ||||||
56 | |||||||
57 | my %code_map = ( | ||||||
58 | '+' => [ 'ins' => 'ins' ], | ||||||
59 | '-' => [ 'del' => 'del' ], | ||||||
60 | ' ' => [ 'span class="ctx"' => 'span' ] | ||||||
61 | ); | ||||||
62 | |||||||
63 | sub hunk { | ||||||
64 | 1 | 1 | 0 | 482 | shift; | ||
65 | 1 | 4 | my $seqs = [ shift, shift ]; | ||||
66 | 1 | 2 | my $ops = shift; | ||||
67 | 1 | 50 | 4 | return unless @$ops; | |||
68 | |||||||
69 | # Start the span element for the first opcode. | ||||||
70 | 1 | 2 | my $last = $ops->[0][ OPCODE ]; | ||||
71 | 1 | 5 | my $hunk = qq{<$code_map{ $last }->[0]>}; | ||||
72 | |||||||
73 | # Output each line of the hunk. | ||||||
74 | 1 | 5 | while (my $op = shift @$ops) { | ||||
75 | 4 | 56 | my $opcode = $op->[OPCODE]; | ||||
76 | 4 | 50 | 11 | my $elem = $code_map{ $opcode } or next; | |||
77 | |||||||
78 | # Close the last span and start a new one for a new opcode. | ||||||
79 | 4 | 100 | 10 | if ($opcode ne $last) { | |||
80 | 2 | 6 | $hunk .= "$code_map{ $last }->[1]><$elem->[0]>"; | ||||
81 | 2 | 4 | $last = $opcode; | ||||
82 | } | ||||||
83 | |||||||
84 | # Output the appropriate line. | ||||||
85 | 4 | 100 | 10 | my $idx = $opcode ne '+' ? SEQ_A_IDX : SEQ_B_IDX; | |||
86 | 4 | 17 | $hunk .= encode_entities("$opcode $seqs->[$idx][$op->[$idx]]"); | ||||
87 | } | ||||||
88 | |||||||
89 | 1 | 18 | return $hunk . "$code_map{ $last }->[1]>"; | ||||
90 | } | ||||||
91 | |||||||
92 | 1; | ||||||
93 | __END__ |