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