-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkindex
executable file
·217 lines (172 loc) · 4.34 KB
/
mkindex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/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 SMArc qw(parse san urlencode initpage endpage index_header
search thread_header threntry);
my $outdir = $ENV{'OUTDIR'};
die 'Set $OUTDIR' unless defined $outdir;
my $tfh; # thread file handle
my $pfh; # page file handle
my $page = 0;
my @pages;
my @files;
my $from_day;
my $to_day;
my $threads_seen = 0;
my $last_level = 0;
my $last_tid;
my $last_date;
my $last_from;
my $last_subj;
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/smarc.index.XXXXXXXXXX";
binmode($pfh, ':utf8');
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 = "page $i";
my $subtitle = $pages[$i-1];
initpage($pfh, $title);
index_header $pfh, $i, $subtitle;
say $pfh "<main>";
nav $pfh, $i if $page > 1;
search $pfh;
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, ["Thread: $subj"];
print $tfh "<div class='thread'><ul class='mails'>\n";
}
sub index_entry {
my ($fh, $mid, $date, $from, $subj) = @_;
# synthetic mail hash
my $mail = {
mid => $mid,
level => 0,
date => $date,
from => $from,
subj => $subj,
};
threntry $fh, "thread", 0, 0, $mail;
}
if (`uname` =~ "OpenBSD") {
use OpenBSD::Pledge;
use OpenBSD::Unveil;
unveil($outdir, "rwc") or die "unveil $outdir: $!";
# can't use tmppath because File::Temp checks whether /tmp exists.
unveil("/tmp", "rwc") or die "unveil /tmp: $!";
# fattr for File::Temp
pledge("stdio rpath wpath cpath fattr") or die "pledge: $!";
}
nextfile;
while (<>) {
my $mail = parse $_;
if ($mail->{level} == 0) {
nextthread $mail->{mid}, $mail->{subj};
$threads++;
if ($threads > $threads_per_page) {
nextfile;
$threads = 0;
$to_day = undef;
$from_day = undef;
}
my $day = $mail->{date} =~ s/ .*//r;
$to_day = mins $day, $to_day;
$from_day = maxs $day, $from_day;
}
$last_level = threntry $tfh, "mail", 0, $last_level, $mail;
$threads_seen = 1;
index_entry $pfh, $last_tid, $last_date, $last_from, $last_subj
if defined $last_tid && $mail->{level} == 0;
# `gt' on dates works because the format used allow for
# lexicographic comparisons.
if ($mail->{level} == 0 || $mail->{date} gt $last_date) {
$last_date = $mail->{date};
$last_from = $mail->{from};
}
if ($mail->{level} == 0) {
$last_tid = $mail->{mid};
$last_subj = $mail->{subj};
}
}
index_entry $pfh, $last_tid, $last_date, $last_from, $last_subj
if defined $last_tid;
endfile;
endthread if $threads_seen;
renderpages;