3 # mkindex was written by Omar Polo <op@openbsd.org> and is placed in
4 # the public domain. The author hereby disclaims copyright to this
7 use open ":std", ":encoding(UTF-8)";
12 use File::Temp qw(tempfile);
18 use GotMArc qw(parse san urlencode initpage endpage index_header thread_header);
20 my $outdir = $ENV{'OUTDIR'};
21 die 'Set $OUTDIR' unless defined $outdir;
23 my $tfh; # thread file handle
24 my $pfh; # page file handle
39 my $threads_per_page = 100;
43 return $a unless defined $b;
44 return $a gt $b ? $a : $b;
49 return $a unless defined $b;
50 return $a lt $b ? $a : $b;
55 return $i == 1 && "index.html" || "$i.html";
59 say $pfh '</ul></div>';
61 push @pages, "$from_day - $to_day";
65 endfile if defined $pfh;
69 ($pfh, $path) = tempfile "/tmp/gotmarc.index.XXXXXXXXXX";
70 binmode($pfh, ':utf8');
72 say $pfh "<div class='thread'><ul>";
77 my ($first, $last) = (pagename(1), pagename($page));
78 my ($next, $prev) = (pagename($n+1), pagename($n-1));
81 say $pfh "<a href='$first'>First</a>" if $n > 2;
82 say $pfh "<a href='$prev'>Prev</a>" if $n > 1;
83 say $pfh "<a href='$next'>Next</a>" if $n < $page;
84 say $pfh "<a href='$last'>Last</a>" if $n < $page - 1;
91 # there are probably faster ways to do this like File::Copy,
92 # but it bypasses the bufio cache...
93 open(my $pfh, '<', $path) or die "can't open $path: $!";
94 print $fh $_ while <$pfh>;
100 for (my $i = 1; $i <= $page; $i++) {
101 my $name = pagename($i);
102 my $path = shift @files;
103 my $dest = "$outdir/$name";
105 open(my $pfh, '>', $dest)
106 or die "can't open $dest for writing: $!";
108 my $title = "Game of Trees Mail Archive | page $i";
109 my $subtitle = $pages[$i-1];
111 initpage($pfh, $title);
112 index_header $pfh, $i, $subtitle;
115 nav $pfh, $i if $page > 1;
116 copyfrom($path, $pfh);
117 nav $pfh, $i if $page > 1;
128 say $tfh "</ul></li>" x $last_level;
129 say $tfh "</ul>\n</div>\n";
137 endthread if defined $tfh;
138 my ($mid, $subj) = @_;
139 my $dest = "$outdir/thread/$mid.html";
140 open($tfh, '>', $dest) or die "can't open $dest: $!";
141 initpage($tfh, $subj);
142 thread_header $tfh, undef, undef, ["Thread: $subj"];
143 print $tfh "<div class='thread'><ul class='mails'>\n";
147 my ($fh, $type, $mid, $date, $from, $subj) = @_;
148 my $encmid = urlencode $mid;
150 print $fh "<li id='$encmid' class='mail'>";
151 print $fh "<p class='mail-meta'>";
152 print $fh "<time>$date</time> ";
153 print $fh "<span class='from'>$from</span>";
154 print $fh "<span class='colon'>:</span>";
156 print $fh "<p class='subject'>";
157 print $fh "<a href='/$type/$encmid.html'>$subj</a>";
163 my ($fh, $mid, $date, $from, $subj) = @_;
165 entry $fh, "thread", $mid, $date, $from, $subj;
169 my ($fh, $mid, $level, $date, $from, $subj) = @_;
171 say $fh "</ul>\n</li>" x ($last_level - $level) if $last_level > $level;
172 say $fh "<li>\n<ul>" if $last_level < $level;
174 entry $fh, "mail", $mid, $date, $from, $subj;
177 unveil($outdir, "rwc") or die "unveil $outdir: $!";
178 unveil(".", "r") or die "unveil .: $!";
180 # can't use tmppath because File::Temp checks wether /tmp exists.
181 unveil("/tmp", "rwc") or die "unveil /tmp: $!";
183 # fattr for File::Temp
184 pledge("stdio rpath wpath cpath fattr") or die "pledge: $!";
191 if ($mail->{level} == 0) {
192 nextthread $mail->{mid}, $mail->{subj};
195 if ($threads > $threads_per_page) {
202 my $day = $mail->{date} =~ s/ .*//r;
203 $to_day = mins $day, $to_day;
204 $from_day = maxs $day, $from_day;
207 thread_entry($tfh, $mail->{mid}, $mail->{level}, $mail->{date},
208 $mail->{from}, $mail->{subj});
209 $last_level = $mail->{level};
212 index_entry $pfh, $last_tid, $last_date, $last_from, $last_subj
213 if defined $last_tid && $mail->{level} == 0;
215 # `gt' on dates works because the format used allow for
216 # lexicographic comparisons.
217 if ($mail->{level} == 0 || $mail->{date} gt $last_date) {
218 $last_date = $mail->{date};
219 $last_from = $mail->{from};
222 if ($mail->{level} == 0) {
223 $last_tid = $mail->{mid};
224 $last_subj = $mail->{subj};
228 index_entry $pfh, $last_tid, $last_date, $last_from, $last_subj
229 if defined $last_tid;
232 endthread if $threads_seen;