]>
Commit | Line | Data |
---|---|---|
832cb45e MG |
1 | package Gruntmaster::Page::Common; |
2 | ||
3 | use 5.014000; | |
4 | use strict; | |
5 | use warnings; | |
6 | use parent qw/Exporter/; | |
2b0036ac | 7 | our @EXPORT_OK = qw/header footer cook_templates reload_templates/; |
6d78fc24 MG |
8 | |
9 | use File::Slurp qw/read_file/; | |
832cb45e | 10 | |
2b0036ac | 11 | my %orig_header_templates = ( |
832cb45e MG |
12 | en => <<'HTML', |
13 | <!DOCTYPE html> | |
14 | <title>TITLE_GOES_HERE</title> | |
15 | <link rel="stylesheet" href="/gm.css"> | |
879063d7 MG |
16 | <script src="/zepto.var" defer></script> |
17 | <script src="/view.js" defer></script> | |
18 | <script src="/form.js" defer></script> | |
832cb45e MG |
19 | <meta charset="utf-8"> |
20 | ||
21 | <span id="admin"></span> | |
22 | <div id="title"><span class="i">i</span><span class="Eval">Eval</span></div> | |
23 | <div id="subtitle">TITLE_GOES_HERE</div> | |
24 | ||
e5a94ee5 | 25 | <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 |
26 | |
27 | HTML | |
28 | ); | |
29 | ||
2b0036ac | 30 | my %orig_footer_templates = ( |
832cb45e MG |
31 | en => <<'HTML', |
32 | ||
33 | <footer> | |
34 | Dilmom: Why don't you call your product the Gruntmaster 6000? | |
35 | Dilbert: What kind of product do you see when you imagine a Gruntmaster 6000? | |
36 | Dilmom: Well, it's a stripped-down version of the Gruntmaster 9000, of course. But it's software-upgradeable. | |
37 | </footer> | |
38 | HTML | |
39 | ); | |
40 | ||
2b0036ac | 41 | sub patch_templates { |
6d78fc24 MG |
42 | my $root = $ENV{GRUNTMASTER_TEMPLATE_ROOT} or return; |
43 | my ($templates, $name) = @_; | |
2b0036ac | 44 | my %out = %$templates; |
6d78fc24 MG |
45 | for (<$root/$name*>) { |
46 | m/\.(.+)$/; | |
2b0036ac | 47 | $out{$1} = read_file $_ |
6d78fc24 | 48 | } |
2b0036ac MG |
49 | |
50 | %out | |
6d78fc24 MG |
51 | } |
52 | ||
2b0036ac MG |
53 | my %header_templates = patch_templates \%orig_header_templates, 'header'; |
54 | my %footer_templates = patch_templates \%orig_footer_templates, 'footer'; | |
55 | ||
56 | sub reload_templates () { $ENV{GRUNTMASTER_RELOAD_TEMPLATES} } | |
6d78fc24 | 57 | |
832cb45e MG |
58 | sub header{ |
59 | my ($language, $title) = @_; | |
2b0036ac | 60 | %header_templates = patch_templates \%orig_header_templates, 'header' if reload_templates; |
832cb45e MG |
61 | $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger; |
62 | } | |
63 | ||
64 | sub footer{ | |
2b0036ac | 65 | %footer_templates = patch_templates \%orig_footer_templates, 'footer' if reload_templates; |
832cb45e MG |
66 | $footer_templates{$_[0]}; |
67 | } | |
68 | ||
2b0036ac MG |
69 | sub cook_templates (\%@) { |
70 | my ($templates, $name, $title) = @_; | |
71 | ||
72 | my %out = patch_templates $templates, $name; | |
73 | $out{$_} = header ($_, $title) . $out{$_} for keys %out; | |
74 | $out{$_} .= footer $_ for keys %out; | |
75 | ||
76 | %out | |
77 | } | |
78 | ||
832cb45e | 79 | 1; |