package WebCoso::Maker; use strict; use warnings; use Slay::Maker; use File::Next; use Path::Class; use File::Cache::Parsed; use Cwd 'abs_path'; use YAML; use WebCoso::Common; use WebCoso::TT; use WebCoso::XSLT; use WebCoso::ReST; use Log::Log4perl ':easy'; my $fc=File::Cache::Parsed->new(follow=>1); $fc->add_parser(qr{\.ya?ml$} => sub { local $YAML::LoadBlessed=1; Load($_[1]) }); $fc->add_writer(qr{\.ya?ml$} => sub { DumpFile($_[0],$_[1]) }); my $template=WebCoso::TT->new(TMPLPATH=>\@WebCoso::Common::TMPLPATH,fc=>$fc); my $xslt=WebCoso::XSLT->new(fc=>$fc); my $rest=WebCoso::ReST->new(fc=>$fc); sub parseRST { my ($maker,$target,$deps,$matches)=@_; DEBUG("parseRST($maker,$target,(@$deps),(@$matches))"); $fc->put($target,$fc->get($deps->[-1])); } sub getTags { my ($maker,$target,$deps,$matches)=@_; DEBUG("getTags($maker,$target,(@$deps),(@$matches))"); $fc->put($target,WebCoso::Common::getTags($fc,@$deps)); } sub getChanges { my ($maker,$target,$deps,$matches)=@_; DEBUG("getChanges($maker,$target,(@$deps),(@$matches))"); $fc->put($target,WebCoso::Common::getDates($fc,@$deps)); } sub ifExists { my ($src)=@_; return sub { my ($maker,$target,$matches)=@_; my $dep=Slay::MakerRule::var_expand_dep($src,$target,$matches); return if -e $target and ! -e $dep; return $dep; } } my %maker_opts=( options => { auto_create_dirs => 1, #debug => 1, }, ); my @passes=( {maker=>Slay::Maker->new({ rules => [ # tags : must come first, otherwise tha "tags" directory will # be taken as a normal document directory ["$WebCoso::Common::SRCPATH/tags/(**)/document.(*).rest.txt", ':', "$WebCoso::Common::SRCPATH/_webcoso/tags.yml", ifExists("$WebCoso::Common::SRCPATH/tags/\$1/document.\$2.rest.tt"), '=', $template->expandTT()], ["$WebCoso::Common::SRCPATH/tags/(**)/document.(*).du.xml", ':', "$WebCoso::Common::SRCPATH/_webcoso/tags.yml", ifExists("$WebCoso::Common::SRCPATH/tags/\$1/document.\$2.rest.txt"), '=', \&parseRST], ["$WebCoso::Common::SRCPATH/tags/()document.(*).rest.txt", ':', "$WebCoso::Common::SRCPATH/_webcoso/tags.yml", ifExists("$WebCoso::Common::SRCPATH/tags/document.\$2.rest.tt"), '=', $template->expandTT()], ["$WebCoso::Common::SRCPATH/tags/()document.(*).du.xml", ':', "$WebCoso::Common::SRCPATH/_webcoso/tags.yml", ifExists("$WebCoso::Common::SRCPATH/tags/document.\$2.rest.txt"), '=', \&parseRST], # normal documents, in subdirs ["$WebCoso::Common::SRCPATH/(**)/document.(*).rest.txt", ':', ifExists("$WebCoso::Common::SRCPATH/\$1/document.\$2.rest.tt"), '=', $template->expandTT()], ["$WebCoso::Common::SRCPATH/(**)/document.(*).du.xml", ':', ifExists("$WebCoso::Common::SRCPATH/\$1/document.\$2.rest.txt"), '=', \&parseRST], # normal documents, in top dir ["$WebCoso::Common::SRCPATH/()document.(*).rest.txt", ':', ifExists("$WebCoso::Common::SRCPATH/document.\$2.rest.tt"), '=', $template->expandTT()], ["$WebCoso::Common::SRCPATH/()document.(*).du.xml", ':', ifExists("$WebCoso::Common::SRCPATH/document.\$2.rest.txt"), '=', \&parseRST], # feeds ["$WebCoso::Common::SRCPATH/(**)/feed.(*).xml", ':', "$WebCoso::Common::SRCPATH/_webcoso/tags.yml", "$WebCoso::Common::SRCPATH/_webcoso/changes.yml", "$WebCoso::Common::SRCPATH/\$1/feed.\$2.tt", '=', $template->expandTT()], ["$WebCoso::Common::SRCPATH/()feed.(*).xml", ':', "$WebCoso::Common::SRCPATH/_webcoso/tags.yml", "$WebCoso::Common::SRCPATH/_webcoso/changes.yml", "$WebCoso::Common::SRCPATH/feed.\$2.tt", '=', $template->expandTT()], # tags from normal documents (tag documents can't be tagged!) ["$WebCoso::Common::SRCPATH/_webcoso/tags.yml", ':', WebCoso::Common::fromTo($WebCoso::Common::SRCPATH, { %WebCoso::Common::docfiles, transform=>sub{WebCoso::Common::typedAs($_[0],'du.xml')} }), '=', \&getTags], # changes ["$WebCoso::Common::SRCPATH/_webcoso/changes.yml", ':', WebCoso::Common::keepEarliest( WebCoso::Common::fromTo( $WebCoso::Common::SRCPATH,{%WebCoso::Common::docfiles} ), WebCoso::Common::fromTo( "$WebCoso::Common::SRCPATH/tags/",{%WebCoso::Common::docfiles} ) ), '=', \&getChanges], ], %maker_opts, }), targets=>[ WebCoso::Common::fromTo("$WebCoso::Common::SRCPATH/", { %WebCoso::Common::docfiles, transform=>sub{WebCoso::Common::typedAs($_[0],'du.xml')}, }), WebCoso::Common::fromTo("$WebCoso::Common::SRCPATH/tags/", { %WebCoso::Common::docfiles, transform=>sub{WebCoso::Common::typedAs($_[0],'du.xml')}, }), WebCoso::Common::fromTo("$WebCoso::Common::SRCPATH/", { %WebCoso::Common::feedfiles, transform=>sub{WebCoso::Common::typedAs($_[0],'xml')}, }), "$WebCoso::Common::SRCPATH/_webcoso/tags.yml", "$WebCoso::Common::SRCPATH/_webcoso/changes.yml", ]}, {maker=>Slay::Maker->new({ rules => [ # tags : must come first, otherwise tha "tags" directory will # be taken as a normal document directory ["$WebCoso::Common::DSTPATH/tags/(**)/document.(*).html", ':', "$WebCoso::Common::SRCPATH/_webcoso/tags.yml", "$WebCoso::Common::SRCPATH/_webcoso/changes.yml", "$WebCoso::Common::SRCPATH/tags/\$1/document.\$2.du.xml", '=', $xslt->du2html()], # normal documents, in subdirs ["$WebCoso::Common::DSTPATH/(**)/document.(*).html", ':', "$WebCoso::Common::SRCPATH/_webcoso/tags.yml", "$WebCoso::Common::SRCPATH/_webcoso/changes.yml", "$WebCoso::Common::SRCPATH/\$1/document.\$2.du.xml", '=', $xslt->du2html()], # normal documents, in top dir ["$WebCoso::Common::DSTPATH/()document.(*).html", ':', "$WebCoso::Common::SRCPATH/_webcoso/tags.yml", "$WebCoso::Common::SRCPATH/_webcoso/changes.yml", "$WebCoso::Common::SRCPATH/document.\$2.du.xml", '=', $xslt->du2html()], # feeds ["$WebCoso::Common::DSTPATH/(**)/feed.(*).xml", ':', "$WebCoso::Common::SRCPATH/\$1/feed.\$2.xml", '=', $xslt->fillFeed()], ["$WebCoso::Common::DSTPATH/()feed.(*).xml", ':', "$WebCoso::Common::SRCPATH/feed.\$2.xml", '=', $xslt->fillFeed()], ], %maker_opts, }), targets=>[WebCoso::Common::fromTo("$WebCoso::Common::SRCPATH/", { %WebCoso::Common::docfiles, transform=>sub{ (my $file=WebCoso::Common::typedAs($_[0],'html')) =~s{^\Q$WebCoso::Common::SRCPATH\E/}{$WebCoso::Common::DSTPATH/}; return $file; }, }), WebCoso::Common::fromTo("$WebCoso::Common::SRCPATH/tags/", { %WebCoso::Common::docfiles, transform=>sub{ (my $file=WebCoso::Common::typedAs($_[0],'html')) =~s{^\Q$WebCoso::Common::SRCPATH\E/tags/}{$WebCoso::Common::DSTPATH/tags/}; return $file; }, }), WebCoso::Common::fromTo("$WebCoso::Common::SRCPATH/", { %WebCoso::Common::feedfiles, transform=>sub{ (my $file=WebCoso::Common::typedAs($_[0],'xml')) =~s{^\Q$WebCoso::Common::SRCPATH\E/}{$WebCoso::Common::DSTPATH/}; return $file; }, }), ]}, ); sub make { $_->{maker}->make(@{$_->{targets}}) for @passes; } 1;