]> iEval git - gruntmaster-page.git/blob - lib/Gruntmaster/Page/Base.pm
Fix template patches glob pattern
[gruntmaster-page.git] / lib / Gruntmaster / Page / Base.pm
1 package Gruntmaster::Page::Base;
2
3 use 5.014000;
4 use strict;
5 use warnings;
6
7 use File::Slurp qw/read_file/;
8 use HTML::Template::Compiled;
9
10 ##################################################
11
12 use POSIX ();
13 use Gruntmaster::Data ();
14 use List::Util ();
15
16 sub import {
17 my $caller = caller;
18 my ($self, $name, $title) = @_;
19
20 Gruntmaster::Data->export_to_level(1, $caller);
21 List::Util->export_to_level(1, $caller, qw/sum/);
22
23 no strict 'refs';
24 *{"${caller}::strftime"} = \&POSIX::strftime;
25 *{"${caller}::NAME"} = sub () { $name };
26 *{"${caller}::TITLE"} = sub () { $title };
27 }
28
29 ##################################################
30
31 my %orig_header_templates = (
32 en => <<'HTML',
33 <!DOCTYPE html>
34 <title>TITLE_GOES_HERE</title>
35 <link rel="stylesheet" href="/gm.css">
36 <script src="/zepto.var" defer></script>
37 <script src="/view.js" defer></script>
38 <script src="/form.js" defer></script>
39 <meta charset="utf-8">
40
41 <span id="admin"></span>
42 <div id="title"><span class="i">i</span><span class="Eval">Eval</span></div>
43 <div id="subtitle">TITLE_GOES_HERE</div>
44
45 <nav><ul><li><a href="/learn.var">Learn</a><li><a href="/pb/">Practice</a><li><a href="/ct/">Compete</a><li><a href="/log/">Job log</a></ul></nav>
46
47 HTML
48 );
49
50 my %orig_footer_templates = (
51 en => <<'HTML',
52
53 <footer>
54 Dilmom: Why don't you call your product the Gruntmaster 6000?
55 Dilbert: What kind of product do you see when you imagine a Gruntmaster 6000?
56 Dilmom: Well, it's a stripped-down version of the Gruntmaster 9000, of course. But it's software-upgradeable.
57 </footer>
58 HTML
59 );
60
61 sub patch_templates {
62 my $root = $ENV{GRUNTMASTER_TEMPLATE_ROOT} or return;
63 my ($templates, $name) = @_;
64 my %out = %$templates;
65 for (<$root/$name.*>) {
66 m/\.(.+)$/;
67 $out{$1} = read_file $_
68 }
69
70 %out
71 }
72
73 sub reload_templates (){ $ENV{GRUNTMASTER_RELOAD_TEMPLATES} }
74
75 my %header_templates = patch_templates \%orig_header_templates, 'header';
76 my %footer_templates = patch_templates \%orig_footer_templates, 'footer';
77
78 sub header{
79 my ($language, $title) = @_;
80 %header_templates = patch_templates \%orig_header_templates, 'header' if reload_templates;
81 $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger;
82 }
83
84 sub footer{
85 %footer_templates = patch_templates \%orig_footer_templates, 'footer' if reload_templates;
86 $footer_templates{$_[0]};
87 }
88
89 sub cook_templates {
90 my ($templates, $name, $title) = @_;
91
92 my %out = patch_templates $templates, $name;
93 $out{$_} = header ($_, $title) . $out{$_} for keys %out;
94 $out{$_} .= footer $_ for keys %out;
95
96 %out
97 }
98
99 ##################################################
100
101 my %templates;
102
103 sub generate{
104 my ($self, $path, $lang) = @_;
105
106 $templates{$self} = { cook_templates $self->TEMPLATES, $self->NAME => $self->TITLE } if !exists $templates{$self} or reload_templates;
107
108 my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$self}{$lang}, default_escape => 'HTML',);
109 $self->_generate($htc, $path, $lang);
110 $htc->output
111 }
112
113 sub _generate {}
114
115 1
This page took 0.052598 seconds and 4 git commands to generate.