1 # GotMArc was written by Omar Polo <op@openbsd.org> and is placed in
2 # the public domain. The author hereby disclaims copyright to this
12 our @ISA = qw(Exporter);
13 our @EXPORT_OK = qw(san urlencode parse initpage endpage index_header
14 thread_header threntry thrslice);
26 unless (defined($str)) {
27 my ($pkg, $file, $line) = caller 1;
28 die "bad $pkg / $file:$line";
30 $str =~ s/([^-_~.A-Za-z0-9])/sprintf("%%%2X", ord($1))/ge;
43 my ($indent, $fname, $date, $from, $subj) = $line =~ m{
44 ^([^-]*)- # the indent level
46 (\d{4}-\d\d-\d\d[ ]\d\d:\d\d) # date
49 }x or die "can't parse: $line";
51 my $level = length($indent);
52 $level = 10 if $indent =~ m/\.\.\d+\.\./;
57 my ($time, $id) = split /\./, basename($fname);
58 my $mid = "$time.$id";
60 return {level => $level, fname => $fname,
61 mid => $mid, date => $date, from => $from, subj => $subj};
67 open my $fh, "<", $path or die "can't open $path: $!";
71 my $small_logo = readall "logo-small.html";
72 my $hdr = readall "head.html";
73 my $foot = readall "foot.html";
74 my $idxhdr = readall "index-header.html";
77 my ($fh, $title) = @_;
78 say $fh $hdr =~ s/TITLE/$title/r;
87 my ($fh, $page, $subtitle) = @_;
88 my $html = $idxhdr =~ s/PAGE/$page/r;
89 $html =~ s/SUBTITLE/$subtitle/;
94 my ($fh, $e, $mail, $p, $n, $spoiler) = @_;
95 $spoiler = 0 unless defined $spoiler;
99 my $enctid = urlencode $mail->{tid} if defined $mail;
100 my $encmid = urlencode $mail->{mid} if defined $mail;
102 print $fh "<header class='mail-header'>\n";
105 print $fh $small_logo;
106 print $fh "<a href='/'>Index</a>";
107 print $fh " | <a href='/thread/$enctid.html#$encmid'>Thread</a>"
109 print $fh " | <a href='/search'>Search</a>";
113 foreach my $entry (@entries) {
114 my ($k, $v) = split /: /, $entry, 2;
116 say $fh "<dt>$k:</dt><dd>$v</dd>";
120 say $fh "<p>Download raw <a href='/text/$encmid.txt'>body</a>.</p>"
123 say $fh "<details>" if $spoiler;
124 say $fh "<summary>Thread</summary>" if $spoiler;
125 thrslice($fh, $mail, $p, $n) if defined $p and defined $n;
126 say $fh "</details>" if $spoiler;
128 say $fh "</header>\n";
132 my ($fh, $type, $base, $last_level, $mail, $cur) = @_;
133 my $level = $mail->{level} - $base;
135 say $fh "</ul></li>" x ($last_level - $level) if $last_level > $level;
136 say $fh "<li><ul>" if $last_level < $level;
138 my $encmid = urlencode $mail->{mid};
140 print $fh "<li id='$encmid' class='mail'>";
141 print $fh "<p class='mail-meta'>";
142 print $fh "<time>$mail->{date}</time> ";
143 print $fh "<span class='from'>$mail->{from}</span>";
144 print $fh "<span class='colon'>:</span>";
146 print $fh "<p class='subject'>";
148 my $subj = $mail->{subj};
149 if (!defined($cur) || $mail->{mid} ne $cur->{mid}) {
150 print $fh "<a href='/$type/$encmid.html'>$subj</a>";
152 print $fh "<span>$subj</span>";
165 $l = $_->{level} if $_->{level} < $l;
171 my ($fh, $mail, $p, $n) = @_;
174 my @thread = (@prev, $mail, @next);
175 return unless @thread;
176 my $base = min_level @thread;
178 print $fh "<div class='thread'>";
179 print $fh "<ul class='mails'>";
180 $level = threntry $fh, "mail", $base, $level, $_, $mail for @thread;
181 print $fh "</ul></li>" x $level;
182 print $fh "</ul></div>";