]>
Commit | Line | Data |
---|---|---|
1 | package Gruntmaster::Page; | |
2 | ||
3 | use 5.014000; | |
4 | use strict; | |
5 | use warnings; | |
6 | ||
7 | use Fcntl qw/:flock/; | |
8 | use File::Basename qw/fileparse/; | |
9 | use File::Path qw/make_path/; | |
10 | use File::Slurp qw/read_file write_file/; | |
11 | use IO::Compress::Gzip qw/gzip/; | |
12 | use IO::File; | |
13 | use Gruntmaster::Data; | |
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 | push @generators, [$regex, $generator]; | |
29 | } | |
30 | ||
31 | { | |
32 | my $component = qr'[^/]+'; | |
33 | my $contest = qr,(?:ct/$component/)?,; | |
34 | declaregen Us => qr,^us/index$,; | |
35 | declaregen 'Us::Entry' => qr,^us/$component$,; | |
36 | declaregen Ct => qr,^ct/index$,; | |
37 | declaregen 'Ct::Entry' => qr,^ct/$component/index$,; | |
38 | declaregen St => qr,^${contest}log/st$,; | |
39 | declaregen Log => qr,^${contest}log/(?:\d+|index)$,; | |
40 | declaregen 'Log::Entry' => qr,^${contest}log/job/$component$,; | |
41 | declaregen Submit => qr,^${contest}submit$,; | |
42 | declaregen Pb => qr,^${contest}pb/index$,; | |
43 | declaregen 'Pb::Entry' => qr,^${contest}pb/$component$,; | |
44 | } | |
45 | ||
46 | sub generate{ | |
47 | my ($path, $topic) = @_; | |
48 | my ($path_noext, $ext) = $path =~ m/^(.*)\.(.*)$/; | |
49 | my ($basename, $directories) = fileparse $path_noext; | |
50 | make_path $directories; | |
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 | ||
57 | my $fill_typemap = sub { | |
58 | for my $lang (@{LANGUAGES()}) { | |
59 | my $page = $_[0]->($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 | 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); | |
82 | } | |
83 | ||
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; | |
90 | } | |
91 | ||
92 | sub gensrc{ | |
93 | my ($contest, $job) = split /\./, $_[0]; | |
94 | local $Gruntmaster::Data::contest = $contest if $contest; | |
95 | my $ext = job_extension $job; | |
96 | my $log = $contest ? "ct/$contest/log" : 'log'; | |
97 | make_path "$log/src/"; | |
98 | say STDERR "Writing to $log/src/$job.$ext"; | |
99 | write_file "$log/src/$job.$ext", job_inmeta($job)->{files}{prog}{content}; | |
100 | } | |
101 | ||
102 | 1; | |
103 | __END__ | |
104 | # Below is stub documentation for your module. You'd better edit it! | |
105 | ||
106 | =head1 NAME | |
107 | ||
108 | Gruntmaster::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 | ||
117 | Stub documentation for Gruntmaster::Page, created by h2xs. It looks like the | |
118 | author of the extension was negligent enough to leave the stub | |
119 | unedited. | |
120 | ||
121 | Blah blah blah. | |
122 | ||
123 | =head2 EXPORT | |
124 | ||
125 | None by default. | |
126 | ||
127 | ||
128 | ||
129 | =head1 SEE ALSO | |
130 | ||
131 | Mention other useful documentation such as the documentation of | |
132 | related modules or operating system documentation (such as man pages | |
133 | in UNIX), or any relevant external documentation such as RFCs or | |
134 | standards. | |
135 | ||
136 | If you have a mailing list set up for your module, mention it here. | |
137 | ||
138 | If you have a web site set up for your module, mention it here. | |
139 | ||
140 | =head1 AUTHOR | |
141 | ||
142 | Marius Gavrilescu, E<lt>marius@E<gt> | |
143 | ||
144 | =head1 COPYRIGHT AND LICENSE | |
145 | ||
146 | Copyright (C) 2013 by Marius Gavrilescu | |
147 | ||
148 | This library is free software; you can redistribute it and/or modify | |
149 | it under the same terms as Perl itself, either Perl version 5.18.1 or, | |
150 | at your option, any later version of Perl 5 you may have available. | |
151 | ||
152 | ||
153 | =cut |