]>
Commit | Line | Data |
---|---|---|
bb95f538 | 1 | package Gruntmaster::Page::Base; |
832cb45e MG |
2 | |
3 | use 5.014000; | |
4 | use strict; | |
5 | use warnings; | |
6d78fc24 MG |
6 | |
7 | use File::Slurp qw/read_file/; | |
bb95f538 MG |
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 | ################################################## | |
832cb45e | 30 | |
2b0036ac | 31 | my %orig_header_templates = ( |
832cb45e MG |
32 | en => <<'HTML', |
33 | <!DOCTYPE html> | |
34 | <title>TITLE_GOES_HERE</title> | |
35 | <link rel="stylesheet" href="/gm.css"> | |
879063d7 MG |
36 | <script src="/zepto.var" defer></script> |
37 | <script src="/view.js" defer></script> | |
38 | <script src="/form.js" defer></script> | |
832cb45e MG |
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 | ||
e5a94ee5 | 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> |
832cb45e MG |
46 | |
47 | HTML | |
48 | ); | |
49 | ||
2b0036ac | 50 | my %orig_footer_templates = ( |
832cb45e MG |
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 | ||
2b0036ac | 61 | sub patch_templates { |
eafc7f54 | 62 | my $root = $ENV{GRUNTMASTER_TEMPLATE_ROOT} or return %{$_[0]}; |
6d78fc24 | 63 | my ($templates, $name) = @_; |
2b0036ac | 64 | my %out = %$templates; |
d1a026f7 | 65 | for (<$root/$name.*>) { |
6d78fc24 | 66 | m/\.(.+)$/; |
2b0036ac | 67 | $out{$1} = read_file $_ |
6d78fc24 | 68 | } |
2b0036ac MG |
69 | |
70 | %out | |
6d78fc24 MG |
71 | } |
72 | ||
bb95f538 MG |
73 | sub reload_templates (){ $ENV{GRUNTMASTER_RELOAD_TEMPLATES} } |
74 | ||
2b0036ac MG |
75 | my %header_templates = patch_templates \%orig_header_templates, 'header'; |
76 | my %footer_templates = patch_templates \%orig_footer_templates, 'footer'; | |
77 | ||
832cb45e MG |
78 | sub header{ |
79 | my ($language, $title) = @_; | |
2b0036ac | 80 | %header_templates = patch_templates \%orig_header_templates, 'header' if reload_templates; |
832cb45e MG |
81 | $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger; |
82 | } | |
83 | ||
84 | sub footer{ | |
2b0036ac | 85 | %footer_templates = patch_templates \%orig_footer_templates, 'footer' if reload_templates; |
832cb45e MG |
86 | $footer_templates{$_[0]}; |
87 | } | |
88 | ||
bb95f538 | 89 | sub cook_templates { |
2b0036ac MG |
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 | ||
bb95f538 MG |
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 |