From 9da2f4958089ae517865b808be61bb12d43b61de Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Sat, 8 Feb 2014 20:06:13 +0200 Subject: [PATCH] Move templates to tmpl/ --- lib/Gruntmaster/Page/Base.pm | 127 +++++++----------------------- lib/Gruntmaster/Page/Ct.pm | 46 ----------- lib/Gruntmaster/Page/Ct/Entry.pm | 11 --- lib/Gruntmaster/Page/Log.pm | 21 ----- lib/Gruntmaster/Page/Log/Entry.pm | 16 ---- lib/Gruntmaster/Page/Pb.pm | 31 -------- lib/Gruntmaster/Page/Pb/Entry.pm | 32 -------- lib/Gruntmaster/Page/St.pm | 17 ---- lib/Gruntmaster/Page/Us.pm | 7 -- lib/Gruntmaster/Page/Us/Entry.pm | 9 --- tmpl/ct.en | 41 ++++++++++ tmpl/ct_entry.en | 6 ++ tmpl/footer.en | 5 ++ tmpl/header.en | 42 ++++++++++ tmpl/log.en | 16 ++++ tmpl/log_entry.en | 11 +++ tmpl/pb.en | 26 ++++++ tmpl/pb_entry.en | 27 +++++++ tmpl/st.en | 12 +++ tmpl/us.en | 2 + tmpl/us_entry.en | 4 + 21 files changed, 219 insertions(+), 290 deletions(-) create mode 100644 tmpl/ct.en create mode 100644 tmpl/ct_entry.en create mode 100644 tmpl/footer.en create mode 100644 tmpl/header.en create mode 100644 tmpl/log.en create mode 100644 tmpl/log_entry.en create mode 100644 tmpl/pb.en create mode 100644 tmpl/pb_entry.en create mode 100644 tmpl/st.en create mode 100644 tmpl/us.en create mode 100644 tmpl/us_entry.en diff --git a/lib/Gruntmaster/Page/Base.pm b/lib/Gruntmaster/Page/Base.pm index cf7d229..89f8478 100644 --- a/lib/Gruntmaster/Page/Base.pm +++ b/lib/Gruntmaster/Page/Base.pm @@ -9,12 +9,34 @@ use HTML::Template::Compiled; ################################################## +sub read_templates { + my $root = 'tmpl'; + my $name = shift; + + map { m/\.(.+)$/; $1 => scalar read_file $_ } ; +} + +my %header_templates = read_templates 'header'; +my %footer_templates = read_templates 'footer'; + +sub header{ + my ($language, $title) = @_; + $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger; +} + +sub footer{ + $footer_templates{$_[0]}; +} + +################################################## + use POSIX (); use Gruntmaster::Data (); use List::Util (); use LWP::UserAgent; my $ua = LWP::UserAgent->new; +my %templates; sub import { my $caller = caller; @@ -25,8 +47,6 @@ sub import { no strict 'refs'; *{"${caller}::strftime"} = \&POSIX::strftime; - *{"${caller}::NAME"} = sub () { $name }; - *{"${caller}::TITLE"} = sub () { $title }; *{"${caller}::debug"} = sub { local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1; $_[0]->{'psgix.logger'}->({qw/level debug message/ => $_[1]}) @@ -37,112 +57,19 @@ sub import { my $req = HTTP::Request->new(PURGE => "http://$ENV{PURGE_HOST}$_[0]"); $ua->request($req) }; -} - -################################################## -my %orig_header_templates = ( - en => <<'HTML', - -TITLE_GOES_HERE - - - - - - - - -
- -

TITLE_GOES_HERE

-
-HTML -); - -my %orig_footer_templates = ( - en => <<'HTML', - -
-Dilmom: Why don't you call your product the Gruntmaster 6000? -Dilbert: What kind of product do you see when you imagine a Gruntmaster 6000? -Dilmom: Well, it's a stripped-down version of the Gruntmaster 9000, of course. But it's software-upgradeable. -
-HTML -); - -sub patch_templates { - my $root = 'tmpl'; - return %{$_[0]} unless -d $root; - my ($templates, $name) = @_; - my %out = %$templates; - for (<$root/$name.*>) { - m/\.(.+)$/; - $out{$1} = read_file $_ + if ($name) { + $templates{$caller} = { read_templates $name }; + $templates{$caller}{$_} = header ($_, $title) . $templates{$caller}{$_} for keys $templates{$caller}; + $templates{$caller}{$_} .= footer $_ for keys $templates{$caller}; } - - %out -} - -my %header_templates = patch_templates \%orig_header_templates, 'header'; -my %footer_templates = patch_templates \%orig_footer_templates, 'footer'; - -sub header{ - my ($language, $title) = @_; - $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger; -} - -sub footer{ - $footer_templates{$_[0]}; -} - -sub cook_templates { - my ($templates, $name, $title) = @_; - - my %out = patch_templates $templates, $name; - $out{$_} = header ($_, $title) . $out{$_} for keys %out; - $out{$_} .= footer $_ for keys %out; - - %out } ################################################## -my %templates; - sub generate{ my ($self, $lang, @args) = @_; - $templates{$self} = { cook_templates $self->TEMPLATES, $self->NAME => $self->TITLE } unless exists $templates{$self}; - my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$self}{$lang}, default_escape => 'HTML',); $self->_generate($htc, $lang, @args); my $out = $htc->output; @@ -155,7 +82,7 @@ sub _generate {} sub max_age { 60 } sub variants { - [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $_[0]->TEMPLATES ] + [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $templates{$_[0]} ] } 1 diff --git a/lib/Gruntmaster/Page/Ct.pm b/lib/Gruntmaster/Page/Ct.pm index ede2562..b62b975 100644 --- a/lib/Gruntmaster/Page/Ct.pm +++ b/lib/Gruntmaster/Page/Ct.pm @@ -7,52 +7,6 @@ use Gruntmaster::Page::Base ct => 'Contests'; our @ISA = qw/Gruntmaster::Page::Base/; our $VERSION = '0.001'; -use constant TEMPLATES => { - en => <<'HTML', - -

Running contests

- - - -
NameStart dateEnd dateOwner -
- - - - -
-
- - -

Pending contests

- - - -
NameStart dateEnd dateOwner -
- - - - -
-
- - -

Finished contests

- - - -
NameStart dateEnd dateOwner -
- - - - -
-
-HTML -}; - sub _generate{ my ($self, $htc, $lang, $env) = @_; debug $env => "language is '$lang'"; diff --git a/lib/Gruntmaster/Page/Ct/Entry.pm b/lib/Gruntmaster/Page/Ct/Entry.pm index 83e2705..acc076e 100644 --- a/lib/Gruntmaster/Page/Ct/Entry.pm +++ b/lib/Gruntmaster/Page/Ct/Entry.pm @@ -7,17 +7,6 @@ use Gruntmaster::Page::Base ct_entry => ''; our @ISA = qw/Gruntmaster::Page::Base/; our $VERSION = '0.001'; -use constant TEMPLATES => { - en => <<'HTML', -Contest start time:
-Contest end time:

- -Problems
-Job log
-Standings
-HTML -}; - sub _generate{ my ($self, $htc, $lang, $env, $id) = @_; debug $env => "language is '$lang' and id is '$id'"; diff --git a/lib/Gruntmaster/Page/Log.pm b/lib/Gruntmaster/Page/Log.pm index 9192d95..68f45fd 100644 --- a/lib/Gruntmaster/Page/Log.pm +++ b/lib/Gruntmaster/Page/Log.pm @@ -9,27 +9,6 @@ our $VERSION = '0.001'; use constant PAGE_SIZE => 10; -use constant TEMPLATES => { - en => <<'HTML', - - - -
IDProblemDateSizeUserResult -
- - - data-private> - - -
- -

-HTML -}; - sub _generate{ my ($self, $htc, $lang, $env, $ct, $page) = @_; debug $env => "language is '$lang', contest is '$ct' and page is '$page'"; diff --git a/lib/Gruntmaster/Page/Log/Entry.pm b/lib/Gruntmaster/Page/Log/Entry.pm index 66dad19..57591b5 100644 --- a/lib/Gruntmaster/Page/Log/Entry.pm +++ b/lib/Gruntmaster/Page/Log/Entry.pm @@ -7,22 +7,6 @@ use Gruntmaster::Page::Base log_entry => 'Job '; our @ISA = qw/Gruntmaster::Page::Base/; our $VERSION = '0.001'; -use constant TEMPLATES => { - en => <<'HTML', -Compiler output: -
- -Results: - - - -
Test numberResultTime -
- -
-HTML -}; - sub _generate{ my ($self, $htc, $lang, $env, $ct, $id) = @_; debug $env => "language is '$lang', contest is '$ct' and id is '$id'"; diff --git a/lib/Gruntmaster/Page/Pb.pm b/lib/Gruntmaster/Page/Pb.pm index ffdde60..ba78869 100644 --- a/lib/Gruntmaster/Page/Pb.pm +++ b/lib/Gruntmaster/Page/Pb.pm @@ -7,37 +7,6 @@ use Gruntmaster::Page::Base pb => 'Problems'; our @ISA = qw/Gruntmaster::Page::Base/; our $VERSION = '0.001'; -use constant TEMPLATES => { - en => <<'HTML', - -

Beginner

-
- -
- -

Easy

-
- -
- -

Medium

-
- -
- -

Hard

-
- -
- - -
- -
-
-HTML -}; - sub _generate{ my ($self, $htc, $lang, $env, $ct) = @_; debug $env => "language is '$lang' and contest is '$ct'"; diff --git a/lib/Gruntmaster/Page/Pb/Entry.pm b/lib/Gruntmaster/Page/Pb/Entry.pm index 2672769..f178b50 100644 --- a/lib/Gruntmaster/Page/Pb/Entry.pm +++ b/lib/Gruntmaster/Page/Pb/Entry.pm @@ -7,38 +7,6 @@ use Gruntmaster::Page::Base pb_entry => ''; our @ISA = qw/Gruntmaster::Page::Base/; our $VERSION = '0.001'; -use constant TEMPLATES => { - en => <<'HTML', -
-
- -
- -
-
-
Author
-
Owner
-
- - -

Submit solution

-
- - - -
-
-
- - -
-
-
-HTML -}; - use constant FORMATS => [qw/C CPP MONO JAVA PASCAL PERL PYTHON/]; sub _generate{ diff --git a/lib/Gruntmaster/Page/St.pm b/lib/Gruntmaster/Page/St.pm index a812cfa..099c193 100644 --- a/lib/Gruntmaster/Page/St.pm +++ b/lib/Gruntmaster/Page/St.pm @@ -7,23 +7,6 @@ use Gruntmaster::Page::Base st => 'Standings'; our @ISA = qw/Gruntmaster::Page::Base/; our $VERSION = '0.001'; -use constant TEMPLATES => { - en => <<'HTML', - - - -
UsernameTotal -
UsernameScore - - -
- - - -
-HTML -}; - use constant LEVEL_VALUES => { beginner => 100, easy => 250, diff --git a/lib/Gruntmaster/Page/Us.pm b/lib/Gruntmaster/Page/Us.pm index b4dcb3f..915ee50 100644 --- a/lib/Gruntmaster/Page/Us.pm +++ b/lib/Gruntmaster/Page/Us.pm @@ -7,13 +7,6 @@ use Gruntmaster::Page::Base us => 'Users'; our @ISA = qw/Gruntmaster::Page::Base/; our $VERSION = '0.001'; -use constant TEMPLATES => { - en => <<'HTML', -
- -HTML -}; - sub _generate{ my ($self, $htc, $lang, $env) = @_; debug $env => "language is '$lang'"; diff --git a/lib/Gruntmaster/Page/Us/Entry.pm b/lib/Gruntmaster/Page/Us/Entry.pm index e375ab8..628f9fa 100644 --- a/lib/Gruntmaster/Page/Us/Entry.pm +++ b/lib/Gruntmaster/Page/Us/Entry.pm @@ -7,15 +7,6 @@ use Gruntmaster::Page::Base us_entry => ''; our @ISA = qw/Gruntmaster::Page::Base/; our $VERSION = '0.001'; -use constant TEMPLATES => { - en => <<'HTML', -

-Town:
-University:
-Level: -HTML -}; - sub _generate{ my ($self, $htc, $lang, $env, $us) = @_; debug $env => "language is '$lang', user is '$us'"; diff --git a/tmpl/ct.en b/tmpl/ct.en new file mode 100644 index 0000000..e18bc2a --- /dev/null +++ b/tmpl/ct.en @@ -0,0 +1,41 @@ + +

Running contests

+ + + +
NameStart dateEnd dateOwner +
+ + + + +
+
+ + +

Pending contests

+ + + +
NameStart dateEnd dateOwner +
+ + + + +
+
+ + +

Finished contests

+ + + +
NameStart dateEnd dateOwner +
+ + + + +
+
diff --git a/tmpl/ct_entry.en b/tmpl/ct_entry.en new file mode 100644 index 0000000..2919bb3 --- /dev/null +++ b/tmpl/ct_entry.en @@ -0,0 +1,6 @@ +Contest start time:
+Contest end time:

+ +Problems
+Job log
+Standings
diff --git a/tmpl/footer.en b/tmpl/footer.en new file mode 100644 index 0000000..c0f928d --- /dev/null +++ b/tmpl/footer.en @@ -0,0 +1,5 @@ +

+Dilmom: Why don't you call your product the Gruntmaster 6000? +Dilbert: What kind of product do you see when you imagine a Gruntmaster 6000? +Dilmom: Well, it's a stripped-down version of the Gruntmaster 9000, of course. But it's software-upgradeable. +
diff --git a/tmpl/header.en b/tmpl/header.en new file mode 100644 index 0000000..5ca7976 --- /dev/null +++ b/tmpl/header.en @@ -0,0 +1,42 @@ + +TITLE_GOES_HERE + + + + + + + + +
+ +

TITLE_GOES_HERE

+
diff --git a/tmpl/log.en b/tmpl/log.en new file mode 100644 index 0000000..2ef59af --- /dev/null +++ b/tmpl/log.en @@ -0,0 +1,16 @@ + + + +
IDProblemDateSizeUserResult +
+ + + data-private> + + +
+ + diff --git a/tmpl/log_entry.en b/tmpl/log_entry.en new file mode 100644 index 0000000..45cd405 --- /dev/null +++ b/tmpl/log_entry.en @@ -0,0 +1,11 @@ +Compiler output: +
+ +Results: + + + +
Test numberResultTime +
+ +
diff --git a/tmpl/pb.en b/tmpl/pb.en new file mode 100644 index 0000000..3a8194f --- /dev/null +++ b/tmpl/pb.en @@ -0,0 +1,26 @@ + +

Beginner

+
+ +
+ +

Easy

+
+ +
+ +

Medium

+
+ +
+ +

Hard

+
+ +
+ + +
+ +
+
diff --git a/tmpl/pb_entry.en b/tmpl/pb_entry.en new file mode 100644 index 0000000..4933cb1 --- /dev/null +++ b/tmpl/pb_entry.en @@ -0,0 +1,27 @@ +
+
+ +
+ +
+
+
Author
+
Owner
+
+ + +

Submit solution

+
+ + + +
+
+
+ + +
+
+
diff --git a/tmpl/st.en b/tmpl/st.en new file mode 100644 index 0000000..1e4c8ec --- /dev/null +++ b/tmpl/st.en @@ -0,0 +1,12 @@ + + + +
UsernameTotal +
UsernameScore + + +
+ + + +
diff --git a/tmpl/us.en b/tmpl/us.en new file mode 100644 index 0000000..174bc80 --- /dev/null +++ b/tmpl/us.en @@ -0,0 +1,2 @@ +
+ diff --git a/tmpl/us_entry.en b/tmpl/us_entry.en new file mode 100644 index 0000000..b6c988d --- /dev/null +++ b/tmpl/us_entry.en @@ -0,0 +1,4 @@ +

+Town:
+University:
+Level: -- 2.39.2