Debian Codegraph @

texmf-dist/doc/latex/confproc/example/generateswitch.pl

raw
1 #!/usr/bin/perl -w
2
3 # generateswitch.pl
4 # created as dafxproctoc.pl by Marc Zadel, 2006-04-28
5 # modified for confproc.cls by Vincent Verfaille, 2007-08-08 (v0.4) & 2009-10-30 (v0.7)
6 # Execute as
7 # ./generateswitch.pl < intputfile.txt >
8
9 use strict;
10 use Text::ParseWords;
11 open(SWI, ">expapersswitch.tex"); #open for write, overwrite
12 open(SESSIONS, ">exsessions.tex"); #open for write, overwrite
13
14 # ----- Configuration
15 # field separator for the input file
16 my $fieldseparator=',';
17
18 # mac line endings: "\r" / Unix line endings: :\n"
19 $/ = "\n"; # line endings for the input file
20 $\ = "\n"; # line endings for the output file
21
22 # ----- Subroutines
23 # -- split one line of input into a hash with named fields
24 sub parseinputline {
25 my ($inputline) = @_;
26
27 # escape single quotes on the input line: they interfere with quotewords()'s
28 # quote handling (ie, they start to quote stuff)
29 $inputline =~ s/'/\\'/g;
30
31 # parse the input line
32 my @wordlist = &quotewords($fieldseparator, 0, $inputline);
33
34 # replace accented characters with latex escaped equivalents. Use it after
35 # quotewords() so the '\' don't get interpreted by quotewords() as escapes
36 foreach my $word ( @wordlist ) {
37 if ( $word ) { $word = &latexifyaccentedcharacters($word); }
38 }
39
40 # extract the fields into local variables. Author names stored as a list
41 my ($type, $number, $pcdecision, $nbpages, $title, $filename,
42 $generatedfrom, $cite) = @wordlist;
43
44 # remove the first 8 elements (just parsed out), leaving only author names.
45 # reminder: list of 8 scalars, though some may be "" if less than 4 authors
46 splice( @wordlist, 0, 8 );
47
48 # store the author names as a list of lists. We end up with a list that looks
49 # like ((Udo,Zoelzer),(Daniel,Arfib))
50 my @authors = ();
51 while ( $wordlist[0] ) {
52 push( @authors, [splice( @wordlist, 0, 2 )] );
53 # "splice( @wordlist, 0, 2 )": cuts the first 2 scalars off of @wordlist
54 # and returns them; calling [splice(@wordlist,0,2)] returns a *reference*
55 # to a list containing the first two scalars. (see perldoc perldsc.)
56 }
57
58 # create a hash reference containing the named fields and return it
59 my $fields = {
60 type => $type,
61 number => $number,
62 pcdecision => $pcdecision,
63 nbpages => $nbpages,
64 title => $title,
65 generatedfrom => $generatedfrom,
66 filename => $filename,
67 cite => $cite,
68 authors => \@authors,
69 };
70 return $fields;
71 }
72
73 # -- takes a string in Mac OS Roman encoding and encode the accented
74 # characters with latex escapes (only for a subset of available characters).
75 sub latexifyaccentedcharacters {
76 # for mapping between unicode and mac os western encoding, see:
77 # http://www.unicode.org/Public/MAPPINGS/VENDORS/APPLE/ROMAN.TXT
78 my ($inputstring) = @_;
79 $inputstring =~ s/\x8a/\\"a/g; # \"a: unicode 0xe4, mac os western 0x8a
80 $inputstring =~ s/\x87/\\'a/g; # \'a: unicode 0xe9, mac os western 0x87
81 $inputstring =~ s/\x88/\\`a/g; # \`a: unicode 0xe8, mac os western 0x88
82 $inputstring =~ s/\x8e/\\'e/g; # \'e: unicode 0xe9, mac os western 0x8e
83 $inputstring =~ s/\x8f/\\`e/g; # \`e: unicode 0xe8, mac os western 0x8f
84 $inputstring =~ s/\x91/\\"e/g; # \"e: unicode 0xeb, mac os western 0x91
85 $inputstring =~ s/\x97/\\'o/g; # \'o: unicode 0xf3, mac os western 0x97
86 $inputstring =~ s/\x98/\\`o/g; # \`o: unicode 0xf2, mac os western 0x98
87 $inputstring =~ s/\x9a/\\"o/g; # \"o: unicode 0xf6, mac os western 0x9a
88 $inputstring =~ s/\x99/\\^o/g; # \^o: unicode 0xf4, mac os western 0x99
89 $inputstring =~ s/\xbf/\\o /g; # \o: unicode 0xf8, mac os western 0xbf
90 $inputstring =~ s/\x96/\\~n /g; # \~n: unicode 0xF1, mac os western 0x96
91 $inputstring =~ s/\x94/\\^{\\i}/g; # \^{\i}: unicode 0xee, mac os western 0x94
92 $inputstring =~ s/\x/\\i/g; # \i: unicode , mac os western
93 $inputstring =~ s/\x9f/\\"u/g; # \"u: unicode 0xfc, mac os western 0x9f
94 $inputstring =~ s/\x5c/\\/g; # \: unicode 0x5C, mac os western 0x5C
95
96 return $inputstring;
97 }
98
99 # -- output the information for a day
100 sub outputdaylatex {
101 my ($fields) = @_;
102 my $sessiontitle = $fields->{'title'};
103 open(SESSIONS, ">>exsessions.tex"); #open for append
104 print SESSIONS ' ';
105 print SESSIONS '%%%== Day';
106 print SESSIONS '\procday{', $sessiontitle, '}'
107 }
108
109 # -- output the information for a session line
110 sub outputsessionlatex {
111 my ($fields) = @_;
112 my $sessiontitle = $fields->{'title'};
113 open(SESSIONS, ">>exsessions.tex"); #open for append
114 print SESSIONS ' ';
115 print SESSIONS '%%%-- session';
116 print SESSIONS '\session{', $sessiontitle, '}'
117 }
118
119 # -- in: ref. to a list of lists of author names ((Udo,Zoelzer),(Daniel,Arfib))
120 # out: ref. to a Perl list w/ entries "Udo Zoelzer" and "Daniel Arfib" (no quotes)
121 sub authorsbyfirstname {
122 my ($authors) = @_;
123 # generate a list of full "first last" author names
124 my @authorlistbyfirstname = map { "$_->[0] $_->[1]" } @$authors;
125 return \@authorlistbyfirstname; # return a ref. to the new list of authors
126 }
127
128 # -- in: ref. to a list of lists of author names ((Udo,Zoelzer),(Daniel,Arfib))
129 # out: ref. to a Perl list w/ entries "Zoelzer, Udo" and "Arfib, Daniel"
130 sub authorsbysurname {
131 my ($authors) = @_;
132 # generate a list of authors with surnames written first
133 my @authorlistbysurname = map { "$_->[1], $_->[0]" } @$authors;
134 return \@authorlistbysurname; # return a ref. to the new list of authors
135 }
136
137 # -- in: ref. to a list of author names: "Zoelzer, Udo" and "Arfib, Daniel"
138 # out: LaTeX index entries: "\index{Zoelzer, Udo}\index{Arfib, Daniel}"
139 sub genindex {
140 my ($authorsbysurname) = @_;
141 my @indexentries = map { "\\index{$_}" } @$authorsbysurname;
142 return join('', @indexentries);
143 }
144
145 # -- in: ref. to a list of author names: "Zoelzer, Udo" and "Arfib, Daniel"
146 # out: bookmarks cmds: "\pdfbookmark[2]{Udo Zoelzer}{#2.Udo Zoelzer}
147 # \pdfbookmark[2]{Daniel Arfib}{#2.Daniel Arfib}"
148 sub genbookmark {
149 my ($authorsbyfirstname) = @_;
150 my @indexentries = map { "\\pdfbookmark[2]{$_}{#2.$_}" }
151 @$authorsbyfirstname;
152 return join('', @indexentries);
153 }
154
155 # -- output the information for a paper line
156 sub outputpaperlatex {
157 my ($fields) = @_;
158 open(SWI, ">>expapersswitch.tex"); #open for append
159 print SWI '%======= PAPER ID = ', $fields->{'number'}, ' =======';
160 print SWI '\ifnum\paperswitch=', $fields->{'number'};
161 print SWI ' \procpaper[xshift=\LaTeXxShift{}, yshift=\LaTeXyShift{}, npages=',
162 $fields->{'nbpages'}, ', switch=\paperswitch,%';
163 print SWI ' title={', $fields->{'title'}, '},% paper title';
164 print SWI ' author={', join( ', ', @{&authorsbyfirstname($fields->{'authors'})}),
165 '},% list of authors';
166 print SWI ' index={', &genindex(&authorsbysurname($fields->{'authors'})),
167 '},% authors index entries';
168 print SWI ' cite={', $fields->{'cite'}, '},% cited bib items';
169 # print SWI ' {#2}{\paperbookmark}';
170 print SWI ' bookmark={', &genbookmark(&authorsbyfirstname($fields->{'authors'})),'}% for PDF bookmark structure';
171 print SWI ' ]{#2}';
172 print SWI '\fi';
173 print SWI ' ';
174 open(SESSIONS, ">>exsessions.tex"); #open for write, overwrite
175 print SESSIONS '\paperid{', $fields->{'number'}, '}{', $fields->{'filename'}, '}';
176 }
177
178 # ----- Main
179 # FIXME: parse a line, and confirm that all of the fields are set up properly
180 # --> correct number of fields, and the fields have the correct values
181 open(SWI, ">>expapersswitch.tex"); #open for write, overwrite
182 print SWI '\newcommand{\paperid}[2]{';
183 print SWI ' ';
184 print SWI '\renewcommand{\paperswitch}{#1}';
185 print SWI ' ';
186
187 while ( <> ) {
188 chomp; # clear the newline character from the end of the line
189 my $fields = &parseinputline($_); # parse the line into fields
190 # take some action depending on what type of line it is; case insensitive
191 if ( lc($fields->{'type'}) eq lc('day') ) {
192 &outputdaylatex($fields);
193 } elsif ( lc($fields->{'type'}) eq lc('session')
194 || lc($fields->{'type'}) eq lc('paper session')
195 || lc($fields->{'type'}) eq lc('demo session')
196 || lc($fields->{'type'}) eq lc('poster session') ) {
197 &outputsessionlatex($fields);
198 } elsif ( lc($fields->{'type'}) eq lc('oral')
199 || lc($fields->{'type'}) eq lc('paper')
200 || lc($fields->{'type'}) eq lc('demo')
201 || lc($fields->{'type'}) eq lc('poster') ) {
202 &outputpaperlatex($fields);
203 } elsif ( lc($fields->{'type'}) eq lc('Type')) {
204 } else { print '!!! a day, session or paper (',
205 $fields->{'type'},') is lost by the script...';
206 }
207 open(SWI, ">>expapersswitch.tex"); #open for append
208 }
209 print SWI '}';
210 close(SWI);
211 close(SESSIONS);