generate program html
[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 my @authors;
108
109 sub abstract2doc {
110         my $id = shift;
111
112         my $abstract = $abstracts->{$id} || die "no $id in abstracts ",dump($abstracts);
113
114         my $t = $doc->appendParagraph(text => $abstract->{Title}->[0], style => 'Heading 1');
115         $doc->setPageBreak( $t, position => 'before', style => 'Heading 1');
116
117         foreach my $a ( @{ $abstract->{PrimaryAuthor} } ) {
118                 my $v;
119                 $v->{$_} = l1($a->{$_}->[0]) foreach qw( FirstName FamilyName Affiliation Email );
120                 $doc->appendParagraph(text => $v->{FirstName} . ' ' . $v->{FamilyName}, style => 'Heading 2');
121                 $doc->appendParagraph(text => $v->{Affiliation}, style => 'Heading 3');
122                 push @{ $stat->{_Affiliation}->{ $v->{Affiliation} } }, $id;
123
124                 push @authors, $v;
125
126         }
127
128 #       $doc->appendParagraph(text => $abstract->{Content}->[0], style => 'Abstract');
129         foreach my $p ( split(/[\n\r]+/, $abstract->{Content}->[0] ) ) {
130                 $doc->appendParagraph(text => $p, style => 'Abstract');
131         }
132
133 #       $doc->appendParagraph(text => "\nTheme: " . $abstract->{Theme}->[0], style => 'Theme');
134
135 #       $doc->appendParagraph(text => "\nContributionType: " . $abstract->{ContributionType}->[0], style => 'Quotations');
136
137         $doc->appendParagraph(text => "\nID: " . $abstract->{Id}->[0], style => 'Abstract');
138
139         warn "# added $id to document\n";
140 }
141
142 #foreach my $id ( keys %$abstracts ) {
143 #       abstract2doc($id);
144 #}
145
146 foreach my $symposium ( keys %{ $stat->{'_symposium'} } ) {
147
148         my $t = $doc->appendParagraph(text => $symposium, style => 'Title');
149         $doc->setPageBreak( $t, position => 'before', style => 'Title');
150         $doc->appendParagraph(text => 'Symposium', style => 'Subtitle');
151
152         my @symposium_ids = sort @{ $stat->{'_symposium'}->{$symposium} };
153         warn "## [$symposium] symposium_ids ", dump( @symposium_ids );
154
155         my $proposal_id = shift @symposium_ids;
156         if ( $proposal_id < 0 ) {
157                 $proposal_id = abs( $proposal_id );
158         } else {
159                 warn "ERROR: symposium [$symposium] doesn't have proposal!";
160                 
161         }
162 #       @symposium_ids = sort { $stat->{_order_by}->{$a} cmp $stat->{_order_by}->{$b} } @symposium_ids;
163         @symposium_ids = sort @symposium_ids;
164         unshift @symposium_ids, $proposal_id;
165
166         $stat->{_order_symposium}->{$symposium} = [ @symposium_ids ];
167
168         foreach my $id ( @symposium_ids ) {
169                 abstract2doc($id);
170         }
171
172 }
173
174 foreach my $type ( 'Oral presentation', 'Poster presentation' ) {
175
176         my $t = $doc->appendParagraph(text => $type, style => 'Title');
177         $doc->setPageBreak( $t, position => 'before', style => 'Title');
178
179         my @order = sort { lc $stat->{_order_by}->{$a} cmp lc $stat->{_order_by}->{$b} }
180                 @{ $stat->{'ContributionType'}->{$type} };
181
182         $stat->{ '_order_' . $type } = [ @order ];
183
184         foreach my $id ( @order ) {
185                 abstract2doc($id);
186         }
187 }
188
189
190 warn "# stat = ",dump($stat);
191
192 $doc->save;
193
194
195 open(my $fh, '>', 'affiliation.csv');
196 foreach my $a ( sort keys %{ $stat->{_Affiliation} } ) {
197         print $fh '"',$a,'","',dump( $stat->{_Affiliation}->{$a} ), '"', "\n";
198 }
199
200
201 my $last_html = '';
202 open(my $authors_fh, '>', 'authors.html');
203 foreach my $a ( sort { $a->{FamilyName} cmp $b->{FamilyName} } @authors ) {
204         my $html = qq{
205 <li>
206  <b>$a->{FamilyName}, $a->{FirstName}</b><br>
207  $a->{Affiliation}<br>
208  <em>$a->{Email}</em>
209 </li>};
210         print $authors_fh $html eq $last_html ? '<!-- dup -->' : $html;
211         $last_html = $html;
212 }
213
214 close $authors_fh;
215
216 __DATA__
217     "Symposium proposal"  => 12,
218         "Symposium abstract"  => 52,
219     "Oral presentation"   => 141,
220     "Poster presentation" => 71,
221