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