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