diff options
author | dakkar <dakkar@luxion> | 2008-03-01 09:37:45 +0000 |
---|---|---|
committer | dakkar <dakkar@luxion> | 2008-03-01 09:37:45 +0000 |
commit | d28bac7032a0d95fffbba01ad7eaa6a85fd7be16 (patch) | |
tree | 7187deed6d9143a8fc0168eaed3edc57ebe60917 | |
parent | refactoring: cleaner (diff) | |
download | WebCoso-d28bac7032a0d95fffbba01ad7eaa6a85fd7be16.tar.gz WebCoso-d28bac7032a0d95fffbba01ad7eaa6a85fd7be16.tar.bz2 WebCoso-d28bac7032a0d95fffbba01ad7eaa6a85fd7be16.zip |
aggiunto supporto date (data creazione deve essere scritta nel documento)
git-svn-id: svn://luxion/repos/WebCoso/trunk@330 fcb26f47-9200-0410-b104-b98ab5b095f3
23 files changed, 149 insertions, 17 deletions
diff --git a/lib/File/Cache/Parsed.pm b/lib/File/Cache/Parsed.pm index bc4036d..c67b16b 100644 --- a/lib/File/Cache/Parsed.pm +++ b/lib/File/Cache/Parsed.pm @@ -93,6 +93,15 @@ sub get { } } +sub stat { + my ($self,$filename)=@_; + + if ($self->{follow}) { + $filename=abs_path($filename); + } + return file($filename)->stat; +} + sub put { my ($self,$filename,$contents)=@_; diff --git a/lib/WebCoso/Common.pm b/lib/WebCoso/Common.pm index 6dbeeae..0a0effe 100644 --- a/lib/WebCoso/Common.pm +++ b/lib/WebCoso/Common.pm @@ -4,6 +4,8 @@ use warnings; use File::Next; use Path::Class; use XML::LibXML::XPathContext; +use DateTime; +use DateTime::Format::DateParse; our $SRCPATH='src'; our $DSTPATH='dst'; @@ -78,6 +80,57 @@ sub getTitleFor { return $title; } +{ + my $zero=DateTime->from_epoch(epoch=>0); +sub getDatesFor { + my ($fc,$lang,$path,$name)=@_; + + my $doc_name=$name; + $doc_name=~s{\.html$}{.du.xml}; + $doc_name=~s{/$}{/document.$lang.du.xml}; + if ($doc_name=~m{^\Q$DSTBASEURL\E}) { + $doc_name=~s{^\Q$DSTBASEURL\E}{$SRCPATH/}; + } + else { + $doc_name=file($doc_name)->absolute(file($path)->parent)->relative($SRCPATH); # absolutize it + $doc_name="$SRCPATH/$doc_name"; + } + warn "getDatesFor($lang,$path,$name)->$doc_name\n"; + + my $doc=$fc->get($doc_name); + unless ($doc) { + warn "No document for <$doc_name>, returning <0>\n"; + return {creation=>$zero,last_change=>$zero}; + } + my ($creation_date)=map {$_->textContent} + $xpath->findnodes( + q{/document/docinfo/field[field_name='CreationDate']/field_body}, + $doc); + $creation_date=DateTime::Format::DateParse->parse_datetime($creation_date)||$zero; + my $last_change=DateTime->from_epoch(epoch=>$fc->stat($doc_name)->mtime); + return {creation=>$creation_date, + last_change=>$last_change}; +} +} + +sub getDates { + my ($fc,@docs)=@_; + + my %dates; + for my $doc_name (@docs) { + my $doc=$fc->get($doc_name); + my ($creation_date)=map {$_->textContent} + $xpath->findnodes( + q{/document/docinfo/field[field_name='CreationDate']/field_body}, + $doc); + $creation_date=DateTime::Format::DateParse->parse_datetime($creation_date); + my $last_change=DateTime->from_epoch($doc->stat->mtime); + $dates{$doc_name}={creation=>$creation_date, + last_change=>$last_change}; + } + return \%dates; +} + sub getTags { my ($fc,@docs)=@_; diff --git a/lib/WebCoso/XSLT.pm b/lib/WebCoso/XSLT.pm index 18d2416..1f9208a 100644 --- a/lib/WebCoso/XSLT.pm +++ b/lib/WebCoso/XSLT.pm @@ -5,6 +5,7 @@ use WebCoso::Common; use Path::Class; use XML::LibXML; use XML::LibXSLT; +use DateTime::Format::Strptime; my $NS='http://webcoso.thenautilus.net/'; @@ -19,6 +20,8 @@ sub new { $self->{xslt_proc}->register_function($NS,'title-for', sub{WebCoso::Common::getTitleFor($self->{fc},@_)}); $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},@_))}); $self->{fc}->add_parser(qr{\.xml$} => sub { $self->{xml_parser}->parse_string($_[1],$_[0]) }); @@ -92,4 +95,23 @@ sub getTagsXML { return $doc; } +{ +my %name_map=(creation=>'wc:creation-date', + last_change=>'wc:last-change'); +my $format=DateTime::Format::Strptime->new(pattern=>'%F %T',time_zone=>'UTC'); +sub getDatesXML { + my ($self,$dates)=@_; + my $doc=XML::LibXML::Document->new(); + return $doc unless defined $dates; + + my $de=$doc->createElementNS($NS,'wc:dates'); + $doc->setDocumentElement($de); + while (my ($k,$v)=each %$dates) { + my $d=$doc->createElementNS($NS,$name_map{$k}); + $d->appendTextNode($format->format_datetime($v)); + $de->appendChild($d); + } + return $doc; +} +} 1; @@ -114,3 +114,9 @@ is($fc->get($wr_file->stringify), } is($fc->get('nosuchfile'),undef,'return undef on non-existant files'); + +{ + my $module_file=$base->file('lib','File','Cache','Parsed.pm'); + is($fc->stat($module_file)->size, + $module_file->stat->size,'ok stat'); +} diff --git a/t/test-site-output/bar/baz/document.en.html b/t/test-site-output/bar/baz/document.en.html index 8399519..530f7bd 100644 --- a/t/test-site-output/bar/baz/document.en.html +++ b/t/test-site-output/bar/baz/document.en.html @@ -18,4 +18,6 @@ </dt><dd><ul><li><a href="/foo/">foo page </a> (<a href="/foo/it">it</a> | en) - </li></ul></dd></dl></div></body></html> + </li></ul></dd></dl></div><div> + Creato: 1970-01-01 00:00:00 + Ultima modifica: 2008-03-01 09:23:11</div></body></html> diff --git a/t/test-site-output/bar/baz/document.it.html b/t/test-site-output/bar/baz/document.it.html index 2fb1fd5..e258865 100644 --- a/t/test-site-output/bar/baz/document.it.html +++ b/t/test-site-output/bar/baz/document.it.html @@ -16,4 +16,6 @@ </li></ul></dd><dt>Categoria 'two' </dt><dd><ul><li><a href="/foo/">Pagina <em>foo</em></a> (it | <a href="/foo/en">en</a>) - </li></ul></dd></dl></div></body></html> + </li></ul></dd></dl></div><div> + Creato: 1970-01-01 00:00:00 + Ultima modifica: 2008-03-01 09:23:11</div></body></html> diff --git a/t/test-site-output/bar/document.en.html b/t/test-site-output/bar/document.en.html index 51ff094..ea706dd 100644 --- a/t/test-site-output/bar/document.en.html +++ b/t/test-site-output/bar/document.en.html @@ -21,4 +21,6 @@ </dt><dd><ul><li><a href="/foo/">foo page </a> (<a href="/foo/it">it</a> | en) - </li></ul></dd></dl></div></body></html> + </li></ul></dd></dl></div><div> + Creato: 1970-01-01 00:00:00 + Ultima modifica: 2008-03-01 09:23:11</div></body></html> diff --git a/t/test-site-output/bar/document.it.html b/t/test-site-output/bar/document.it.html index 9adfb7f..5ad0574 100644 --- a/t/test-site-output/bar/document.it.html +++ b/t/test-site-output/bar/document.it.html @@ -19,4 +19,6 @@ </li></ul></dd><dt>Categoria 'two' </dt><dd><ul><li><a href="/foo/">Pagina <em>foo</em></a> (it | <a href="/foo/en">en</a>) - </li></ul></dd></dl></div></body></html> + </li></ul></dd></dl></div><div> + Creato: 1970-01-01 00:00:00 + Ultima modifica: 2008-03-01 09:23:11</div></body></html> diff --git a/t/test-site-output/document.en.html b/t/test-site-output/document.en.html index bd4b6c3..1692418 100644 --- a/t/test-site-output/document.en.html +++ b/t/test-site-output/document.en.html @@ -18,4 +18,6 @@ </dt><dd><ul><li><a href="/foo/">foo page </a> (<a href="/foo/it">it</a> | en) - </li></ul></dd></dl></div></body></html> + </li></ul></dd></dl></div><div> + Creato: 2008-03-01 10:04:00 + Ultima modifica: 2008-03-01 09:23:11</div></body></html> diff --git a/t/test-site-output/document.it.html b/t/test-site-output/document.it.html index 44dd21c..386f70a 100644 --- a/t/test-site-output/document.it.html +++ b/t/test-site-output/document.it.html @@ -15,4 +15,6 @@ </li></ul></dd><dt>Categoria 'two' </dt><dd><ul><li><a href="/foo/">Pagina <em>foo</em></a> (it | <a href="/foo/en">en</a>) - </li></ul></dd></dl></div></body></html> + </li></ul></dd></dl></div><div> + Creato: 1970-01-01 00:00:00 + Ultima modifica: 2008-03-01 09:23:11</div></body></html> diff --git a/t/test-site-output/foo/document.en.html b/t/test-site-output/foo/document.en.html index 2fdd465..7d11fc7 100644 --- a/t/test-site-output/foo/document.en.html +++ b/t/test-site-output/foo/document.en.html @@ -18,4 +18,6 @@ </dt><dd><ul><li><a href="/foo/">foo page </a> (<a href="/foo/it">it</a> | en) - </li></ul></dd></dl></div></body></html> + </li></ul></dd></dl></div><div> + Creato: 1970-01-01 00:00:00 + Ultima modifica: 2008-03-01 09:23:11</div></body></html> diff --git a/t/test-site-output/foo/document.it.html b/t/test-site-output/foo/document.it.html index 3f59dd8..12f2016 100644 --- a/t/test-site-output/foo/document.it.html +++ b/t/test-site-output/foo/document.it.html @@ -14,4 +14,6 @@ </li></ul></dd><dt>Categoria 'two' </dt><dd><ul><li><a href="/foo/">Pagina <em>foo</em></a> (it | <a href="/foo/en">en</a>) - </li></ul></dd></dl></div></body></html> + </li></ul></dd></dl></div><div> + Creato: 1970-01-01 00:00:00 + Ultima modifica: 2008-03-01 09:23:11</div></body></html> diff --git a/t/test-site-output/tags/one/document.en.html b/t/test-site-output/tags/one/document.en.html index 582622b..12fcba4 100644 --- a/t/test-site-output/tags/one/document.en.html +++ b/t/test-site-output/tags/one/document.en.html @@ -19,4 +19,6 @@ </dt><dd><ul><li><a href="/foo/">foo page </a> (<a href="/foo/it">it</a> | en) - </li></ul></dd></dl></div></body></html> + </li></ul></dd></dl></div><div> + Creato: 1970-01-01 00:00:00 + Ultima modifica: 2008-03-01 09:23:11</div></body></html> diff --git a/t/test-site-output/tags/one/document.it.html b/t/test-site-output/tags/one/document.it.html index aafbee7..85655e8 100644 --- a/t/test-site-output/tags/one/document.it.html +++ b/t/test-site-output/tags/one/document.it.html @@ -16,4 +16,6 @@ </li></ul></dd><dt>Categoria 'two' </dt><dd><ul><li><a href="/foo/">Pagina <em>foo</em></a> (it | <a href="/foo/en">en</a>) - </li></ul></dd></dl></div></body></html> + </li></ul></dd></dl></div><div> + Creato: 1970-01-01 00:00:00 + Ultima modifica: 2008-03-01 09:23:11</div></body></html> diff --git a/t/test-site-output/tags/three/bis/document.en.html b/t/test-site-output/tags/three/bis/document.en.html index f352c7d..cc93ccb 100644 --- a/t/test-site-output/tags/three/bis/document.en.html +++ b/t/test-site-output/tags/three/bis/document.en.html @@ -18,4 +18,6 @@ </dt><dd><ul><li><a href="/foo/">foo page </a> (<a href="/foo/it">it</a> | en) - </li></ul></dd></dl></div></body></html> + </li></ul></dd></dl></div><div> + Creato: 1970-01-01 00:00:00 + Ultima modifica: 2008-03-01 09:23:11</div></body></html> diff --git a/t/test-site-output/tags/three/bis/document.it.html b/t/test-site-output/tags/three/bis/document.it.html index bbd498f..0e6d159 100644 --- a/t/test-site-output/tags/three/bis/document.it.html +++ b/t/test-site-output/tags/three/bis/document.it.html @@ -16,4 +16,6 @@ </li></ul></dd><dt>Categoria 'two' </dt><dd><ul><li><a href="/foo/">Pagina <em>foo</em></a> (it | <a href="/foo/en">en</a>) - </li></ul></dd></dl></div></body></html> + </li></ul></dd></dl></div><div> + Creato: 1970-01-01 00:00:00 + Ultima modifica: 2008-03-01 09:23:11</div></body></html> diff --git a/t/test-site-output/tags/three/document.en.html b/t/test-site-output/tags/three/document.en.html index 2bc5bbe..97f482b 100644 --- a/t/test-site-output/tags/three/document.en.html +++ b/t/test-site-output/tags/three/document.en.html @@ -17,4 +17,6 @@ </dt><dd><ul><li><a href="/foo/">foo page </a> (<a href="/foo/it">it</a> | en) - </li></ul></dd></dl></div></body></html> + </li></ul></dd></dl></div><div> + Creato: 1970-01-01 00:00:00 + Ultima modifica: 2008-03-01 09:23:11</div></body></html> diff --git a/t/test-site-output/tags/three/document.it.html b/t/test-site-output/tags/three/document.it.html index a422f3b..4c225a0 100644 --- a/t/test-site-output/tags/three/document.it.html +++ b/t/test-site-output/tags/three/document.it.html @@ -15,4 +15,6 @@ </li></ul></dd><dt>Categoria 'two' </dt><dd><ul><li><a href="/foo/">Pagina <em>foo</em></a> (it | <a href="/foo/en">en</a>) - </li></ul></dd></dl></div></body></html> + </li></ul></dd></dl></div><div> + Creato: 1970-01-01 00:00:00 + Ultima modifica: 2008-03-01 09:23:11</div></body></html> diff --git a/t/test-site-output/tags/two/document.en.html b/t/test-site-output/tags/two/document.en.html index 4bca5cb..6439ab1 100644 --- a/t/test-site-output/tags/two/document.en.html +++ b/t/test-site-output/tags/two/document.en.html @@ -18,4 +18,6 @@ </dt><dd><ul><li><a href="/foo/">foo page </a> (<a href="/foo/it">it</a> | en) - </li></ul></dd></dl></div></body></html> + </li></ul></dd></dl></div><div> + Creato: 1970-01-01 00:00:00 + Ultima modifica: 2008-03-01 09:23:11</div></body></html> diff --git a/t/test-site-output/tags/two/document.it.html b/t/test-site-output/tags/two/document.it.html index eae0997..9b57700 100644 --- a/t/test-site-output/tags/two/document.it.html +++ b/t/test-site-output/tags/two/document.it.html @@ -15,4 +15,6 @@ </li></ul></dd><dt>Categoria 'two' </dt><dd><ul><li><a href="/foo/">Pagina <em>foo</em></a> (it | <a href="/foo/en">en</a>) - </li></ul></dd></dl></div></body></html> + </li></ul></dd></dl></div><div> + Creato: 1970-01-01 00:00:00 + Ultima modifica: 2008-03-01 09:23:11</div></body></html> diff --git a/t/test-site/src/common/du2html.xsl b/t/test-site/src/common/du2html.xsl index 291fbbf..88c70b0 100644 --- a/t/test-site/src/common/du2html.xsl +++ b/t/test-site/src/common/du2html.xsl @@ -52,10 +52,18 @@ <div> <x:apply-templates select="wc:tagged()"/> </div> + <div> + <x:apply-templates select="wc:dates-for($language,$filename,'./')"/> + </div> </body> </html> </x:template> + <x:template match="/wc:dates"> + Creato: <x:value-of select="wc:creation-date"/> + Ultima modifica: <x:value-of select="wc:last-change"/> + </x:template> + <x:template match="/wc:tags"> <dl> <x:apply-templates/> diff --git a/t/test-site/src/document.en.rest.txt b/t/test-site/src/document.en.rest.txt index b7449d0..d5862b4 100644 --- a/t/test-site/src/document.en.rest.txt +++ b/t/test-site/src/document.en.rest.txt @@ -1,6 +1,8 @@ =========== Main page =========== +:CreationDate: 2008-03-01 10:04:00 + * `<foo/>`_ * `bar! <bar/>`_ diff --git a/t/whole-01.t b/t/whole-01.t index 054d2ee..639ecc0 100644 --- a/t/whole-01.t +++ b/t/whole-01.t @@ -14,7 +14,7 @@ system($^X,qw(blib/script/webcoso.pl -s t/test-site/src/ -d t/test-site/output/ system($^X,qw(blib/script/webcoso.pl -s t/test-site/src/ -d t/test-site/output/ -I t/test-site/common/)) and die "Problems running webcoso.pl: $?\n"; -is(system(qw(diff -r -x .svn t/test-site/output/ t/test-site-output/)),0,'output as expected'); +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)) and die "Problems running webcoso.pl (clean): $?\n"; |