return undef for empty tags
[star2016] / star2016-contributions.pl
1 #!/usr/bin/perl
2 use autodie;
3 use warnings;
4 use strict;
5
6 use utf8;
7 use XML::Simple;
8 use Data::Dump qw(dump);
9
10 my $odt_file   = 'contributions.odt';
11
12 my $xs = XML::Simple->new();
13
14 use     OpenOffice::OODoc       2.101;
15 my $doc = odfDocument(file => $odt_file, create => 'text');
16
17 my $stat;
18 my $abstracts;
19
20 $doc->createStyle(
21         "Abstract",
22         family          => 'paragraph',
23         parent          => 'Standard',
24 #       category        => 'auto',
25         properties      => {
26                 -area                   => 'paragraph',
27                 'fo:text-align' => 'justify'
28         }
29 );
30
31
32 my $nr = 0;
33 #foreach my $file ( glob 'contributions/*.xml' ) {
34
35 my $xml;
36 open(my $fh, '<:encoding(utf-8)', 'contributions/all.xml');
37 {
38         local $/ = undef;
39         $xml = <$fh>;
40         close($fh);
41 }
42
43 warn "---xml---", dump($xml), "---/xml---\n";
44
45 foreach my $xml ( split(/\Q<?xml\E/, $xml) ) {
46
47         next unless $xml;
48
49         $xml = '<?xml' . $xml;
50
51         my $abstract;
52
53         $abstract = $xs->XMLin( $xml, ForceArray => qw(PrimaryAuthor) );
54
55         warn "# abstract = ", dump($abstract);
56
57         my $id = $abstract->{'Id'}->[0] || die "no Id in ",dump($abstract);
58         warn "# $id abstract = ", dump($abstract);
59
60         foreach my $k (qw( ContributionType )) {
61                 push @{ $stat->{$k}->{ $abstract->{$k}->[0] } }, $id;
62         }
63
64         if ( $abstract->{'ContributionType'}->[0] eq 'Symposium proposal' ) {
65                 my $t = $abstract->{Title}->[0];
66                 push @{ $stat->{_symposium}->{ $t } }, -$id;
67         }
68
69         if ( my $s = $abstract->{Symposium_title} ) {
70                 $s = $s->[0];
71                 push @{ $stat->{_symposium}->{$s} }, $id;
72         }
73
74         if ( exists $abstract->{PrimaryAuthor}->[0]->{FamilyName} ) {
75                 $stat->{_order_by}->{$id} = $abstract->{PrimaryAuthor}->[0]->{FamilyName}->[0];
76         }
77
78         $abstracts->{ $id } = $abstract;
79
80 }
81
82 #warn "# stat = ",dump($stat);
83
84 use Encode;
85 sub enc {
86         my $b = shift;
87         warn "# enc in  ",Encode::is_utf8($b), dump($b);
88
89 #       $b = Encode::decode('iso-8859-1',$b); # XXX doesn't work!
90
91 #       Encode::from_to($b, 'iso-8859-1','utf-8');
92 #       Encode::_utf8_on($b);
93
94         Encode::_utf8_off($b);
95
96         warn "# enc out ",Encode::is_utf8($b), dump($b);
97         return $b;
98 }
99 sub l1 {
100         my $l = shift;
101         #return 'FIXME ref ' . dump($l) if ref $l;
102         return undef if ref $l;
103         return enc($l) if $l =~ m/([\x{80}-\x{ff}])/;
104         return $l;
105 }
106
107 sub abstract2doc {
108         my $id = shift;
109
110         my $abstract = $abstracts->{$id} || die "no $id in abstracts ",dump($abstracts);
111
112         my $t = $doc->appendParagraph(text => $abstract->{Title}->[0], style => 'Heading 1');
113         $doc->setPageBreak( $t, position => 'before', style => 'Heading 1');
114
115         foreach my $a ( @{ $abstract->{PrimaryAuthor} } ) {
116                 $doc->appendParagraph(text => join(' ',
117 #                       map { s/([\x{80}-\x{ff}])/enc($1)/eg && warn "# PrimaryAuthor Id: $id ",Encode::is_utf8($_), dump($_),"\n"; $_ }
118                         map { l1($a->{$_}->[0]) } qw( FirstName FamilyName )
119                 ), style => 'Heading 2');
120                 $doc->appendParagraph(text => join(' ',
121                         map { l1($a->{$_}->[0]) } qw( Affiliation )
122                 ), style => 'Heading 3');
123                 push @{ $stat->{_Affiliation}->{ $a->{Affiliation}->[0] } }, $id;
124         }
125
126 #       $doc->appendParagraph(text => $abstract->{Content}->[0], style => 'Abstract');
127         foreach my $p ( split(/[\n\r]+/, $abstract->{Content}->[0] ) ) {
128                 $doc->appendParagraph(text => $p, style => 'Abstract');
129         }
130
131         $doc->appendParagraph(text => "\nTheme: " . $abstract->{Theme}->[0], style => 'Theme');
132
133         $doc->appendParagraph(text => "\nContributionType: " . $abstract->{ContributionType}->[0], style => 'Quotations');
134
135         $doc->appendParagraph(text => "\nID: " . $abstract->{Id}->[0], style => 'Abstract');
136
137         warn "# added $id to document\n";
138 }
139
140 #foreach my $id ( keys %$abstracts ) {
141 #       abstract2doc($id);
142 #}
143
144 foreach my $symposium ( keys %{ $stat->{'_symposium'} } ) {
145
146         my $t = $doc->appendParagraph(text => $symposium, style => 'Title');
147         $doc->setPageBreak( $t, position => 'before', style => 'Title');
148         $doc->appendParagraph(text => 'Symposium', style => 'Subtitle');
149
150         my @symposium_ids = sort @{ $stat->{'_symposium'}->{$symposium} };
151         warn "## [$symposium] symposium_ids ", dump( @symposium_ids );
152
153         my $proposal_id = shift @symposium_ids;
154         if ( $proposal_id < 0 ) {
155                 $proposal_id = abs( $proposal_id );
156         } else {
157                 warn "ERROR: symposium [$symposium] doesn't have proposal!";
158                 
159         }
160 #       @symposium_ids = sort { $stat->{_order_by}->{$a} cmp $stat->{_order_by}->{$b} } @symposium_ids;
161         @symposium_ids = sort @symposium_ids;
162         unshift @symposium_ids, $proposal_id;
163
164         $stat->{_order_symposium}->{$symposium} = [ @symposium_ids ];
165
166         foreach my $id ( @symposium_ids ) {
167                 abstract2doc($id);
168         }
169
170 }
171
172 foreach my $type ( 'Oral presentation', 'Poster presentation' ) {
173
174         my $t = $doc->appendParagraph(text => $type, style => 'Title');
175         $doc->setPageBreak( $t, position => 'before', style => 'Title');
176
177         my @order = sort { lc $stat->{_order_by}->{$a} cmp lc $stat->{_order_by}->{$b} }
178                 @{ $stat->{'ContributionType'}->{$type} };
179
180         $stat->{ '_order_' . $type } = [ @order ];
181
182         foreach my $id ( @order ) {
183                 abstract2doc($id);
184         }
185 }
186
187
188 warn "# stat = ",dump($stat);
189
190 $doc->save;
191
192
193 open(my $fh, '>', 'affiliation.csv');
194 foreach my $a ( sort keys %{ $stat->{_Affiliation} } ) {
195         print $fh '"',$a,'","',dump( $stat->{_Affiliation}->{$a} ), '"', "\n";
196 }
197
198 __DATA__
199     "Symposium proposal"  => 12,
200         "Symposium abstract"  => 52,
201     "Oral presentation"   => 141,
202     "Poster presentation" => 71,
203