gitweb: Refactor git_header_html

Extract the following parts into separate subroutines: * finding correct MIME content type for HTML pages (text/html or application/xhtml+xml?) into get_content_type_html() * printing <link ...> elements in HTML head into print_header_links() * printing navigation "breadcrumbs" for given action into print_nav_breadcrumbs() * printing search form into print_search_form() This reduces git_header_html to two pages long (53 lines), making gitweb code easier to read. Signed-off-by: Jakub Narebski <jnareb@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jakub Narebski committed Jun 22, 2011 at 13:50 UTC 6ee9033d6715db2f01aaf333a89e98b3b0079222
1 file changed +95 -77
gitweb/gitweb.perl
+95 -77
@@ -3683,6 +3683,20 @@ sub get_page_title {
3683 return $title;
3684 }
3685
3686 +sub get_content_type_html {
3687 + # require explicit support from the UA if we are to send the page as
3688 + # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3689 + # we have to do this because MSIE sometimes globs '*/*', pretending to
3690 + # support xhtml+xml but choking when it gets what it asked for.
3691 + if (defined $cgi->http('HTTP_ACCEPT') &&
3692 + $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3693 + $cgi->Accept('application/xhtml+xml') != 0) {
3694 + return 'application/xhtml+xml';
3695 + } else {
3696 + return 'text/html';
3697 + }
3698 +}
3699 +
3700 sub print_feed_meta {
3701 if (defined $project) {
3702 my %href_params = get_feed_info();
@@ -3728,24 +3742,90 @@ sub print_feed_meta {
3742 }
3743 }
3744
3745 +sub print_header_links {
3746 + my $status = shift;
3747 +
3748 + # print out each stylesheet that exist, providing backwards capability
3749 + # for those people who defined $stylesheet in a config file
3750 + if (defined $stylesheet) {
3751 + print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3752 + } else {
3753 + foreach my $stylesheet (@stylesheets) {
3754 + next unless $stylesheet;
3755 + print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3756 + }
3757 + }
3758 + print_feed_meta()
3759 + if ($status eq '200 OK');
3760 + if (defined $favicon) {
3761 + print qq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);
3762 + }
3763 +}
3764 +
3765 +sub print_nav_breadcrumbs {
3766 + my %opts = @_;
3767 +
3768 + print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
3769 + if (defined $project) {
3770 + print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
3771 + if (defined $action) {
3772 + my $action_print = $action ;
3773 + if (defined $opts{-action_extra}) {
3774 + $action_print = $cgi->a({-href => href(action=>$action)},
3775 + $action);
3776 + }
3777 + print " / $action_print";
3778 + }
3779 + if (defined $opts{-action_extra}) {
3780 + print " / $opts{-action_extra}";
3781 + }
3782 + print "\n";
3783 + }
3784 +}
3785 +
3786 +sub print_search_form {
3787 + if (!defined $searchtext) {
3788 + $searchtext = "";
3789 + }
3790 + my $search_hash;
3791 + if (defined $hash_base) {
3792 + $search_hash = $hash_base;
3793 + } elsif (defined $hash) {
3794 + $search_hash = $hash;
3795 + } else {
3796 + $search_hash = "HEAD";
3797 + }
3798 + my $action = $my_uri;
3799 + my $use_pathinfo = gitweb_check_feature('pathinfo');
3800 + if ($use_pathinfo) {
3801 + $action .= "/".esc_url($project);
3802 + }
3803 + print $cgi->startform(-method => "get", -action => $action) .
3804 + "<div class=\"search\">\n" .
3805 + (!$use_pathinfo &&
3806 + $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
3807 + $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
3808 + $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
3809 + $cgi->popup_menu(-name => 'st', -default => 'commit',
3810 + -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
3811 + $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
3812 + " search:\n",
3813 + $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
3814 + "<span title=\"Extended regular expression\">" .
3815 + $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
3816 + -checked => $search_use_regexp) .
3817 + "</span>" .
3818 + "</div>" .
3819 + $cgi->end_form() . "\n";
3820 +}
3821 +
3822 sub git_header_html {
3823 my $status = shift || "200 OK";
3824 my $expires = shift;
3825 my %opts = @_;
3826
3827 my $title = get_page_title();
3737 - my $content_type;
3738 - # require explicit support from the UA if we are to send the page as
3739 - # 'application/xhtml+xml', otherwise send it as plain old 'text/html'.
3740 - # we have to do this because MSIE sometimes globs '*/*', pretending to
3741 - # support xhtml+xml but choking when it gets what it asked for.
3742 - if (defined $cgi->http('HTTP_ACCEPT') &&
3743 - $cgi->http('HTTP_ACCEPT') =~ m/(,|;|\s|^)application\/xhtml\+xml(,|;|\s|$)/ &&
3744 - $cgi->Accept('application/xhtml+xml') != 0) {
3745 - $content_type = 'application/xhtml+xml';
3746 - } else {
3747 - $content_type = 'text/html';
3748 - }
3828 + my $content_type = get_content_type_html();
3829 print $cgi->header(-type=>$content_type, -charset => 'utf-8',
3830 -status=> $status, -expires => $expires)
3831 unless ($opts{'-no_http_header'});
@@ -3767,22 +3847,7 @@ EOF
3847 if ($ENV{'PATH_INFO'}) {
3848 print "<base href=\"".esc_url($base_url)."\" />\n";
3849 }
3770 - # print out each stylesheet that exist, providing backwards capability
3771 - # for those people who defined $stylesheet in a config file
3772 - if (defined $stylesheet) {
3773 - print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3774 - } else {
3775 - foreach my $stylesheet (@stylesheets) {
3776 - next unless $stylesheet;
3777 - print '<link rel="stylesheet" type="text/css" href="'.esc_url($stylesheet).'"/>'."\n";
3778 - }
3779 - }
3780 - print_feed_meta()
3781 - if ($status eq '200 OK');
3782 - if (defined $favicon) {
3783 - print qq(<link rel="shortcut icon" href=").esc_url($favicon).qq(" type="image/png" />\n);
3784 - }
3785 -
3850 + print_header_links($status);
3851 print "</head>\n" .
3852 "<body>\n";
3853
@@ -3799,59 +3864,12 @@ EOF
3864 -alt => "git",
3865 -class => "logo"}));
3866 }
3802 - print $cgi->a({-href => esc_url($home_link)}, $home_link_str) . " / ";
3803 - if (defined $project) {
3804 - print $cgi->a({-href => href(action=>"summary")}, esc_html($project));
3805 - if (defined $action) {
3806 - my $action_print = $action ;
3807 - if (defined $opts{-action_extra}) {
3808 - $action_print = $cgi->a({-href => href(action=>$action)},
3809 - $action);
3810 - }
3811 - print " / $action_print";
3812 - }
3813 - if (defined $opts{-action_extra}) {
3814 - print " / $opts{-action_extra}";
3815 - }
3816 - print "\n";
3817 - }
3867 + print_nav_breadcrumbs(%opts);
3868 print "</div>\n";
3869
3870 my $have_search = gitweb_check_feature('search');
3871 if (defined $project && $have_search) {
3822 - if (!defined $searchtext) {
3823 - $searchtext = "";
3824 - }
3825 - my $search_hash;
3826 - if (defined $hash_base) {
3827 - $search_hash = $hash_base;
3828 - } elsif (defined $hash) {
3829 - $search_hash = $hash;
3830 - } else {
3831 - $search_hash = "HEAD";
3832 - }
3833 - my $action = $my_uri;
3834 - my $use_pathinfo = gitweb_check_feature('pathinfo');
3835 - if ($use_pathinfo) {
3836 - $action .= "/".esc_url($project);
3837 - }
3838 - print $cgi->startform(-method => "get", -action => $action) .
3839 - "<div class=\"search\">\n" .
3840 - (!$use_pathinfo &&
3841 - $cgi->input({-name=>"p", -value=>$project, -type=>"hidden"}) . "\n") .
3842 - $cgi->input({-name=>"a", -value=>"search", -type=>"hidden"}) . "\n" .
3843 - $cgi->input({-name=>"h", -value=>$search_hash, -type=>"hidden"}) . "\n" .
3844 - $cgi->popup_menu(-name => 'st', -default => 'commit',
3845 - -values => ['commit', 'grep', 'author', 'committer', 'pickaxe']) .
3846 - $cgi->sup($cgi->a({-href => href(action=>"search_help")}, "?")) .
3847 - " search:\n",
3848 - $cgi->textfield(-name => "s", -value => $searchtext) . "\n" .
3849 - "<span title=\"Extended regular expression\">" .
3850 - $cgi->checkbox(-name => 'sr', -value => 1, -label => 're',
3851 - -checked => $search_use_regexp) .
3852 - "</span>" .
3853 - "</div>" .
3854 - $cgi->end_form() . "\n";
3872 + print_search_form();
3873 }
3874 }
3875