From 8d25800c67ad65defd98771bba2331ee64104f98 Mon Sep 17 00:00:00 2001 From: dakkar Date: Wed, 7 Jan 2009 12:51:13 +0000 Subject: feed fatti, e pure col test manca: testare feed intl, fare feed dei tag, mettere i tag nei feed git-svn-id: svn://luxion/repos/WebCoso/trunk@402 fcb26f47-9200-0410-b104-b98ab5b095f3 --- lib/WebCoso/Common.pm | 32 +++++++++++++++++++++++++++----- lib/WebCoso/TT.pm | 36 ++++++++++++++++++++++++++++++------ lib/WebCoso/XSLT.pm | 4 +++- 3 files changed, 60 insertions(+), 12 deletions(-) (limited to 'lib/WebCoso') diff --git a/lib/WebCoso/Common.pm b/lib/WebCoso/Common.pm index 5420063..eed6f3a 100644 --- a/lib/WebCoso/Common.pm +++ b/lib/WebCoso/Common.pm @@ -37,14 +37,20 @@ sub typedAs { } sub dstUriFor { - my ($name,$short)=(@_,1); - DEBUG("dstUriFor($name,$short)"); + my ($name,$path,$short)=@_; + $short=1 unless defined $short; + $path||=''; + DEBUG("dstUriFor($name,$path,$short)"); if ($short) { $name=~s{/[^/]+$}{/}; } else { $name=typedAs($name,'html'); } + if ($path) { + $name=file($name)->absolute(file($path)->parent)->relative($SRCPATH); + $name="$SRCPATH/$name"; + } $name=~s{^\Q$SRCPATH\E/}{$DSTBASEURL}; DEBUG("dstUriFor -> $name"); return $name; @@ -53,9 +59,7 @@ sub dstUriFor { sub srcXMLFor { my ($lang,$path,$name)=@_; - $name="$name"; # force it to a string, since it might be an XML Node - - my $doc_name=$name; + my $doc_name="$name"; # force it to a string, since it might be an XML Node $doc_name=~s{\.html$}{.du.xml}; $doc_name=~s{/$}{/document.$lang.du.xml}; if ($doc_name=~m{^\Q$DSTBASEURL\E}) { @@ -94,6 +98,24 @@ sub getTitleFor { return $title; } +sub getAtomIdFor { + my ($fc,$lang,$path,$name)=@_; + DEBUG("getAtomIdFor($lang,$path,$name)"); + my $doc_name=srcXMLFor($lang,$path,$name); + DEBUG("getAtomIdFor -> $doc_name"); + + my $doc=$fc->get($doc_name); + unless ($doc) { + LOGWARN("No document for <$doc_name>, returning ''"); + return ''; + } + my $atomId=$xpath->findnodes( + q{/document/docinfo/field[field_name='Id']/field_body}, + $doc); + $atomId=~s{(?:\A\s+)|(?:\s+\z)}{}smgx; + return $atomId; +} + { my $zero=DateTime->from_epoch(epoch=>0); sub getDatesFor { diff --git a/lib/WebCoso/TT.pm b/lib/WebCoso/TT.pm index b6962fd..15e2072 100644 --- a/lib/WebCoso/TT.pm +++ b/lib/WebCoso/TT.pm @@ -4,18 +4,43 @@ use warnings; use WebCoso::Common; use Path::Class; use Template; +use Template::Stash; use Log::Log4perl ':easy'; +$Template::Stash::HASH_OPS->{sortf}=sub { + my ($hash,$field)=@_; + + if ($field) { + return [ sort { lc $hash->{$a}{$field} cmp lc $hash->{$b}{$field} } (keys %$hash )]; + } + else { + return [ sort { lc $hash->{$a} cmp lc $hash->{$b} } (keys %$hash )]; + } +}; + +$Template::Stash::HASH_OPS->{nsortf}=sub { + my ($hash,$field)=@_; + + if ($field) { + return [ sort { $hash->{$a}{$field} <=> $hash->{$b}{$field} } (keys %$hash )]; + } + else { + return [ sort { $hash->{$a} <=> $hash->{$b} } (keys %$hash )]; + } +}; + sub new { my ($class,%opts)=@_; - $opts{stash}||={ + my $self={%opts}; + bless $self,$class; + + $self->{stash}||={ dstUriFor => \&WebCoso::Common::dstUriFor, isLang => \&WebCoso::Common::isLang, + atomIdFor => sub{WebCoso::Common::getAtomIdFor($self->{fc},@_)}, }; - my $self={%opts}; - $self->{template_provider}=Template::Provider->new({ INCLUDE_PATH=> $self->{TMPLPATH}, ABSOLUTE=>1, @@ -57,14 +82,13 @@ sub new { pop @{$self->{template_provider}->include_path}; }; - bless $self,$class; + return $self; } - sub expandTT { my ($self)=@_; return $self->{expander} - } +} 1; diff --git a/lib/WebCoso/XSLT.pm b/lib/WebCoso/XSLT.pm index cc64cfb..09c5b3f 100644 --- a/lib/WebCoso/XSLT.pm +++ b/lib/WebCoso/XSLT.pm @@ -20,6 +20,8 @@ sub new { $self->{xslt_proc}->register_function($NS,'title-for', sub{$self->getTitleXML(WebCoso::Common::getTitleFor($self->{fc},@_))}); + $self->{xslt_proc}->register_function($NS,'dst-uri-for', + sub{WebCoso::Common::dstUriFor(@_)}); $self->{xslt_proc}->register_function($NS,'tagged',sub{$self->getTagsXML}); $self->{xslt_proc}->register_function($NS,'dates-for', sub{$self->getDatesXML(WebCoso::Common::getDatesFor($self->{fc},@_))}); @@ -79,7 +81,7 @@ sub new { language => $matches->[1], filename => $deps->[-1], )); - $self->{fc}->put($target,$xslt->output_string($out)); + $self->{fc}->put($target,$out); }; bless $self,$class; -- cgit v1.2.3