diff options
author | dakkar <dakkar@luxion> | 2009-01-07 12:51:13 +0000 |
---|---|---|
committer | dakkar <dakkar@luxion> | 2009-01-07 12:51:13 +0000 |
commit | 8d25800c67ad65defd98771bba2331ee64104f98 (patch) | |
tree | 622f61a0c5609c61904d9da3a6ab4b8db6688a24 | |
parent | a parte i namespace sbagliati, direi che i feed possono andare bene (diff) | |
download | WebCoso-8d25800c67ad65defd98771bba2331ee64104f98.tar.gz WebCoso-8d25800c67ad65defd98771bba2331ee64104f98.tar.bz2 WebCoso-8d25800c67ad65defd98771bba2331ee64104f98.zip |
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
-rw-r--r-- | lib/WebCoso/Common.pm | 32 | ||||
-rw-r--r-- | lib/WebCoso/TT.pm | 36 | ||||
-rw-r--r-- | lib/WebCoso/XSLT.pm | 4 | ||||
-rw-r--r-- | t/test-site-output/feed.it.xml | 12 | ||||
-rw-r--r-- | t/test-site/src/common/base-feed.tt | 18 | ||||
-rw-r--r-- | t/test-site/src/common/du2html.xsl | 16 | ||||
-rw-r--r-- | t/test-site/src/common/fillFeed.xsl | 46 | ||||
-rw-r--r-- | t/test-site/src/feed.it.tt | 15 | ||||
-rw-r--r-- | t/whole-01.t | 6 |
9 files changed, 141 insertions, 44 deletions
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; diff --git a/t/test-site-output/feed.it.xml b/t/test-site-output/feed.it.xml new file mode 100644 index 0000000..df11489 --- /dev/null +++ b/t/test-site-output/feed.it.xml @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="utf-8"?> +<!DOCTYPE a:feed PUBLIC "" ""> +<a:feed xmlns:a="http://www.w3.org/2005/Atom" xmlns="http://www.w3.org/1999/xhtml" xml:lang="it" xml:base="http://www.thenautilus.net/"><a:author><a:name>Gianni Ceccarelli</a:name><a:email>dakkar@thenautilus.net</a:email></a:author><a:id>http://www.thenautilus.net/</a:id><a:link rel="alternate" type="text/html" href="http://www.thenautilus.net/"/><a:title type="text">thenautilus.net</a:title><a:updated>2008-03-01T09:36:00+0000</a:updated><a:entry><a:id/><a:updated>2008-02-29T21:09:09+0000</a:updated><a:link rel="alternate" type="text/html">/bar/baz/</a:link><a:title type="xhtml"><div xml:base="/bar/baz/">bar-baz italiano +</div></a:title><a:content type="xhtml"><div xml:base="/bar/baz/"><p>sticaz +</p></div></a:content></a:entry><a:entry><a:id/><a:updated>2008-02-29T21:09:09+0000</a:updated><a:link rel="alternate" type="text/html">/foo/</a:link><a:title type="xhtml"><div xml:base="/foo/">Pagina <em>foo</em></div></a:title><a:content type="xhtml"><div xml:base="/foo/"><p>foo +</p></div></a:content></a:entry><a:entry><a:id/><a:updated>2008-03-01T09:06:41+0000</a:updated><a:link rel="alternate" type="text/html">/bar/</a:link><a:title type="xhtml"><div xml:base="/bar/">bar (generato) +</div></a:title><a:content type="xhtml"><div xml:base="/bar/"><ul><li>line 1 +</li><li>line 2 +</li><li>line 3 +</li></ul><p>link: <a href="baz/">bar-baz italiano +</a></p></div></a:content></a:entry><a:entry><a:id/><a:updated>2008-03-01T09:36:00+0000</a:updated><a:link rel="alternate" type="text/html">/</a:link><a:title type="xhtml"><div xml:base="/">Pagina principale +</div></a:title><a:content type="xhtml"><div xml:base="/"><ul><li><a href="foo/">Pagina <em>foo</em></a></li><li><a href="bar/">bar!</a></li><li><a href="bar/baz/">baz</a></li></ul></div></a:content></a:entry></a:feed> diff --git a/t/test-site/src/common/base-feed.tt b/t/test-site/src/common/base-feed.tt new file mode 100644 index 0000000..e2669e3 --- /dev/null +++ b/t/test-site/src/common/base-feed.tt @@ -0,0 +1,18 @@ +[% USE df = DateTime::Format('DateTime::Format::Strptime', + { pattern => '%FT%T%z', + locale => 'en_US', + time_zone => 'GMT' }) -%] +[% USE Dumper -%] +[% docs=changed.nsortf('last_change') -%] +<?xml version="1.0" encoding="utf-8"?> +<a:feed xmlns:a="http://www.w3.org/2005/Atom"> +<a:updated>[% l=docs.last;df.format(changed.$l.last_change) %]</a:updated> +[% FOR doc IN docs; NEXT UNLESS isLang(language,doc) -%] +<a:entry> + [% IF changed.$doc.creation %]<a:published>[% df.format(changed.$doc.creation) %]</a:published>[% END %] + [% IF changed.$doc.last_change %]<a:updated>[% df.format(changed.$doc.last_change) %]</a:updated>[% END %] + <a:id>[% atomIdFor(language,doc,path) %]</a:id> + <a:PH>[% dstUriFor(doc,path) %]</a:PH> +</a:entry> +[% END -%] +</a:feed> diff --git a/t/test-site/src/common/du2html.xsl b/t/test-site/src/common/du2html.xsl index 88c70b0..8b73a82 100644 --- a/t/test-site/src/common/du2html.xsl +++ b/t/test-site/src/common/du2html.xsl @@ -15,6 +15,15 @@ doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN" indent="yes"/> + <!-- + i casini con quel ancestor::document sono dovuti a: + + 1) il fatto che $filename non รจ il nome del documento che stiamo + toccando, quando espandiamo i documenti dentro al feed + + 2) un bug di LibXML o LibXSLT, per cui /document/@source non va + --> + <x:template match="reference"> <x:element name="a"> <x:attribute name="href"> @@ -23,14 +32,14 @@ <x:value-of select="concat('#', @refid)" /> </x:when> <x:when test="@refuri"> - <x:value-of select="@refuri" /> + <x:value-of select="@refuri"/> </x:when> </x:choose> </x:attribute> <x:choose> <x:when test="@refuri and string(.)=string(@refuri)"> - <x:apply-templates select="wc:title-for($language,$filename,@refuri)"/> + <x:apply-templates select="wc:title-for($language,ancestor::document/@source,@refuri)"/> </x:when> <x:otherwise> <x:apply-templates /> @@ -39,6 +48,9 @@ </x:element> </x:template> + <!-- qui ce ne freghiamo del @source, tanto viene usata solo per i + documenti stand-alone --> + <x:template match="/document"> <html xml:lang="{$language}" lang="{$language}"> <head> diff --git a/t/test-site/src/common/fillFeed.xsl b/t/test-site/src/common/fillFeed.xsl index e6077eb..04a6de3 100644 --- a/t/test-site/src/common/fillFeed.xsl +++ b/t/test-site/src/common/fillFeed.xsl @@ -1,7 +1,8 @@ <?xml version="1.0" encoding="utf-8"?> <x:stylesheet xmlns:x="http://www.w3.org/1999/XSL/Transform" xmlns:wc="http://webcoso.thenautilus.net/" - xmlns:f="http://webcoso.thenautilus.net/feeds" + xmlns:a="http://www.w3.org/2005/Atom" + xmlns="http://www.w3.org/1999/xhtml" exclude-result-prefixes="wc" version="1.0"> @@ -13,23 +14,42 @@ <x:output method="xml" encoding="utf-8" doctype-public=""/> - <x:template match="f:*"> - <x:copy> - <x:apply-templates select="@*"/> - <x:apply-templates select="*|text()"/> - </x:copy> + <x:template match="a:feed"> + <a:feed xml:lang="{$language}" xml:base="http://www.thenautilus.net/"> + <a:author> + <a:name>Gianni Ceccarelli</a:name> + <a:email>dakkar@thenautilus.net</a:email> + </a:author> + <a:id>http://www.thenautilus.net/</a:id> + <a:link rel="alternate" type="text/html" href="http://www.thenautilus.net{wc:dst-uri-for($filename)}"/> + <a:title type="text">thenautilus.net</a:title> + <x:copy-of select="a:updated"/> + <x:apply-templates select="a:entry"/> + </a:feed> </x:template> - <x:template match="@f:*"> - <x:copy/> + <x:template match="a:entry"> + <a:entry> + <x:copy-of select="a:category"/> + <x:copy-of select="a:id"/> + <x:copy-of select="a:published"/> + <x:copy-of select="a:updated"/> + <x:apply-templates select="a:PH"/> + </a:entry> </x:template> - <x:template match="f:PH"> - <f:url><x:value-of select="."/></f:url> - <f:title><x:apply-templates select="wc:title-for($language,$filename,.)/node()"/></f:title> - <f:body> + <x:template match="a:PH"> + <a:link rel="alternate" type="text/html"><x:value-of select="."/></a:link> + <a:title type="xhtml"> + <div xml:base="{.}"> + <x:apply-templates select="wc:title-for($language,$filename,.)/node()"/> + </div> + </a:title> + <a:content type="xhtml"> + <div xml:base="{.}"> <x:apply-templates select="wc:document($language,$filename,.)/document/node()"/> - </f:body> + </div> + </a:content> </x:template> </x:stylesheet>
\ No newline at end of file diff --git a/t/test-site/src/feed.it.tt b/t/test-site/src/feed.it.tt index 9a7a4cd..f7b7730 100644 --- a/t/test-site/src/feed.it.tt +++ b/t/test-site/src/feed.it.tt @@ -1,14 +1 @@ -[% USE df = DateTime::Format('DateTime::Format::Strptime', - { pattern => '%F %T', - locale => 'en_US', - time_zone => 'GMT' }) -%] -<?xml version="1.0" encoding="utf-8"?> -<f:feed xmlns:f="http://webcoso.thenautilus.net/feeds"> -[% FOR c IN changed; NEXT UNLESS isLang(language,c.key) -%] -<f:item> - [% IF c.value.creation %]<f:created>[% df.format(c.value.creation) %]</f:created>[% END %] - [% IF c.value.last_change %]<f:last>[% df.format(c.value.last_change) %]</f:last>[% END %] - <f:PH>[% dstUriFor(c.key) %]</f:PH> -</f:item> -[% END -%] -</f:feed> +[% INCLUDE 'base-feed.tt' %] diff --git a/t/whole-01.t b/t/whole-01.t index 639ecc0..f270c5e 100644 --- a/t/whole-01.t +++ b/t/whole-01.t @@ -8,13 +8,13 @@ chdir file(__FILE__)->parent->parent->stringify; $ENV{PERL5LIB}=join ':',@INC; -system($^X,qw(blib/script/webcoso.pl -s t/test-site/src/ -d t/test-site/output/ -I t/test-site/common/ --clean)) +system($^X,qw(blib/script/webcoso.pl -s t/test-site/src/ -d t/test-site/output/ -I t/test-site/src/common/ --clean)) and die "Problems running webcoso.pl (clean): $?\n"; -system($^X,qw(blib/script/webcoso.pl -s t/test-site/src/ -d t/test-site/output/ -I t/test-site/common/)) +system($^X,qw(blib/script/webcoso.pl -s t/test-site/src/ -d t/test-site/output/ -I t/test-site/src/common/)) and die "Problems running webcoso.pl: $?\n"; is(system(qw(diff -r -x .svn -I),'\(Ultima modifica\|Latest change\)',qw(t/test-site/output/ t/test-site-output/)),0,'output as expected'); -system($^X,qw(blib/script/webcoso.pl -s t/test-site/src/ -d t/test-site/output/ -I t/test-site/common/ --clean)) +system($^X,qw(blib/script/webcoso.pl -s t/test-site/src/ -d t/test-site/output/ -I t/test-site/src/common/ --clean)) and die "Problems running webcoso.pl (clean): $?\n"; |