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