gitweb: refactor author name insertion
Collect all author display code in appropriate functions, making it easier to extend these functions on the CGI side. We also move some of the presentation code from hard-coded HTML to CSS, for easier customization. A side effect of the refactoring is that now localtime is always displayed with the 'at night' warning. Signed-off-by: Giuseppe Bilotta <giuseppe.bilotta@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Giuseppe Bilotta committed
Jun 30, 2009 at 00:00 UTC
1c49a4e1f324dcaa000ce92ed44d0e5b9eb16843
2 files changed
+59
-39
gitweb/gitweb.css
+4
-1
@@ -132,11 +132,14 @@ div.list_head {
132
font-style: italic;
133
}
134
135
+.author_date, .author {
136
+ font-style: italic;
137
+}
138
+
139
div.author_date {
140
padding: 8px;
141
border: solid #d9d8d1;
142
border-width: 0px 0px 1px 0px;
139
- font-style: italic;
143
}
144
145
a.list {
gitweb/gitweb.perl
+55
-38
@@ -1469,6 +1469,16 @@ sub format_subject_html {
1469
}
1470
}
1471
1472
+# format the author name of the given commit with the given tag
1473
+# the author name is chopped and escaped according to the other
1474
+# optional parameters (see chop_str).
1475
+sub format_author_html {
1476
+ my $tag = shift;
1477
+ my $co = shift;
1478
+ my $author = chop_and_escape_str($co->{'author_name'}, @_);
1479
+ return "<$tag class=\"author\">" . $author . "</$tag>";
1480
+}
1481
+
1482
# format git diff header line, i.e. "diff --(git|combined|cc) ..."
1483
sub format_git_diff_header_line {
1484
my $line = shift;
@@ -3214,21 +3224,50 @@ sub git_print_header_div {
3224
"\n</div>\n";
3225
}
3226
3227
+sub print_local_time {
3228
+ my %date = @_;
3229
+ if ($date{'hour_local'} < 6) {
3230
+ printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3231
+ $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3232
+ } else {
3233
+ printf(" (%02d:%02d %s)",
3234
+ $date{'hour_local'}, $date{'minute_local'}, $date{'tz_local'});
3235
+ }
3236
+}
3237
+
3238
+# Outputs the author name and date in long form
3239
sub git_print_authorship {
3240
my $co = shift;
3241
+ my %opts = @_;
3242
+ my $tag = $opts{-tag} || 'div';
3243
3244
my %ad = parse_date($co->{'author_epoch'}, $co->{'author_tz'});
3221
- print "<div class=\"author_date\">" .
3245
+ print "<$tag class=\"author_date\">" .
3246
esc_html($co->{'author_name'}) .
3247
" [$ad{'rfc2822'}";
3224
- if ($ad{'hour_local'} < 6) {
3225
- printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
3226
- $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3227
- } else {
3228
- printf(" (%02d:%02d %s)",
3229
- $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
3248
+ print_local_time(%ad) if ($opts{-localtime});
3249
+ print "]</$tag>\n";
3250
+}
3251
+
3252
+# Outputs table rows containing the full author or committer information,
3253
+# in the format expected for 'commit' view (& similia).
3254
+# Parameters are a commit hash reference, followed by the list of people
3255
+# to output information for. If the list is empty it defalts to both
3256
+# author and committer.
3257
+sub git_print_authorship_rows {
3258
+ my $co = shift;
3259
+ # too bad we can't use @people = @_ || ('author', 'committer')
3260
+ my @people = @_;
3261
+ @people = ('author', 'committer') unless @people;
3262
+ foreach my $who (@people) {
3263
+ my %wd = parse_date($co->{"${who}_epoch"}, $co->{"${who}_tz"});
3264
+ print "<tr><td>$who</td><td>" . esc_html($co->{$who}) . "</td></tr>\n".
3265
+ "<tr>" .
3266
+ "<td></td><td> $wd{'rfc2822'}";
3267
+ print_local_time(%wd);
3268
+ print "</td>" .
3269
+ "</tr>\n";
3270
}
3231
- print "]</div>\n";
3271
}
3272
3273
sub git_print_page_path {
@@ -4142,11 +4181,9 @@ sub git_shortlog_body {
4181
print "<tr class=\"light\">\n";
4182
}
4183
$alternate ^= 1;
4145
- my $author = chop_and_escape_str($co{'author_name'}, 10);
4184
# git_summary() used print "<td><i>$co{'age_string'}</i></td>\n" .
4185
print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4148
- "<td><i>" . $author . "</i></td>\n" .
4149
- "<td>";
4186
+ format_author_html('td', \%co, 10) . "<td>";
4187
print format_subject_html($co{'title'}, $co{'title_short'},
4188
href(action=>"commit", hash=>$commit), $ref);
4189
print "</td>\n" .
@@ -4193,11 +4230,9 @@ sub git_history_body {
4230
print "<tr class=\"light\">\n";
4231
}
4232
$alternate ^= 1;
4196
- # shortlog uses chop_str($co{'author_name'}, 10)
4197
- my $author = chop_and_escape_str($co{'author_name'}, 15, 3);
4233
print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4199
- "<td><i>" . $author . "</i></td>\n" .
4200
- "<td>";
4234
+ # shortlog: format_author_html('td', \%co, 10)
4235
+ format_author_html('td', \%co, 15, 3) . "<td>";
4236
# originally git_history used chop_str($co{'title'}, 50)
4237
print format_subject_html($co{'title'}, $co{'title_short'},
4238
href(action=>"commit", hash=>$commit), $ref);
@@ -4350,9 +4385,8 @@ sub git_search_grep_body {
4385
print "<tr class=\"light\">\n";
4386
}
4387
$alternate ^= 1;
4353
- my $author = chop_and_escape_str($co{'author_name'}, 15, 5);
4388
print "<td title=\"$co{'age_string_age'}\"><i>$co{'age_string_date'}</i></td>\n" .
4355
- "<td><i>" . $author . "</i></td>\n" .
4389
+ format_author_html('td', \%co, 15, 5) .
4390
"<td>" .
4391
$cgi->a({-href => href(action=>"commit", hash=>$co{'id'}),
4392
-class => "list subject"},
@@ -5094,9 +5128,9 @@ sub git_log {
5128
" | " .
5129
$cgi->a({-href => href(action=>"tree", hash=>$commit, hash_base=>$commit)}, "tree") .
5130
"<br/>\n" .
5097
- "</div>\n" .
5098
- "<i>" . esc_html($co{'author_name'}) . " [$ad{'rfc2822'}]</i><br/>\n" .
5131
"</div>\n";
5132
+ git_print_authorship(\%co, -tag => 'span');
5133
+ print "<br/>\n</div>\n";
5134
5135
print "<div class=\"log_body\">\n";
5136
git_print_log($co{'comment'}, -final_empty_line=> 1);
@@ -5115,8 +5149,6 @@ sub git_commit {
5149
$hash ||= $hash_base || "HEAD";
5150
my %co = parse_commit($hash)
5151
or die_error(404, "Unknown commit object");
5118
- my %ad = parse_date($co{'author_epoch'}, $co{'author_tz'});
5119
- my %cd = parse_date($co{'committer_epoch'}, $co{'committer_tz'});
5152
5153
my $parent = $co{'parent'};
5154
my $parents = $co{'parents'}; # listref
@@ -5183,22 +5215,7 @@ sub git_commit {
5215
}
5216
print "<div class=\"title_text\">\n" .
5217
"<table class=\"object_header\">\n";
5186
- print "<tr><td>author</td><td>" . esc_html($co{'author'}) . "</td></tr>\n".
5187
- "<tr>" .
5188
- "<td></td><td> $ad{'rfc2822'}";
5189
- if ($ad{'hour_local'} < 6) {
5190
- printf(" (<span class=\"atnight\">%02d:%02d</span> %s)",
5191
- $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
5192
- } else {
5193
- printf(" (%02d:%02d %s)",
5194
- $ad{'hour_local'}, $ad{'minute_local'}, $ad{'tz_local'});
5195
- }
5196
- print "</td>" .
5197
- "</tr>\n";
5198
- print "<tr><td>committer</td><td>" . esc_html($co{'committer'}) . "</td></tr>\n";
5199
- print "<tr><td></td><td> $cd{'rfc2822'}" .
5200
- sprintf(" (%02d:%02d %s)", $cd{'hour_local'}, $cd{'minute_local'}, $cd{'tz_local'}) .
5201
- "</td></tr>\n";
5218
+ git_print_authorship_rows(\%co);
5219
print "<tr><td>commit</td><td class=\"sha1\">$co{'id'}</td></tr>\n";
5220
print "<tr>" .
5221
"<td>tree</td>" .
@@ -5579,7 +5596,7 @@ sub git_commitdiff {
5596
git_header_html(undef, $expires);
5597
git_print_page_nav('commitdiff','', $hash,$co{'tree'},$hash, $formats_nav);
5598
git_print_header_div('commit', esc_html($co{'title'}) . $ref, $hash);
5582
- git_print_authorship(\%co);
5599
+ git_print_authorship(\%co, -localtime => 1);
5600
print "<div class=\"page_body\">\n";
5601
if (@{$co{'comment'}} > 1) {
5602
print "<div class=\"log\">\n";