]> iEval git - plack-app-gruntmaster.git/blob - lib/Gruntmaster/Page.pm
Move header / footer to a separate module and update MANIFEST
[plack-app-gruntmaster.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 File::Basename qw/fileparse/;
10 use File::Slurp qw/write_file/;
11 use IO::Compress::Gzip qw/gzip/;
12
13 our $VERSION = '0.001';
14 our @generators;
15
16 use constant LANGUAGES => [ 'en' ];
17 use constant CONTENT_TYPES => {
18 html => 'text/html; charset=UTF-8',
19 txt => 'text/plain; charset=UTF-8',
20 };
21
22 sub declaregen{
23 my ($generator, $regex) = @_;
24 $generator = "Gruntmaster::Page::$generator";
25 eval "require $generator";
26 my $gensub = $generator->can('generate') or die "No such generator: $generator";
27 push @generators, [$regex, $gensub];
28 }
29
30 {
31 my $component = qr'[^/]+';
32 my $contest = qr,(?:ct/$component/)?,;
33 declaregen Index => qr,^index$,;
34 declaregen Ct => qr,^ct/index$,;
35 declaregen 'Ct::Entry' => qr,^ct/$component/index$,;
36 #declaregen St => qr,^ct/$component/st/index$,;
37 declaregen Log => qr,^${contest}log/index$,;
38 declaregen 'Log::Entry' => qr,^${contest}log/$component/index$,;
39 declaregen Submit => qr,^${contest}submit$,;
40 declaregen Pb => qr,^${contest}pb/index$,;
41 declaregen 'Pb::Entry' => qr,^${contest}pb/$component/index$,;
42 }
43
44 sub generate{
45 my ($path) = @_;
46 my ($path_noext, $ext) = $path =~ m/^(.*)\.(.*)$/;
47 my $basename = fileparse $path_noext;
48
49 open my $typemap, ">$path_noext.var";
50 say $typemap "URI: $basename\n";
51 for my $gen(@generators) {
52 my ($regex, $generator) = @$gen;
53 next unless $path_noext =~ $regex;
54 for my $lang (@{LANGUAGES()}) {
55 my $page = $generator->($path, $lang);
56 write_file "$path_noext.$lang.$ext", $page;
57 say $typemap "URI: $basename.$lang.$ext\nContent-Language: $lang\nContent-Type: " . CONTENT_TYPES->{$ext} . "\n";
58 gzip \$page => "$path_noext.$lang.gz.$ext", Minimal => 1;
59 say $typemap "URI: $basename.$lang.gz.$ext\nContent-Language: $lang\nContent-Encoding: gzip\nContent-Type: " . CONTENT_TYPES->{$ext} . "\n";
60 }
61 }
62 close $typemap;
63 }
64
65 1;
66 __END__
67 # Below is stub documentation for your module. You'd better edit it!
68
69 =head1 NAME
70
71 Gruntmaster::Page - Perl extension for blah blah blah
72
73 =head1 SYNOPSIS
74
75 use Gruntmaster::Page;
76 blah blah blah
77
78 =head1 DESCRIPTION
79
80 Stub documentation for Gruntmaster::Page, created by h2xs. It looks like the
81 author of the extension was negligent enough to leave the stub
82 unedited.
83
84 Blah blah blah.
85
86 =head2 EXPORT
87
88 None by default.
89
90
91
92 =head1 SEE ALSO
93
94 Mention other useful documentation such as the documentation of
95 related modules or operating system documentation (such as man pages
96 in UNIX), or any relevant external documentation such as RFCs or
97 standards.
98
99 If you have a mailing list set up for your module, mention it here.
100
101 If you have a web site set up for your module, mention it here.
102
103 =head1 AUTHOR
104
105 Marius Gavrilescu, E<lt>marius@E<gt>
106
107 =head1 COPYRIGHT AND LICENSE
108
109 Copyright (C) 2013 by Marius Gavrilescu
110
111 This library is free software; you can redistribute it and/or modify
112 it under the same terms as Perl itself, either Perl version 5.18.1 or,
113 at your option, any later version of Perl 5 you may have available.
114
115
116 =cut
This page took 0.040881 seconds and 4 git commands to generate.