aboutsummaryrefslogtreecommitdiff
path: root/lib/WebCoso/Step/ReST/ToXml.pm
blob: f5cf5a983030e0e55684d39d9b4d32ebacc0d9a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package WebCoso::Step::ReST::ToXml; 
use strict;
use warnings;
use base 'WebCoso::Step';
use Class::Std;
use Inline 'Python';
use XML::LibXML;
use Encode;
 
{
 
=head2 Che fa
 
prende i {language=>'quelchece'}->rstdoc, li passa a docutils, prende
l'xml, lo passa al parser, e salva il dom in
{language=>'quelchece'}->xmldom
 
suppone venga tutto da un solo file
 
Fa tutto alla prima passata, e raccatta un po' di meta
 
=cut
 
my %srckey_of :ATTR(:init_arg<from> :get<srckey> :default<rstdoc>);
my %dstkey_of :ATTR(:init_arg<to> :get<dstkey> :default<xmldom>);
 
my $xml_parser=XML::LibXML->new();
$xml_parser->load_ext_dtd(0);
$xml_parser->clean_namespaces(1);
 
sub process {
    my ($self,$resource,$stage)=@_;
 
    return unless $stage eq 'meta';
 
    my ($src_path)=$resource->get_axis_values('filename');
 
    my $srckey=$self->get_srckey();
    my $dstkey=$self->get_dstkey();
 
    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,
            $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,
                $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,'en');
    $xml_parser->base_uri($source_path);
    return $xml_parser->parse_string($xml_string);
}
 
}
1;
__DATA__
__Python__
 
import locale
import docutils.core
 
def _rst2xml(source,source_path,language):
    return docutils.core.publish_string(
        source,source_path=source_path,
        writer_name='xml',
        settings_overrides={
            'input_encoding':'utf-8',
            'output_encoding':'utf-8',
            'language_code':language,
        },
    )