markdownCache.pl
#!/usr/bin/perl
use strict;
use warnings FATAL => qw(all);
use Sloop::Logger;
use Sloop::Server;
use Sloop::Static::Cache;
use Template;
use Text::Markdown 'markdown';
my $Logger = Sloop::Logger->new();
my $sloop = Sloop::Server->new (
port => 8080,
logger => $Logger
);
die if !$sloop;
$sloop->{handlers} = {
'/' => sub {
Sloop::Static::Cache::page (
shift,
'./markdownCache.html',
$0,
\&generatePage
);
}
};
$sloop->run;
sub generatePage {
$Logger->out (
LOG_REQUEST,
"Generating HTML page for caching."
);
my $template = <<END;
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>[% title %]</title>
<body>
[% body %]
</body>
</html>
END
my $markdown;
$markdown .= $_ while (<DATA>);
my $html = markdown($markdown);
my $tt = Template->new ({
ANYCASE => 1
});
if (!$tt) {
$Logger->out (
LOG_ERR,
'Template->new() fail: '.Template->error()
);
return undef;
}
my $page;
if (!$tt->process (
\$template,
{
title => 'Sloop Markdown Demo',
body => $html
}, \$page
)) {
$Logger->out (
LOG_ERR,
'Template::process() fail: '.$tt->error
);
return undef;
}
return $page;
}