Commit Diff


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 "<nav>";
 
+	if (@prev) {
+		my $mail = $prev[-1];
+		my $encmid = $mail->{mid};
+		say $fh "<a href='/mail/$encmid.html'>Previous</a>";
+	} else {
+		say $fh "<span>Previous</span>";
+	}
+
+	if (defined($mid) && defined($tid)) {
+		my $encmid = urlencode $mid;
+		my $enctid = urlencode $tid;
+		say $fh "<a href='/text/$encmid.txt'>Raw body</a>";
+		say $fh "<a href='/thread/$enctid.html#$encmid'>Thread</a>";
+	}
+
+	if (@next) {
+		my $mail = $next[0];
+		my $encmid = $mail->{mid};
+		say $fh "<a href='/mail/$encmid.html'>Next</a>";
+	} else {
+		say $fh "<span>Next</span>";
+	}
+
+	print $fh "</nav>";
+}
+
+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 "<pre>";
 	while (<$mshow>) {
@@ -141,14 +173,8 @@ while (<>) {
 	}
 	say $fh "</ul>" if $partno > 0;
 
-	my $encmid = urlencode $mail->{mid};
-	my $enctid = urlencode $tid;
+	thrnav $fh, $prev, $next, $mail->{mid}, $mail->{tid};
 
-	print $fh "<nav>";
-	print $fh "<a href='/text/$encmid.txt'>Raw body</a>";
-	print $fh "<a href='/thread/$enctid.html#$encmid'>Thread</a>";
-	print $fh "</nav>\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;