]>
Commit | Line | Data |
---|---|---|
1 | package Gruntmaster::Page::Base; | |
2 | ||
3 | use 5.014000; | |
4 | use strict; | |
5 | use warnings; | |
6 | ||
7 | use Encode qw/encode/; | |
8 | use File::Slurp qw/read_file/; | |
9 | use HTML::Template::Compiled; | |
10 | ||
11 | ################################################## | |
12 | ||
13 | use POSIX (); | |
14 | use Gruntmaster::Data (); | |
15 | use List::Util (); | |
16 | ||
17 | sub import { | |
18 | my $caller = caller; | |
19 | my ($self, $name, $title) = @_; | |
20 | ||
21 | Gruntmaster::Data->export_to_level(1, $caller); | |
22 | List::Util->export_to_level(1, $caller, qw/sum/); | |
23 | ||
24 | no strict 'refs'; | |
25 | *{"${caller}::strftime"} = \&POSIX::strftime; | |
26 | *{"${caller}::NAME"} = sub () { $name }; | |
27 | *{"${caller}::TITLE"} = sub () { $title }; | |
28 | *{"${caller}::debug"} = sub { | |
29 | local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1; | |
30 | $_[0]->{'psgix.logger'}->({qw/level debug message/ => $_[1]}) | |
31 | }; | |
32 | *{"${caller}::reply"} = sub { [200, ['Content-Type' => 'text/plain'], [ @_ ] ] } | |
33 | ||
34 | } | |
35 | ||
36 | ################################################## | |
37 | ||
38 | my %orig_header_templates = ( | |
39 | en => <<'HTML', | |
40 | <!DOCTYPE html> | |
41 | <title>TITLE_GOES_HERE</title> | |
42 | <meta charset="utf-8"> | |
43 | <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
44 | ||
45 | <link rel="stylesheet" href="/css/cyborg" id="stylesheet"> | |
46 | <script src="/js" type="text/javascript"></script> | |
47 | ||
48 | <nav class="navbar navbar-default navbar-static-top" role="navigation"> | |
49 | <div class="container-fluid"> | |
50 | <div class="navbar-header"> | |
51 | <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> | |
52 | <a class="navbar-brand" href="/">Gruntmaster 6000</a> | |
53 | </div> | |
54 | ||
55 | <div class="collapse navbar-collapse"> | |
56 | <ul class="nav navbar-nav"> | |
57 | <li><a href="/pb/">Problems</a> | |
58 | <li><a href="/ct/">Contests</a> | |
59 | <li><a href="/account">Account</a> | |
60 | </ul> | |
61 | ||
62 | <ul class="nav navbar-nav navbar-right"> | |
63 | <li><a class="dropdown-toggle" data-toggle="dropdown"> Theme <span class="caret"></span></a> | |
64 | ||
65 | <ul class="dropdown-menu" role="menu"> | |
66 | <li><a href="#" id="theme_slate">Gunmetal gray</a> | |
67 | <li><a href="#" id="theme_cyborg">Black</a> | |
68 | <li><a href="#" id="theme_cerulean">White</a> | |
69 | <li><a href="#" id="theme_cosmo">Metro</a> | |
70 | </ul> | |
71 | ||
72 | <li><a href="/log/">Job log</a> | |
73 | </ul> | |
74 | </div> | |
75 | </div> | |
76 | </nav> | |
77 | ||
78 | <div class="container-fluid"> | |
79 | ||
80 | <div id="subtitle">TITLE_GOES_HERE</div> | |
81 | <div id="result"></div> | |
82 | HTML | |
83 | ); | |
84 | ||
85 | my %orig_footer_templates = ( | |
86 | en => <<'HTML', | |
87 | ||
88 | <footer> | |
89 | Dilmom: Why don't you call your product the Gruntmaster 6000? | |
90 | Dilbert: What kind of product do you see when you imagine a Gruntmaster 6000? | |
91 | Dilmom: Well, it's a stripped-down version of the Gruntmaster 9000, of course. But it's software-upgradeable. | |
92 | </footer> | |
93 | HTML | |
94 | ); | |
95 | ||
96 | sub patch_templates { | |
97 | my $root = 'tmpl'; | |
98 | return %{$_[0]} unless -d $root; | |
99 | my ($templates, $name) = @_; | |
100 | my %out = %$templates; | |
101 | for (<$root/$name.*>) { | |
102 | m/\.(.+)$/; | |
103 | $out{$1} = read_file $_ | |
104 | } | |
105 | ||
106 | %out | |
107 | } | |
108 | ||
109 | my %header_templates = patch_templates \%orig_header_templates, 'header'; | |
110 | my %footer_templates = patch_templates \%orig_footer_templates, 'footer'; | |
111 | ||
112 | sub header{ | |
113 | my ($language, $title) = @_; | |
114 | $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger; | |
115 | } | |
116 | ||
117 | sub footer{ | |
118 | $footer_templates{$_[0]}; | |
119 | } | |
120 | ||
121 | sub cook_templates { | |
122 | my ($templates, $name, $title) = @_; | |
123 | ||
124 | my %out = patch_templates $templates, $name; | |
125 | $out{$_} = header ($_, $title) . $out{$_} for keys %out; | |
126 | $out{$_} .= footer $_ for keys %out; | |
127 | ||
128 | %out | |
129 | } | |
130 | ||
131 | ################################################## | |
132 | ||
133 | my %templates; | |
134 | ||
135 | sub generate{ | |
136 | my ($self, $lang, @args) = @_; | |
137 | ||
138 | $templates{$self} = { cook_templates $self->TEMPLATES, $self->NAME => $self->TITLE } unless exists $templates{$self}; | |
139 | ||
140 | my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$self}{$lang}, default_escape => 'HTML',); | |
141 | $self->_generate($htc, $lang, @args); | |
142 | [200, ['Content-Type' => 'text/html', 'Content-Language' => $_[1], 'Vary' => 'Accept-Language'], [ encode 'UTF-8' => $htc->output ] ] | |
143 | } | |
144 | ||
145 | sub _generate {} | |
146 | ||
147 | sub variants { | |
148 | [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $_[0]->TEMPLATES ] | |
149 | } | |
150 | ||
151 | 1 |