#!/usr/bin/perl

use strict;
use utf8;
use Template::Simple;

my $fn = "/home/scrato/sighara/sighara.exz";

open my $exz, "<:encoding(cp1252)", $fn
    or die "Couldn't open `$fn': $!";

# read quotes
my @quotes;
while(my $line = <$exz>) {
    my ($name, $nicks, $quote) = split /\x{AE}/, $line;
    my ($nick1, $nick2) = split /\x{AA}/, $nicks;
    push @quotes, {
        name => $name,
        quote => $quote,
        nick1 => $nick1,
        nick2 => $nick2,
    };
}

# escape special chars
for my $quote (@quotes) {
    for(values %$quote) {
        s/([\P{IsASCII}<>&])/'&#'.ord($1).';'/ge;
    }
}


# collect template data
my $ts = Template::Simple->new();
my $template = join '', <DATA>;
my $data = {
    quotes => [ @quotes ],
};

# render page
print "Content-type: text/html; charset=UTF-8\n\n";
binmode(STDOUT, ":utf8");
print ${$ts->render($template, $data)};

__DATA__
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="de" xml:lang="de">
<head>
  <style type="text/css"><!--
  body {
    background-color: #eef;
    font: sans-serif;
  }
  h1 {
    text-align: center;
  }
  #quotes {
    width: 550px;
    border: 1px dashed black;
    margin: 0 auto;
    background-color: #fff;
    padding: 1em;
  }
  #quotes dt {
    font-weight: bold;
  }
  #quotes dd {
    border-bottom: 1px solid gray;
    margin-bottom: 1em;
  }
  #quotes dd > p {
    font-style: italic;
  }
  --></style>
  <title>Sighara quotes</title>
</head>

<body>

  <h1>#sighara</h1>
  <dl id="quotes">
  [%START quotes %]
    <dt>[% name %]</dt>
    <dd>
        <blockquote><p>
            [% quote %]
        </p></blockquote>
        <p>&copy; by [% nick1 %] ([% nick2 %])</p>
    </dd>
  [%END quotes %]
  </dl>
</body>

</html>
