##################################################
+sub read_templates {
+ my $root = 'tmpl';
+ my $name = shift;
+
+ map { m/\.(.+)$/; $1 => scalar read_file $_ } <tmpl/$name.*>;
+}
+
+my %header_templates = read_templates 'header';
+my %footer_templates = read_templates 'footer';
+
+sub header{
+ my ($language, $title) = @_;
+ $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger;
+}
+
+sub footer{
+ $footer_templates{$_[0]};
+}
+
+##################################################
+
use POSIX ();
use Gruntmaster::Data ();
use List::Util ();
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
+my %templates;
sub import {
my $caller = caller;
no strict 'refs';
*{"${caller}::strftime"} = \&POSIX::strftime;
- *{"${caller}::NAME"} = sub () { $name };
- *{"${caller}::TITLE"} = sub () { $title };
*{"${caller}::debug"} = sub {
local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1;
$_[0]->{'psgix.logger'}->({qw/level debug message/ => $_[1]})
my $req = HTTP::Request->new(PURGE => "http://$ENV{PURGE_HOST}$_[0]");
$ua->request($req)
};
-}
-
-##################################################
-my %orig_header_templates = (
- en => <<'HTML',
-<!DOCTYPE html>
-<title>TITLE_GOES_HERE</title>
-<meta charset="utf-8">
-<meta name="viewport" content="width=device-width, initial-scale=1.0">
-
-<link rel="stylesheet" href="/css/cyborg" id="stylesheet">
-<script src="/js" type="text/javascript"></script>
-
-<nav class="navbar navbar-default navbar-static-top" role="navigation">
-<div class="container-fluid">
-<div class="navbar-header">
-<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>
-<a class="navbar-brand" href="/">Gruntmaster 6000</a>
-</div>
-
-<div class="collapse navbar-collapse">
-<ul class="nav navbar-nav">
-<li><a href="/pb/">Problems</a>
-<li><a href="/ct/">Contests</a>
-<li><a href="/account">Account</a>
-</ul>
-
-<ul class="nav navbar-nav navbar-right">
-<li><a class="dropdown-toggle" data-toggle="dropdown"> Theme <span class="caret"></span></a>
-
-<ul class="dropdown-menu" role="menu">
-<li><a href="#" id="theme_slate">Gunmetal gray</a>
-<li><a href="#" id="theme_cyborg">Black</a>
-<li><a href="#" id="theme_cerulean">White</a>
-<li><a href="#" id="theme_cosmo">Metro</a>
-</ul>
-
-<li><a href="/log/">Job log</a>
-</ul>
-</div>
-</div>
-</nav>
-
-<div class="container-fluid">
-
-<h1 id="title">TITLE_GOES_HERE</h1>
-<div id="result"></div>
-HTML
-);
-
-my %orig_footer_templates = (
- en => <<'HTML',
-
-<footer>
-Dilmom: Why don't you call your product the Gruntmaster 6000?
-Dilbert: What kind of product do you see when you imagine a Gruntmaster 6000?
-Dilmom: Well, it's a stripped-down version of the Gruntmaster 9000, of course. But it's software-upgradeable.
-</footer>
-HTML
-);
-
-sub patch_templates {
- my $root = 'tmpl';
- return %{$_[0]} unless -d $root;
- my ($templates, $name) = @_;
- my %out = %$templates;
- for (<$root/$name.*>) {
- m/\.(.+)$/;
- $out{$1} = read_file $_
+ if ($name) {
+ $templates{$caller} = { read_templates $name };
+ $templates{$caller}{$_} = header ($_, $title) . $templates{$caller}{$_} for keys $templates{$caller};
+ $templates{$caller}{$_} .= footer $_ for keys $templates{$caller};
}
-
- %out
-}
-
-my %header_templates = patch_templates \%orig_header_templates, 'header';
-my %footer_templates = patch_templates \%orig_footer_templates, 'footer';
-
-sub header{
- my ($language, $title) = @_;
- $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger;
-}
-
-sub footer{
- $footer_templates{$_[0]};
-}
-
-sub cook_templates {
- my ($templates, $name, $title) = @_;
-
- my %out = patch_templates $templates, $name;
- $out{$_} = header ($_, $title) . $out{$_} for keys %out;
- $out{$_} .= footer $_ for keys %out;
-
- %out
}
##################################################
-my %templates;
-
sub generate{
my ($self, $lang, @args) = @_;
- $templates{$self} = { cook_templates $self->TEMPLATES, $self->NAME => $self->TITLE } unless exists $templates{$self};
-
my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$self}{$lang}, default_escape => 'HTML',);
$self->_generate($htc, $lang, @args);
my $out = $htc->output;
sub max_age { 60 }
sub variants {
- [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $_[0]->TEMPLATES ]
+ [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $templates{$_[0]} ]
}
1
our @ISA = qw/Gruntmaster::Page::Base/;
our $VERSION = '0.001';
-use constant TEMPLATES => {
- en => <<'HTML',
-<tmpl_if running>
-<h1>Running contests</h1>
-<table border class="table table-bordered table-striped">
-<thead>
-<tr><th>Name<th>Start date<th>End date<th>Owner
-<tbody>
-<tmpl_loop running><tr><td><a href="<tmpl_var id>"><tmpl_var name></a>
-<td><tmpl_var start>
-<td><tmpl_var end>
-<td><tmpl_var owner>
-</tmpl_loop>
-</table>
-</tmpl_if>
-
-<tmpl_if pending>
-<h1>Pending contests</h1>
-<table border class="table table-bordered table-striped">
-<thead>
-<tr><th>Name<th>Start date<th>End date<th>Owner
-<tbody>
-<tmpl_loop pending><tr><td><a href="<tmpl_var id>"><tmpl_var name></a>
-<td><tmpl_var start>
-<td><tmpl_var end>
-<td><tmpl_var owner>
-</tmpl_loop>
-</table>
-</tmpl_if>
-
-<tmpl_if finished>
-<h1>Finished contests</h1>
-<table border class="table table-bordered table-striped">
-<thead>
-<tr><th>Name<th>Start date<th>End date<th>Owner
-<tbody>
-<tmpl_loop finished><tr><td><a href="<tmpl_var id>"><tmpl_var name></a>
-<td><tmpl_var start>
-<td><tmpl_var end>
-<td><tmpl_var owner>
-</tmpl_loop>
-</table>
-</tmpl_if>
-HTML
-};
-
sub _generate{
my ($self, $htc, $lang, $env) = @_;
debug $env => "language is '$lang'";
our @ISA = qw/Gruntmaster::Page::Base/;
our $VERSION = '0.001';
-use constant TEMPLATES => {
- en => <<'HTML',
-Contest start time: <tmpl_var start><br>
-Contest end time: <tmpl_var end><p>
-
-<tmpl_if started><a href="<tmpl_var id>/pb/">Problems</a><br>
-<a href="<tmpl_var id>/log/">Job log</a><br>
-<a href="<tmpl_var id>/log/st">Standings</a></tmpl_if>
-HTML
-};
-
sub _generate{
my ($self, $htc, $lang, $env, $id) = @_;
debug $env => "language is '$lang' and id is '$id'";
use constant PAGE_SIZE => 10;
-use constant TEMPLATES => {
- en => <<'HTML',
-<table border class="table table-bordered table-striped">
-<thead>
-<tr><th>ID<th>Problem<th>Date<th>Size<th>User<th>Result
-<tbody>
-<tmpl_loop log><tr><td><a href="job/<tmpl_var id>"><tmpl_var id></a>
-<td><a href="../pb/<tmpl_var problem>"><tmpl_var name></a>
-<td><tmpl_var date>
-<td><a href="src/<tmpl_var id>.<tmpl_var extension>"<tmpl_if private> data-private</tmpl_if>><tmpl_var size></a>
-<td><tmpl_var user><td class="r<tmpl_var result>"><tmpl_var result_text>
-</tmpl_loop>
-</table>
-
-<ul class="pager">
-<tmpl_if prev><li class="previous"><a href="<tmpl_var prev>">Previous</a></tmpl_if>
-<tmpl_if next><li class="next"><a href="<tmpl_var next>">Next</a></tmpl_if>
-</ul>
-HTML
-};
-
sub _generate{
my ($self, $htc, $lang, $env, $ct, $page) = @_;
debug $env => "language is '$lang', contest is '$ct' and page is '$page'";
our @ISA = qw/Gruntmaster::Page::Base/;
our $VERSION = '0.001';
-use constant TEMPLATES => {
- en => <<'HTML',
-Compiler output:
-<pre><tmpl_var errors></pre>
-
-Results:
-<table border class="table table-border table-striped">
-<thead>
-<tr><th>Test number<th>Result<th>Time
-<tbody>
-<tmpl_loop tests><tr><td><tmpl_var id><td class="r<tmpl_var result>"><tmpl_var result_text><td><tmpl_var time>
-</tmpl_loop>
-</table>
-HTML
-};
-
sub _generate{
my ($self, $htc, $lang, $env, $ct, $id) = @_;
debug $env => "language is '$lang', contest is '$ct' and id is '$id'";
our @ISA = qw/Gruntmaster::Page::Base/;
our $VERSION = '0.001';
-use constant TEMPLATES => {
- en => <<'HTML',
-<tmpl_if levels>
-<h2>Beginner</h2>
-<div class="list-group">
-<tmpl_loop beginner><a class="list-group-item" href="<tmpl_var id>"><tmpl_var name></a>
-</tmpl_loop></div>
-
-<h2>Easy</h2>
-<div class="list-group">
-<tmpl_loop easy><a class="list-group-item" href="<tmpl_var id>"><tmpl_var name></a>
-</tmpl_loop></div>
-
-<h2>Medium</h2>
-<div class="list-group">
-<tmpl_loop medium><a class="list-group-item" href="<tmpl_var id>"><tmpl_var name></a>
-</tmpl_loop></div>
-
-<h2>Hard</h2>
-<div class="list-group">
-<tmpl_loop hard><a class="list-group-item" href="<tmpl_var id>"><tmpl_var name></a>
-</tmpl_loop></div>
-
-<tmpl_else>
-<div class="list-group">
-<tmpl_loop problems><a class="list-group-item" href="<tmpl_var id>"><tmpl_var name></a>
-</tmpl_loop></div>
-</tmpl_if>
-HTML
-};
-
sub _generate{
my ($self, $htc, $lang, $env, $ct) = @_;
debug $env => "language is '$lang' and contest is '$ct'";
our @ISA = qw/Gruntmaster::Page::Base/;
our $VERSION = '0.001';
-use constant TEMPLATES => {
- en => <<'HTML',
-<div class="row">
-<div class="col-md-9">
-<tmpl_var ESCAPE=0 statement>
-</div>
-
-<div class="col-md-3">
-<dl>
-<dt>Author</dt> <dd><tmpl_var author></dd>
-<dt>Owner</dt> <dd><tmpl_var owner></dd>
-</dl>
-
-<tmpl_if cansubmit>
-<h1>Submit solution</h1>
-<form action="<tmpl_var id>/submit" method="POST" enctype="multipart/form-data" role="form">
-<input type="hidden" name="problem" value="<tmpl_var id>">
-<tmpl_if_defined contest><input type="hidden" name="contest" value="<tmpl_var contest>"></tmpl_if_defined>
-
-<div class="form-group"><label for="prog">File:</label><input id="prog" name="prog" type="file"></div>
-<div class="form-group"><label for="source_code">Source code:</label> <textarea class="form-control" id="source_code" name="source_code"></textarea></div>
-<div class="form-group"><label for="prog_format">File format:</label><select id="prog_format" name="prog_format" class="form-control" required>
-<tmpl_loop formats><option value="<tmpl_var _>"><tmpl_var _></option>
-</tmpl_loop></select></div>
-
-<input type="submit" value="Submit job" class="btn btn-primary">
-</form>
-</tmpl_if>
-</div>
-HTML
-};
-
use constant FORMATS => [qw/C CPP MONO JAVA PASCAL PERL PYTHON/];
sub _generate{
our @ISA = qw/Gruntmaster::Page::Base/;
our $VERSION = '0.001';
-use constant TEMPLATES => {
- en => <<'HTML',
-<table border class="table table-border table-striped">
-<thead>
-<tmpl_if problems><tr><th>Username<tmpl_loop problems><th><tmpl_var _></tmpl_loop><th>Total
-<tmpl_else><tr><th>Username<th>Score
-</tmpl_if>
-
-<tbody>
-<tmpl_loop st><tr><td><tmpl_var user>
-<tmpl_if problems><tmpl_loop scores><td><tmpl_var _>
-</tmpl_loop></tmpl_if><td><tmpl_var score>
-</tmpl_loop>
-</table>
-HTML
-};
-
use constant LEVEL_VALUES => {
beginner => 100,
easy => 250,
our @ISA = qw/Gruntmaster::Page::Base/;
our $VERSION = '0.001';
-use constant TEMPLATES => {
- en => <<'HTML',
-<div class="list-group"><tmpl_loop users><a class="list-group-item" href="<tmpl_var id>"><tmpl_var name></a>
-</tmpl_loop></ol>
-HTML
-};
-
sub _generate{
my ($self, $htc, $lang, $env) = @_;
debug $env => "language is '$lang'";
our @ISA = qw/Gruntmaster::Page::Base/;
our $VERSION = '0.001';
-use constant TEMPLATES => {
- en => <<'HTML',
-<h1><tmpl_var name></h1>
-Town: <tmpl_var town><br>
-University: <tmpl_var university><br>
-Level: <tmpl_var level>
-HTML
-};
-
sub _generate{
my ($self, $htc, $lang, $env, $us) = @_;
debug $env => "language is '$lang', user is '$us'";
--- /dev/null
+<tmpl_if running>
+<h1>Running contests</h1>
+<table border class="table table-bordered table-striped">
+<thead>
+<tr><th>Name<th>Start date<th>End date<th>Owner
+<tbody>
+<tmpl_loop running><tr><td><a href="<tmpl_var id>"><tmpl_var name></a>
+<td><tmpl_var start>
+<td><tmpl_var end>
+<td><tmpl_var owner>
+</tmpl_loop>
+</table>
+</tmpl_if>
+
+<tmpl_if pending>
+<h1>Pending contests</h1>
+<table border class="table table-bordered table-striped">
+<thead>
+<tr><th>Name<th>Start date<th>End date<th>Owner
+<tbody>
+<tmpl_loop pending><tr><td><a href="<tmpl_var id>"><tmpl_var name></a>
+<td><tmpl_var start>
+<td><tmpl_var end>
+<td><tmpl_var owner>
+</tmpl_loop>
+</table>
+</tmpl_if>
+
+<tmpl_if finished>
+<h1>Finished contests</h1>
+<table border class="table table-bordered table-striped">
+<thead>
+<tr><th>Name<th>Start date<th>End date<th>Owner
+<tbody>
+<tmpl_loop finished><tr><td><a href="<tmpl_var id>"><tmpl_var name></a>
+<td><tmpl_var start>
+<td><tmpl_var end>
+<td><tmpl_var owner>
+</tmpl_loop>
+</table>
+</tmpl_if>
--- /dev/null
+Contest start time: <tmpl_var start><br>
+Contest end time: <tmpl_var end><p>
+
+<tmpl_if started><a href="<tmpl_var id>/pb/">Problems</a><br>
+<a href="<tmpl_var id>/log/">Job log</a><br>
+<a href="<tmpl_var id>/log/st">Standings</a></tmpl_if>
--- /dev/null
+<footer>
+Dilmom: Why don't you call your product the Gruntmaster 6000?
+Dilbert: What kind of product do you see when you imagine a Gruntmaster 6000?
+Dilmom: Well, it's a stripped-down version of the Gruntmaster 9000, of course. But it's software-upgradeable.
+</footer>
--- /dev/null
+<!DOCTYPE html>
+<title>TITLE_GOES_HERE</title>
+<meta charset="utf-8">
+<meta name="viewport" content="width=device-width, initial-scale=1.0">
+
+<link rel="stylesheet" href="/css/cyborg" id="stylesheet">
+<script src="/js" type="text/javascript"></script>
+
+<nav class="navbar navbar-default navbar-static-top" role="navigation">
+<div class="container-fluid">
+<div class="navbar-header">
+<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button>
+<a class="navbar-brand" href="/">Gruntmaster 6000</a>
+</div>
+
+<div class="collapse navbar-collapse">
+<ul class="nav navbar-nav">
+<li><a href="/pb/">Problems</a>
+<li><a href="/ct/">Contests</a>
+<li><a href="/account">Account</a>
+</ul>
+
+<ul class="nav navbar-nav navbar-right">
+<li><a class="dropdown-toggle" data-toggle="dropdown"> Theme <span class="caret"></span></a>
+
+<ul class="dropdown-menu" role="menu">
+<li><a href="#" id="theme_slate">Gunmetal gray</a>
+<li><a href="#" id="theme_cyborg">Black</a>
+<li><a href="#" id="theme_cerulean">White</a>
+<li><a href="#" id="theme_cosmo">Metro</a>
+</ul>
+
+<li><a href="/log/">Job log</a>
+</ul>
+</div>
+</div>
+</nav>
+
+<div class="container-fluid">
+
+<h1 id="title">TITLE_GOES_HERE</h1>
+<div id="result"></div>
--- /dev/null
+<table border class="table table-bordered table-striped">
+<thead>
+<tr><th>ID<th>Problem<th>Date<th>Size<th>User<th>Result
+<tbody>
+<tmpl_loop log><tr><td><a href="job/<tmpl_var id>"><tmpl_var id></a>
+<td><a href="../pb/<tmpl_var problem>"><tmpl_var name></a>
+<td><tmpl_var date>
+<td><a href="src/<tmpl_var id>.<tmpl_var extension>"<tmpl_if private> data-private</tmpl_if>><tmpl_var size></a>
+<td><tmpl_var user><td class="r<tmpl_var result>"><tmpl_var result_text>
+</tmpl_loop>
+</table>
+
+<ul class="pager">
+<tmpl_if prev><li class="previous"><a href="<tmpl_var prev>">Previous</a></tmpl_if>
+<tmpl_if next><li class="next"><a href="<tmpl_var next>">Next</a></tmpl_if>
+</ul>
--- /dev/null
+Compiler output:
+<pre><tmpl_var errors></pre>
+
+Results:
+<table border class="table table-border table-striped">
+<thead>
+<tr><th>Test number<th>Result<th>Time
+<tbody>
+<tmpl_loop tests><tr><td><tmpl_var id><td class="r<tmpl_var result>"><tmpl_var result_text><td><tmpl_var time>
+</tmpl_loop>
+</table>
--- /dev/null
+<tmpl_if levels>
+<h2>Beginner</h2>
+<div class="list-group">
+<tmpl_loop beginner><a class="list-group-item" href="<tmpl_var id>"><tmpl_var name></a>
+</tmpl_loop></div>
+
+<h2>Easy</h2>
+<div class="list-group">
+<tmpl_loop easy><a class="list-group-item" href="<tmpl_var id>"><tmpl_var name></a>
+</tmpl_loop></div>
+
+<h2>Medium</h2>
+<div class="list-group">
+<tmpl_loop medium><a class="list-group-item" href="<tmpl_var id>"><tmpl_var name></a>
+</tmpl_loop></div>
+
+<h2>Hard</h2>
+<div class="list-group">
+<tmpl_loop hard><a class="list-group-item" href="<tmpl_var id>"><tmpl_var name></a>
+</tmpl_loop></div>
+
+<tmpl_else>
+<div class="list-group">
+<tmpl_loop problems><a class="list-group-item" href="<tmpl_var id>"><tmpl_var name></a>
+</tmpl_loop></div>
+</tmpl_if>
--- /dev/null
+<div class="row">
+<div class="col-md-9">
+<tmpl_var ESCAPE=0 statement>
+</div>
+
+<div class="col-md-3">
+<dl>
+<dt>Author</dt> <dd><tmpl_var author></dd>
+<dt>Owner</dt> <dd><tmpl_var owner></dd>
+</dl>
+
+<tmpl_if cansubmit>
+<h1>Submit solution</h1>
+<form action="<tmpl_var id>/submit" method="POST" enctype="multipart/form-data" role="form">
+<input type="hidden" name="problem" value="<tmpl_var id>">
+<tmpl_if_defined contest><input type="hidden" name="contest" value="<tmpl_var contest>"></tmpl_if_defined>
+
+<div class="form-group"><label for="prog">File:</label><input id="prog" name="prog" type="file"></div>
+<div class="form-group"><label for="source_code">Source code:</label> <textarea class="form-control" id="source_code" name="source_code"></textarea></div>
+<div class="form-group"><label for="prog_format">File format:</label><select id="prog_format" name="prog_format" class="form-control" required>
+<tmpl_loop formats><option value="<tmpl_var _>"><tmpl_var _></option>
+</tmpl_loop></select></div>
+
+<input type="submit" value="Submit job" class="btn btn-primary">
+</form>
+</tmpl_if>
+</div>
--- /dev/null
+<table border class="table table-border table-striped">
+<thead>
+<tmpl_if problems><tr><th>Username<tmpl_loop problems><th><tmpl_var _></tmpl_loop><th>Total
+<tmpl_else><tr><th>Username<th>Score
+</tmpl_if>
+
+<tbody>
+<tmpl_loop st><tr><td><tmpl_var user>
+<tmpl_if problems><tmpl_loop scores><td><tmpl_var _>
+</tmpl_loop></tmpl_if><td><tmpl_var score>
+</tmpl_loop>
+</table>
--- /dev/null
+<div class="list-group"><tmpl_loop users><a class="list-group-item" href="<tmpl_var id>"><tmpl_var name></a>
+</tmpl_loop></ol>
--- /dev/null
+<h1><tmpl_var name></h1>
+Town: <tmpl_var town><br>
+University: <tmpl_var university><br>
+Level: <tmpl_var level>