]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/View.pm
doc: various overview-level module comments
[public-inbox.git] / lib / PublicInbox / View.pm
1 # Copyright (C) 2014-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # Used for displaying the HTML web interface.
5 # See Documentation/design_www.txt for this.
6 package PublicInbox::View;
7 use strict;
8 use warnings;
9 use PublicInbox::MsgTime qw(msg_datestamp);
10 use PublicInbox::Hval qw/ascii_html obfuscate_addrs/;
11 use PublicInbox::Linkify;
12 use PublicInbox::MID qw/id_compress mid_escape mids references/;
13 use PublicInbox::MsgIter;
14 use PublicInbox::Address;
15 use PublicInbox::WwwStream;
16 use PublicInbox::Reply;
17 require POSIX;
18 use Time::Local qw(timegm);
19
20 use constant COLS => 72;
21 use constant INDENT => '  ';
22 use constant TCHILD => '` ';
23 sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
24
25 # public functions: (unstable)
26
27 sub msg_html {
28         my ($ctx, $mime, $more, $smsg) = @_;
29         my $hdr = $mime->header_obj;
30         my $ibx = $ctx->{-inbox};
31         my $obfs_ibx = $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
32         my $tip = _msg_html_prepare($hdr, $ctx, $more, 0);
33         my $end = 2;
34         PublicInbox::WwwStream->response($ctx, 200, sub {
35                 my ($nr, undef) = @_;
36                 if ($nr == 1) {
37                         # $more cannot be true w/o $smsg being defined:
38                         my $upfx = $more ? '../'.mid_escape($smsg->mid).'/' : '';
39                         $tip . multipart_text_as_html($mime, $upfx, $obfs_ibx) .
40                                 '</pre><hr>'
41                 } elsif ($more && @$more) {
42                         ++$end;
43                         msg_html_more($ctx, $more, $nr);
44                 } elsif ($nr == $end) {
45                         # fake an EOF if generating the footer fails;
46                         # we want to at least show the message if something
47                         # here crashes:
48                         eval {
49                                 '<pre>' . html_footer($hdr, 1, $ctx) .
50                                 '</pre>' . msg_reply($ctx, $hdr)
51                         };
52                 } else {
53                         undef
54                 }
55         });
56 }
57
58 sub msg_page {
59         my ($ctx) = @_;
60         my $mid = $ctx->{mid};
61         my $ibx = $ctx->{-inbox};
62         my ($first, $more);
63         my $smsg;
64         if (my $srch = $ibx->search) {
65                 my ($id, $prev);
66                 $smsg = $srch->next_by_mid($mid, \$id, \$prev);
67                 $first = $ibx->msg_by_smsg($smsg) if $smsg;
68                 if ($first) {
69                         my $next = $srch->next_by_mid($mid, \$id, \$prev);
70                         $more = [ $id, $prev, $next ] if $next;
71                 }
72                 return unless $first;
73         } else {
74                 $first = $ibx->msg_by_mid($mid) or return;
75         }
76         msg_html($ctx, PublicInbox::MIME->new($first), $more, $smsg);
77 }
78
79 sub msg_html_more {
80         my ($ctx, $more, $nr) = @_;
81         my $str = eval {
82                 my ($id, $prev, $smsg) = @$more;
83                 my $mid = $ctx->{mid};
84                 $smsg = $ctx->{-inbox}->smsg_mime($smsg);
85                 my $next = $ctx->{srch}->next_by_mid($mid, \$id, \$prev);
86                 @$more = $next ? ($id, $prev, $next) : ();
87                 if ($smsg) {
88                         my $mime = $smsg->{mime};
89                         my $upfx = '../' . mid_escape($smsg->mid) . '/';
90                         _msg_html_prepare($mime->header_obj, $ctx, $more, $nr) .
91                                 multipart_text_as_html($mime, $upfx,
92                                                         $ctx->{-obfs_ibx}) .
93                                 '</pre><hr>'
94                 } else {
95                         '';
96                 }
97         };
98         if ($@) {
99                 warn "Error lookup up additional messages: $@\n";
100                 $str = '<pre>Error looking up additional messages</pre>';
101         }
102         $str;
103 }
104
105 # /$INBOX/$MESSAGE_ID/#R
106 sub msg_reply {
107         my ($ctx, $hdr) = @_;
108         my $se_url =
109          'https://kernel.org/pub/software/scm/git/docs/git-send-email.html';
110         my $p_url =
111          'https://en.wikipedia.org/wiki/Posting_style#Interleaved_style';
112
113         my $info = '';
114         my $ibx = $ctx->{-inbox};
115         if (my $url = $ibx->{infourl}) {
116                 $url = PublicInbox::Hval::prurl($ctx->{env}, $url);
117                 $info = qq(\n  List information: <a\nhref="$url">$url</a>\n);
118         }
119
120         my ($arg, $link, $reply_to_all) =
121                         PublicInbox::Reply::mailto_arg_link($ibx, $hdr);
122         if (ref($arg) eq 'SCALAR') {
123                 return '<pre id=R>'.ascii_html($$arg).'</pre>';
124         }
125
126         # mailto: link only works if address obfuscation is disabled
127         if ($link) {
128                 $link = <<EOF;
129
130 * If your mail client supports setting the <b>In-Reply-To</b> header
131   via mailto: links, try the <a
132 href="$link">mailto: link</a>
133 EOF
134         }
135
136         push @$arg, '/path/to/YOUR_REPLY';
137         $arg = ascii_html(join(" \\\n    ", '', @$arg));
138         <<EOF
139 <hr><pre
140 id=R><b>Reply instructions:</b>
141
142 You may reply publically to <a
143 href=#t>this message</a> via plain-text email
144 using any one of the following methods:
145
146 * Save the following mbox file, import it into your mail client,
147   and $reply_to_all from there: <a
148 href=raw>mbox</a>
149
150   Avoid top-posting and favor interleaved quoting:
151   <a
152 href="$p_url">$p_url</a>
153 $info
154 * Reply using the <b>--to</b>, <b>--cc</b>, and <b>--in-reply-to</b>
155   switches of git-send-email(1):
156
157   git send-email$arg
158
159   <a
160 href="$se_url">$se_url</a>
161 $link</pre>
162 EOF
163 }
164
165 sub in_reply_to {
166         my ($hdr) = @_;
167         my $refs = references($hdr);
168         $refs->[-1];
169 }
170
171 sub fold_addresses ($) {
172         return $_[0] if length($_[0]) <= COLS;
173         # try to fold on commas after non-word chars before $lim chars,
174         # Try to get the "," preceeded by ">" or ")", but avoid folding
175         # on the comma where somebody uses "Lastname, Firstname".
176         # We also try to keep the last and penultimate addresses in
177         # the list on the same line if possible, hence the extra \z
178         # Fall back to folding on spaces at $lim + 1 chars
179         my $lim = COLS - 8; # 8 = "\t" display width
180         my $too_long = $lim + 1;
181         $_[0] =~ s/\s*\z//s; # Email::Simple doesn't strip trailing spaces
182         $_[0] = join("\n\t",
183                 ($_[0] =~ /(.{0,$lim}\W(?:,|\z)|
184                                 .{1,$lim}(?:,|\z)|
185                                 .{1,$lim}|
186                                 .{$too_long,}?)(?:\s|\z)/xgo));
187 }
188
189 sub _hdr_names_html ($$) {
190         my ($hdr, $field) = @_;
191         my $val = $hdr->header($field) or return '';
192         ascii_html(join(', ', PublicInbox::Address::names($val)));
193 }
194
195 sub nr_to_s ($$$) {
196         my ($nr, $singular, $plural) = @_;
197         return "0 $plural" if $nr == 0;
198         $nr == 1 ? "$nr $singular" : "$nr $plural";
199 }
200
201 # this is already inside a <pre>
202 sub index_entry {
203         my ($smsg, $ctx, $more) = @_;
204         my $srch = $ctx->{srch};
205         my $subj = $smsg->subject;
206         my $mid_raw = $smsg->mid;
207         my $id = id_compress($mid_raw, 1);
208         my $id_m = 'm'.$id;
209
210         my $root_anchor = $ctx->{root_anchor} || '';
211         my $irt;
212         my $obfs_ibx = $ctx->{-obfs_ibx};
213
214         my $rv = "<a\nhref=#e$id\nid=m$id>*</a> ";
215         $subj = '<b>'.ascii_html($subj).'</b>';
216         obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
217         $subj = "<u\nid=u>$subj</u>" if $root_anchor eq $id_m;
218         $rv .= $subj . "\n";
219         $rv .= _th_index_lite($mid_raw, \$irt, $id, $ctx);
220         my @tocc;
221         my $ds = $smsg->ds; # for v1 non-Xapian/SQLite users
222         my $mime = delete $smsg->{mime}; # critical to memory use
223         my $hdr = $mime->header_obj;
224         my $from = _hdr_names_html($hdr, 'From');
225         obfuscate_addrs($obfs_ibx, $from) if $obfs_ibx;
226         $rv .= "From: $from @ ".fmt_ts($ds)." UTC";
227         my $upfx = $ctx->{-upfx};
228         my $mhref = $upfx . mid_escape($mid_raw) . '/';
229         $rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
230         $rv .= qq{<a\nhref="${mhref}raw">raw</a>)\n};
231         my $to = fold_addresses(_hdr_names_html($hdr, 'To'));
232         my $cc = fold_addresses(_hdr_names_html($hdr, 'Cc'));
233         my ($tlen, $clen) = (length($to), length($cc));
234         my $to_cc = '';
235         if (($tlen + $clen) > COLS) {
236                 $to_cc .= '  To: '.$to."\n" if $tlen;
237                 $to_cc .= '  Cc: '.$cc."\n" if $clen;
238         } else {
239                 if ($tlen) {
240                         $to_cc .= '  To: '.$to;
241                         $to_cc .= '; <b>+Cc:</b> '.$cc if $clen;
242                 } else {
243                         $to_cc .= '  Cc: '.$cc if $clen;
244                 }
245                 $to_cc .= "\n";
246         }
247         obfuscate_addrs($obfs_ibx, $to_cc) if $obfs_ibx;
248         $rv .= $to_cc;
249
250         my $mapping = $ctx->{mapping};
251         if (!$mapping && (defined($irt) || defined($irt = in_reply_to($hdr)))) {
252                 my $mirt = PublicInbox::Hval->new_msgid($irt);
253                 my $href = $upfx . $mirt->{href}. '/';
254                 my $html = $mirt->as_html;
255                 $rv .= qq(In-Reply-To: &lt;<a\nhref="$href">$html</a>&gt;\n)
256         }
257         $rv .= "\n";
258
259         # scan through all parts, looking for displayable text
260         msg_iter($mime, sub { $rv .= add_text_body($mhref, $obfs_ibx, $_[0]) });
261
262         # add the footer
263         $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
264                 "<a\nhref=\"$mhref\">permalink</a>" .
265                 " <a\nhref=\"${mhref}raw\">raw</a>" .
266                 " <a\nhref=\"${mhref}#R\">reply</a>";
267
268         my $hr;
269         if (my $pct = $ctx->{pct}) { # used by SearchView.pm
270                 $rv .= "\t[relevance $pct->{$mid_raw}%]";
271                 $hr = 1;
272         } elsif ($mapping) {
273                 my $nested = 'nested';
274                 my $flat = 'flat';
275                 my $end = '';
276                 if ($ctx->{flat}) {
277                         $hr = 1;
278                         $flat = "<b>$flat</b>";
279                 } else {
280                         $nested = "<b>$nested</b>";
281                 }
282                 $rv .= "\t[<a\nhref=\"${mhref}T/#u\">$flat</a>";
283                 $rv .= "|<a\nhref=\"${mhref}t/#u\">$nested</a>]";
284                 $rv .= " <a\nhref=#r$id>$ctx->{s_nr}</a>";
285         } else {
286                 $hr = $ctx->{-hr};
287         }
288
289         $rv .= $more ? '</pre><hr><pre>' : '</pre>' if $hr;
290         $rv;
291 }
292
293 sub pad_link ($$;$) {
294         my ($mid, $level, $s) = @_;
295         $s ||= '...';
296         my $id = id_compress($mid, 1);
297         (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
298 }
299
300 sub _th_index_lite {
301         my ($mid_raw, $irt, $id, $ctx) = @_;
302         my $rv = '';
303         my $mapping = $ctx->{mapping} or return $rv;
304         my $pad = '  ';
305         my $mid_map = $mapping->{$mid_raw};
306         defined $mid_map or
307                 return 'public-inbox BUG: '.ascii_html($mid_raw).' not mapped';
308         my ($attr, $node, $idx, $level) = @$mid_map;
309         my $children = $node->{children};
310         my $nr_c = scalar @$children;
311         my $nr_s = 0;
312         my $siblings;
313         if (my $smsg = $node->{smsg}) {
314                 ($$irt) = (($smsg->{references} || '') =~ m/<([^>]+)>\z/);
315         }
316         my $irt_map = $mapping->{$$irt} if defined $$irt;
317         if (defined $irt_map) {
318                 $siblings = $irt_map->[1]->{children};
319                 $nr_s = scalar(@$siblings) - 1;
320                 $rv .= $pad . $irt_map->[0];
321                 if ($idx > 0) {
322                         my $prev = $siblings->[$idx - 1];
323                         my $pmid = $prev->{id};
324                         if ($idx > 2) {
325                                 my $s = ($idx - 1). ' preceding siblings ...';
326                                 $rv .= pad_link($pmid, $level, $s);
327                         } elsif ($idx == 2) {
328                                 my $ppmid = $siblings->[0]->{id};
329                                 $rv .= $pad . $mapping->{$ppmid}->[0];
330                         }
331                         $rv .= $pad . $mapping->{$pmid}->[0];
332                 }
333         }
334         my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
335         my $s_c = nr_to_s($nr_c, 'reply', 'replies');
336         $attr =~ s!\n\z!</b>\n!s;
337         $attr =~ s!<a\nhref.*</a> !!s; # no point in duplicating subject
338         $attr =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
339         $rv .= "<b>@ $attr";
340         if ($nr_c) {
341                 my $cmid = $children->[0]->{id};
342                 $rv .= $pad . $mapping->{$cmid}->[0];
343                 if ($nr_c > 2) {
344                         my $s = ($nr_c - 1). ' more replies';
345                         $rv .= pad_link($cmid, $level + 1, $s);
346                 } elsif (my $cn = $children->[1]) {
347                         $rv .= $pad . $mapping->{$cn->{id}}->[0];
348                 }
349         }
350
351         my $next = $siblings->[$idx+1] if $siblings && $idx >= 0;
352         if ($next) {
353                 my $nmid = $next->{id};
354                 $rv .= $pad . $mapping->{$nmid}->[0];
355                 my $nnext = $nr_s - $idx;
356                 if ($nnext > 2) {
357                         my $s = ($nnext - 1).' subsequent siblings';
358                         $rv .= pad_link($nmid, $level, $s);
359                 } elsif (my $nn = $siblings->[$idx + 2]) {
360                         $rv .= $pad . $mapping->{$nn->{id}}->[0];
361                 }
362         }
363         $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
364 }
365
366 sub walk_thread {
367         my ($rootset, $ctx, $cb) = @_;
368         my @q = map { (0, $_, -1) } @$rootset;
369         while (@q) {
370                 my ($level, $node, $i) = splice(@q, 0, 3);
371                 defined $node or next;
372                 $cb->($ctx, $level, $node, $i) or return;
373                 ++$level;
374                 $i = 0;
375                 unshift @q, map { ($level, $_, $i++) } @{$node->{children}};
376         }
377 }
378
379 sub pre_thread  {
380         my ($ctx, $level, $node, $idx) = @_;
381         $ctx->{mapping}->{$node->{id}} = [ '', $node, $idx, $level ];
382         skel_dump($ctx, $level, $node);
383 }
384
385 sub thread_index_entry {
386         my ($ctx, $level, $smsg) = @_;
387         my ($beg, $end) = thread_adj_level($ctx, $level);
388         $beg . '<pre>' . index_entry($smsg, $ctx, 0) . '</pre>' . $end;
389 }
390
391 sub stream_thread ($$) {
392         my ($rootset, $ctx) = @_;
393         my $inbox = $ctx->{-inbox};
394         my @q = map { (0, $_) } @$rootset;
395         my $level;
396         my $smsg;
397         while (@q) {
398                 $level = shift @q;
399                 my $node = shift @q or next;
400                 my $cl = $level + 1;
401                 unshift @q, map { ($cl, $_) } @{$node->{children}};
402                 $smsg = $inbox->smsg_mime($node->{smsg}) and last;
403         }
404         return missing_thread($ctx) unless $smsg;
405
406         $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
407         $ctx->{-title_html} = ascii_html($smsg->subject);
408         $ctx->{-html_tip} = thread_index_entry($ctx, $level, $smsg);
409         $smsg = undef;
410         PublicInbox::WwwStream->response($ctx, 200, sub {
411                 return unless $ctx;
412                 while (@q) {
413                         $level = shift @q;
414                         my $node = shift @q or next;
415                         my $cl = $level + 1;
416                         unshift @q, map { ($cl, $_) } @{$node->{children}};
417                         if ($smsg = $inbox->smsg_mime($node->{smsg})) {
418                                 return thread_index_entry($ctx, $level, $smsg);
419                         } else {
420                                 return ghost_index_entry($ctx, $level, $node);
421                         }
422                 }
423                 my $ret = join('', thread_adj_level($ctx, 0));
424                 $ret .= ${$ctx->{dst}}; # skel
425                 $ctx = undef;
426                 $ret;
427         });
428 }
429
430 sub thread_html {
431         my ($ctx) = @_;
432         my $mid = $ctx->{mid};
433         my $srch = $ctx->{srch};
434         my ($nr, $msgs) = $srch->get_thread($mid);
435         return missing_thread($ctx) if $nr == 0;
436         my $skel = '<hr><pre>';
437         $skel .= $nr == 1 ? 'only message in thread' : 'end of thread';
438         $skel .= ", back to <a\nhref=\"../../\">index</a>\n\n";
439         $skel .= "<b\nid=t>Thread overview:</b> ";
440         $skel .= $nr == 1 ? '(only message)' : "$nr+ messages";
441         $skel .= " (download: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
442         $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
443         $skel .= "-- links below jump to the message on this page --\n";
444         $ctx->{-upfx} = '../../';
445         $ctx->{cur_level} = 0;
446         $ctx->{dst} = \$skel;
447         $ctx->{prev_attr} = '';
448         $ctx->{prev_level} = 0;
449         $ctx->{root_anchor} = anchor_for($mid);
450         $ctx->{mapping} = {};
451         $ctx->{s_nr} = "$nr+ messages in thread";
452
453         my $rootset = thread_results($ctx, $msgs);
454
455         # reduce hash lookups in pre_thread->skel_dump
456         my $inbox = $ctx->{-inbox};
457         $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef;
458         walk_thread($rootset, $ctx, *pre_thread);
459
460         $skel .= '</pre>';
461         return stream_thread($rootset, $ctx) unless $ctx->{flat};
462
463         # flat display: lazy load the full message from smsg
464         my $smsg;
465         while (my $m = shift @$msgs) {
466                 $smsg = $inbox->smsg_mime($m) and last;
467         }
468         return missing_thread($ctx) unless $smsg;
469         $ctx->{-title_html} = ascii_html($smsg->subject);
470         $ctx->{-html_tip} = '<pre>'.index_entry($smsg, $ctx, scalar @$msgs);
471         $smsg = undef;
472         PublicInbox::WwwStream->response($ctx, 200, sub {
473                 return unless $msgs;
474                 $smsg = undef;
475                 while (my $m = shift @$msgs) {
476                         $smsg = $inbox->smsg_mime($m) and last;
477                 }
478                 return index_entry($smsg, $ctx, scalar @$msgs) if $smsg;
479                 $msgs = undef;
480                 $skel;
481         });
482 }
483
484 sub multipart_text_as_html {
485         my ($mime, $upfx, $obfs_ibx) = @_;
486         my $rv = "";
487
488         # scan through all parts, looking for displayable text
489         msg_iter($mime, sub { $rv .= add_text_body($upfx, $obfs_ibx, $_[0]) });
490         $rv;
491 }
492
493 sub flush_quote {
494         my ($s, $l, $quot) = @_;
495
496         # show everything in the full version with anchor from
497         # short version (see above)
498         my $rv = $l->linkify_1(join('', @$quot));
499         @$quot = ();
500
501         # we use a <span> here to allow users to specify their own
502         # color for quoted text
503         $rv = $l->linkify_2(ascii_html($rv));
504         $$s .= qq(<span\nclass="q">) . $rv . '</span>'
505 }
506
507 sub attach_link ($$$$;$) {
508         my ($upfx, $ct, $p, $fn, $err) = @_;
509         my ($part, $depth, @idx) = @$p;
510         my $nl = $idx[-1] > 1 ? "\n" : '';
511         my $idx = join('.', @idx);
512         my $size = bytes::length($part->body);
513
514         # hide attributes normally, unless we want to aid users in
515         # spotting MUA problems:
516         $ct =~ s/;.*// unless $err;
517         $ct = ascii_html($ct);
518         my $desc = $part->header('Content-Description');
519         $desc = $fn unless defined $desc;
520         $desc = '' unless defined $desc;
521         my $sfn;
522         if (defined $fn && $fn =~ /\A[[:alnum:]][\w\.-]+[[:alnum:]]\z/) {
523                 $sfn = $fn;
524         } elsif ($ct eq 'text/plain') {
525                 $sfn = 'a.txt';
526         } else {
527                 $sfn = 'a.bin';
528         }
529         my $ret = qq($nl<a\nhref="$upfx$idx-$sfn">);
530         if ($err) {
531                 $ret .=
532 "[-- Warning: decoded text below may be mangled --]\n";
533         }
534         $ret .= "[-- Attachment #$idx: ";
535         my $ts = "Type: $ct, Size: $size bytes";
536         $desc = ascii_html($desc);
537         $ret .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
538         $ret .= "</a>\n";
539 }
540
541 sub add_text_body {
542         my ($upfx, $obfs_ibx, $p) = @_;
543         # $p - from msg_iter: [ Email::MIME, depth, @idx ]
544         my ($part, $depth) = @$p; # attachment @idx is unused
545         my $ct = $part->content_type || 'text/plain';
546         my $fn = $part->filename;
547         my ($s, $err) = msg_part_text($part, $ct);
548
549         return attach_link($upfx, $ct, $p, $fn) unless defined $s;
550
551         my @lines = split(/^/m, $s);
552         $s = '';
553         if (defined($fn) || $depth > 0 || $err) {
554                 # badly-encoded message with $err? tell the world about it!
555                 $s .= attach_link($upfx, $ct, $p, $fn, $err);
556                 $s .= "\n";
557         }
558         my @quot;
559         my $l = PublicInbox::Linkify->new;
560         foreach my $cur (@lines) {
561                 if ($cur !~ /^>/) {
562                         # show the previously buffered quote inline
563                         flush_quote(\$s, $l, \@quot) if @quot;
564
565                         # regular line, OK
566                         $l->linkify_1($cur);
567                         $s .= $l->linkify_2(ascii_html($cur));
568                 } else {
569                         push @quot, $cur;
570                 }
571         }
572
573         if (@quot) { # ugh, top posted
574                 flush_quote(\$s, $l, \@quot);
575                 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
576                 $s;
577         } else {
578                 obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx;
579                 if ($s =~ /\n\z/s) { # common, last line ends with a newline
580                         $s;
581                 } else { # some editors don't do newlines...
582                         $s .= "\n";
583                 }
584         }
585 }
586
587 sub _msg_html_prepare {
588         my ($hdr, $ctx, $more, $nr) = @_;
589         my $srch = $ctx->{srch} if $ctx;
590         my $atom = '';
591         my $obfs_ibx = $ctx->{-obfs_ibx};
592         my $rv = '';
593         my $mids = mids($hdr);
594         my $multiple = scalar(@$mids) > 1; # zero, one, infinity
595         if ($nr == 0) {
596                 if ($more) {
597                         $rv .=
598 "<pre>WARNING: multiple messages refer to this Message-ID\n</pre>";
599                 }
600                 $rv .= "<pre\nid=b>"; # anchor for body start
601         } else {
602                 $rv .= '<pre>';
603         }
604         if ($srch) {
605                 $ctx->{-upfx} = '../';
606         }
607         my @title;
608         my $v;
609         if (defined($v = $hdr->header('From'))) {
610                 $v = PublicInbox::Hval->new($v);
611                 my @n = PublicInbox::Address::names($v->raw);
612                 $title[1] = ascii_html(join(', ', @n));
613                 $v = $v->as_html;
614                 if ($obfs_ibx) {
615                         obfuscate_addrs($obfs_ibx, $v);
616                         obfuscate_addrs($obfs_ibx, $title[1]);
617                 }
618                 $rv .= "From: $v\n" if $v ne '';
619         }
620         foreach my $h (qw(To Cc)) {
621                 defined($v = $hdr->header($h)) or next;
622                 fold_addresses($v);
623                 $v = ascii_html($v);
624                 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
625                 $rv .= "$h: $v\n" if $v ne '';
626         }
627         if (defined($v = $hdr->header('Subject')) && ($v ne '')) {
628                 $v = ascii_html($v);
629                 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
630                 if ($srch) {
631                         $rv .= qq(Subject: <a\nhref="#r"\nid=t>$v</a>\n);
632                 } else {
633                         $rv .= "Subject: $v\n";
634                 }
635                 $title[0] = $v;
636         } else { # dummy anchor for thread skeleton at bottom of page
637                 $rv .= qq(<a\nhref="#r"\nid=t></a>) if $srch;
638                 $title[0] = '(no subject)';
639         }
640         if (defined($v = $hdr->header('Date'))) {
641                 $v = ascii_html($v);
642                 obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx; # possible :P
643                 $rv .= "Date: $v\n";
644         }
645         $ctx->{-title_html} = join(' - ', @title);
646         foreach (@$mids) {
647                 my $mid = PublicInbox::Hval->new_msgid($_) ;
648                 my $mhtml = $mid->as_html;
649                 if ($multiple) {
650                         my $href = $mid->{href};
651                         $rv .= "Message-ID: ";
652                         $rv .= "<a\nhref=\"../$href/\">";
653                         $rv .= "&lt;$mhtml&gt;</a> ";
654                         $rv .= "(<a\nhref=\"../$href/raw\">raw</a>)\n";
655                 } else {
656                         $rv .= "Message-ID: &lt;$mhtml&gt; ";
657                         $rv .= "(<a\nhref=\"raw\">raw</a>)\n";
658                 }
659         }
660         $rv .= _parent_headers($hdr, $srch);
661         $rv .= "\n";
662 }
663
664 sub thread_skel {
665         my ($dst, $ctx, $hdr, $tpfx) = @_;
666         my $srch = $ctx->{srch};
667         my $mid = mids($hdr)->[0];
668         my ($nr, $msgs) = $srch->get_thread($mid);
669         my $expand = qq(expand[<a\nhref="${tpfx}T/#u">flat</a>) .
670                         qq(|<a\nhref="${tpfx}t/#u">nested</a>]  ) .
671                         qq(<a\nhref="${tpfx}t.mbox.gz">mbox.gz</a>  ) .
672                         qq(<a\nhref="${tpfx}t.atom">Atom feed</a>);
673
674         my $parent = in_reply_to($hdr);
675         $$dst .= "\n<b>Thread overview: </b>";
676         if ($nr <= 1) {
677                 if (defined $parent) {
678                         $$dst .= "$expand\n ";
679                         $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
680                 } else {
681                         $$dst .= "[no followups] $expand\n";
682                 }
683                 $ctx->{next_msg} = undef;
684                 $ctx->{parent_msg} = $parent;
685                 return;
686         }
687
688         $$dst .= "$nr+ messages / $expand";
689         $$dst .= qq!  <a\nhref="#b">top</a>\n!;
690
691         my $subj = $hdr->header('Subject');
692         defined $subj or $subj = '';
693         $ctx->{prev_subj} = [ split(/ /, $srch->subject_normalized($subj)) ];
694         $ctx->{cur} = $mid;
695         $ctx->{prev_attr} = '';
696         $ctx->{prev_level} = 0;
697         $ctx->{dst} = $dst;
698
699         # reduce hash lookups in skel_dump
700         my $ibx = $ctx->{-inbox};
701         $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
702         walk_thread(thread_results($ctx, $msgs), $ctx, *skel_dump);
703
704         $ctx->{parent_msg} = $parent;
705 }
706
707 sub _parent_headers {
708         my ($hdr, $srch) = @_;
709         my $rv = '';
710
711         my $refs = references($hdr);
712         my $irt = pop @$refs;
713         if (defined $irt) {
714                 my $v = PublicInbox::Hval->new_msgid($irt);
715                 my $html = $v->as_html;
716                 my $href = $v->{href};
717                 $rv .= "In-Reply-To: &lt;";
718                 $rv .= "<a\nhref=\"../$href/\">$html</a>&gt;\n";
719         }
720
721         # do not display References: if search is present,
722         # we show the thread skeleton at the bottom, instead.
723         return $rv if $srch;
724
725         if (@$refs) {
726                 @$refs = map { linkify_ref_nosrch($_) } @$refs;
727                 $rv .= 'References: '. join("\n\t", @$refs) . "\n";
728         }
729         $rv;
730 }
731
732 sub html_footer {
733         my ($hdr, $standalone, $ctx, $rhref) = @_;
734
735         my $srch = $ctx->{srch} if $ctx;
736         my $upfx = '../';
737         my $tpfx = '';
738         my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
739         my $irt = '';
740         if ($idx && $srch) {
741                 $idx .= "\n";
742                 thread_skel(\$idx, $ctx, $hdr, $tpfx);
743                 my ($next, $prev);
744                 my $parent = '       ';
745                 $next = $prev = '    ';
746
747                 if (my $n = $ctx->{next_msg}) {
748                         $n = PublicInbox::Hval->new_msgid($n)->{href};
749                         $next = "<a\nhref=\"$upfx$n/\"\nrel=next>next</a>";
750                 }
751                 my $u;
752                 my $par = $ctx->{parent_msg};
753                 if ($par) {
754                         $u = PublicInbox::Hval->new_msgid($par)->{href};
755                         $u = "$upfx$u/";
756                 }
757                 if (my $p = $ctx->{prev_msg}) {
758                         $prev = PublicInbox::Hval->new_msgid($p)->{href};
759                         if ($p && $par && $p eq $par) {
760                                 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
761                                         'rel=prev>prev parent</a>';
762                                 $parent = '';
763                         } else {
764                                 $prev = "<a\nhref=\"$upfx$prev/\"\n" .
765                                         'rel=prev>prev</a>';
766                                 $parent = " <a\nhref=\"$u\">parent</a>" if $u;
767                         }
768                 } elsif ($u) { # unlikely
769                         $parent = " <a\nhref=\"$u\"\nrel=prev>parent</a>";
770                 }
771                 $irt = "$next $prev$parent ";
772         } else {
773                 $irt = '';
774         }
775         $rhref ||= '#R';
776         $irt .= qq(<a\nhref="$rhref">reply</a>);
777         $irt .= $idx;
778 }
779
780 sub linkify_ref_nosrch {
781         my $v = PublicInbox::Hval->new_msgid($_[0]);
782         my $html = $v->as_html;
783         my $href = $v->{href};
784         "&lt;<a\nhref=\"../$href/\">$html</a>&gt;";
785 }
786
787 sub anchor_for {
788         my ($msgid) = @_;
789         'm' . id_compress($msgid, 1);
790 }
791
792 sub ghost_parent {
793         my ($upfx, $mid) = @_;
794
795         $mid = PublicInbox::Hval->new_msgid($mid);
796         my $href = $mid->{href};
797         my $html = $mid->as_html;
798         qq{[parent not found: &lt;<a\nhref="$upfx$href/">$html</a>&gt;]};
799 }
800
801 sub indent_for {
802         my ($level) = @_;
803         $level ? INDENT x ($level - 1) : '';
804 }
805
806 sub find_mid_root {
807         my ($ctx, $level, $node, $idx) = @_;
808         ++$ctx->{root_idx} if $level == 0;
809         if ($node->{id} eq $ctx->{mid}) {
810                 $ctx->{found_mid_at} = $ctx->{root_idx};
811                 return 0;
812         }
813         1;
814 }
815
816 sub strict_loose_note ($) {
817         my ($nr) = @_;
818         my $msg =
819 "  -- strict thread matches above, loose matches on Subject: below --\n";
820
821         if ($nr > PublicInbox::Over::DEFAULT_LIMIT()) {
822                 $msg .=
823 "  -- use mbox.gz link to download all $nr messages --\n";
824         }
825         $msg;
826 }
827
828 sub thread_results {
829         my ($ctx, $msgs) = @_;
830         require PublicInbox::SearchThread;
831         my $ibx = $ctx->{-inbox};
832         my $rootset = PublicInbox::SearchThread::thread($msgs, *sort_ds, $ibx);
833
834         # FIXME: `tid' is broken on --reindex, so that needs to be fixed
835         # and preserved in the future.  This bug is hidden by `sid' matches
836         # in get_thread, so we never noticed it until now.  And even when
837         # reindexing is fixed, we'll keep this code until a SCHEMA_VERSION
838         # bump since reindexing is expensive and users may not do it
839
840         # loose threading could've returned too many results,
841         # put the root the message we care about at the top:
842         my $mid = $ctx->{mid};
843         if (defined($mid) && scalar(@$rootset) > 1) {
844                 $ctx->{root_idx} = -1;
845                 my $nr = scalar @$msgs;
846                 walk_thread($rootset, $ctx, *find_mid_root);
847                 my $idx = $ctx->{found_mid_at};
848                 if (defined($idx) && $idx != 0) {
849                         my $tip = splice(@$rootset, $idx, 1);
850                         @$rootset = reverse @$rootset;
851                         unshift @$rootset, $tip;
852                         $ctx->{sl_note} = strict_loose_note($nr);
853                 }
854         }
855         $rootset
856 }
857
858 sub missing_thread {
859         my ($ctx) = @_;
860         require PublicInbox::ExtMsg;
861         PublicInbox::ExtMsg::ext_msg($ctx);
862 }
863
864 sub _msg_date {
865         my ($hdr) = @_;
866         fmt_ts(msg_datestamp($hdr));
867 }
868
869 sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
870
871 sub dedupe_subject {
872         my ($prev_subj, $subj, $val) = @_;
873
874         my $omit = ''; # '"' denotes identical text omitted
875         my (@prev_pop, @curr_pop);
876         while (@$prev_subj && @$subj && $subj->[-1] eq $prev_subj->[-1]) {
877                 push(@prev_pop, pop(@$prev_subj));
878                 push(@curr_pop, pop(@$subj));
879                 $omit ||= $val;
880         }
881         pop @$subj if @$subj && $subj->[-1] =~ /^re:\s*/i;
882         if (scalar(@curr_pop) == 1) {
883                 $omit = '';
884                 push @$prev_subj, @prev_pop;
885                 push @$subj, @curr_pop;
886         }
887         $omit;
888 }
889
890 sub skel_dump {
891         my ($ctx, $level, $node) = @_;
892         my $smsg = $node->{smsg} or return _skel_ghost($ctx, $level, $node);
893
894         my $dst = $ctx->{dst};
895         my $cur = $ctx->{cur};
896         my $mid = $smsg->{mid};
897
898         if ($level == 0 && $ctx->{skel_dump_roots}++) {
899                 $$dst .= delete $ctx->{sl_note} || '';
900         }
901
902         my $f = ascii_html($smsg->from_name);
903         my $obfs_ibx = $ctx->{-obfs_ibx};
904         obfuscate_addrs($obfs_ibx, $f) if $obfs_ibx;
905
906         my $d = fmt_ts($smsg->{ds}) . ' ' . indent_for($level) . th_pfx($level);
907         my $attr = $f;
908         $ctx->{first_level} ||= $level;
909
910         if ($attr ne $ctx->{prev_attr} || $ctx->{prev_level} > $level) {
911                 $ctx->{prev_attr} = $attr;
912         }
913         $ctx->{prev_level} = $level;
914
915         if ($cur) {
916                 if ($cur eq $mid) {
917                         delete $ctx->{cur};
918                         $$dst .= "<b>$d<a\nid=r\nhref=\"#t\">".
919                                  "$attr [this message]</a></b>\n";
920                         return 1;
921                 } else {
922                         $ctx->{prev_msg} = $mid;
923                 }
924         } else {
925                 $ctx->{next_msg} ||= $mid;
926         }
927
928         # Subject is never undef, this mail was loaded from
929         # our Xapian which would've resulted in '' if it were
930         # really missing (and Filter rejects empty subjects)
931         my @subj = split(/ /, $ctx->{srch}->subject_normalized($smsg->subject));
932
933         # remove common suffixes from the subject if it matches the previous,
934         # so we do not show redundant text at the end.
935         my $prev_subj = $ctx->{prev_subj} || [];
936         $ctx->{prev_subj} = [ @subj ];
937         my $omit = dedupe_subject($prev_subj, \@subj, '&#34; ');
938         my $end;
939         if (@subj) {
940                 my $subj = join(' ', @subj);
941                 $subj = ascii_html($subj);
942                 obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
943                 $end = "$subj</a> $omit$f\n"
944         } else {
945                 $end = "$f</a>\n";
946         }
947         my $m;
948         my $id = '';
949         my $mapping = $ctx->{mapping};
950         if ($mapping) {
951                 my $map = $mapping->{$mid};
952                 $id = id_compress($mid, 1);
953                 $m = '#m'.$id;
954                 $map->[0] = "$d<a\nhref=\"$m\">$end";
955                 $id = "\nid=r".$id;
956         } else {
957                 $m = $ctx->{-upfx}.mid_escape($mid).'/';
958         }
959         $$dst .=  $d . "<a\nhref=\"$m\"$id>" . $end;
960         1;
961 }
962
963 sub _skel_ghost {
964         my ($ctx, $level, $node) = @_;
965
966         my $mid = $node->{id};
967         my $d = $ctx->{pct} ? '    [irrelevant] ' # search result
968                             : '     [not found] ';
969         $d .= indent_for($level) . th_pfx($level);
970         my $upfx = $ctx->{-upfx};
971         my $m = PublicInbox::Hval->new_msgid($mid);
972         my $href = $upfx . $m->{href} . '/';
973         my $html = $m->as_html;
974
975         my $mapping = $ctx->{mapping};
976         my $map = $mapping->{$mid} if $mapping;
977         if ($map) {
978                 my $id = id_compress($mid, 1);
979                 $map->[0] = $d . qq{&lt;<a\nhref=#r$id>$html</a>&gt;\n};
980                 $d .= qq{&lt;<a\nhref="$href"\nid=r$id>$html</a>&gt;\n};
981         } else {
982                 $d .= qq{&lt;<a\nhref="$href">$html</a>&gt;\n};
983         }
984         my $dst = $ctx->{dst};
985         $$dst .= $d;
986         1;
987 }
988
989 sub sort_ds {
990         [ sort {
991                 (eval { $a->topmost->{smsg}->ds } || 0) <=>
992                 (eval { $b->topmost->{smsg}->ds } || 0)
993         } @{$_[0]} ];
994 }
995
996 # accumulate recent topics if search is supported
997 # returns 200 if done, 404 if not
998 sub acc_topic {
999         my ($ctx, $level, $node) = @_;
1000         my $srch = $ctx->{srch};
1001         my $mid = $node->{id};
1002         my $x = $node->{smsg} || $ctx->{-inbox}->smsg_by_mid($mid);
1003         my ($subj, $ds);
1004         my $topic;
1005         if ($x) {
1006                 $subj = $x->subject;
1007                 $subj = $srch->subject_normalized($subj);
1008                 $ds = $x->ds;
1009                 if ($level == 0) {
1010                         $topic = [ $ds, 1, { $subj => $mid }, $subj ];
1011                         $ctx->{-cur_topic} = $topic;
1012                         push @{$ctx->{order}}, $topic;
1013                         return 1;
1014                 }
1015
1016                 $topic = $ctx->{-cur_topic}; # should never be undef
1017                 $topic->[0] = $ds if $ds > $topic->[0];
1018                 $topic->[1]++;
1019                 my $seen = $topic->[2];
1020                 if (scalar(@$topic) == 3) { # parent was a ghost
1021                         push @$topic, $subj;
1022                 } elsif (!$seen->{$subj}) {
1023                         push @$topic, $level, $subj;
1024                 }
1025                 $seen->{$subj} = $mid; # latest for subject
1026         } else { # ghost message
1027                 return 1 if $level != 0; # ignore child ghosts
1028                 $topic = [ -666, 0, {} ];
1029                 $ctx->{-cur_topic} = $topic;
1030                 push @{$ctx->{order}}, $topic;
1031         }
1032         1;
1033 }
1034
1035 sub dump_topics {
1036         my ($ctx) = @_;
1037         my $order = delete $ctx->{order}; # [ ds, subj1, subj2, subj3, ... ]
1038         if (!@$order) {
1039                 $ctx->{-html_tip} = '<pre>[No topics in range]</pre>';
1040                 return 404;
1041         }
1042
1043         my @out;
1044         my $ibx = $ctx->{-inbox};
1045         my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef;
1046         my $srch = $ctx->{srch};
1047
1048         # sort by recency, this allows new posts to "bump" old topics...
1049         foreach my $topic (sort { $b->[0] <=> $a->[0] } @$order) {
1050                 my ($ds, $n, $seen, $top, @ex) = @$topic;
1051                 @$topic = ();
1052                 next unless defined $top;  # ghost topic
1053                 my $mid = delete $seen->{$top};
1054                 my $href = mid_escape($mid);
1055                 my $prev_subj = [ split(/ /, $top) ];
1056                 $top = PublicInbox::Hval->new($top)->as_html;
1057                 $ds = fmt_ts($ds);
1058
1059                 # $n isn't the total number of posts on the topic,
1060                 # just the number of posts in the current results window
1061                 my $anchor;
1062                 if ($n == 1) {
1063                         $n = '';
1064                         $anchor = '#u'; # top of only message
1065                 } else {
1066                         $n = " ($n+ messages)";
1067                         $anchor = '#t'; # thread skeleton
1068                 }
1069
1070                 my $mbox = qq(<a\nhref="$href/t.mbox.gz">mbox.gz</a>);
1071                 my $atom = qq(<a\nhref="$href/t.atom">Atom</a>);
1072                 my $s = "<a\nhref=\"$href/T/$anchor\"><b>$top</b></a>\n" .
1073                         " $ds UTC $n - $mbox / $atom\n";
1074                 for (my $i = 0; $i < scalar(@ex); $i += 2) {
1075                         my $level = $ex[$i];
1076                         my $subj = $ex[$i + 1];
1077                         $mid = delete $seen->{$subj};
1078                         my @subj = split(/ /, $srch->subject_normalized($subj));
1079                         my @next_prev = @subj; # full copy
1080                         my $omit = dedupe_subject($prev_subj, \@subj, ' &#34;');
1081                         $prev_subj = \@next_prev;
1082                         $subj = ascii_html(join(' ', @subj));
1083                         obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
1084                         $href = mid_escape($mid);
1085                         $s .= indent_for($level) . TCHILD;
1086                         $s .= "<a\nhref=\"$href/T/#u\">$subj</a>$omit\n";
1087                 }
1088                 push @out, $s;
1089         }
1090         $ctx->{-html_tip} = '<pre>' . join("\n", @out) . '</pre>';
1091         200;
1092 }
1093
1094 sub ts2str ($) {
1095         my ($ts) = @_;
1096         POSIX::strftime('%Y%m%d%H%M%S', gmtime($ts));
1097 }
1098
1099 sub str2ts ($) {
1100         my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $_[0]);
1101         timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy);
1102 }
1103
1104 sub pagination_footer ($$) {
1105         my ($ctx, $latest) = @_;
1106         delete $ctx->{qp} or return;
1107         my $next = $ctx->{next_page} || '';
1108         my $prev = $ctx->{prev_page} || '';
1109         if ($prev) {
1110                 $next = $next ? "$next " : '     ';
1111                 $prev .= qq! <a\nhref='$latest'>latest</a>!;
1112         }
1113         "<hr><pre>page: $next$prev</pre>";
1114 }
1115
1116 sub index_nav { # callback for WwwStream
1117         my (undef, $ctx) = @_;
1118         pagination_footer($ctx, '.')
1119 }
1120
1121 sub paginate_recent ($$) {
1122         my ($ctx, $lim) = @_;
1123         my $t = $ctx->{qp}->{t} || '';
1124         my $opts = { limit => $lim };
1125         my ($after, $before);
1126
1127         # Xapian uses '..' but '-' is perhaps friendier to URL linkifiers
1128         # if only $after exists "YYYYMMDD.." because "." could be skipped
1129         # if interpreted as an end-of-sentence
1130         $t =~ s/\A(\d{8,14})-// and $after = str2ts($1);
1131         $t =~ /\A(\d{8,14})\z/ and $before = str2ts($1);
1132
1133         my $ibx = $ctx->{-inbox};
1134         my $msgs = $ibx->recent($opts, $after, $before);
1135         my $nr = scalar @$msgs;
1136         if ($nr < $lim && defined($after)) {
1137                 $after = $before = undef;
1138                 $msgs = $ibx->recent($opts);
1139                 $nr = scalar @$msgs;
1140         }
1141         my $more = $nr == $lim;
1142         my ($newest, $oldest);
1143         if ($nr) {
1144                 $newest = $msgs->[0]->{ts};
1145                 $oldest = $msgs->[-1]->{ts};
1146                 # if we only had $after, our SQL query in ->recent ordered
1147                 if ($newest < $oldest) {
1148                         ($oldest, $newest) = ($newest, $oldest);
1149                         $more = 0 if defined($after) && $after < $oldest;
1150                 }
1151         }
1152         if (defined($oldest) && $more) {
1153                 my $s = ts2str($oldest);
1154                 $ctx->{next_page} = qq!<a\nhref="?t=$s"\nrel=next>next</a>!;
1155         }
1156         if (defined($newest) && (defined($before) || defined($after))) {
1157                 my $s = ts2str($newest);
1158                 $ctx->{prev_page} = qq!<a\nhref="?t=$s-"\nrel=prev>prev</a>!;
1159         }
1160         $msgs;
1161 }
1162
1163 sub index_topics {
1164         my ($ctx) = @_;
1165         my $msgs = paginate_recent($ctx, 200); # 200 is our window
1166         if (@$msgs) {
1167                 walk_thread(thread_results($ctx, $msgs), $ctx, *acc_topic);
1168         }
1169         PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav);
1170 }
1171
1172 sub thread_adj_level {
1173         my ($ctx, $level) = @_;
1174
1175         my $max = $ctx->{cur_level};
1176         if ($level <= 0) {
1177                 return ('', '') if $max == 0; # flat output
1178
1179                 # reset existing lists
1180                 my $beg = $max > 1 ? ('</ul></li>' x ($max - 1)) : '';
1181                 $ctx->{cur_level} = 0;
1182                 ("$beg</ul>", '');
1183         } elsif ($level == $max) { # continue existing list
1184                 qw(<li> </li>);
1185         } elsif ($level < $max) {
1186                 my $beg = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
1187                 $ctx->{cur_level} = $level;
1188                 ("$beg<li>", '</li>');
1189         } else { # ($level > $max) # start a new level
1190                 $ctx->{cur_level} = $level;
1191                 my $beg = ($max ? '<li>' : '') . '<ul><li>';
1192                 ($beg, '</li>');
1193         }
1194 }
1195
1196 sub ghost_index_entry {
1197         my ($ctx, $level, $node) = @_;
1198         my ($beg, $end) = thread_adj_level($ctx,  $level);
1199         $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $node->{id})
1200                 . '</pre>' . $end;
1201 }
1202
1203 1;