001
2022-08-25
op
package GotMArc;
002
2022-08-25
op
use strict;
003
2022-08-25
op
use warnings;
004
2022-08-25
op
use v5.32;
005
2022-08-25
op
use Exporter;
007
2022-08-25
op
our @ISA = qw(Exporter);
008
2022-08-27
op
our @EXPORT_OK = qw(san parse initpage endpage index_header thread_header);
011
2022-08-25
op
my $str = shift;
012
2022-08-25
op
$str =~ s/&/\&/g;
013
2022-08-25
op
$str =~ s/</\</g;
014
2022-08-25
op
$str =~ s/>/\>/g;
015
2022-08-25
op
return $str;
018
2022-08-26
op
sub ssan {
019
2022-08-26
op
my $str = shift;
020
2022-08-26
op
$str =~ s/\s+/ /g;
021
2022-08-26
op
$str =~ s/\s+$//;
022
2022-08-26
op
return san($str);
025
2022-08-25
op
sub mid2path {
026
2022-08-25
op
my $mid = shift;
027
2022-08-25
op
$mid =~ s,_,__,g;
028
2022-08-25
op
$mid =~ s,/,_,g;
029
2022-08-25
op
return $mid;
032
2022-08-26
op
sub parse {
033
2022-08-26
op
my ($indent, $fname, $mid, $date, $from, $subj) = m{
034
2022-08-26
op
^([^-]*)- # the indent level
035
2022-08-26
op
([^ ]+)\s # filename
036
2022-08-26
op
<([^>]+)> # message id
037
2022-08-26
op
(\d{4}-\d\d-\d\d[ ]\d\d:\d\d) # date
038
2022-08-26
op
<([^>]+)> # from
039
2022-08-26
op
(.*) # subject
040
2022-08-26
op
}x or die "can't parse: $_";
042
2022-08-26
op
my $level = length($indent);
043
2022-08-26
op
$level = 10 if $indent =~ m/\.\.\d+\.\./;
045
2022-08-26
op
$mid = mid2path($mid);
046
2022-08-26
op
$from = ssan($from);
047
2022-08-26
op
$subj = ssan($subj);
049
2022-08-26
op
return ($level, $fname, $mid, $date, $from, $subj);
052
2022-08-27
op
sub readall {
053
2022-08-27
op
my $path = shift;
054
2022-08-25
op
local $/ = undef;
055
2022-08-27
op
open my $fh, "<", $path or die "can't open $path: $!";
059
2022-08-27
op
my $small_logo = readall "logo-small.html";
060
2022-08-27
op
my $hdr = readall "head.html";
061
2022-08-27
op
my $foot = readall "foot.html";
062
2022-08-27
op
my $idxhdr = readall "index-header.html";
064
2022-08-25
op
sub initpage {
065
2022-08-25
op
my ($fh, $title) = @_;
066
2022-08-25
op
say $fh $hdr =~ s/TITLE/$title/r;
069
2022-08-25
op
sub endpage {
070
2022-08-25
op
my $fh = shift;
071
2022-08-25
op
say $fh $foot;
074
2022-08-27
op
sub index_header {
075
2022-08-27
op
my ($fh, $page, $subtitle) = @_;
076
2022-08-27
op
my $html = $idxhdr =~ s/PAGE/$page/r;
077
2022-08-27
op
$html =~ s/SUBTITLE/$subtitle/;
078
2022-08-27
op
print $fh $html;
081
2022-08-27
op
sub thread_header {
082
2022-08-27
op
my ($fh, $tid, $mid, $e) = @_;
083
2022-08-27
op
my @entries = @$e;
085
2022-08-27
op
print $fh "<header class='mail-header'>\n";
087
2022-08-27
op
print $fh "<p>";
088
2022-08-27
op
print $fh $small_logo;
089
2022-08-27
op
print $fh "<a href='/'>Index</a>";
090
2022-08-27
op
print $fh " | <a href='/thread/$tid.html#$mid'>Thread</a>"
091
2022-08-27
op
if defined $tid;
092
2022-08-27
op
print $fh "</p>\n";
094
2022-08-27
op
say $fh "<dl>";
095
2022-08-27
op
foreach my $entry (@entries) {
096
2022-08-27
op
my ($k, $v) = split /: /, $entry, 2;
097
2022-08-27
op
chomp $v;
098
2022-08-27
op
say $fh "<dt>$k:</dt><dd>$v</dd>";
100
2022-08-27
op
say $fh "</dl>";
102
2022-08-27
op
say $fh "<p>Download raw <a href='/text/$mid.txt'>body</a>.</p>"
103
2022-08-27
op
if defined $mid;
105
2022-08-27
op
say $fh "</header>\n";