]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/View.pm
view: simplify topic handling based on subjects
[public-inbox.git] / lib / PublicInbox / View.pm
index ad90eb3d09e593e3cbe73b7cee6b9b6e652408c4..fc877b7cfe9b9773881c5230362bd65eccaae52e 100644 (file)
@@ -103,11 +103,7 @@ sub index_entry {
        }
 
        my $ts = _msg_date($mime);
-       my $rv = "<table\nsummary=l$level><tr>";
-       if ($level) {
-               $rv .= '<td><pre>' . (INDENT x $level) . '</pre></td>';
-       }
-       $rv .= "<td\nid=s$midx><pre>";
+       my $rv = "<pre\nid=s$midx>";
        $rv .= "<b\nid=$id>$subj</b>\n";
        $rv .= "- $from @ $ts UTC - ";
        $rv .= "<a\nhref=\"#s$next\">next</a>";
@@ -144,8 +140,8 @@ sub index_entry {
                }
                $rv .= " <a\nhref=\"$parent_anchor\">parent</a>";
        }
-       if (my $pct = $state->{pct}) {
-               $rv .= " [$pct->{$mid_raw}%]";
+       if (my $pct = $state->{pct}) { # used by SearchView.pm
+               $rv .= " [relevance $pct->{$mid_raw}%]";
        } elsif ($srch) {
                if ($ctx->{flat}) {
                        $rv .= " [<a\nhref=\"${path}$href/t/#u\">threaded</a>" .
@@ -155,8 +151,7 @@ sub index_entry {
                                "<a\nhref=\"${path}$href/T/#u\">flat</a>]";
                }
        }
-
-       $fh->write($rv .= '</pre></td></tr></table>');
+       $fh->write($rv .= '</pre>');
 }
 
 sub thread_html {
@@ -181,6 +176,7 @@ sub emit_thread_html {
                seen => $seen,
                root_anchor => anchor_for($mid),
                anchor_idx => 0,
+               cur_level => 0,
        };
 
        require PublicInbox::Git;
@@ -191,6 +187,9 @@ sub emit_thread_html {
        } else {
                my $th = thread_results($msgs);
                thread_entry(\$cb, $git, $state, $_, 0) for $th->rootset;
+               if (my $max = $state->{cur_level}) {
+                       $cb->write(('</ul></li>' x ($max - 1)) . '</ul>');
+               }
        }
        $git = undef;
        Email::Address->purge_cache;
@@ -315,6 +314,9 @@ sub linkify_2 {
 sub flush_quote {
        my ($quot, $n, $part_nr, $full_pfx, $final, $do_anchor) = @_;
 
+       # n.b.: do not use <blockquote> since it screws up alignment
+       # w.r.t. unquoted text.  Repliers may rely on pre-formatted
+       # alignment to point out a certain word in quoted text.
        if ($full_pfx) {
                if (!$final && scalar(@$quot) <= MAX_INLINE_QUOTED) {
                        # show quote inline
@@ -467,14 +469,22 @@ sub thread_inline {
        $$dst .= 'Thread: ';
        my $parent = in_reply_to($cur);
        if ($nr <= 1) {
-               $$dst .= "[no followups, yet] ($expand)\n";
+               if (defined $parent) {
+                       $$dst .= "($expand)\n ";
+                       $$dst .= ghost_parent($upfx, $parent) . "\n";
+               } else {
+                       $$dst .= "[no followups, yet] ($expand)\n";
+               }
                $ctx->{next_msg} = undef;
                $ctx->{parent_msg} = $parent;
                return;
        }
 
-       $$dst .= "~$nr messages (<a\nhref=\"#b\">skip</a> / " .
-                $expand . ")\n";
+       $$dst .= "~$nr messages ($expand";
+       if ($nr > MAX_INLINE_QUOTED) {
+               $$dst .= qq! / <a\nhref="#b">[scroll down]</a>!;
+       }
+       $$dst .= ")\n";
 
        my $subj = $srch->subject_path($cur->header('Subject'));
        my $state = {
@@ -559,10 +569,11 @@ sub html_footer {
 
        my $srch = $ctx->{srch} if $ctx;
        my $upfx = $full_pfx ? '../' : '../../';
+       my $tpfx = $full_pfx ? '' : '../';
        my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
 
        if ($srch && $standalone) {
-               $idx .= qq{ / follow: <a\nhref="t.atom">Atom feed</a>\n};
+               $idx .= qq{ / follow: <a\nhref="${tpfx}t.atom">Atom feed</a>\n};
        }
        if ($idx && $srch) {
                my $p = $ctx->{parent_msg};
@@ -580,7 +591,7 @@ sub html_footer {
                        $irt .= ' ' x length('next ');
                }
                if ($p || $next) {
-                       $irt .= "<a\nhref=\"#r\">thread</a> ";
+                       $irt .= "<a\nhref=\"${tpfx}t/#u\">thread</a> ";
                } else {
                        $irt .= ' ' x length('thread ');
                }
@@ -637,12 +648,37 @@ sub ghost_parent {
        qq{[parent not found: &lt;<a\nhref="$upfx$href/">$html</a>&gt;]};
 }
 
-sub ghost_table {
-       my ($upfx, $mid, $level) = @_;
-       "<table\nsummary=ghost><tr><td>" .
-               (INDENT x $level) . '</td><td><pre>' .
-               ghost_parent($upfx, $mid) .
-               '</pre></td></table>';
+sub thread_adj_level {
+       my ($fh, $state, $level) = @_;
+
+       my $max = $state->{cur_level};
+       if ($level <= 0) {
+               return '' if $max == 0; # flat output
+
+               # reset existing lists
+               my $x = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
+               $fh->write($x . '</ul>');
+               $state->{cur_level} = 0;
+               return '';
+       }
+       if ($level == $max) { # continue existing list
+               $fh->write('<li>');
+       } elsif ($level < $max) {
+               my $x = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
+               $fh->write($x .= '<li>');
+               $state->{cur_level} = $level;
+       } else { # ($level > $max) # start a new level
+               $state->{cur_level} = $level;
+               $fh->write(($max ? '<li>' : '') . '<ul><li>');
+       }
+       '</li>';
+}
+
+sub ghost_flush {
+       my ($fh, $state, $upfx, $mid, $level) = @_;
+
+       my $end = thread_adj_level($fh, $state, $level);
+       $fh->write('<pre>'. ghost_parent($upfx, $mid) .  '</pre>' . $end);
 }
 
 sub __thread_entry {
@@ -655,20 +691,28 @@ sub __thread_entry {
        } or return;
 
        if ($state->{anchor_idx} == 0) {
-               thread_html_head($cb, $mime, $state);
+               thread_html_head($cb, $mime, $state, $level);
        }
-
+       my $fh = $$cb;
        if (my $ghost = delete $state->{ghost}) {
                # n.b. ghost messages may only be parents, not children
                foreach my $g (@$ghost) {
-                       $$cb->write(ghost_table('../../', @$g));
+                       ghost_flush($fh, $state, '../../', @$g);
                }
        }
-       index_entry($$cb, $mime, $level, $state);
+       my $end = thread_adj_level($fh, $state, $level);
+       index_entry($fh, $mime, $level, $state);
+       $fh->write($end) if $end;
+
        1;
 }
 
-sub __ghost_entry {
+sub indent_for {
+       my ($level) = @_;
+       INDENT x ($level - 1);
+}
+
+sub __ghost_prepare {
        my ($state, $node, $level) = @_;
        my $ghost = $state->{ghost} ||= [];
        push @$ghost, [ $node->messageid, $level ];
@@ -679,10 +723,10 @@ sub thread_entry {
        return unless $node;
        if (my $mime = $node->message) {
                unless (__thread_entry($cb, $git, $state, $mime, $level)) {
-                       __ghost_entry($state, $node, $level);
+                       __ghost_prepare($state, $node, $level);
                }
        } else {
-               __ghost_entry($state, $node, $level);
+               __ghost_prepare($state, $node, $level);
        }
 
        thread_entry($cb, $git, $state, $node->child, $level + 1);
@@ -735,9 +779,8 @@ sub _inline_header {
        my $mid = mid_clean($mime->header('Message-ID'));
        my $f = $mime->header('X-PI-From');
        my $d = _msg_date($mime);
-       $f = PublicInbox::Hval->new($f)->as_html;
-       $d = PublicInbox::Hval->new($d)->as_html;
-       my $pfx = ' ' . $d . ' ' . (INDENT x $level);
+       $f = PublicInbox::Hval->new_oneline($f)->as_html;
+       my $pfx = ' ' . $d . ' ' . indent_for($level);
        my $attr = $f;
        $state->{first_level} ||= $level;
 
@@ -793,7 +836,7 @@ sub inline_dump {
        } else {
                my $dot = $level == 0 ? '' : '` ';
                my $pfx = (' ' x length(' 1970-01-01 13:37 ')).
-                       (INDENT x $level) . $dot;
+                       indent_for($level) . $dot;
                $$dst .= $pfx;
                $$dst .= ghost_parent("$upfx../", $node->messageid) . "\n";
        }
@@ -824,27 +867,21 @@ sub add_topic {
 
        if (my $x = $node->message) {
                $x = $x->header_obj;
-               my ($topic, $subj);
+               my $subj;
 
                $subj = $x->header('Subject');
                $subj = $state->{srch}->subject_normalized($subj);
-               $topic = $subj;
-
-               # kill "[PATCH v2]" etc. for summarization
-               unless ($level == 0) {
-                       $topic =~ s/\A\s*\[[^\]]+\]\s*//g;
-               }
 
-               if (++$state->{subjs}->{$topic} == 1) {
-                       push @{$state->{order}}, [ $level, $subj, $topic ];
+               if (++$state->{subjs}->{$subj} == 1) {
+                       push @{$state->{order}}, [ $level, $subj ];
                }
 
                my $mid = mid_clean($x->header('Message-ID'));
 
                my $ts = $x->header('X-PI-TS');
-               my $exist = $state->{latest}->{$topic};
+               my $exist = $state->{latest}->{$subj};
                if (!$exist || $exist->[1] < $ts) {
-                       $state->{latest}->{$topic} = [ $mid, $ts ];
+                       $state->{latest}->{$subj} = [ $mid, $ts ];
                }
        } else {
                # ghost message, do not bump level
@@ -866,12 +903,12 @@ sub dump_topics {
        my $prev = 0;
        my $prev_attr = '';
        while (defined(my $info = shift @$order)) {
-               my ($level, $subj, $topic) = @$info;
-               my $n = delete $subjs->{$topic};
-               my ($mid, $ts) = @{delete $latest->{$topic}};
-               $mid = PublicInbox::Hval->new($mid)->as_href;
+               my ($level, $subj) = @$info;
+               my $n = delete $subjs->{$subj};
+               my ($mid, $ts) = @{delete $latest->{$subj}};
+               $mid = PublicInbox::Hval->new_msgid($mid)->as_href;
                $subj = PublicInbox::Hval->new($subj)->as_html;
-               $pfx = INDENT x ($level - 1);
+               $pfx = indent_for($level);
                my $nl = $level == $prev ? "\n" : '';
                my $dot = $level == 0 ? '' : '` ';
                $dst .= "$nl$pfx$dot<a\nhref=\"$mid/t/#u\"><b>$subj</b></a>\n";