]> iEval git - gruntmaster-page.git/blob - lib/Gruntmaster/Page.pm
d014ac18fa8064ca71d15db4c24edc3e356e6d11
[gruntmaster-page.git] / lib / Gruntmaster / Page.pm
1 package Gruntmaster::Page;
2
3 use 5.014000;
4 use strict;
5 use warnings;
6 use parent qw/Exporter/;
7 our @EXPORT_OK = qw/generate header footer/;
8
9 use File::Basename qw/fileparse/;
10 use File::Slurp qw/write_file/;
11 use IO::Compress::Gzip qw/gzip/;
12
13 our $VERSION = '0.001';
14 our @generators;
15
16 use constant LANGUAGES => [ 'en' ];
17 use constant CONTENT_TYPES => {
18 html => 'text/html; charset=UTF-8',
19 txt => 'text/plain; charset=UTF-8',
20 };
21
22 my %header_templates = (
23 en => <<'HTML',
24 <!DOCTYPE html>
25 <title>TITLE_GOES_HERE</title>
26 <link rel="stylesheet" href="/gm.css">
27 <script src="/jquery-2.0.3.min.js"></script>
28 <script src="/view.js"></script>
29 <meta charset="utf-8">
30
31 <span id="admin"></span>
32 <div id="title"><span class="i">i</span><span class="Eval">Eval</span></div>
33 <div id="subtitle">TITLE_GOES_HERE</div>
34
35 <nav><ul><li><a href="/">Home</a><li><a href="/log/">View job log</a><li><a href="/submit.var">Submit job</a><li><a href="/pb/">Problem list</a><li><a href="/ct/">Contests</a></ul></nav>
36
37 HTML
38 );
39
40 my %footer_templates = (
41 en => <<'HTML',
42
43 <footer>
44 Dilmom: Why don't you call your product the Gruntmaster 6000?
45 Dilbert: What kind of product do you see when you imagine a Gruntmaster 6000?
46 Dilmom: Well, it's a stripped-down version of the Gruntmaster 9000, of course. But it's software-upgradeable.
47 </footer>
48 HTML
49 );
50
51 sub header{
52 my ($language, $title) = @_;
53 $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger;
54 }
55
56 sub footer{
57 $footer_templates{$_[0]};
58 }
59
60 sub declaregen{
61 my ($generator, $regex) = @_;
62 $generator = "Gruntmaster::Page::$generator";
63 eval "require $generator";
64 my $gensub = $generator->can('generate') or die "No such generator: $generator";
65 push @generators, [$regex, $gensub];
66 }
67
68 {
69 my $component = qr'[^/]+';
70 my $contest = qr,(?:ct/$component/)?,;
71 declaregen Index => qr,^index$,;
72 declaregen Ct => qr,^ct/index$,;
73 declaregen 'Ct::Entry' => qr,^ct/$component/index$,;
74 #declaregen St => qr,^ct/$component/st/index$,;
75 declaregen Log => qr,^${contest}log/index$,;
76 declaregen 'Log::Entry' => qr,^${contest}log/$component/index$,;
77 declaregen Submit => qr,^${contest}submit$,;
78 declaregen Pb => qr,^${contest}pb/index$,;
79 declaregen 'Pb::Entry' => qr,^${contest}pb/$component/index$,;
80 }
81
82 sub generate{
83 my ($path) = @_;
84 my ($path_noext, $ext) = $path =~ m/^(.*)\.(.*)$/;
85 my $basename = fileparse $path_noext;
86
87 open my $typemap, ">$path_noext.var";
88 say $typemap "URI: $basename\n";
89 for my $gen(@generators) {
90 my ($regex, $generator) = @$gen;
91 next unless $path_noext =~ $regex;
92 for my $lang (@{LANGUAGES()}) {
93 my $page = $generator->($path, $lang);
94 write_file "$path_noext.$lang.$ext", $page;
95 say $typemap "URI: $basename.$lang.$ext\nContent-Language: $lang\nContent-Type: " . CONTENT_TYPES->{$ext} . "\n";
96 gzip \$page => "$path_noext.$lang.gz.$ext", Minimal => 1;
97 say $typemap "URI: $basename.$lang.gz.$ext\nContent-Language: $lang\nContent-Encoding: gzip\nContent-Type: " . CONTENT_TYPES->{$ext} . "\n";
98 }
99 }
100 close $typemap;
101 }
102
103 1;
104 __END__
105 # Below is stub documentation for your module. You'd better edit it!
106
107 =head1 NAME
108
109 Gruntmaster::Page - Perl extension for blah blah blah
110
111 =head1 SYNOPSIS
112
113 use Gruntmaster::Page;
114 blah blah blah
115
116 =head1 DESCRIPTION
117
118 Stub documentation for Gruntmaster::Page, created by h2xs. It looks like the
119 author of the extension was negligent enough to leave the stub
120 unedited.
121
122 Blah blah blah.
123
124 =head2 EXPORT
125
126 None by default.
127
128
129
130 =head1 SEE ALSO
131
132 Mention other useful documentation such as the documentation of
133 related modules or operating system documentation (such as man pages
134 in UNIX), or any relevant external documentation such as RFCs or
135 standards.
136
137 If you have a mailing list set up for your module, mention it here.
138
139 If you have a web site set up for your module, mention it here.
140
141 =head1 AUTHOR
142
143 Marius Gavrilescu, E<lt>marius@E<gt>
144
145 =head1 COPYRIGHT AND LICENSE
146
147 Copyright (C) 2013 by Marius Gavrilescu
148
149 This library is free software; you can redistribute it and/or modify
150 it under the same terms as Perl itself, either Perl version 5.18.1 or,
151 at your option, any later version of Perl 5 you may have available.
152
153
154 =cut
This page took 0.05197 seconds and 3 git commands to generate.