| 1 | # -*- cperl -*- | ||||||||||
| 2 | # ABSTRACT: NotesFactory | ||||||||||
| 3 | |||||||||||
| 4 | |||||||||||
| 5 | package BeamerReveal::NotesFactory; | ||||||||||
| 6 | 1 | our $VERSION = '20261115.1249'; # VERSION | |||||||||
| 7 | |||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 1 | ▶ | use File::Which; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | #use File::Path; | ||||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | use Data::UUID; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | 1 | ▶ | use BeamerReveal::IPC::Run; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | 1 | ▶ | use IPC::Run qw(harness start pump finish); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 20 | |||||||||||
| 21 | 1 | ▶ | use BeamerReveal::Log; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | |||||||||||
| 23 | 0 | ▶ | sub nofdigits { length( "$_[0]" ) } | ||||||||
|
Subroutine
nofdigits: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||
| 24 | |||||||||||
| 25 | |||||||||||
| 26 | 0 | ▶ | sub new { | ||||||||
|
Subroutine
new: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||
| 27 | 0 | my $class = shift; | |||||||||
| 28 | 0 | my ( $base, $output_dir, $pdf_dir, $presentationparameters, $xres, $yres, $progressId, $debug ) = @_; | |||||||||
| 29 | |||||||||||
| 30 | 0 | my $self = { | |||||||||
| 31 | base => $base, | ||||||||||
| 32 | output_dir => $output_dir, | ||||||||||
| 33 | pdf_dir => $pdf_dir, | ||||||||||
| 34 | presentationparameters => $presentationparameters, | ||||||||||
| 35 | xres => int( $xres ), | ||||||||||
| 36 | yres => int( $yres ), | ||||||||||
| 37 | progressId => $progressId, | ||||||||||
| 38 | debug => $debug, | ||||||||||
| 39 | }; | ||||||||||
| 40 | 0 | ▶ | $class = (ref $class ? ref $class : $class ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 0 | bless $self, $class; | |||||||||
| 42 | |||||||||||
| 43 | 0 | my $logger = $BeamerReveal::Log::logger; | |||||||||
| 44 | |||||||||||
| 45 | $self->{compiler} = File::Which::which( $self->{presentationparameters}->{compiler} ) | ||||||||||
| 46 | 0 | ▶ | or $logger->fatal( "Error: your setup is incomplete, I cannot find your $self->{presentationparameters}->{compiler } compiler (should be part of your TeX installation)\n" . | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | "Make sure it is accessible in a directory on your PATH list variable\n" ); | ||||||||||
| 48 | |||||||||||
| 49 | |||||||||||
| 50 | 0 | ▶ | $self->{pdftoppm} = File::Which::which( 'pdftoppm' ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | or $logger->fatal( "Error: your setup is incomplete, I cannot find pdftoppm (part of the poppler library)\n" . | ||||||||||
| 52 | "Install 'Poppler-utils' and make sure pdftoppm is accessible in a directory on your PATH list variable\n" ); | ||||||||||
| 53 | |||||||||||
| 54 | 0 | $self->{slides} = "$self->{base}/media/Slides"; | |||||||||
| 55 | |||||||||||
| 56 | 0 | return $self; | |||||||||
| 57 | } | ||||||||||
| 58 | |||||||||||
| 59 | |||||||||||
| 60 | |||||||||||
| 61 | |||||||||||
| 62 | 0 | ▶ | sub toJPG { | ||||||||
|
Subroutine
toJPG: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||
| 63 | 0 | my $self = shift; | |||||||||
| 64 | |||||||||||
| 65 | 0 | my $logger = $BeamerReveal::Log::logger; | |||||||||
| 66 | |||||||||||
| 67 | # pdf generate of n slides, and jpeg generation of n slides | ||||||||||
| 68 | $logger->progress( $self->{progressId}, | ||||||||||
| 69 | 0 | 0, "notes generation", 2 * $self->{presentationparameters}->{nofnotes} ); | |||||||||
| 70 | |||||||||||
| 71 | ############################# | ||||||||||
| 72 | # write altered source file | ||||||||||
| 73 | |||||||||||
| 74 | # 1. read the source file | ||||||||||
| 75 | 0 | $logger->log( 2, "- Reading LaTeX source file" ); | |||||||||
| 76 | 0 | my $sourceFileName = $self->{presentationparameters}->{sourcefilename}; | |||||||||
| 77 | 0 | my $source = do { | |||||||||
| 78 | 0 | local $/ = undef; | |||||||||
| 79 | 0 | ▶ | open my $fh, "<". $sourceFileName | ||||||||
|
Branch: 1 branch
| |||||||||||
| 80 | or $logger->fatal( "Error: could not open your LaTeX source file '$sourceFileName' for reading" ); | ||||||||||
| 81 | 0 | <$fh>; | |||||||||
| 82 | }; | ||||||||||
| 83 | |||||||||||
| 84 | # 2. alter it | ||||||||||
| 85 | 0 | my $notesMagic = ''; | |||||||||
| 86 | 0 | $source =~ s/\\begin\{document\}/\\setbeamertemplate\{note page\}\{\\insertnote\}\n\\setbeameroption\{show only notes\}\n\\begin\{document\}/; | |||||||||
| 87 | |||||||||||
| 88 | |||||||||||
| 89 | # 3. write it to a temporary file | ||||||||||
| 90 | 0 | $logger->log( 2, "- Writing LaTeX notes file" ); | |||||||||
| 91 | 0 | my $id = Data::UUID->new(); | |||||||||
| 92 | 0 | my $notesFileName; | |||||||||
| 93 | 0 | ▶ | do { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | 0 | my $uuid = $id->create(); | |||||||||
| 95 | 0 | $uuid = $id->to_string( $uuid ); | |||||||||
| 96 | 0 | $notesFileName = $sourceFileName; | |||||||||
| 97 | 0 | $notesFileName =~ s/(\.\w)/-notes-$uuid$1/; | |||||||||
| 98 | } until( ! -e $notesFileName ); | ||||||||||
| 99 | 0 | my $notesFile = IO::File->new(); | |||||||||
| 100 | 0 | ▶ | $notesFile->open( ">$notesFileName" ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | or $logger->fatal( "Error could not open notes file '$notesFileName' for writing" ); | ||||||||||
| 102 | 0 | print $notesFile $source; | |||||||||
| 103 | 0 | $notesFile->close(); | |||||||||
| 104 | |||||||||||
| 105 | ############# | ||||||||||
| 106 | # compile it | ||||||||||
| 107 | |||||||||||
| 108 | # 4. run TeX | ||||||||||
| 109 | my $cmd = [ $self->{compiler}, | ||||||||||
| 110 | 0 | "-halt-on-error", "-interaction=nonstopmode", "-output-directory=$self->{pdf_dir}", "$notesFileName" ]; | |||||||||
| 111 | 0 | my $logFilename = $notesFileName; | |||||||||
| 112 | 0 | $logFilename =~ s/\.\w+$/.log/; | |||||||||
| 113 | |||||||||||
| 114 | |||||||||||
| 115 | |||||||||||
| 116 | 0 | my $counter = 0; | |||||||||
| 117 | 0 | my $progress = MCE::Shared::Scalar->new( 0 ); | |||||||||
| 118 | BeamerReveal::IPC::Run::runsmart( $cmd, 1, qr/\[(\d+)\]/, | ||||||||||
| 119 | 0 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||
| 120 | 0 | ▶ | while( scalar @_ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 121 | 0 | my $a = shift @_; | |||||||||
| 122 | 0 | ▶ | while( $a > $counter ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 123 | 0 | ++$counter; | |||||||||
| 124 | 0 | $progress->incr(); | |||||||||
| 125 | $logger->progress( $self->{progressId}, | ||||||||||
| 126 | 0 | $progress->get(), "pdf generation" ); | |||||||||
| 127 | } | ||||||||||
| 128 | } | ||||||||||
| 129 | }, | ||||||||||
| 130 | 0 | 1, # coreId | |||||||||
| 131 | 2, # indent | ||||||||||
| 132 | undef, # directory | ||||||||||
| 133 | "Error: notes generation failed: check $logFilename" | ||||||||||
| 134 | ); | ||||||||||
| 135 | |||||||||||
| 136 | #################### | ||||||||||
| 137 | # convert it to jpg | ||||||||||
| 138 | 0 | $notesFileName =~ s/\.\w+$/.pdf/; | |||||||||
| 139 | $cmd = [ $self->{pdftoppm}, | ||||||||||
| 140 | $notesFileName, | ||||||||||
| 141 | "$self->{output_dir}/$self->{slides}/notes", | ||||||||||
| 142 | '-jpeg', | ||||||||||
| 143 | '-jpegopt', | ||||||||||
| 144 | 'optimize=y,quality=85', | ||||||||||
| 145 | 0 | '-scale-to-x', @{[1.5*$self->{xres}]}, | |||||||||
| 146 | 0 | '-scale-to-y', @{[1.5*$self->{yres}]}, | |||||||||
| 147 | '-progress', | ||||||||||
| 148 | ]; | ||||||||||
| 149 | 0 | my $maxNote = 0; | |||||||||
| 150 | BeamerReveal::IPC::Run::runsmart( $cmd, 2, qr/^(\d+) (\d+) .*$/, | ||||||||||
| 151 | 0 | ▶ | sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 152 | 0 | ▶ | while( scalar @_ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 153 | 0 | my ( $a, $b ) = ( shift @_, shift @_ ); | |||||||||
| 154 | $logger->progress( $self->{progressId}, | ||||||||||
| 155 | 0 | $a, "note $1/$2", $b ); | |||||||||
| 156 | 0 | $maxNote = $b; | |||||||||
| 157 | } | ||||||||||
| 158 | }, | ||||||||||
| 159 | 0 | 0, # coreId | |||||||||
| 160 | 2, # indent | ||||||||||
| 161 | undef, # directory | ||||||||||
| 162 | "Error: notes conversion failed, is your notes PDF damaged?" | ||||||||||
| 163 | ); | ||||||||||
| 164 | |||||||||||
| 165 | 0 | $notesFileName =~ s/\.\w+$//; | |||||||||
| 166 | 0 | foreach my $file ( glob( "$notesFileName*" ) ) { | |||||||||
| 167 | 0 | ▶ | unlink $file unless( defined $self->{debug} ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 168 | } | ||||||||||
| 169 | |||||||||||
| 170 | 0 | my @list = ( 0 ); # create a dummy element to allow for one-indexed addressing | |||||||||
| 171 | |||||||||||
| 172 | 0 | ▶ | for (my $i = 1; $i <= $maxNote; ++$i ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 173 | 0 | push @list, sprintf( "$self->{slides}/notes-%0" . nofdigits( $maxNote ) . 'd.jpg', | |||||||||
| 174 | $i ); | ||||||||||
| 175 | } | ||||||||||
| 176 | 0 | return \@list; | |||||||||
| 177 | } | ||||||||||
| 178 | |||||||||||
| 179 | 1 | 1; | |||||||||
| 180 | |||||||||||
| 181 | __END__ | ||||||||||