Blame


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