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