gitweb: Extract formatting of diff chunk header

Refactor main parts of HTML-formatting for diff chunk headers
(formatting means here adding links and syntax hightlighting) into
separate subroutines:

 * format_unidiff_chunk_header for ordinary diff,
 * format_cc_diff_chunk_header for combined diff
   (more than one parent)

This makes format_diff_line() subroutine easier to follow.

Signed-off-by: Jakub Narebski <jnareb@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
Jakub Narebski 2011-10-31 00:36:21 +01:00 committed by Junio C Hamano
parent 20a864cd83
commit f1310cf5e7

View File

@ -2254,19 +2254,11 @@ sub diff_line_class {
return ""; return "";
} }
# format patch (diff) line (not to be used for diff headers) # assumes that $from and $to are defined and correctly filled,
sub format_diff_line { # and that $line holds a line of chunk header for unified diff
my $line = shift; sub format_unidiff_chunk_header {
my ($from, $to) = @_; my ($line, $from, $to) = @_;
my $diff_class = diff_line_class($line, $from, $to);
my $diff_classes = "diff";
$diff_classes .= " $diff_class" if ($diff_class);
chomp $line;
$line = untabify($line);
if ($from && $to && $line =~ m/^\@{2} /) {
my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) = my ($from_text, $from_start, $from_lines, $to_text, $to_start, $to_lines, $section) =
$line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/; $line =~ m/^\@{2} (-(\d+)(?:,(\d+))?) (\+(\d+)(?:,(\d+))?) \@{2}(.*)$/;
@ -2283,8 +2275,14 @@ sub format_diff_line {
} }
$line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" . $line = "<span class=\"chunk_info\">@@ $from_text $to_text @@</span>" .
"<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>"; "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
return "<div class=\"$diff_classes\">$line</div>\n"; return $line;
} elsif ($from && $to && $line =~ m/^\@{3}/) { }
# assumes that $from and $to are defined and correctly filled,
# and that $line holds a line of chunk header for combined diff
sub format_cc_diff_chunk_header {
my ($line, $from, $to) = @_;
my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/; my ($prefix, $ranges, $section) = $line =~ m/^(\@+) (.*?) \@+(.*)$/;
my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines); my (@from_text, @from_start, @from_nlines, $to_text, $to_start, $to_nlines);
@ -2316,7 +2314,29 @@ sub format_diff_line {
} }
$line .= " $prefix</span>" . $line .= " $prefix</span>" .
"<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>"; "<span class=\"section\">" . esc_html($section, -nbsp=>1) . "</span>";
return $line;
}
# format patch (diff) line (not to be used for diff headers)
sub format_diff_line {
my $line = shift;
my ($from, $to) = @_;
my $diff_class = diff_line_class($line, $from, $to);
my $diff_classes = "diff";
$diff_classes .= " $diff_class" if ($diff_class);
chomp $line;
$line = untabify($line);
if ($from && $to && $line =~ m/^\@{2} /) {
$line = format_unidiff_chunk_header($line, $from, $to);
return "<div class=\"$diff_classes\">$line</div>\n"; return "<div class=\"$diff_classes\">$line</div>\n";
} elsif ($from && $to && $line =~ m/^\@{3}/) {
$line = format_cc_diff_chunk_header($line, $from, $to);
return "<div class=\"$diff_classes\">$line</div>\n";
} }
return "<div class=\"$diff_classes\">" . esc_html($line, -nbsp=>1) . "</div>\n"; return "<div class=\"$diff_classes\">" . esc_html($line, -nbsp=>1) . "</div>\n";
} }