Blob
Date:
Tue Aug 30 11:40:44 2022
UTC
Message:
urlencode the mail/thread id
reminded by semarie@, thanks!
#!/usr/bin/env perl## mkindex was written by Omar Polo <op@openbsd.org> and is placed in# the public domain. The author hereby disclaims copyright to this# source code.use open ":std", ":encoding(UTF-8)";use utf8;use strict;use warnings;use v5.32;use File::Temp qw(tempfile);use OpenBSD::Pledge;use OpenBSD::Unveil;use lib ".";use GotMArc qw(parse san urlencode initpage endpage index_header thread_header);my $outdir = $ENV{'OUTDIR'};die 'Set $OUTDIR' unless defined $outdir;my $tfh; # thread file handlemy $pfh; # page file handlemy $page = 0;my @pages;my @files;my $from_day;my $to_day;my $threads_seen = 0;my $last_level = 0;my $threads = 0;my $threads_per_page = 100;sub maxs {my ($a, $b) = @_;return $a unless defined $b;return $a gt $b ? $a : $b;}sub mins {my ($a, $b) = @_;return $a unless defined $b;return $a lt $b ? $a : $b;}sub pagename {my $i = shift;return $i == 1 && "index.html" || "$i.html";}sub endfile {say $pfh '</ul></div>';close($pfh);push @pages, "$from_day - $to_day";}sub nextfile {endfile if defined $pfh;$page += 1;my $path;($pfh, $path) = tempfile "/tmp/gotmarc.index.XXXXXXXXXX";push @files, $path;say $pfh "<div class='thread'><ul>";}sub nav {my ($pfh, $n) = @_;my ($first, $last) = (pagename(1), pagename($page));my ($next, $prev) = (pagename($n+1), pagename($n-1));say $pfh "<nav>";say $pfh "<a href='$first'>First</a>" if $n > 2;say $pfh "<a href='$prev'>Prev</a>" if $n > 1;say $pfh "<a href='$next'>Next</a>" if $n < $page;say $pfh "<a href='$last'>Last</a>" if $n < $page - 1;say $pfh "</nav>";}sub copyfrom {my ($path, $fh) = @_;# there are probably faster ways to do this like File::Copy,# but it bypasses the bufio cache...open(my $pfh, '<', $path) or die "can't open $path: $!";print $fh $_ while <$pfh>;}sub renderpages {close($pfh);for (my $i = 1; $i <= $page; $i++) {my $name = pagename($i);my $path = shift @files;my $dest = "$outdir/$name";open(my $pfh, '>', $dest)or die "can't open $dest for writing: $!";my $title = "Game of Trees Mail Archive | page $i";my $subtitle = $pages[$i-1];initpage($pfh, $title);index_header $pfh, $i, $subtitle;say $pfh "<main>";nav $pfh, $i if $page > 1;copyfrom($path, $pfh);nav $pfh, $i if $page > 1;say $pfh "</main>";endpage($pfh);close($pfh);unlink $path;}}sub endthread {say $tfh "</ul></li>" x $last_level;say $tfh "</ul>\n</div>\n";endpage($tfh);close($tfh);$last_level = 0;}sub nextthread {endthread if defined $tfh;my ($mid, $subj) = @_;my $dest = "$outdir/thread/$mid.html";open($tfh, '>', $dest) or die "can't open $dest: $!";initpage($tfh, $subj);thread_header $tfh, undef, undef, ["Thread: $subj"];print $tfh "<div class='thread'><ul class='mails'>\n";}sub entry {my ($fh, $type, $mid, $date, $from, $subj) = @_;my $encmid = urlencode $mid;print $fh "<li id='$encmid' class='mail'>";print $fh "<p class='mail-meta'>";print $fh "<time>$date</time> ";print $fh "<span class='from'>$from</span>";print $fh "<span class='colon'>:</span>";print $fh "</p>";print $fh "<p class='subject'>";print $fh "<a href='/$type/$encmid.html'>$subj</a>";print $fh "</p>";print $fh "</li>\n";}sub thread_entry {my ($fh, $mid, $level, $date, $from, $subj) = @_;say $fh "</ul>\n</li>" x ($last_level - $level) if $last_level > $level;say $fh "<li>\n<ul>" if $last_level < $level;entry $fh, "mail", $mid, $date, $from, $subj;}unveil($outdir, "rwc") or die "unveil $outdir: $!";unveil(".", "r") or die "unveil .: $!";# can't use tmppath because File::Temp checks wether /tmp exists.unveil("/tmp", "rwc") or die "unveil /tmp: $!";# fattr for File::Temppledge("stdio rpath wpath cpath fattr") or die "pledge: $!";nextfile;while (<>) {my ($level, $fname, $mid, $date, $from, $subj) = parse;if ($level == 0) {nextthread $mid, $subj;$threads++;if ($threads > $threads_per_page) {nextfile;$threads = 0;$to_day = undef;$from_day = undef;}my $day = $date =~ s/ .*//r;$to_day = mins $day, $to_day;$from_day = maxs $day, $from_day;entry $pfh, "thread", $mid, $date, $from, $subj;}thread_entry $tfh, $mid, $level, $date, $from, $subj;$last_level = $level;$threads_seen = 1;}endfile;endthread if $threads_seen;renderpages;
Omar Polo