fix latin1 encoding problems
[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 enc($l) if $l =~ m/([\x{80}-\x{ff}])/;
103         return $l;
104 }
105
106 sub abstract2doc {
107         my $id = shift;
108
109         my $abstract = $abstracts->{$id} || die "no $id in abstracts ",dump($abstracts);
110
111         my $t = $doc->appendParagraph(text => $abstract->{Title}->[0], style => 'Heading 1');
112         $doc->setPageBreak( $t, position => 'before', style => 'Heading 1');
113
114         foreach my $a ( @{ $abstract->{PrimaryAuthor} } ) {
115                 $doc->appendParagraph(text => join(' ',
116 #                       map { s/([\x{80}-\x{ff}])/enc($1)/eg && warn "# PrimaryAuthor Id: $id ",Encode::is_utf8($_), dump($_),"\n"; $_ }
117                         map { l1($a->{$_}->[0]) } qw( FirstName FamilyName )
118                 ), style => 'Heading 2');
119                 $doc->appendParagraph(text => join(' ',
120                         map { l1($a->{$_}->[0]) } qw( Affiliation )
121                 ), style => 'Heading 3');
122                 push @{ $stat->{_Affiliation}->{ $a->{Affiliation}->[0] } }, $id;
123         }
124
125 #       $doc->appendParagraph(text => $abstract->{Content}->[0], style => 'Abstract');
126         foreach my $p ( split(/[\n\r]+/, $abstract->{Content}->[0] ) ) {
127                 $doc->appendParagraph(text => $p, style => 'Abstract');
128         }
129
130         $doc->appendParagraph(text => "\nTheme: " . $abstract->{Theme}->[0], style => 'Theme');
131
132         $doc->appendParagraph(text => "\nContributionType: " . $abstract->{ContributionType}->[0], style => 'Quotations');
133
134         $doc->appendParagraph(text => "\nID: " . $abstract->{Id}->[0], style => 'Abstract');
135
136         warn "# added $id to document\n";
137 }
138
139 #foreach my $id ( keys %$abstracts ) {
140 #       abstract2doc($id);
141 #}
142
143 foreach my $symposium ( keys %{ $stat->{'_symposium'} } ) {
144
145         my $t = $doc->appendParagraph(text => $symposium, style => 'Title');
146         $doc->setPageBreak( $t, position => 'before', style => 'Title');
147         $doc->appendParagraph(text => 'Symposium', style => 'Subtitle');
148
149         my @symposium_ids = sort @{ $stat->{'_symposium'}->{$symposium} };
150         warn "## [$symposium] symposium_ids ", dump( @symposium_ids );
151
152         my $proposal_id = shift @symposium_ids;
153         if ( $proposal_id < 0 ) {
154                 $proposal_id = abs( $proposal_id );
155         } else {
156                 warn "ERROR: symposium [$symposium] doesn't have proposal!";
157                 
158         }
159 #       @symposium_ids = sort { $stat->{_order_by}->{$a} cmp $stat->{_order_by}->{$b} } @symposium_ids;
160         @symposium_ids = sort @symposium_ids;
161         unshift @symposium_ids, $proposal_id;
162
163         $stat->{_order_symposium}->{$symposium} = [ @symposium_ids ];
164
165         foreach my $id ( @symposium_ids ) {
166                 abstract2doc($id);
167         }
168
169 }
170
171 foreach my $type ( 'Oral presentation', 'Poster presentation' ) {
172
173         my $t = $doc->appendParagraph(text => $type, style => 'Title');
174         $doc->setPageBreak( $t, position => 'before', style => 'Title');
175
176         my @order = sort { lc $stat->{_order_by}->{$a} cmp lc $stat->{_order_by}->{$b} }
177                 @{ $stat->{'ContributionType'}->{$type} };
178
179         $stat->{ '_order_' . $type } = [ @order ];
180
181         foreach my $id ( @order ) {
182                 abstract2doc($id);
183         }
184 }
185
186
187 warn "# stat = ",dump($stat);
188
189 $doc->save;
190
191
192 open(my $fh, '>', 'affiliation.csv');
193 foreach my $a ( sort keys %{ $stat->{_Affiliation} } ) {
194         print $fh '"',$a,'","',dump( $stat->{_Affiliation}->{$a} ), '"', "\n";
195 }
196
197 __DATA__
198     "Symposium proposal"  => 12,
199         "Symposium abstract"  => 52,
200     "Oral presentation"   => 141,
201     "Poster presentation" => 71,
202