]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/ViewVCS.pm
viewvcs: wire up syntax-highlighting for blobs
[public-inbox.git] / lib / PublicInbox / ViewVCS.pm
index 90c09078beb4bd759c47430a4434fce1faf0b360..a8aa0b61e2934b96b3fd4eff8fdba151017a074d 100644 (file)
@@ -21,43 +21,43 @@ use PublicInbox::SolverGit;
 use PublicInbox::WwwStream;
 use PublicInbox::Linkify;
 use PublicInbox::Hval qw(ascii_html to_filename);
+my $hl = eval {
+       require PublicInbox::HlMod;
+       PublicInbox::HlMod->new;
+};
+
 my %QP_MAP = ( A => 'oid_a', B => 'oid_b', a => 'path_a', b => 'path_b' );
 my $max_size = 1024 * 1024; # TODO: configurable
 my $enc_utf8 = find_encoding('UTF-8');
 
 sub html_page ($$$) {
        my ($ctx, $code, $strref) = @_;
-       $ctx->{-upfx} = '../'; # from "/$INBOX/$OID/s"
-       PublicInbox::WwwStream->response($ctx, $code, sub {
+       my $wcb = delete $ctx->{-wcb};
+       $ctx->{-upfx} = '../../'; # from "/$INBOX/$OID/s/"
+       my $res = PublicInbox::WwwStream->response($ctx, $code, sub {
                my ($nr, undef) =  @_;
                $nr == 1 ? $$strref : undef;
        });
+       $wcb->($res);
 }
 
-sub show ($$;$) {
-       my ($ctx, $oid_b, $fn) = @_;
-       my $ibx = $ctx->{-inbox};
-       my $inboxes = [ $ibx ];
-       my $solver = PublicInbox::SolverGit->new($ibx->{-repo_objs}, $inboxes);
-       my $qp = $ctx->{qp};
-       my $hints = {};
-       while (my ($from, $to) = each %QP_MAP) {
-               defined(my $v = $qp->{$from}) or next;
-               $hints->{$to} = $v;
-       }
-
-       open my $log, '+>', undef or die "open: $!";
-       my $res = $solver->solve($log, $oid_b, $hints);
+sub solve_result {
+       my ($ctx, $res, $log, $hints, $fn) = @_;
 
-       seek($log, 0, 0) or die "seek: $!";
+       unless (seek($log, 0, 0)) {
+               $ctx->{env}->{'psgi.errors'}->print("seek(log): $!\n");
+               return html_page($ctx, 500, \'seek error');
+       }
        $log = do { local $/; <$log> };
 
+       my $ref = ref($res);
        my $l = PublicInbox::Linkify->new;
        $l->linkify_1($log);
        $log = '<pre>debug log:</pre><hr /><pre>' .
                $l->linkify_2(ascii_html($log)) . '</pre>';
 
        $res or return html_page($ctx, 404, \$log);
+       $ref eq 'ARRAY' or return html_page($ctx, 500, \$log);
 
        my ($git, $oid, $type, $size, $di) = @$res;
        if ($size > $max_size) {
@@ -78,11 +78,11 @@ sub show ($$;$) {
        if ($fn) {
                my $h = [ 'Content-Length', $size, 'Content-Type' ];
                push(@$h, ($binary ? 'application/octet-stream' : 'text/plain'));
-               return [ 200, $h, [ $$blob ]];
+               return delete($ctx->{-wcb})->([200, $h, [ $$blob ]]);
        }
 
        my $path = to_filename($di->{path_b} || $hints->{path_b} || 'blob');
-       my $raw_link = "(<a\nhref=_$path>raw</a>)";
+       my $raw_link = "(<a\nhref=$path>raw</a>)";
        if ($binary) {
                $log = "<pre>$oid $type $size bytes (binary)" .
                        " $raw_link</pre>" . $log;
@@ -93,6 +93,14 @@ sub show ($$;$) {
        my $nl = ($$blob =~ tr/\n/\n/);
        my $pad = length($nl);
 
+       $l->linkify_1($$blob);
+       my $ok = $hl->do_hl($blob, $path) if $hl;
+       if ($ok) {
+               $blob = $ok;
+       } else {
+               $$blob = ascii_html($$blob);
+       }
+
        # using some of the same CSS class names and ids as cgit
        $log = "<pre>$oid $type $size bytes $raw_link</pre>" .
                "<hr /><table\nclass=blob>".
@@ -100,10 +108,32 @@ sub show ($$;$) {
                        sprintf("<a id=n$_ href=#n$_>% ${pad}u</a>\n", $_)
                } (1..$nl)) . '</pre></td>' .
                '<td><pre> </pre></td>'. # pad for non-CSS users
-               "<td\nclass=lines><pre><code>" .  ascii_html($$blob) .
+               "<td\nclass=lines><pre\nstyle='white-space:pre'><code>" .
+               $l->linkify_2($$blob) .
                '</code></pre></td></tr></table>' . $log;
 
        html_page($ctx, 200, \$log);
 }
 
+sub show ($$;$) {
+       my ($ctx, $oid_b, $fn) = @_;
+       my $qp = $ctx->{qp};
+       my $hints = {};
+       while (my ($from, $to) = each %QP_MAP) {
+               defined(my $v = $qp->{$from}) or next;
+               $hints->{$to} = $v;
+       }
+
+       open my $log, '+>', undef or die "open: $!";
+       my $solver = PublicInbox::SolverGit->new($ctx->{-inbox}, sub {
+               solve_result($ctx, $_[0], $log, $hints, $fn);
+       });
+
+       # PSGI server will call this and give us a callback
+       sub {
+               $ctx->{-wcb} = $_[0]; # HTTP write callback
+               $solver->solve($ctx->{env}, $log, $oid_b, $hints);
+       };
+}
+
 1;