]> iEval git - gruntmaster-page.git/blobdiff - lib/Gruntmaster/Page/Base.pm
Import some changes from the mindcoding branch
[gruntmaster-page.git] / lib / Gruntmaster / Page / Base.pm
index 80fa027d5b2651249c504405476b182ab2ac57d2..1a68b3c2b48658513d8796ad02cad0434ac8327b 100644 (file)
@@ -3,150 +3,106 @@ package Gruntmaster::Page::Base;
 use 5.014000;
 use strict;
 use warnings;
+our $VERSION = '5999.000_001';
 
-use Encode qw/encode/;
 use File::Slurp qw/read_file/;
 use HTML::Template::Compiled;
 
 ##################################################
 
-use POSIX ();
-use Gruntmaster::Data ();
-use List::Util ();
-
-sub import {
-       my $caller = caller;
-       my ($self, $name, $title) = @_;
-
-       Gruntmaster::Data->export_to_level(1, $caller);
-       List::Util->export_to_level(1, $caller, qw/sum/);
-
-       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]->({qw/level debug message/ => $_[1]})
-       };
-}
-
-##################################################
-
-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/">Problem list</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">
-
-<div id="subtitle">TITLE_GOES_HERE</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 = $ENV{GRUNTMASTER_TEMPLATE_ROOT} or return %{$_[0]};
-       my ($templates, $name) = @_;
-       my %out = %$templates;
-       for (<$root/$name.*>) {
-               m/\.(.+)$/;
-               $out{$1} = read_file $_
-       }
+sub read_templates {
+       my $root = 'tmpl';
+       my $name = shift;
 
-       %out
+       map { m/\.(.+)$/; $1 => scalar read_file $_ } <tmpl/$name.*>;
 }
 
-sub reload_templates (){ $ENV{GRUNTMASTER_RELOAD_TEMPLATES} }
-
-my %header_templates = patch_templates \%orig_header_templates, 'header';
-my %footer_templates = patch_templates \%orig_footer_templates, 'footer';
+my %header_templates = read_templates 'header';
+my %footer_templates = read_templates 'footer';
 
 sub header{
   my ($language, $title) = @_;
-  %header_templates = patch_templates \%orig_header_templates, 'header' if reload_templates;
   $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger;
 }
 
 sub footer{
-  %footer_templates = patch_templates \%orig_footer_templates, 'footer' if reload_templates;
   $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;
+use POSIX ();
+use List::Util ();
+use LWP::UserAgent;
+use Plack::Request ();
+use feature ();
 
-       %out
+my $ua = LWP::UserAgent->new;
+my %templates;
+
+use Carp qw/cluck/;
+
+sub import_to {
+       my ($self, $caller, $name, $title) = @_;
+
+       strict->import;
+       feature->import(':5.14');
+       warnings->import;
+       File::Slurp->export_to_level(1, $caller, qw/read_file/);
+       List::Util->export_to_level(1, $caller, qw/sum/);
+
+       no strict 'refs';
+       *{"${caller}::ISA"} = [__PACKAGE__];
+       *{"${caller}::VERSION"} = $VERSION;
+       *{"${caller}::strftime"} = \&POSIX::strftime;
+       *{"${caller}::debug"} = sub {
+               local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1;
+               $_[0]->{'psgix.logger'}->({qw/level debug message/ => $_[1]})
+       };
+       *{"${caller}::db"} = sub { $_[0]->{'gruntmaster.dbic'} };
+       *{"${caller}::reply"} = sub { [200, ['Content-Type' => 'text/plain', 'Cache-Control' => 'no-cache'], [ @_ ] ] };
+       *{"${caller}::purge"} = sub {
+               return unless $ENV{PURGE_HOST};
+               my $req = HTTP::Request->new(PURGE => "http://$ENV{PURGE_HOST}$_[0]");
+               $ua->request($req)
+       };
+
+       if ($name) {
+               $templates{$caller} = { read_templates $name };
+               $templates{$caller}{$_}  = header ($_, $title) . $templates{$caller}{$_} for keys $templates{$caller};
+               $templates{$caller}{$_} .= footer  $_  for keys $templates{$caller};
+       }
 }
 
-##################################################
+sub import {
+       return unless $_[0] eq __PACKAGE__;
+       splice @_, 1, 0, scalar caller;
+       goto &import_to
+}
 
-my %templates;
+##################################################
 
 sub generate{
        my ($self, $lang, @args) = @_;
 
-       $templates{$self} = { cook_templates $self->TEMPLATES, $self->NAME => $self->TITLE } if !exists $templates{$self} or reload_templates;
-
-       my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$self}{$lang}, default_escape => 'HTML',);
+       my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$self}{$lang}, default_escape => 'HTML', use_perl => 1);
        $self->_generate($htc, $lang, @args);
-       [200, ['Content-Type' => 'text/html', 'Content-Language' => $_[1], 'Vary' => 'Accept-Language'], [ encode 'UTF-8' => $htc->output ] ]
+       my $out = $htc->output;
+       utf8::decode($out) for 1 .. 3;
+       utf8::encode($out);
+       my $vary = 'Accept-Language, ' . $self->vary;
+       [200, ['Content-Type' => 'text/html', 'Content-Language' => $_[1], 'Vary' => $vary, 'X-Forever' => 1, 'Cache-Control' => 'max-age=' . $self->max_age], [ $out ] ]
 }
 
 sub _generate {}
 
+sub vary { '' }
+
+sub max_age { 60 }
+
 sub variants {
-       [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $_[0]->TEMPLATES ]
+       return [] unless exists $templates{$_[0]};
+       [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $templates{$_[0]} ]
 }
 
 1
This page took 0.023003 seconds and 4 git commands to generate.