/
makebeamerinfo.pm
443 lines (351 loc) · 10.3 KB
/
makebeamerinfo.pm
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package App::makebeamerinfo;
use strict;
use warnings;
use Cwd 'abs_path';
use File::Basename;
use File::Find;
our $VERSION = "2.000";
$VERSION = eval $VERSION;
#list of the available transitions
my @available_transitions = ( qw/
Crossfade
None
PagePeel PageTurn
SlideDown SlideLeft SlideRight SlideUp
SpinOutIn SpiralOutIn
SqueezeDown SqueezeLeft SqueezeRight SqueezeUp
WipeBlobs
WipeCenterIn WipeCenterOut
WipeUp WipeDown WipeLeft WipeRight
WipeDownRight WipeUpLeft
ZoomOutIn
/ );
# the "clean" transition set is a basic set useful in all circumstances
my %clean = (
increment => ["WipeRight"],
frame => "PageTurn"
);
# the "sane" transition set sorts the available transitions
# into the two uses as appropriate for a beamer presentation
my %sane = (
increment => [ qw/
WipeCenterIn WipeCenterOut
WipeUp WipeDown WipeLeft WipeRight
WipeDownRight WipeUpLeft
/ ],
frame => [ qw/
Crossfade
PagePeel PageTurn
SlideDown SlideLeft SlideRight SlideUp
SpinOutIn SpiralOutIn
SqueezeDown SqueezeLeft SqueezeRight SqueezeUp
WipeBlobs
ZoomOutIn
/ ]
);
#==========================
# Builder methods
sub new {
my $class = shift;
my $args = ref $_[0] ? shift() : { @_ };
my $self = {
files => { #holder for file names
pdf => '',
nav => '',
},
pages => {}, #holder for page information from nav file
sections => {}, #holder for section information
transitions => undef, #initialized later
# holder for making sure that the next random transition
# is not the same as the last
last_transition => '',
options => {
# set option to collapse AtBeginSection
# and AtBeginSubsection elements (default true)
collapse => 1,
# set_transition default to clean
transition_set => 'clean',
},
};
# pull files from arguments if present
$self->{files}{pdf} = abs_path($args->{pdf}) if $args->{pdf};
$self->{files}{nav} = abs_path($args->{nav}) if $args->{nav};
bless $self, $class;
$self->_init_transitions;
$self->_hunt_for_files;
return $self;
}
sub _init_transitions {
my $self = shift;
# Initialize Hash of the custom transitions set
# from array of available transitions
my %transitions;
foreach my $trans (@available_transitions) {
$transitions{'increment'}{$trans} = 0;
$transitions{'frame'}{$trans} = 0;
}
# set some base selections for the 'custom' set
foreach my $trans (@{ $clean{'increment'} }) {
$transitions{'increment'}{$trans} = 1;
}
foreach my $trans (@{ $sane{'frame'} }) {
$transitions{'frame'}{$trans} = 1;
}
$self->{transitions} = \%transitions;
}
sub _hunt_for_files {
my $self = shift;
my $files = $self->{files};
if (! $files->{pdf} and $files->{nav}) {
$files->{pdf} = $self->findFile( $files->{nav} );
}
if (! $files->{nav} and $files->{pdf}) {
$files->{nav} = $self->findFile( $files->{pdf} );
}
}
#==========================
# common message methods
# Sub that displays "about" information
# Possibly to be deprecated and replaced with a version sub and a usage sub
sub aboutMBI {
my $self = shift;
$self->userMessage(
"About MakeBeamerInfo",
"Version: $VERSION"
);
}
# Sub (mostly for GUI) to display an exit message and quit before attempting to create an info file
sub exitProgramEarly {
my $self = shift;
$self->userMessage(
"Goodbye",
"No .info file has been created!"
);
exit(1);
}
# Sub that after creation of an info file files, says goodbye and quits
sub exitProgramFinished {
my $self = shift;
$self->userMessage(
"Goodbye",
"Your .info file has been created."
);
exit();
}
#============================
# Overloadable methods
sub userMessage {
my $self = shift;
my ($title, $message) = @_;
}
sub run {
my $self = shift;
$self->createInfo;
}
#============================
# Methods for finding and opening files
# method that takes the full path of a specified file
# and returns the other file if possible
sub findFile {
my $self = shift;
# burst the full file path into pieces
my $full_path = shift or return '';
my ($file, $dirs, $suffix) = fileparse( $full_path );
$file .= ($suffix eq '.pdf') ? '.nav' : '.pdf';
my $found = '';
my $wanted = sub {
return if $found;
if ($_ eq $file) {
$found = $File::Find::name;
}
};
find( $wanted , $dirs );
return $found;
}
sub openFile {
my $self = shift;
my ($filename, $type, $mode) = @_;
unless ($filename) {
$self->userMessage(
"Error",
"Please specify a .$type file."
);
$self->exitProgramEarly();
}
my $handle;
unless ( open($handle, $mode, $filename) ) {
$self->userMessage(
"Error",
"Could not open $filename: $!"
);
$self->exitProgramEarly();
}
return $handle;
}
#============================
# Subs that perform the "meat" of the work
# super-sub that controls all the actions to generate info file
sub createInfo {
my $self = shift;
$self->readNav();
$self->writeInfo();
$self->exitProgramFinished();
}
# sub to read the nav file. The information is fed into %pages and %sections.
# by reading twice we are able to use the collapse relate frames (declared after sections) and sections
sub readNav {
my $self = shift;
# if a handle is given as an arg use it. This is for testing.
my $nav = @_ ? shift :
$self->openFile($self->{files}{'nav'}, 'nav', '<');
my $pages = $self->{pages};
my $sections = $self->{sections};
my $collapse = $self->{options}{collapse};
# first read through the nav file for framepages
while (<$nav>) {
if( /framepages {(\d+)}{(\d+)}/ ) {
for ( my $i = $1; $i < $2; $i++) {
$pages->{$i} = { page => $i, type => 'increment' };
}
$pages->{$2} = { page => $2, type => 'frame' };
}
}
# go back to the top of the .nav file
seek($nav,0,0);
# then read the file again to determine other information
while (<$nav>) {
if( /\\sectionentry {(\d+)}{([^\}]+)}{(\d+)}/ ) {
$sections->{$1}{'page'} = $3;
$sections->{$1}{'title'} = $2;
$pages->{$3}{'is_section'} = $1;
}
if( /\@subsectionentry {\d+}{(\d+)}{(\d+)}{(\d+)}{([^\}]+)}/ ) {
$pages->{$3}{'is_subsection'} = $2;
$pages->{$3}{'of_section'} = $1;
$sections->{$1}{$2}{'page'} = $3;
$sections->{$1}{$2}{'title'} = $4;
if ($collapse and $sections->{$1}{'page'} == ($3 - 1)) {
$pages->{ $sections->{$1}{'page'} }{'to_collapse'} = 1;
}
}
}
}
sub writeInfo {
my $self = shift;
# if a handle is given as an arg use it. This is for testing.
my $info = @_ ? shift :
$self->openFile($self->{files}{'pdf'} . '.info', 'info', '>');
my $pages = $self->{pages};
my $sections = $self->{sections};
my $options = $self->{options};
print $info "PageProps = {\n";
foreach my $page (sort { $a <=> $b } keys %$pages) {
print $info " " . $pages->{$page}{page} . ":\t{\n";
if ($pages->{$page}{'type'} eq 'increment' || $pages->{$page}{'to_collapse'}) {
print $info "\t \'overview\': False,\n";
}
if ($pages->{$page}{'is_section'}) {
print $info "\t \'title\': \"" . $sections->{ $pages->{$page}{'is_section'} }{'title'} . "\",\n";
} elsif ($page == 1) {
print $info "\t \'title\': \"Title\",\n";
}
if ($pages->{$page}{'is_subsection'}) {
print $info "\t \'title\': \"" . $sections->{ $pages->{$page}{'of_section'} }{'title'} . ": " . $sections->{ $pages->{$page}{'of_section'} }{ $pages->{$page}{'is_subsection'} }{'title'} . "\",\n";
}
if (
$pages->{$page}{'type'} eq 'frame'
&& ! $pages->{$page}{'to_collapse'}
&& $options->{'transition_set'} ne 'default'
) {
print $info "\t \'transition\': " . $self->getFrameTransition() . ",\n";
}
print $info "\t},\n";
}
print $info "}\n";
unless ($options->{'transition_set'} eq 'default') {
print $info "AvailableTransitions = [";
print $info join(", ", $self->getOverallTransitions());
print $info "]";
}
}
#============================
# Subs that select certain transitions in certain cases
sub getFrameTransition {
my $self = shift;
my $options = $self->{options};
my $result;
if ($options->{'transition_set'} eq 'custom') {
$result = $self->getRandomElement(
$self->get_selected( $self->{transitions}{'frame'} )
);
} elsif ($options->{'transition_set'} eq 'clean') {
$result = $clean{'frame'};
} elsif ($options->{'transition_set'} eq 'sane') {
$result = $self->getRandomElement( @{ $sane{'frame'} } );
}
return $result;
}
sub getOverallTransitions {
my $self = shift;
my $options = $self->{options};
my @result;
if ($options->{'transition_set'} eq 'custom') {
@result = $self->get_selected( $self->{transitions}{'increment'} );
} elsif ($options->{'transition_set'} eq 'clean') {
@result = @{ $clean{'increment'} };
} elsif ($options->{'transition_set'} eq 'sane') {
@result = @{ $sane{'increment'} };
}
return @result;
}
#============================
# Simple methods to perform actions with data structures
# return the contents of a random element of an array
sub getRandomElement {
my $self = shift;
my @input = @_;
my $length = @input;
my $rand = int(rand($length));
my $return = $input[$rand];
# prevent repeated transitions
while ($length > 1 && $return eq $self->{last_transition}) {
$rand = int(rand($length));
$return = $input[$rand];
}
$self->{last_transition} = $return;
return $return;
}
# get the hash keys whose values are true
sub get_selected {
my ($self, $input) = @_;
return grep { $input->{$_} } keys %$input;
}
1;
__END__
__POD__
=head1 NAME
App::makebeamerinfo - Creates .info file for use with Impressive and LaTeX Beamer
=head1 SYNOPSIS
use App::makebeamerinfo;
my $app = App::makebeamerinfo->new();
$app->run;
=head1 DESCRIPTION
This module and its subclasses serve as the backend for L<makebeamerinfo>.
=head1 SEE ALSO
=over
=item *
L<makebeamerinfo>
=item *
L<Impressive|http://impressive.sourceforge.net/>
=item *
L<LaTeX Beamer|http://latex-beamer.sourceforge.net/>
=back
=head1 SOURCE REPOSITORY
L<http://github.com/jberger/MakeBeamerInfo>
=head1 AUTHOR
Joel Berger, E<lt>joel.a.berger@gmail.comE<gt>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2012 by Joel Berger
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut