##
##  page.pl -- generate corresponding .html version for each .txt 
##

use strict;

use IO::File;

my @PR = glob("PR-*.txt");

my $sidebar = '<u>Press Releases:</u><p>';
foreach my $pr (reverse sort @PR) {
    my ($base, $name) = ($pr =~ m|^(PR-(.+))\.txt$|);
    next if ($name =~ m|^0000|);
    $sidebar .= "<a href=\"$base.html\">$name</a><br>";
}

foreach my $pr (@PR) {
    $pr =~ s|\.txt$||s;

    print "$pr\n";
    my $io = new IO::File "<$pr.txt" or die;
    my $txt = '';
    $txt .= $_ while (<$io>);
    $io->close;

    my $L = {};
    my $x = $txt;
    $txt =~ s%^(\s*\[(\d+)\]\s+((?:http|ftp)://\S+))%$L->{$2} = $3, $1%mge;
    $txt =~ s/&/&amp;/sg;
    $txt =~ s/</&lt;/sg;
    $txt =~ s/>/&gt;/sg;
    $txt =~ s/((?:http|ftp):\/\/[^\s]+[^\s\.\)\&])/<a href="$1">$1<\/a>/sg;
    $txt =~ s/([a-zA-Z0-9_.-]+\@[^\s\)\&]+)/<a href="mailto:$1">$1<\/a>/sg;
    $txt =~ s/^(\s*)([A-Z']{3,}\s+[A-Z]{2,}.+)$/$1<b>$2<\/b>/mg;
    $txt =~ s%\[(\d+)\]%"[<a href=\"".$L->{$1}."\">$1</a>]"%mge;

    my $head = '';
    $io = new IO::File "<page.head.html" || die;
    $head .= $_ while (<$io>);
    $io->close;
    my $foot = '';
    $io = new IO::File "<page.foot.html" || die;
    $foot .= $_ while (<$io>);
    $io->close;

    $head =~ s|\@TXT\@|$pr.txt|sg;
    $head =~ s|\@ID\@|$pr|sg;
    $head =~ s|\@SIDEBAR\@|$sidebar|s;

    my $out = $head .
              "<pre>\n" . $txt . "</pre>\n" .
              $foot;

    $io = new IO::File ">$pr.html" || die;
    $io->print($out);
    $io->close;
}

