script/beamer-reveal.pl untested

filter: Statementstmt 0.0% 0 / 177 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 242 CC 1 SCAR 6.9
CC
1
Cov
0%
unixify
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/local/bin/perl -w
2 # -*- cperl -*-
3 # PODNAME: beamer-reveal.pl
4 # ABSTRACT: converts the .rvl file and the corresponding pdf file to a full reveal website
5 our $VERSION = '20261115.1249'; # VERSION
6  
7  
8 use strict;
9  
10 use Getopt::Long;
11 use Pod::Usage;
12  
13 use Config::Tiny;
14  
15 use File::Spec;
16 use File::ShareDir;
17 use File::Copy::Recursive;
18  
19 use Data::Dumper;
20  
21 use BeamerReveal;
22 use BeamerReveal::TemplateStore;
23 use BeamerReveal::FrameConverter;
24 use BeamerReveal::NotesFactory;
25 use BeamerReveal::Log;
26  
27 sub unixify;
28  
29 ######################################
30 # Read command-line options/arguments
31 ####################################
32  
33 my $opt_help;
34 my $opt_version;
35 my $opt_man;
36 my $opt_debug;
37 my $output_dir;
38 my $aux_dir;
39 my $pdf_dir;
40  
41 my $result =
42 GetOptions( "help" => \$opt_help,
43 "version" => \$opt_version,
44 "man" => \$opt_man,
45 "debug" => \$opt_debug,
46 "output-directory=s" => \$output_dir,
47 "aux-directory=s" => \$aux_dir,
48 "pdf-directory=s" => \$pdf_dir,
49 )
50 || pod2usage( -exitval => 1,
51 -output => \*STDERR );
52 pod2usage( -exitval => 100, -verbose => 1 ) if ($opt_help);
53 pod2usage( -exitval => 101, -verbose => 2 ) if ($opt_man);
54  
55 my $argument = $ARGV[0] if( @ARGV == 1 );
56  
57 if ( $opt_version ) {
58 say STDOUT "v${BeamerReveal::VERSION}";
59 exit(0);
60 }
61  
62 pod2usage( -message => "Incorrect number of arguments",
63 -exitval => 1,
64 -output => \*STDERR ) unless( defined( $argument ) );
65  
66 ###################
67 # do the hard work
68 #################
69  
70 # fetch the jobname and get rid of these nasty windows backslashes...
71 my ( $jobname ) = @ARGV;
72 $jobname =~ s/\\/\//g;
73  
74 my $openinglines =
75 [
76 '-|-',
77 "-- beamer-reveal.pl|v${BeamerReveal::VERSION} --",
78 '-- (C) 2025-2026 Walter PM Daems <wdaems@cpan.org>|GPLv3 --',
79 '-- This script uses materials from:|--',
80 '-- - reveal.js (C) Hakim El Hattab and contributors|MIT license --',
81 '-- - Quarto (C) Posit Software, PBC and contributors|MIT license --',
82 '-|-',
83 ];
84 my $closinglines = [ '-|-' ];
85  
86 my $logger = BeamerReveal::Log->new( opening => $openinglines,
87 closing => $closinglines,
88 logfilename => "$jobname.brlog",
89 labelsize => 21,
90 activitysize => 28 );
91  
92 ###############################
93 # set defaults for directories
94 $pdf_dir ||= '.';
95 $aux_dir ||= $pdf_dir;
96 $output_dir ||= '.';
97  
98 # unixify these directories
99 foreach my $path ( \$pdf_dir, \$aux_dir, \$output_dir ) {
100 $$path = unixify( $$path );
101 }
102  
103  
104 $logger->log( 0, "- Using the following directories:" );
105 $logger->log( 2, "aux-files : '$aux_dir'" );
106 $logger->log( 2, "pdf-files : '$pdf_dir'" );
107 $logger->log( 2, "output-files : '$output_dir'" );
108  
109 my $fc_id =
110 $logger->registerTask( label => "Frame conversion",
111 progress => 0,
112 total => 1 );
113 my $ng_id =
114 $logger->registerTask( label => "Note generation",
115 progress => 0,
116 total => 1 );
117 my $mmanimgen_id =
118 $logger->registerTask( label => "Animation generation",
119 progress => 0,
120 total => 1 );
121 my $mmstillgen_id =
122 $logger->registerTask( label => "Still generation",
123 progress => 0,
124 total => 1 );
125 my $mmvovergen_id =
126 $logger->registerTask( label => "Voice-over generation",
127 progress => 0,
128 total => 1 );
129 my $proc_id =
130 $logger->registerTask( label => "Slide processing",
131 progress => 0,
132 total => 1 );
133 my $mmcop_id =
134 $logger->registerTask( label => "Media copying",
135 progress => 0,
136 total => 1 );
137 my $overall_id =
138 $logger->registerTask( label => "Overall progress",
139 progress => 0,
140 total => 9 );
141  
142 $logger->activate();
143  
144 ##################
145 # Read input file
146  
147 # fetch the reveal file
148 my $rvlFileName = "$aux_dir/$jobname.rvl";
149 $logger->log( 0, "- Reading driver file $rvlFileName" );
150 $logger->progress( $overall_id, 0, 'reading driver file' );
151  
152 my $rvlFile = IO::File->new();
153 $rvlFile->open( "<$rvlFileName" )
154 or $logger->fatal( "Error: cannot read reveal file '$rvlFileName'\n" );
155  
156 # skip first comment lines
157 my $lineCtr = 0;
158 my $line = '%%';
159 while ( $line =~ /^%%/ ) {
160 ++$lineCtr;
161 $line = <$rvlFile>;
162 }
163  
164 # slurp the entire file while keepign track of the linenumbers
165 my $content = $line . do { local $/; <$rvlFile> };
166 my @chunks = split( '@@', $content );
167 shift @chunks;
168 my @chunksLineNrs;
169 $chunksLineNrs[0] = $lineCtr;
170 for( my $i = 0; $i < @chunks; ++$i ) {
171 $lineCtr += $chunks[$i] =~ tr/\n//;
172 $chunksLineNrs[$i+1] = $lineCtr;
173 }
174  
175 #################
176 # parse the file
177  
178 $logger->log( 0, "- Parsing driver file $rvlFileName" );
179 $logger->progress( $overall_id, 0.25, 'parsing driver file' );
180  
181 my $factory = BeamerReveal->new();
182  
183 ## the first chunk needs to be a presentation chunk
184 my $presentation = $factory->createFromChunk( $chunks[0], $chunksLineNrs[0] );
185  
186 my $mediaManager =
187 BeamerReveal::MediaManager->new( $jobname,
188 $output_dir,
189 "${jobname}_files",
190 $presentation->{parameters},
191 $opt_debug );
192  
193 ## parse all slides
194 my $slides = [];
195 my $nofNotes = 0;
196 eval {
197 for( my $i = 1; $i < @chunks; ++$i ) {
198 my $object = $factory->createFromChunk( $chunks[$i], $chunksLineNrs[$i] );
199 $object->{hasnotes} = ++$nofNotes if( $object->{hasnotes} ); #if notes, add sequence number
200 my $contentToGenerate = $object->extractContentToGenerate( $i, $mediaManager, $presentation );
201 push @$slides, {
202 slide => $object,
203 contentToGenerate => $contentToGenerate
204 };
205 }
206 1;
207 } or do {
208 $logger->fatal( "$@" );
209 };
210 $presentation->{parameters}->{nofnotes} = $nofNotes;
211  
212 $logger->progress( $overall_id, 0.5, 'installing boiler plate' );
213  
214 # storing the reveal framework
215 $logger->log( 0, "- Installing quarto/reveal.js boilerplate" );
216 $mediaManager->revealToStore();
217  
218 ######################
219 # generate all images
220 $logger->progress( $overall_id, 1, 'generating backgrounds' );
221  
222 $logger->log( 0, "- Generating the images" );
223 my $frameConvertor = BeamerReveal::FrameConverter->new( "${jobname}_files",
224 $output_dir,
225 "$pdf_dir/$jobname.pdf",
226 $presentation->{parameters}->{canvaswidth},
227 $presentation->{parameters}->{canvasheight},
228 $fc_id,
229 $opt_debug );
230 my $list = $frameConvertor->toJPG();
231 $mediaManager->backgroundsToStore( $list );
232  
233 ##########################
234 # geneate the notes pages
235 $logger->progress( $overall_id, 2, 'generating notes pages' );
236  
237 $logger->log( 0, "- Generating the notes pages" );
238 if ( $nofNotes ) {
239 my $notesFactory = BeamerReveal::NotesFactory->new( "${jobname}_files",
240 $output_dir,
241 $pdf_dir,
242 $presentation->{parameters},
243 $presentation->{parameters}->{canvaswidth} / 2,
244 $presentation->{parameters}->{canvasheight} / 2,
245 $ng_id,
246 $opt_debug);
247 my $list = $notesFactory->toJPG();
248 $mediaManager->notesToStore( $list );
249 }
250 else {
251 $logger->progress( $ng_id, 1, 'no notes found', 1 );
252 }
253  
254 # here's the plan/issue
255 # - creating slidecollection late because we need images and notes to be present for embedding
256 # - problem: at that moment, the animation generation and media-copying has not been done yet.
257 # - yet, these need to be present also for embedding before we can create the slide collection.
258 # Question: can we postpone making the slidecollection until after copying media and generating animations/stills?
259 # - problem: MM->mediaFromStore() composes the basic info that is needed for the generation. It is only called
260 # - when processing the slidecollection.
261 # Possible solution: work in two passes
262 # Can we only treat animations/stills and the detection of notes in the first pass?
263 # Make a method next to makeSlide() that is extractContentToGenerate(), that only treats animations and stills
264 # and prepares the backorders with the required info.
265 # then we can generate the backorders
266 # processs the backorders
267 # and go to makeslide.
268 # The only issue is that the filenaming using the hamac_sha256_hex may not be unique if the same expand-once
269 # command is used in multiple stills and animations... the filename will not be unique
270 # the naming needs to be based on the full stamped tex-file instead of on the argument of the \animation command only.
271 # remaining issue:
272 # _fromStore does a correct job for embedding, but not for the non-embedding, because this
273 # tires to coume up with a new filename...
274  
275 ################################
276 # generate the generation back-orders
277  
278 $logger->progress( $overall_id, 3, 'animation generation' );
279 $logger->log( 0, "- Generating animations" );
280 $mediaManager->processAnimationBackOrders( $mmanimgen_id );
281  
282 $logger->progress( $overall_id, 4, 'still generation' );
283 $logger->log( 0, "- Generating stills" );
284 $logger->log( 0, "- Generating all new media" );
285 $mediaManager->processStillBackOrders( $mmstillgen_id );
286  
287 $logger->progress( $overall_id, 5, 'voice-over generation' );
288 $logger->log( 0, "- Generating voice-overs" );
289 $logger->log( 0, "- Generating all new media" );
290 $mediaManager->processVoiceoverBackOrders( $mmvovergen_id );
291  
292 ######################
293 # process the content
294 # generating the content
295 $logger->log( 0, "- Processing the presentation" );
296 $logger->progress( $overall_id, 6, 'processing presentation' );
297  
298 my $slideCollection;
299 my $i = 0;
300 my $nofSlides = @$slides;
301 foreach my $object ( @$slides ) {
302 $logger->progress( $proc_id, $i++, "slide $i/$nofSlides", $nofSlides );
303 $slideCollection .= $object->{slide}->makeSlide( $i, $mediaManager, $presentation,
304 $object->{contentToGenerate} );
305 }
306 $logger->progress( $proc_id, $i );
307  
308  
309 ################################
310 # generate the copy back-orders
311 $logger->progress( $overall_id, 7, 'media copying' );
312  
313 $logger->log( 0, "- Collecting all existing media" );
314 $mediaManager->processCopyBackOrders( $mmcop_id );
315  
316  
317 ######################
318 # write the main file
319 $logger->progress( $overall_id, 8, 'writing presentation file' );
320  
321 $logger->log( 0, "- Producing presentation" );
322  
323 my $store = BeamerReveal::TemplateStore->new();
324 my $mainTemplate = $store->fetch( 'html', 'main.html' );
325  
326 my $oFileName = "$output_dir/$jobname.html";
327 $logger->log( 2, "- Writing $oFileName" );
328 my $oFile = IO::File->new();
329  
330 $presentation->{parameters}->{autoslide} ||= 'false';
331 $oFile->open( ">$oFileName" )
332 or $logger->fatal( "Error: cannot write to '$oFileName'" );
333 print $oFile
334 BeamerReveal::TemplateStore::stampTemplate( $mainTemplate,
335 { PRODUCER => "beamer-reveal.sty $presentation->{parameters}->{latexversion} / beamer-reveal.pl v${BeamerReveal::VERSION}",
336 TITLE => $presentation->{parameters}->{title},
337 AUTHOR => $presentation->{parameters}->{author},
338 AUTOSLIDE => $presentation->{parameters}->{autoslide},
339 LOOP => $presentation->{parameters}->{loop},
340 CANVASWIDTH => $presentation->{parameters}->{canvaswidth},
341 CANVASHEIGHT => $presentation->{parameters}->{canvasheight},
342 SLIDES => $slideCollection,
343 SUBDIR => "${jobname}_files" } );
344 $oFile->close();
345  
346 # finally let's create an index.html link
347 $logger->log( 2, "- Creating index.html link" );
348 link( "$oFileName", "$output_dir/index.html" );
349 $logger->progress( $overall_id, 9, 'done' );
350  
351 ########################
352 # generate closing line
353 ######################
354 $logger->finalize();
355  
356 sub unixify {
357 $_[0] =~ s/\\/\//g;
358 $_[0] =~ s/\/$//;
359 return $_[0];
360 }
361  
362 #########################################################
363  
364 __END__