Blob


1 #!/usr/bin/env perl
3 use open ":std", ":encoding(UTF-8)";
4 use utf8;
5 use strict;
6 use warnings;
7 use v5.32;
9 my $outdir = $ENV{'OUTDIR'};
10 die 'Set $OUTDIR' unless defined $outdir;
11 mkdir $outdir;
13 my $tfh;
15 my $logo = <<EOF;
16 <img srcset='/got-tiny.png, /got-tiny@2x.png 2x'
17 src='/got-tiny.png'
18 alt='"GOT", but the "O" is a cute, smiling sun' />
19 EOF
21 sub san {
22 my $str = shift;
23 $str =~ s/&/\&amp;/g;
24 $str =~ s/</\&lt;/g;
25 $str =~ s/>/\&gt;/g;
26 return $str;
27 }
29 sub initpage {
30 my ($fh, $title) = @_;
31 open(my $hdr, '<', 'head.html')
32 or die "can't open head.html: $!";
33 while (<$hdr>) {
34 s/TITLE/$title/;
35 print $fh $_;
36 }
37 }
39 sub endpage {
40 my $fh = shift;
41 open(my $foot, '<', 'foot.html')
42 or die "can't open foot.html: $!";
43 print $fh $_ while <$foot>;
44 }
46 sub nextthread {
47 endthread() if defined($tfh);
48 my ($mid, $subj) = @_;
49 my $dest = "$outdir/thread/$mid.html";
50 open($tfh, '>', $dest) or die "can't open $dest: $!";
51 initpage($tfh, $subj);
53 print $tfh "<header class='mail-header'>";
54 print $tfh "<p>";
55 print $tfh $logo;
56 print $tfh "<a href='/'>‹ Back to the index</a>";
57 print $tfh "</p>";
58 print $tfh "<dl><dt>Thread:</dt><dd>$subj</dd>";
59 print $tfh "</header>\n";
60 print $tfh "<div class='thread'>\n";
61 }
63 sub endthread {
64 print $tfh "</div>\n";
65 endpage($tfh);
66 close($tfh);
67 }
69 sub entry_raw {
70 my ($fh, $mid, $level, $date, $from, $subj) = @_;
72 my $class = "";
73 $class = "reply indent-$level" unless $level == 0;
74 print $fh "<div id='$mid' class='mail $class'>";
75 print $fh "<p class='mail-meta'>";
76 print $fh "<time>$date</time> ";
77 print $fh "<span class='from'>$from</span>";
78 print $fh "<span class='colon'>:</span>";
79 if ($fh != $tfh && $level == 0) {
80 print $fh " (<a href='/thread/$mid.html'>thread</a>)";
81 }
82 print $fh "</p>";
83 print $fh "<p class='subject'>";
84 print $fh "<a href='/$mid.html'>$subj</a>";
85 print $fh "</p>";
86 print $fh "</div>\n";
87 }
89 sub entry {
90 entry_raw(\*STDOUT, @_);
91 entry_raw($tfh, @_);
92 }
94 initpage(\*STDOUT, "Game of Trees Mail Archive");
95 print <<EOF;
96 <header class='index-header'>
97 <a href="https://gameoftrees.org" target="_blank">
98 <img src='/got.png'
99 srcset='/got.png, /got@2x.png 2x'
100 alt='"GOT" where the "O" is a cute smiling sun.' />
101 </a>
102 <h1>Game of Trees Mail Archive</h1>
103 </header>
104 <main>
105 EOF
107 my $threads_seen = 0;
108 while (<>) {
109 chomp;
110 m/^[^ ]+ <([^>]+)> (.+)(\d{4}-\d{2}-\d{2} \d{2}:\d{2}) <([^>]+)> (.*)/;
111 die "woops; $_\n" unless defined $1;
113 my ($mid, $indent, $date, $from, $subj) = ($1, $2, $3, $4, $5);
114 $from =~ s/\s+$//;
115 $from = san($from);
116 $subj = san($subj);
118 my $level = length($indent) - 1;
119 $level = 10 if $indent =~ m/\.\.\d{2}\.\./;
121 $mid =~ s,_,__,g;
122 $mid =~ s,/,_,g;
124 $subj =~ s/\s+/ /g;
125 $subj =~ s/\s+$//g;
127 my $new_thread = $indent eq " ";
128 say "</section> <hr />" if $threads_seen && $new_thread;
129 if ($new_thread) {
130 nextthread($mid, $subj);
131 say "<section class='thread'>";
134 $threads_seen = 1;
136 entry($mid, $level, $date, $from, $subj);
139 if ($threads_seen) {
140 say "</section>" ;
141 endthread();
144 say "</main>";
145 endpage(\*STDOUT);