From eafc7f54b9d964aac41b0715e18caf5ed58ef89a Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Sun, 26 Jan 2014 15:40:40 +0200 Subject: [PATCH] Add article support --- Makefile.PL | 2 +- gruntmaster-genallpages | 3 --- gruntmaster-genarticle | 6 ++++++ gruntmaster-paged | 2 +- lib/Gruntmaster/Data.pm | 5 +++++ lib/Gruntmaster/Page.pm | 33 ++++++++++++++++++++++----------- lib/Gruntmaster/Page/Base.pm | 2 +- 7 files changed, 36 insertions(+), 17 deletions(-) create mode 100755 gruntmaster-genarticle diff --git a/Makefile.PL b/Makefile.PL index cc79d4f..c8bd84b 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -4,7 +4,7 @@ use ExtUtils::MakeMaker; WriteMakefile( NAME => 'Gruntmaster::Page', VERSION_FROM => 'lib/Gruntmaster/Page.pm', - EXE_FILES => [ qw/gruntmaster-genpage gruntmaster-genallpages gruntmaster-paged gruntmaster-contest gruntmaster-problem gruntmaster-job/ ], + EXE_FILES => [ qw/gruntmaster-genarticle gruntmaster-genpage gruntmaster-genallpages gruntmaster-paged gruntmaster-contest gruntmaster-problem gruntmaster-job/ ], ABSTRACT_FROM => 'lib/Gruntmaster/Page.pm', AUTHOR => 'Marius Gavrilescu ', MIN_PERL_VERSION => '5.14.0', diff --git a/gruntmaster-genallpages b/gruntmaster-genallpages index 8d18eaa..7588f02 100755 --- a/gruntmaster-genallpages +++ b/gruntmaster-genallpages @@ -11,9 +11,6 @@ sub generate{ PUBLISH genpage => $what; } -generate "index.html"; -generate "learn.html"; - generate "ct/index.html"; generate "ct/$_/index.thml" for contests; generate "ct/$_/log/st.html" for contests; diff --git a/gruntmaster-genarticle b/gruntmaster-genarticle new file mode 100755 index 0000000..f5b4b5f --- /dev/null +++ b/gruntmaster-genarticle @@ -0,0 +1,6 @@ +#!/usr/bin/perl -w +use v5.14; + +use Gruntmaster::Data qw/PUBLISH/; + +PUBLISH genarticle => $_ for @ARGV; diff --git a/gruntmaster-paged b/gruntmaster-paged index 5e7bd57..2dd5001 100755 --- a/gruntmaster-paged +++ b/gruntmaster-paged @@ -6,7 +6,7 @@ use Gruntmaster::Page; SUBSCRIBE 'genpage', \&Gruntmaster::Page::generate; SUBSCRIBE 'gensrc', \&Gruntmaster::Page::gensrc; -SUBSCRIBE 'genarticle', \&Gruntmaster::Page::genarticle; +SUBSCRIBE 'genarticle', \&Gruntmaster::Page::generate; WAIT_FOR_MESSAGES 86400 while 1; 1; diff --git a/lib/Gruntmaster/Data.pm b/lib/Gruntmaster/Data.pm index aac9ad1..bf5d2e1 100644 --- a/lib/Gruntmaster/Data.pm +++ b/lib/Gruntmaster/Data.pm @@ -52,6 +52,11 @@ sub defhash{ dynsub "set_${name}_$key", sub ($$) { HSET cp . "$name.$_[0]", $key, $_[1] }; } + dynsub "edit_$name", sub { + my ($key, %values) = @_; + HMSET cp . "$name.$key", %values; + }; + dynsub "insert_$name", sub { my ($key, %values) = @_; SADD cp . $name, $key or return; diff --git a/lib/Gruntmaster/Page.pm b/lib/Gruntmaster/Page.pm index eedbec5..451ed8e 100644 --- a/lib/Gruntmaster/Page.pm +++ b/lib/Gruntmaster/Page.pm @@ -7,7 +7,7 @@ use warnings; use Fcntl qw/:flock/; use File::Basename qw/fileparse/; use File::Path qw/make_path/; -use File::Slurp qw/write_file/; +use File::Slurp qw/read_file write_file/; use IO::Compress::Gzip qw/gzip/; use IO::File; use Gruntmaster::Data; @@ -47,7 +47,7 @@ sub declaregen{ } sub generate{ - my ($path) = @_; + my ($path, $topic) = @_; my ($path_noext, $ext) = $path =~ m/^(.*)\.(.*)$/; my ($basename, $directories) = fileparse $path_noext; make_path $directories; @@ -56,17 +56,32 @@ sub generate{ flock my $lockfh = IO::File->new("<$path_noext.var"), LOCK_EX; open my $typemap, ">$path_noext.var.new"; say $typemap "URI: $basename\n"; - for my $gen (@generators) { - my ($regex, $generator) = @$gen; - next unless $path_noext =~ $regex; + + my $fill_typemap = sub { for my $lang (@{LANGUAGES()}) { - my $page = $generator->generate($path, $lang); + my $page = $_[0]->($lang); write_file "$path_noext.$lang.$ext.new", $page; say $typemap "URI: $basename.$lang.$ext\nContent-Language: $lang\nContent-Type: " . CONTENT_TYPES->{$ext} . "\n"; gzip \$page => "$path_noext.$lang.gz.$ext.new", Minimal => 1; say $typemap "URI: $basename.$lang.gz.$ext\nContent-Language: $lang\nContent-Encoding: gzip\nContent-Type: " . CONTENT_TYPES->{$ext} . "\n"; } - last + }; + + if ($topic eq 'genpage') { + for my $gen (@generators) { + my ($regex, $generator) = @$gen; + next unless $path_noext =~ $regex; + $fill_typemap->(sub { $generator->generate($path, $_[0]) }); + last + } + } else { + my $get_article = sub { + my $article = read_file "$ENV{GRUNTMASTER_ARTICLE_ROOT}/$basename.$_[0]"; + my $title = read_file "$ENV{GRUNTMASTER_ARTICLE_ROOT}/$basename.$_[0].title"; + Gruntmaster::Page::Base::header($_[0], $title) . $article . Gruntmaster::Page::Base::footer($_[0]) + }; + + $fill_typemap->($get_article); } for my $lang (@{LANGUAGES()}) { @@ -84,10 +99,6 @@ sub gensrc{ write_file "log/src/$job.$ext", job_inmeta($job)->{files}{prog}{content}; } -sub genarticle{ - -} - 1; __END__ # Below is stub documentation for your module. You'd better edit it! diff --git a/lib/Gruntmaster/Page/Base.pm b/lib/Gruntmaster/Page/Base.pm index b0b4c57..5b93f20 100644 --- a/lib/Gruntmaster/Page/Base.pm +++ b/lib/Gruntmaster/Page/Base.pm @@ -59,7 +59,7 @@ HTML ); sub patch_templates { - my $root = $ENV{GRUNTMASTER_TEMPLATE_ROOT} or return; + my $root = $ENV{GRUNTMASTER_TEMPLATE_ROOT} or return %{$_[0]}; my ($templates, $name) = @_; my %out = %$templates; for (<$root/$name.*>) { -- 2.39.2