]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/View.pm
dead code cleanup
[public-inbox.git] / lib / PublicInbox / View.pm
index 4f2833b3f4531aa7ad650bcff03d64da3d1ef069..e29922ef9e37747a9dd4ee8e56f98fab664967ff 100644 (file)
@@ -33,8 +33,9 @@ sub msg_html {
        }
        headers_to_html_header($mime, $full_pfx, $srch) .
                multipart_text_as_html($mime, $full_pfx) .
-               '</pre><hr />' . PRE_WRAP .
-               html_footer($mime, 1, $full_pfx, $srch) . $footer .
+               '</pre><hr /><pre>' .
+               html_footer($mime, 1, $full_pfx, $srch) .
+               $footer .
                '</pre></body></html>';
 }
 
@@ -51,7 +52,6 @@ sub index_entry {
        my ($srch, $seen, $first_commit) = @$state;
        my $midx = $state->[3]++;
        my ($prev, $next) = ($midx - 1, $midx + 1);
-       my $rv = '';
        my $part_nr = 0;
        my $enc_msg = enc_for($mime->header("Content-Type"));
        my $subj = $mime->header('Subject');
@@ -69,15 +69,9 @@ sub index_entry {
 
        $from = PublicInbox::Hval->new_oneline($from)->as_html;
        $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
-       my $pfx = ('  ' x $level);
        my $root_anchor = $seen->{root_anchor};
-       my $path;
        my $more = 'permalink';
-       if ($root_anchor) {
-               $path = '../';
-       } else {
-               $path = '';
-       }
+       my $path = $root_anchor ? '../' : '';
        my $href = $mid->as_href;
        my $irt = $header_obj->header_raw('In-Reply-To');
        my ($anchor_idx, $anchor, $t_anchor);
@@ -88,7 +82,6 @@ sub index_entry {
        } else {
                $t_anchor = '';
        }
-
        if (defined $srch) {
                $subj = "<a\nhref=\"${path}t/$href.html#u\">$subj</a>";
        }
@@ -97,27 +90,39 @@ sub index_entry {
        }
 
        my $ts = $mime->header('X-PI-TS');
-       my $fmt = '%Y-%m-%d %H:%M UTC';
+       unless (defined $ts) {
+               $ts = msg_timestamp($mime);
+       }
+       my $fmt = '%Y-%m-%d %H:%M';
        $ts = POSIX::strftime($fmt, gmtime($ts));
 
-       $rv .= "$pfx<b\nid=\"$id\">$subj</b>\n$pfx";
-       $rv .= "- by $from @ $ts - ";
+       my $rv = "<table\nsummary=l$level><tr>";
+       if ($level) {
+               $rv .= '<td><pre>' . ('  ' x $level) . '</pre></td>';
+       }
+       $rv .= '<td>' . PRE_WRAP;
+       $rv .= "<b\nid=\"$id\">$subj</b>\n";
+       $rv .= "- by $from @ $ts UTC - ";
        $rv .= "<a\nid=\"s$midx\"\nhref=\"#s$next\">next</a>";
        if ($prev >= 0) {
                $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
        }
        $rv .= "\n\n";
 
+       my ($fhref, $more_ref);
        my $mhref = "${path}m/$href.html";
-       my $fhref = "${path}f/$href.html";
+       if ($level > 0) {
+               $fhref = "${path}f/$href.html";
+               $more_ref = \$more;
+       }
        # scan through all parts, looking for displayable text
        $mime->walk_parts(sub {
-               $rv .= index_walk($_[0], $pfx, $enc_msg, $part_nr, $fhref,
-                                 \$more);
+               $rv .= index_walk($_[0], $enc_msg, $part_nr, $fhref, $more_ref);
                $part_nr++;
        });
+       $mime->body_set('');
 
-       $rv .= "\n$pfx<a\nhref=\"$mhref\">$more</a> ";
+       $rv .= "\n<a\nhref=\"$mhref\">$more</a> ";
        my $txt = "${path}m/$href.txt";
        $rv .= "<a\nhref=\"$txt\">raw</a> ";
        $rv .= html_footer($mime, 0);
@@ -137,7 +142,7 @@ sub index_entry {
                       "threadlink</a>";
        }
 
-       $rv . "\n\n";
+       $rv .= '</pre></td></tr></table>';
 }
 
 sub thread_html {
@@ -145,15 +150,16 @@ sub thread_html {
        my $mid = mid_compressed($ctx->{mid});
        my $res = $srch->get_thread($mid);
        my $rv = '';
-       my $msgs = load_results($ctx, $res);
+       my $msgs = load_results($res);
        my $nr = scalar @$msgs;
        return $rv if $nr == 0;
-       require PublicInbox::Thread;
-       my $th = PublicInbox::Thread->new(@$msgs);
-       $th->thread;
-       $th->order(*PublicInbox::Thread::sort_ts);
+       my $th = thread_results($msgs);
        my $state = [ $srch, { root_anchor => anchor_for($mid) }, undef, 0 ];
-       thread_entry(\$rv, $state, $_, 0) for $th->rootset;
+       {
+               require PublicInbox::GitCatFile;
+               my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
+               thread_entry(\$rv, $git, $state, $_, 0) for $th->rootset;
+       }
        my $final_anchor = $state->[3];
        my $next = "<a\nid=\"s$final_anchor\">";
 
@@ -162,71 +168,55 @@ sub thread_html {
        } else {
                $next .= 'end of thread';
        }
-       $next .= ", back to <a\nhref=\"../\">index</a>\n";
-
-       $rv .= "</pre><hr />" . PRE_WRAP . $next . $foot . "</pre>";
-}
-
-sub subject_path_html {
-       my (undef, $ctx, $foot, $srch) = @_;
-       my $path = $ctx->{subject_path};
-       my $res = $srch->get_subject_path($path);
-       my $rv = '';
-       my $msgs = load_results($ctx, $res);
-       my $nr = scalar @$msgs;
-       return $rv if $nr == 0;
-       require PublicInbox::Thread;
-       my $th = PublicInbox::Thread->new(@$msgs);
-       $th->thread;
-       $th->order(*PublicInbox::Thread::sort_ts);
-       my $state = [ $srch, { root_anchor => 'dummy' }, undef, 0 ];
-       thread_entry(\$rv, $state, $_, 0) for $th->rootset;
-       my $final_anchor = $state->[3];
-       my $next = "<a\nid=\"s$final_anchor\">end of thread</a>\n";
+       $next .= "</a>, back to <a\nhref=\"../\">index</a>\n";
 
-       $rv .= "</pre><hr />" . PRE_WRAP . $next . $foot . "</pre>";
+       $rv .= "<hr />" . PRE_WRAP . $next . $foot . "</pre>";
 }
 
 # only private functions below.
 
 sub index_walk {
-       my ($part, $pfx, $enc_msg, $part_nr, $fhref, $more) = @_;
+       my ($part, $enc_msg, $part_nr, $fhref, $more) = @_;
        my $rv = '';
        return $rv if $part->subparts; # walk_parts already recurses
        my $ct = $part->content_type;
 
        # account for filter bugs...
-       return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
+       if (defined $ct && $ct =~ m!\btext/[xh]+tml\b!i) {
+               $part->body_set('');
+               return '';
+       }
 
        my $enc = enc_for($ct, $enc_msg);
 
        if ($part_nr > 0) {
                my $fn = $part->filename;
                defined($fn) or $fn = "part #" . ($part_nr + 1);
-               $rv .= $pfx . add_filename_line($enc->decode($fn));
+               $rv .= add_filename_line($enc->decode($fn));
        }
 
-       my $s = add_text_body_short($enc, $part, $part_nr, $fhref);
+       my $s = add_text_body($enc, $part, $part_nr, $fhref);
 
-       # drop the remainder of git patches, they're usually better
-       # to review when the full message is viewed
-       $s =~ s!^---+\n.*\z!!ms and $$more = 'more...';
+       if ($more) {
+               # drop the remainder of git patches, they're usually better
+               # to review when the full message is viewed
+               $s =~ s!^---+\n.*\z!!ms and $$more = 'more...';
 
-       # Drop signatures
-       $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
+               # Drop signatures
+               $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
+       }
 
        # kill any leading or trailing whitespace lines
        $s =~ s/^\s*$//sgm;
        $s =~ s/\s+\z//s;
 
        if (length $s) {
-               # add prefix:
-               $s =~ s/^/$pfx/sgm;
-
                # kill per-line trailing whitespace
                $s =~ s/[ \t]+$//sgm;
 
-               $rv .= $s . "\n";
+               $rv .= $s;
+               $s = undef;
+               $rv .= "\n";
        }
        $rv;
 }
@@ -268,15 +258,11 @@ sub multipart_text_as_html {
                        $rv .= add_filename_line($enc->decode($fn));
                }
 
-               if (defined $full_pfx) {
-                       $rv .= add_text_body_short($enc, $part, $part_nr,
-                                               $full_pfx);
-               } else {
-                       $rv .= add_text_body_full($enc, $part, $part_nr);
-               }
+               $rv .= add_text_body($enc, $part, $part_nr, $full_pfx);
                $rv .= "\n" unless $rv =~ /\n\z/s;
                ++$part_nr;
        });
+       $mime->body_set('');
        $rv;
 }
 
@@ -295,57 +281,77 @@ my $LINK_RE = qr!\b((?:ftp|https?|nntp)://[@\w\+\&\?\.\%\;/#=-]+)!;
 sub linkify {
        # no newlines added here since it'd break the splitting we do
        # to fold quotes
-       $_[0] =~ s!$LINK_RE!<a href="$1">$1</a>!g;
+       $_[0] =~ s!$LINK_RE!<a\nhref="$1">$1</a>!g;
 }
 
-sub add_text_body_short {
-       my ($enc, $part, $part_nr, $full_pfx) = @_;
-       my $n = 0;
-       my $s = ascii_html($enc->decode($part->body));
-       linkify($s);
-       $s =~ s!^((?:(?:&gt;[^\n]*)\n)+)!
-               my $cur = $1;
-               my @lines = split(/\n/, $cur);
-               if (@lines > MAX_INLINE_QUOTED) {
-                       # show a short snippet of quoted text
-                       $cur = join(' ', @lines);
-                       $cur =~ s/^&gt;\s*//;
-
-                       my @sum = split(/\s+/, $cur);
-                       $cur = '';
-                       do {
-                               my $tmp = shift(@sum);
-                               my $len = length($tmp) + length($cur);
-                               if ($len > MAX_TRUNC_LEN) {
-                                       @sum = ();
-                               } else {
-                                       $cur .= $tmp . ' ';
-                               }
-                       } while (@sum && length($cur) < MAX_TRUNC_LEN);
-                       $cur =~ s/ \z/ .../;
-                       "&gt; &lt;<a\nhref=\"${full_pfx}#q${part_nr}_" . $n++ .
-                               "\">$cur<\/a>&gt;\n";
-               } else {
-                       $cur;
+sub flush_quote {
+       my ($quot, $n, $part_nr, $full_pfx, $final) = @_;
+
+       if ($full_pfx) {
+               if (!$final && scalar(@$quot) <= MAX_INLINE_QUOTED) {
+                       # show quote inline
+                       my $rv = join("\n", map { linkify($_); $_ } @$quot);
+                       @$quot = ();
+                       return $rv . "\n";
                }
-       !emg;
-       $s;
+
+               # show a short snippet of quoted text and link to full version:
+               @$quot = map { s/^(?:&gt;\s*)+//gm; $_ } @$quot;
+               my $cur = join(' ', @$quot);
+               @$quot = split(/\s+/, $cur);
+               $cur = '';
+               do {
+                       my $tmp = shift(@$quot);
+                       my $len = length($tmp) + length($cur);
+                       if ($len > MAX_TRUNC_LEN) {
+                               @$quot = ();
+                       } else {
+                               $cur .= $tmp . ' ';
+                       }
+               } while (@$quot && length($cur) < MAX_TRUNC_LEN);
+               @$quot = ();
+               $cur =~ s/ \z/ .../s;
+               my $nr = ++$$n;
+               "&gt; [<a\nhref=\"$full_pfx#q${part_nr}_$nr\">$cur</a>]\n";
+       } else {
+               # show everything in the full version with anchor from
+               # short version (see above)
+               my $nr = ++$$n;
+               my $rv = "<a\nid=q${part_nr}_$nr></a>";
+               $rv .= join("\n", map { linkify($_); $_ } @$quot) . "\n";
+               @$quot = ();
+               $rv;
+       }
 }
 
-sub add_text_body_full {
-       my ($enc, $part, $part_nr) = @_;
+sub add_text_body {
+       my ($enc, $part, $part_nr, $full_pfx) = @_;
        my $n = 0;
-       my $s = ascii_html($enc->decode($part->body));
-       linkify($s);
-       $s =~ s!^((?:(?:&gt;[^\n]*)\n)+)!
-               my $cur = $1;
-               my @lines = split(/\n/, $cur);
-               if (@lines > MAX_INLINE_QUOTED) {
-                       "<a\nid=q${part_nr}_" . $n++ . ">$cur</a>";
+       my $nr = 0;
+       my $s = $part->body;
+       $part->body_set('');
+       $s = $enc->decode($s);
+       $s = ascii_html($s);
+       my @lines = split(/\n/, $s);
+       $s = '';
+       my @quot;
+       while (defined(my $cur = shift @lines)) {
+               if ($cur !~ /^&gt;/) {
+                       # show the previously buffered quote inline
+                       if (scalar @quot) {
+                               $s .= flush_quote(\@quot, \$n, $part_nr,
+                                                 $full_pfx, 0);
+                       }
+
+                       # regular line, OK
+                       linkify($cur);
+                       $s .= $cur;
+                       $s .= "\n";
                } else {
-                       $cur;
+                       push @quot, $cur;
                }
-       !emg;
+       }
+       $s .= flush_quote(\@quot, \$n, $part_nr, $full_pfx, 1) if scalar @quot;
        $s;
 }
 
@@ -393,10 +399,19 @@ sub headers_to_html_header {
 
        my $refs = $header_obj->header_raw('References');
        if ($refs) {
-               $refs =~ s/\s*\Q$irt\E\s*// if (defined $irt);
-               my @refs = ($refs =~ /<([^>]+)>/g);
+               # avoid redundant URLs wasting bandwidth
+               my %seen;
+               $seen{mid_clean($irt)} = 1 if defined $irt;
+               my @refs;
+               my @raw_refs = ($refs =~ /<([^>]+)>/g);
+               foreach my $ref (@raw_refs) {
+                       next if $seen{$ref};
+                       $seen{$ref} = 1;
+                       push @refs, linkify_ref($ref);
+               }
+
                if (@refs) {
-                       $rv .= 'References: '. linkify_refs(@refs) . "\n";
+                       $rv .= 'References: '. join(' ', @refs) . "\n";
                }
        }
 
@@ -446,6 +461,7 @@ sub html_footer {
                if (my $c = $res->{count}) {
                        $c = $c == 1 ? '1 followup' : "$c followups";
                        $idx .= "\n$c:\n";
+                       $res->{srch} = $srch;
                        thread_followups(\$idx, $mime, $res);
                } else {
                        $idx .= "\n(no followups, yet)\n";
@@ -464,29 +480,32 @@ sub html_footer {
        "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
 }
 
-sub linkify_refs {
-       join(' ', map {
-               my $v = PublicInbox::Hval->new_msgid($_);
-               my $html = $v->as_html;
-               my $href = $v->as_href;
-               "&lt;<a\nhref=\"$href.html\">$html</a>&gt;";
-       } @_);
+sub linkify_ref {
+       my $v = PublicInbox::Hval->new_msgid($_[0]);
+       my $html = $v->as_html;
+       my $href = $v->as_href;
+       "&lt;<a\nhref=\"$href.html\">$html</a>&gt;";
 }
 
 sub anchor_for {
        my ($msgid) = @_;
-       'm' . mid_compressed(mid_clean($msgid));
+       my $id = $msgid;
+       if ($id !~ /\A[a-f0-9]{40}\z/) {
+               $id = mid_compressed(mid_clean($id), 1);
+       }
+       'm' . $id;
 }
 
 sub simple_dump {
        my ($dst, $root, $node, $level) = @_;
-       my $pfx = '  ' x $level;
-       $$dst .= $pfx;
+       # $root = [ Root Message-ID, \%seen, $srch ];
        if (my $x = $node->message) {
                my $mid = $x->header('Message-ID');
                if ($root->[0] ne $mid) {
+                       my $pfx = '  ' x $level;
+                       $$dst .= $pfx;
                        my $s = $x->header('Subject');
-                       my $h = hash_subj($s);
+                       my $h = $root->[2]->subject_path($s);
                        if ($root->[1]->{$h}) {
                                $s = '';
                        } else {
@@ -512,25 +531,16 @@ sub simple_dump {
        simple_dump($dst, $root, $node->next, $level) if $node->next;
 }
 
-sub hash_subj {
-       my ($subj) = @_;
-       $subj =~ s/\A\s+//;
-       $subj =~ s/\s+\z//;
-       $subj =~ s/^(?:re|aw):\s*//i; # remove reply prefix (aw: German)
-       $subj =~ s/\s+/ /;
-       Digest::SHA::sha1($subj);
-}
-
 sub thread_followups {
        my ($dst, $root, $res) = @_;
-       my @msgs = map { $_->mini_mime } @{$res->{msgs}};
-       require PublicInbox::Thread;
        $root->header_set('X-PI-TS', '0');
-       my $th = PublicInbox::Thread->new($root, @msgs);
-       $th->thread;
-       $th->order(*PublicInbox::Thread::sort_ts);
-       $root = [ $root->header('Message-ID'),
-                 { hash_subj($root->header('Subject')) => 1 } ];
+       my $msgs = load_results($res);
+       push @$msgs, $root;
+       my $th = thread_results($msgs);
+       my $srch = $res->{srch};
+       my $subj = $srch->subject_path($root->header('Subject'));
+       my %seen = ($subj => 1);
+       $root = [ $root->header('Message-ID'), \%seen, $srch ];
        simple_dump($dst, $root, $_, 0) for $th->rootset;
 }
 
@@ -538,50 +548,54 @@ sub thread_html_head {
        my ($mime) = @_;
        my $s = PublicInbox::Hval->new_oneline($mime->header('Subject'));
        $s = $s->as_html;
-       "<html><head><title>$s</title></head><body>" . PRE_WRAP
-
+       "<html><head><title>$s</title></head><body>";
 }
 
 sub thread_entry {
-       my ($dst, $state, $node, $level) = @_;
+       my ($dst, $git, $state, $node, $level) = @_;
        # $state = [ $search_res, $seen, undef, 0 (msg_nr) ];
        # $seen is overloaded with 3 types of fields:
        #       1) "root_anchor" => anchor_for(Message-ID),
        #       2) seen subject hashes: sha1(subject) => 1
        #       3) anchors hashes: "#$sha1_hex" (same as $seen in index_entry)
        if (my $mime = $node->message) {
-               if (length($$dst) == 0) {
-                       $$dst .= thread_html_head($mime);
+
+               # lazy load the full message from mini_mime:
+               my $path = mid2path(mid_clean($mime->header('Message-ID')));
+               $mime = eval { Email::MIME->new($git->cat_file("HEAD:$path")) };
+               if ($mime) {
+                       if (length($$dst) == 0) {
+                               $$dst .= thread_html_head($mime);
+                       }
+                       $$dst .= index_entry(undef, $mime, $level, $state);
                }
-               $$dst .= index_entry(undef, $mime, $level, $state);
        }
-       thread_entry($dst, $state, $node->child, $level + 1) if $node->child;
-       thread_entry($dst, $state, $node->next, $level) if $node->next;
+       my $cur;
+       $cur = $node->child and
+               thread_entry($dst, $git, $state, $cur, $level + 1);
+       $cur = $node->next and
+               thread_entry($dst, $git, $state, $cur, $level);
 }
 
 sub load_results {
-       my ($ctx, $res) = @_;
-
-       require PublicInbox::GitCatFile;
-       my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
-       my @msgs;
-       while (my $smsg = shift @{$res->{msgs}}) {
-               my $m = $smsg->mid;
-               my $path = mid2path($m);
-
-               # FIXME: duplicated code from Feed.pm
-               my $mime = eval {
-                       my $str = $git->cat_file("HEAD:$path");
-                       Email::MIME->new($str);
-               };
-               unless ($@) {
-                       my $t = eval { str2time($mime->header('Date')) };
-                       defined($t) or $t = 0;
-                       $mime->header_set('X-PI-TS', $t);
-                       push @msgs, $mime;
-               }
-       }
-       \@msgs;
+       my ($res) = @_;
+
+       [ map { $_->mini_mime } @{delete $res->{msgs}} ];
+}
+
+sub msg_timestamp {
+       my ($mime) = @_;
+       my $ts = eval { str2time($mime->header('Date')) };
+       defined($ts) ? $ts : 0;
+}
+
+sub thread_results {
+       my ($msgs) = @_;
+       require PublicInbox::Thread;
+       my $th = PublicInbox::Thread->new(@$msgs);
+       $th->thread;
+       $th->order(*PublicInbox::Thread::sort_ts);
+       $th
 }
 
 1;