]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/WWW.pm
view: display replies in per-message view
[public-inbox.git] / lib / PublicInbox / WWW.pm
index 2d8f2afc0a61350f5c67e10a7204e2af65aee2f5..32cc0b27be365c25b85f6b28393ec412c44ec556 100644 (file)
@@ -130,13 +130,19 @@ sub get_index {
 # just returns a string ref for the blob in the current ctx
 sub mid2blob {
        my ($ctx) = @_;
-       require Digest::SHA;
-       my $hex = Digest::SHA::sha1_hex($ctx->{mid});
-       $hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/i or
-                       die "BUG: not a SHA-1 hex: $hex";
+       my $hex = $ctx->{mid};
+       my ($x2, $x38) = ($hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
+
+       unless (defined $x38) {
+               # compatibility with old links
+               require Digest::SHA;
+               $hex = Digest::SHA::sha1_hex($hex);
+               ($x2, $x38) = ($hex =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
+               defined $x38 or die "BUG: not a SHA-1 hex: $hex";
+       }
 
        my @cmd = ('git', "--git-dir=$ctx->{git_dir}",
-                       qw(cat-file blob), "HEAD:$1/$2");
+                       qw(cat-file blob), "HEAD:$x2/$x38");
        my $cmd = join(' ', @cmd);
        my $pid = open my $fh, '-|';
        defined $pid or die "fork failed: $!\n";
@@ -169,8 +175,10 @@ sub get_mid_html {
        my $pfx = "../f/$mid_href.html";
        my $foot = footer($ctx);
        require Email::MIME;
+       my $mime = Email::MIME->new($x);
+       my $srch = searcher($ctx);
        [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
-         [ PublicInbox::View->msg_html(Email::MIME->new($x), $pfx, $foot) ] ];
+         [ PublicInbox::View->msg_html($mime, $pfx, $foot, $srch) ] ];
 }
 
 # /$LISTNAME/f/$MESSAGE_ID.html                   -> HTML content (fullquotes)
@@ -179,10 +187,12 @@ sub get_full_html {
        my $x = mid2blob($ctx);
        return r404() unless $x;
        require PublicInbox::View;
-       require Email::MIME;
        my $foot = footer($ctx);
+       require Email::MIME;
+       my $mime = Email::MIME->new($x);
+       my $srch = searcher($ctx);
        [ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
-         [ PublicInbox::View->msg_html(Email::MIME->new($x), undef, $foot)] ];
+         [ PublicInbox::View->msg_html($mime, undef, $foot, $srch)] ];
 }
 
 sub self_url {
@@ -252,10 +262,10 @@ sub footer {
        if ($nurls == 0) {
                $urls = '($GIT_DIR/cloneurl missing)';
        } elsif ($nurls == 1) {
-               $urls = 'git URL for <a href="' . SSOMA_URL .
+               $urls = "git URL for <a\nhref=\"" . SSOMA_URL .
                        '">ssoma</a>: ' . $urls[0];
        } else {
-               $urls = 'git URLs for <a href="' . SSOMA_URL .
+               $urls = "git URLs for <a\nhref=\"" . SSOMA_URL .
                        "\">ssoma</a>:\n" . join("\n", map { "\t$_" } @urls);
        }
 
@@ -264,15 +274,25 @@ sub footer {
                $addr = $addr->[0]; # first address is primary
        }
 
-       $addr = "<a href=\"mailto:$addr\">$addr</a>";
+       $addr = "<a\nhref=\"mailto:$addr\">$addr</a>";
        $desc =  $desc;
        join("\n",
                '- ' . $desc,
-               'A <a href="' . PI_URL .  '">public-inbox</a>, ' .
+               "A <a\nhref=\"" . PI_URL .  '">public-inbox</a>, ' .
                        'anybody may post in plain-text (not HTML):',
                $addr,
                $urls
        );
 }
 
+# search support is optional, returns undef if Xapian is not installed
+# or not configured for the given GIT_DIR
+sub searcher {
+       my ($ctx) = @_;
+       eval {
+               require PublicInbox::Search;
+               PublicInbox::Search->new($ctx->{git_dir});
+       };
+}
+
 1;