script/beamer-reveal.pl untested

filter: Statementstmt 0.0% 0 / 172 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 21 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 224 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 = '20260208.1851'; # 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 $proc_id =
126 $logger->registerTask( label => "Slide processing",
127 progress => 0,
128 total => 1 );
129 my $mmcop_id =
130 $logger->registerTask( label => "Media copying",
131 progress => 0,
132 total => 1 );
133 my $overall_id =
134 $logger->registerTask( label => "Overall progress",
135 progress => 0,
136 total => 8 );
137  
138 $logger->activate();
139  
140 ##################
141 # Read input file
142  
143 # fetch the reveal file
144 my $rvlFileName = "$aux_dir/$jobname.rvl";
145 $logger->log( 0, "- Reading driver file $rvlFileName" );
146 $logger->progress( $overall_id, 0, 'reading driver file' );
147  
148 my $rvlFile = IO::File->new();
149 $rvlFile->open( "<$rvlFileName" )
150 or $logger->fatal( "Error: cannot read reveal file '$rvlFileName'\n" );
151  
152 # skip first comment lines
153 my $lineCtr = 0;
154 my $line = '%%';
155 while ( $line =~ /^%%/ ) {
156 ++$lineCtr;
157 $line = <$rvlFile>;
158 }
159  
160 # slurp the entire file while keepign track of the linenumbers
161 my $content = $line . do { local $/; <$rvlFile> };
162 my @chunks = split( '@@', $content );
163 shift @chunks;
164 my @chunksLineNrs;
165 $chunksLineNrs[0] = $lineCtr;
166 for( my $i = 0; $i < @chunks; ++$i ) {
167 $lineCtr += $chunks[$i] =~ tr/\n//;
168 $chunksLineNrs[$i+1] = $lineCtr;
169 }
170  
171 #################
172 # parse the file
173  
174 $logger->log( 0, "- Parsing driver file $rvlFileName" );
175 $logger->progress( $overall_id, 0.25, 'parsing driver file' );
176  
177 my $factory = BeamerReveal->new();
178  
179 ## the first chunk needs to be a presentation chunk
180 my $presentation = $factory->createFromChunk( $chunks[0], $chunksLineNrs[0] );
181  
182 my $mediaManager =
183 BeamerReveal::MediaManager->new( $jobname,
184 $output_dir,
185 "${jobname}_files",
186 $presentation->{parameters},
187 $opt_debug );
188  
189 ## parse all slides
190 my $slides = [];
191 my $nofNotes = 0;
192 eval {
193 for( my $i = 1; $i < @chunks; ++$i ) {
194 my $object = $factory->createFromChunk( $chunks[$i], $chunksLineNrs[$i] );
195 $object->{hasnotes} = ++$nofNotes if( $object->{hasnotes} ); #if notes, add sequence number
196 my $generatedContent = $object->extractGenerationContent( $i, $mediaManager, $presentation );
197 push @$slides, {
198 slide => $object,
199 generatedContent => $generatedContent
200 };
201 }
202 1;
203 } or do {
204 $logger->fatal( "$@" );
205 };
206 $presentation->{parameters}->{nofnotes} = $nofNotes;
207  
208 $logger->progress( $overall_id, 0.5, 'installing boiler plate' );
209  
210 # storing the reveal framework
211 $logger->log( 0, "- Installing quarto/reveal.js boilerplate" );
212 $mediaManager->revealToStore();
213  
214 ######################
215 # generate all images
216 $logger->progress( $overall_id, 1, 'generating backgrounds' );
217  
218 $logger->log( 0, "- Generating the images" );
219 my $frameConvertor = BeamerReveal::FrameConverter->new( "${jobname}_files",
220 $output_dir,
221 "$pdf_dir/$jobname.pdf",
222 $presentation->{parameters}->{canvaswidth},
223 $presentation->{parameters}->{canvasheight},
224 $fc_id,
225 $opt_debug );
226 my $list = $frameConvertor->toJPG();
227 $mediaManager->backgroundsToStore( $list );
228  
229 ##########################
230 # geneate the notes pages
231 $logger->progress( $overall_id, 2, 'generating notes pages' );
232  
233 $logger->log( 0, "- Generating the notes pages" );
234 if ( $nofNotes ) {
235 my $notesFactory = BeamerReveal::NotesFactory->new( "${jobname}_files",
236 $output_dir,
237 $pdf_dir,
238 $presentation->{parameters},
239 $presentation->{parameters}->{canvaswidth} / 2,
240 $presentation->{parameters}->{canvasheight} / 2,
241 $ng_id,
242 $opt_debug);
243 my $list = $notesFactory->toJPG();
244 $mediaManager->notesToStore( $list );
245 }
246 else {
247 $logger->progress( $ng_id, 1, 'no notes found', 1 );
248 }
249  
250 # here's the plan/issue
251 # - creating slidecollection late because we need images and notes to be present for embedding
252 # - problem: at that moment, the animation generation and media-copying has not been done yet.
253 # - yet, these need to be present also for embedding before we can create the slide collection.
254 # Question: can we postpone making the slidecollection until after copying media and generating animations/stills?
255 # - problem: MM->mediaFromStore() composes the basic info that is needed for the generation. It is only called
256 # - when processing the slidecollection.
257 # Possible solution: work in two passes
258 # Can we only treat animations/stills and the detection of notes in the first pass?
259 # Make a method next to makeSlide() that is extractGenerationContent(), that only treats animations and stills
260 # and prepares the backorders with the required info.
261 # then we can generate the backorders
262 # processs the backorders
263 # and go to makeslide.
264 # The only issue is that the filenaming using the hamac_sha256_hex may not be unique if the same expand-once
265 # command is used in multiple stills and animations... the filename will not be unique
266 # the naming needs to be based on the full stamped tex-file instead of on the argument of the \animation command only.
267 # remaining issue:
268 # _fromStore does a correct job for embedding, but not for the non-embedding, because this
269 # tires to coume up with a new filename...
270  
271 ################################
272 # generate the generation back-orders
273  
274 $logger->progress( $overall_id, 3, 'animation generation' );
275 $logger->log( 0, "- Generating animations" );
276 $mediaManager->processAnimationBackOrders( $mmanimgen_id );
277  
278 $logger->progress( $overall_id, 4, 'still generation' );
279 $logger->log( 0, "- Generating stills" );
280 $logger->log( 0, "- Generating all new media" );
281 $mediaManager->processStillBackOrders( $mmstillgen_id );
282  
283 ######################
284 # process the content
285 # generating the content
286 $logger->log( 0, "- Processing the presentation" );
287 $logger->progress( $overall_id, 5, 'processing presentation' );
288  
289 my $slideCollection;
290 my $i = 0;
291 my $nofSlides = @$slides;
292 foreach my $object ( @$slides ) {
293 $logger->progress( $proc_id, $i++, "slide $i/$nofSlides", $nofSlides );
294 $slideCollection .= $object->{slide}->makeSlide( $i, $mediaManager, $presentation,
295 $object->{generatedContent} );
296 }
297 $logger->progress( $proc_id, $i );
298  
299  
300 ################################
301 # generate the copy back-orders
302 $logger->progress( $overall_id, 6, 'media copying' );
303  
304 $logger->log( 0, "- Collecting all existing media" );
305 $mediaManager->processCopyBackOrders( $mmcop_id );
306  
307  
308 ######################
309 # write the main file
310 $logger->progress( $overall_id, 7, 'writing presentation file' );
311  
312 $logger->log( 0, "- Producing presentation" );
313  
314 my $store = BeamerReveal::TemplateStore->new();
315 my $mainTemplate = $store->fetch( 'html', 'main.html' );
316  
317 my $oFileName = "$output_dir/$jobname.html";
318 $logger->log( 2, "- Writing $oFileName" );
319 my $oFile = IO::File->new();
320  
321 $presentation->{parameters}->{autoslide} ||= 'false';
322 $oFile->open( ">$oFileName" )
323 or $logger->fatal( "Error: cannot write to '$oFileName'" );
324 print $oFile
325 BeamerReveal::TemplateStore::stampTemplate( $mainTemplate,
326 { PRODUCER => "beamer-reveal.sty $presentation->{parameters}->{latexversion} / beamer-reveal.pl v${BeamerReveal::VERSION}",
327 TITLE => $presentation->{parameters}->{title},
328 AUTHOR => $presentation->{parameters}->{author},
329 AUTOSLIDE => $presentation->{parameters}->{autoslide},
330 LOOP => $presentation->{parameters}->{loop},
331 CANVASWIDTH => $presentation->{parameters}->{canvaswidth},
332 CANVASHEIGHT => $presentation->{parameters}->{canvasheight},
333 SLIDES => $slideCollection,
334 SUBDIR => "${jobname}_files" } );
335 $oFile->close();
336  
337 # finally let's create an index.html link
338 $logger->log( 2, "- Creating index.html link" );
339 link( "$oFileName", "$output_dir/index.html" );
340 $logger->progress( $overall_id, 8, 'done' );
341  
342 ########################
343 # generate closing line
344 ######################
345 $logger->finalize();
346  
347 sub unixify {
348 $_[0] =~ s/\\/\//g;
349 $_[0] =~ s/\/$//;
350 return $_[0];
351 }
352  
353 #########################################################
354  
355 __END__