aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/WebCoso/Step/ReST/ToXml.pm57
-rw-r--r--t/steps/rest-xml.t47
2 files changed, 100 insertions, 4 deletions
diff --git a/lib/WebCoso/Step/ReST/ToXml.pm b/lib/WebCoso/Step/ReST/ToXml.pm
index f65e71c..f5cf5a9 100644
--- a/lib/WebCoso/Step/ReST/ToXml.pm
+++ b/lib/WebCoso/Step/ReST/ToXml.pm
@@ -17,7 +17,7 @@ l'xml, lo passa al parser, e salva il dom in
suppone venga tutto da un solo file
-Fa tutto alla prima passata
+Fa tutto alla prima passata, e raccatta un po' di meta
=cut
@@ -41,31 +41,80 @@ sub process {
my ($rst_doc,$xml_dom);
$rst_doc=$resource->get_property_string($srckey);
if (defined $rst_doc) { # monolingua
+ my $dom=rst2xml($rst_doc,$src_path);
$resource->set_property(
$dstkey,
- rst2xml($rst_doc,$src_path)
+ $dom,
);
+ $self->_set_meta($resource,$dom);
}
else { # multilingua
my @langs=$resource->get_axis_values('language');
for my $cur_lang (@langs) {
$rst_doc=$resource->get_property_string({language=>$cur_lang},$srckey);
+ my $dom=rst2xml($rst_doc,$src_path,$cur_lang);
$resource->set_property(
{language=>$cur_lang},
$dstkey,
- rst2xml($rst_doc,$src_path,$cur_lang)
+ $dom,
);
+ $self->_set_meta($resource,$cur_lang,$dom);
}
}
return;
}
+{
+my %docinfo_fields=(
+ title => '/document/title',
+ subtitle => '/document/subtitle',
+ author => '/document/docinfo/author|/document/docinfo/authors/author',
+ version => '/document/docinfo/version',
+ status => '/document/docinfo/status',
+ date => '/document/docinfo/date',
+ creation_date => '/document/docinfo/field[field_name="CreationDate"]/field_body',
+);
+my $collections='/document/docinfo/field[field_name="Collection"]/field_body|/document/docinfo/field[field_name="Collections"]/field_body//list_item';
+sub _set_meta {
+ my ($self,$res,$lang,$dom)=@_;
+
+ if ($dom) { # 4 parametri
+ $lang={language=>$lang};
+ }
+ else { # 3 parametri: monolingua
+ $dom=$lang;
+ $lang={};
+ }
+
+ for my $meta (keys %docinfo_fields) {
+ my @nodes=$dom->findnodes($docinfo_fields{$meta});
+ next unless @nodes;
+ @nodes=map {$_->textContent()} @nodes;
+ if (@nodes==1) {
+ $res->set_property($lang,$meta,$nodes[0]);
+ }
+ else {
+ $res->set_property($lang,$meta,[@nodes]);
+ }
+ }
+
+ # per questo serve poter chiamare coll e res per nome TODO
+ #my @collections=$dom->findnodes($collections);
+ #return unless @collections;
+ #@collections=map {$_->textContent()} @collections;
+
+ #$res->add_coll($_) for @collections;
+
+ return;
+}
+}
+
sub rst2xml {
my ($rst_string,$source_path,$language)=@_;
$rst_string=Encode::encode('utf-8',$rst_string);
- my $xml_string=_rst2xml($rst_string,$source_path,$language||'it');
+ my $xml_string=_rst2xml($rst_string,$source_path,'en');
$xml_parser->base_uri($source_path);
return $xml_parser->parse_string($xml_string);
}
diff --git a/t/steps/rest-xml.t b/t/steps/rest-xml.t
index 2b351ef..1d9f956 100644
--- a/t/steps/rest-xml.t
+++ b/t/steps/rest-xml.t
@@ -107,3 +107,50 @@ END_REST
$step->process($resource,'meta');
$dom=$resource->get_property('xml');
isa_ok($dom,'XML::LibXML::Document','chiavi arbitrarie');
+
+# test dei metadati
+$step=WebCoso::Step::ReST::ToXml->new();
+$srckey='rstdoc';
+$resource=make_res(<<'END_REST');
+========
+ Titolo
+========
+
+sottotitolo
+===========
+
+:Authors: - dakkar
+ - doraemon
+:Version: 12
+:Status: draft
+:Date: 2006-02-07
+:CreationDate: 2006-01-30
+:Collections: - uno
+ - due
+ - tre
+
+testo
+
+END_REST
+$step->process($resource,'meta');
+my %meta_fields=(
+ author => ['dakkar','doraemon'],
+ title => 'Titolo',
+ subtitle => 'sottotitolo',
+ version => 12,
+ status => 'draft',
+ date => '2006-02-07',
+ creation_date => '2006-01-30',
+);
+while (my ($f,$v)=each %meta_fields) {
+ if (ref $v) {
+ is_deeply([sort @{$resource->get_property($f)}],
+ $v,
+ "meta: $f");
+ }
+ else {
+ is($resource->get_property($f),
+ $v,
+ "meta: $f");
+ }
+}