-sub thread_adj_level {
- my ($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)) : '';
- $state->{fh}->write($x . '</ul>');
- $state->{cur_level} = 0;
- return '';
- }
- if ($level == $max) { # continue existing list
- $state->{fh}->write('<li>');
- } elsif ($level < $max) {
- my $x = $max > 1 ? ('</ul></li>' x ($max - $level)) : '';
- $state->{fh}->write($x .= '<li>');
- $state->{cur_level} = $level;
- } else { # ($level > $max) # start a new level
- $state->{cur_level} = $level;
- $state->{fh}->write(($max ? '<li>' : '') . '<ul><li>');
- }
- '</li>';
-}
-
-sub ghost_flush {
- my ($state, $upfx, $mid, $level) = @_;
- my $end = '<pre>'. ghost_parent($upfx, $mid) . '</pre>';
- $state->{fh}->write($end .= thread_adj_level($state, $level));
-}
-
-sub __thread_entry {
- my ($state, $mime, $level) = @_;
-
- # lazy load the full message from mini_mime:
- $mime = eval {
- my $path = mid2path(mid_clean(mid_mime($mime)));
- Email::MIME->new($state->{ctx}->{git}->cat_file('HEAD:'.$path));
- } or return;
-
- thread_html_head($mime, $state) if $state->{anchor_idx} == 0;
- if (my $ghost = delete $state->{ghost}) {
- # n.b. ghost messages may only be parents, not children
- foreach my $g (@$ghost) {
- ghost_flush($state, '../../', @$g);
- }
- }
- my $end = thread_adj_level($state, $level);
- index_entry($mime, $level, $state);
- $state->{fh}->write($end) if $end;
-
- 1;
-}
-