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