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