]> iEval git - gruntmaster-page.git/blame_incremental - lib/Gruntmaster/Page.pm
Make generate more reliable
[gruntmaster-page.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/;
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;
14
15our $VERSION = '0.001';
16our @generators;
17
18use constant LANGUAGES => [ 'en' ];
19use constant CONTENT_TYPES => {
20 html => 'text/html; charset=UTF-8',
21 txt => 'text/plain; charset=UTF-8',
22};
23
24sub 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/st/index$,;
39 declaregen Log => qr,^${contest}log/index$,;
40 declaregen 'Log::Entry' => qr,^${contest}log/$component/index$,;
41 declaregen Submit => qr,^${contest}submit$,;
42 declaregen Pb => qr,^${contest}pb/index$,;
43 declaregen 'Pb::Entry' => qr,^${contest}pb/$component/index$,;
44}
45
46sub generate{
47 my ($path) = @_;
48 my ($path_noext, $ext) = $path =~ m/^(.*)\.(.*)$/;
49 my $basename = fileparse $path_noext;
50
51 IO::File->new(">$path_noext.var")->close unless -f "$path_noext.var";
52 flock my $lockfh = IO::File->new("<$path_noext.var"), LOCK_EX;
53 open my $typemap, ">$path_noext.var.new";
54 say $typemap "URI: $basename\n";
55 for my $gen(@generators) {
56 my ($regex, $generator) = @$gen;
57 next unless $path_noext =~ $regex;
58 for my $lang (@{LANGUAGES()}) {
59 my $page = $generator->($path, $lang);
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 }
65 }
66
67 for my $lang(@{LANGUAGES()}) {
68 rename "$path_noext.$lang.$ext.new", "$path_noext.$lang.$ext";
69 rename "$path_noext.$lang.gz.$ext.new", "$path_noext.$lang.gz.$ext";
70 }
71 rename "$path_noext.var.new", "$path_noext.var";
72 close $typemap;
73}
74
751;
76__END__
77# Below is stub documentation for your module. You'd better edit it!
78
79=head1 NAME
80
81Gruntmaster::Page - Perl extension for blah blah blah
82
83=head1 SYNOPSIS
84
85 use Gruntmaster::Page;
86 blah blah blah
87
88=head1 DESCRIPTION
89
90Stub documentation for Gruntmaster::Page, created by h2xs. It looks like the
91author of the extension was negligent enough to leave the stub
92unedited.
93
94Blah blah blah.
95
96=head2 EXPORT
97
98None by default.
99
100
101
102=head1 SEE ALSO
103
104Mention other useful documentation such as the documentation of
105related modules or operating system documentation (such as man pages
106in UNIX), or any relevant external documentation such as RFCs or
107standards.
108
109If you have a mailing list set up for your module, mention it here.
110
111If you have a web site set up for your module, mention it here.
112
113=head1 AUTHOR
114
115Marius Gavrilescu, E<lt>marius@E<gt>
116
117=head1 COPYRIGHT AND LICENSE
118
119Copyright (C) 2013 by Marius Gavrilescu
120
121This library is free software; you can redistribute it and/or modify
122it under the same terms as Perl itself, either Perl version 5.18.1 or,
123at your option, any later version of Perl 5 you may have available.
124
125
126=cut
This page took 0.020078 seconds and 4 git commands to generate.