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