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