commit 1edd511a76a93626b5d324cb508bfec023ba4fc5 from: Omar Polo date: Sat Apr 01 20:56:50 2023 UTC print prev/next link in mail page commit - 5aeea3f33329be176c00909867498a471b7ff86b commit + 1edd511a76a93626b5d324cb508bfec023ba4fc5 blob - ecd5011e9c71eaac72ae668c2b6fe954eab40182 blob + e3d4aba6d2a57b5ca5ac38769899ba01ccf3bdd5 --- mexp +++ mexp @@ -10,6 +10,8 @@ use strict; use warnings; use v5.32; +use List::Util qw(max min); + use OpenBSD::Pledge; use OpenBSD::Unveil; @@ -56,13 +58,42 @@ sub humanize { } } -my $tid; -while (<>) { - my $mail = parse $_; +sub thrnav { + my ($fh, $p, $n, $mid, $tid) = @_; + my @prev = @{$p}; + my @next = @{$n}; - $tid = $mail->{mid} if $mail->{level} == 0; - die "unknown tid" unless defined $tid; + return if !@prev && !@next; + print $fh ""; +} + +sub export_one { + my ($mail, $prev, $next) = @_; my $dest = "$outdir/mail/$mail->{mid}.html"; open(my $fh, '>', "$dest") or die "can't open $dest: $!"; @@ -86,7 +117,8 @@ while (<>) { } say $text ""; - thread_header $fh, $tid, $mail->{mid}, \@hdrs; + thread_header $fh, $mail->{tid}, $mail->{mid}, \@hdrs; + thrnav $fh, $prev, $next; print $fh "
";
 	while (<$mshow>) {
@@ -141,14 +173,8 @@ while (<>) {
 	}
 	say $fh "" if $partno > 0;
 
-	my $encmid = urlencode $mail->{mid};
-	my $enctid = urlencode $tid;
+	thrnav $fh, $prev, $next, $mail->{mid}, $mail->{tid};
 
-	print $fh "\n";
-
 	endpage $fh;
 
 	close($text);
@@ -158,3 +184,35 @@ while (<>) {
 
 	unlink $parts;
 }
+
+sub export {
+	my @thread = @_;
+
+	for (my $i = 0; $i < @thread; $i++) {
+		my (@prev, @next);
+		@prev = @thread[max($i-3, 0)..$i-1] if $i > 0;
+		@next = @thread[$i+1..min($i+3, @thread - 1)]
+		    if $i + 1 < @thread;
+		export_one $thread[$i], \@prev, \@next;
+	}
+}
+
+my $tid;
+my @thread;
+while (<>) {
+	my $mail = parse $_;
+
+	if ($mail->{level} == 0 && @thread) {
+		export @thread;
+		@thread = ();
+	}
+
+	$tid = $mail->{mid} if $mail->{level} == 0;
+	die "unknown tid" unless defined $tid;
+	$mail->{tid} = $tid;
+
+	# export_one $mail, $tid
+	push @thread, $mail;
+}
+
+export @thread if @thread;