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 $hdr = do {
14 local $/ = undef;
15 open my $fh, "<", "head.html"
16 or die "can't open head.html: $!";
17 <$fh>;
18 };
20 my $foot = do {
21 local $/ = undef;
22 open my $fh, "<", "foot.html"
23 or die "can't open foot.html: $!";
24 <$fh>;
25 };
27 my $logo = <<EOF;
28 <img srcset='/got-tiny.png, /got-tiny@2x.png 2x'
29 src='/got-tiny.png'
30 alt='"GOT", but the "O" is a cute, smiling sun' />
31 EOF
33 sub san {
34 my $str = shift;
35 $str =~ s/&/\&amp;/g;
36 $str =~ s/</\&lt;/g;
37 $str =~ s/>/\&gt;/g;
38 return $str;
39 }
41 my $tid;
42 while (<>) {
43 chomp;
44 say; # continue the pipeline
46 m/^([^ ]+) <([^>]+)> (.+)(\d{4}-\d{2}-\d{2} \d{2}:\d{2}) <([^>]+)> (.*)/;
47 die "can't parse: $_" unless defined $1;
48 my ($fname, $mid, $indent, $date, $from, $subj) = ($1, $2, $3, $4, $5, $6);
49 $subj = san($subj);
50 $subj =~ s/\s+/ /g;
51 $subj =~ s/\s+$//;
53 $mid =~ s,_,__,g;
54 $mid =~ s,/,_,g;
56 chomp($mid);
58 next if -f "$outdir/$mid.html";
60 my $level = length($indent) - 1;
61 $level = 10 if $indent =~ m/\.\.\d{2}\.\./;
63 $tid = $mid if $level == 0;
65 my $dest = "$outdir/$mid.html";
66 open(my $fh, '>', "$dest") or die "can't open $dest: $!";
68 say $fh $hdr =~ s/TITLE/$subj/r;
70 open(my $mshow, "-|", "mshow", "-nNA", "text/plain", $fname)
71 or die "can't exec mshow: $!";
73 print $fh "<header class='mail-header'>";
74 print $fh "<p>";
75 print $fh $logo;
76 print $fh "<a href='/'>← Back to the index</a>";
77 print $fh " or ";
78 print $fh "<a href='/thread/$tid.html#$mid'>→ go to the thread</a>.";
79 print $fh "</p>";
80 print $fh "<dl>";
81 while (<$mshow>) {
82 chomp;
83 last if /^$/;
84 my ($h, $v) = m/^([-A-Za-z]+): (.*)/;
85 die "bogus line? $fname : $_" unless (defined $h and defined $v);
87 # drop the (1 day ago) string
88 $v =~ s/\(.*\)//g if ($h eq "Date");
90 print $fh "<dt>", san($h), ":</dt>";
91 print $fh "<dd>", san($v), "</dd>";
92 }
93 print $fh "</dl></header>";
95 my $body = do {
96 local $/ = undef;
97 <$mshow>;
98 };
100 print $fh "<pre>";
101 # print $fh san($_) while <>;
102 print $fh san($body // "");
103 print $fh "</pre>";
105 print $fh $foot;
107 close($mshow);
108 close($fh);
110 # exit(0);