]> iEval git - gruntmaster-page.git/blame - lib/Gruntmaster/Page.pm
Make all generators inherit from a common base
[gruntmaster-page.git] / lib / Gruntmaster / Page.pm
CommitLineData
42546e6c
MG
1package Gruntmaster::Page;
2
3use 5.014000;
4use strict;
5use warnings;
6use parent qw/Exporter/;
22642746 7our @EXPORT_OK = qw/generate _generate/;
42546e6c 8
35073130 9use Fcntl qw/:flock/;
42546e6c 10use File::Basename qw/fileparse/;
ea6ce64d 11use File::Path qw/make_path/;
42546e6c
MG
12use File::Slurp qw/write_file/;
13use IO::Compress::Gzip qw/gzip/;
35073130 14use IO::File;
22642746 15use Gruntmaster::Data qw/PUBLISH/;
42546e6c
MG
16
17our $VERSION = '0.001';
18our @generators;
19
20use constant LANGUAGES => [ 'en' ];
21use constant CONTENT_TYPES => {
cd9af27e
MG
22 html => 'text/html; charset=UTF-8',
23 txt => 'text/plain; charset=UTF-8',
42546e6c
MG
24};
25
42546e6c 26sub declaregen{
cd9af27e
MG
27 my ($generator, $regex) = @_;
28 $generator = "Gruntmaster::Page::$generator";
29 eval "require $generator";
bb95f538 30 push @generators, [$regex, $generator];
42546e6c
MG
31}
32
fe78f0c1 33{
cd9af27e
MG
34 my $component = qr'[^/]+';
35 my $contest = qr,(?:ct/$component/)?,;
36 declaregen Index => qr,^index$,;
37 declaregen Learn => qr,^learn$,;
088a8d62 38 declaregen Account => qr,^account$,;
4aa8ba86
MG
39 declaregen Us => qr,^us/index$,;
40 declaregen 'Us::Entry' => qr,^us/$component$,;
cd9af27e
MG
41 declaregen Ct => qr,^ct/index$,;
42 declaregen 'Ct::Entry' => qr,^ct/$component/index$,;
43 declaregen St => qr,^ct/$component/log/st$,;
44 declaregen Log => qr,^${contest}log/(?:\d+|index)$,;
e5f2bda6 45 declaregen 'Log::Entry' => qr,^${contest}log/job/$component$,;
cd9af27e
MG
46 declaregen Submit => qr,^${contest}submit$,;
47 declaregen Pb => qr,^${contest}pb/index$,;
3da9c3c2 48 declaregen 'Pb::Entry' => qr,^${contest}pb/$component$,;
fe78f0c1 49}
42546e6c 50
22642746 51sub _generate{
cd9af27e
MG
52 my ($path) = @_;
53 my ($path_noext, $ext) = $path =~ m/^(.*)\.(.*)$/;
ea6ce64d
MG
54 my ($basename, $directories) = fileparse $path_noext;
55 make_path $directories;
42546e6c 56
cd9af27e
MG
57 IO::File->new(">$path_noext.var")->close unless -f "$path_noext.var";
58 flock my $lockfh = IO::File->new("<$path_noext.var"), LOCK_EX;
59 open my $typemap, ">$path_noext.var.new";
60 say $typemap "URI: $basename\n";
61 for my $gen (@generators) {
62 my ($regex, $generator) = @$gen;
63 next unless $path_noext =~ $regex;
64 for my $lang (@{LANGUAGES()}) {
bb95f538 65 my $page = $generator->generate($path, $lang);
cd9af27e
MG
66 write_file "$path_noext.$lang.$ext.new", $page;
67 say $typemap "URI: $basename.$lang.$ext\nContent-Language: $lang\nContent-Type: " . CONTENT_TYPES->{$ext} . "\n";
68 gzip \$page => "$path_noext.$lang.gz.$ext.new", Minimal => 1;
69 say $typemap "URI: $basename.$lang.gz.$ext\nContent-Language: $lang\nContent-Encoding: gzip\nContent-Type: " . CONTENT_TYPES->{$ext} . "\n";
70 }
3da9c3c2 71 last
42546e6c 72 }
35073130 73
cd9af27e
MG
74 for my $lang (@{LANGUAGES()}) {
75 rename "$path_noext.$lang.$ext.new", "$path_noext.$lang.$ext";
76 rename "$path_noext.$lang.gz.$ext.new", "$path_noext.$lang.gz.$ext";
77 }
78 rename "$path_noext.var.new", "$path_noext.var";
79 close $typemap;
42546e6c
MG
80}
81
22642746
MG
82sub generate{
83 PUBLISH 'genpage', shift;
84}
85
42546e6c
MG
861;
87__END__
88# Below is stub documentation for your module. You'd better edit it!
89
90=head1 NAME
91
92Gruntmaster::Page - Perl extension for blah blah blah
93
94=head1 SYNOPSIS
95
96 use Gruntmaster::Page;
97 blah blah blah
98
99=head1 DESCRIPTION
100
101Stub documentation for Gruntmaster::Page, created by h2xs. It looks like the
102author of the extension was negligent enough to leave the stub
103unedited.
104
105Blah blah blah.
106
107=head2 EXPORT
108
109None by default.
110
111
112
113=head1 SEE ALSO
114
115Mention other useful documentation such as the documentation of
116related modules or operating system documentation (such as man pages
117in UNIX), or any relevant external documentation such as RFCs or
118standards.
119
120If you have a mailing list set up for your module, mention it here.
121
122If you have a web site set up for your module, mention it here.
123
124=head1 AUTHOR
125
126Marius Gavrilescu, E<lt>marius@E<gt>
127
128=head1 COPYRIGHT AND LICENSE
129
130Copyright (C) 2013 by Marius Gavrilescu
131
132This library is free software; you can redistribute it and/or modify
133it under the same terms as Perl itself, either Perl version 5.18.1 or,
134at your option, any later version of Perl 5 you may have available.
135
136
137=cut
This page took 0.033648 seconds and 4 git commands to generate.