in reply to PDL Book as epub?

Newest version (2024-10-18), this now includes the source code file from the "Graphics with PLplot" chapter into the epub.

#!/usr/bin/env perl use v5.36; use strict; use warnings; use Carp; use App::Pod2Epub; #use Archive::Zip; use GD; use MIME::Base64; use English; my $parser = App::Pod2Epub->new(); my @full; my @subfiles; { # Read and parse TOC pod my @lines = _readFile('PDL/Book.pod'); my $istoc = 0; foreach my $line (@lines) { if($line =~ /^\=over/) { $istoc = 1; #push @full, $line; next; } elsif($line =~ /^\=back/) { $istoc = 0; #push @full, $line; next; } elsif($istoc && $line =~ /^\=item/) { my $subfile = ''; if($line =~ /\<(.+?)\|/) { $subfile = $1; $line =~ s/.*\>\ //; $subfile =~ s/\:\:/\//g; #next if($subfile =~ /PLplot/); $subfile .= '.pod'; if(!-f $subfile) { warn("$subfile does not exist"); next; } else { push @subfiles, $subfile; } } push @full, $line; next; } push @full, $line; } push @full, ''; } { # Process chapters foreach my $subfile (@subfiles) { my @lines = _readFile($subfile); foreach my $line (@lines) { if($line =~ /^\=for\ html/) { if($line =~ /\<img.*src\=\"(.+?)\"/) { my $img = 'PDL/Book/' . $1; if(-f $img) { my $imgdata = _slurpBinFile($img); $imgdata = encode_base64($imgdata, ''); my $imggd = GD::Image->new($img); my ($width, $height) = $imggd->getBounds(); my $realline = '=for html <img width="' . $wid +th . '" height="' . $height . '" src="data:image/png;base64, ' . $img +data . '"/>'; push @full, $realline; } else { warn("Missing image $img in file $subfile"); push @full, "Missing image: $img"; } next; } elsif($line =~ /\<a.*href\=\"(.+?)\"/) { my $rawfname = $1; my $fnameonly = '' . $rawfname; $fnameonly =~ s/^.*\///g; my $pathonly = '' . $rawfname; $pathonly =~ s/\/(.+?)$//g; push @full, ''; push @full, '=head4 Source code ' . $fnameonly; push @full, ''; my $realpath = 'PDL/Book/' . $pathonly . '/work/' +. $fnameonly; open(my $srcfh, '<', $realpath) or croak("File not + found: $realpath"); while((my $srcline = <$srcfh>)) { chomp $srcline; push @full, ' ' . $srcline; } close $srcfh; push @full, ''; push @full, '=over'; push @full, ''; next; } else { warn("Unknown 'for html' tag: $line in $subfile"); next; } } push @full, $line; } push @full, ''; } } { # Write complete POD file open(my $ofh, '>', 'complete_book.pod') or croak($!); foreach my $line (@full) { print $ofh $line, "\n"; } close $ofh; } if(0){ # Convert file to epub open(my $ifh, '<', 'complete_book.pod') or croak($!); open(my $ofh, '>', 'complete_book.epub') or croak($!); binmode $ofh; $parser->output_fh($ofh); $parser->parse_file($ifh); close $ifh; close $ofh; } `pod2epub complete_book.pod -o complete_book.epub`; #{ # Add images to ZIP file # my $zip = Archive::Zip->new('complete_book.epub'); # foreach my $img (@images) { # $zip->addFile('PDL/Book/' . $img => $img); # } # $zip->overwrite(); #} sub _readFile($fname) { my @lines; open(my $ifh, '<', $fname) or croak($!); while((my $line = <$ifh>)) { chomp $line; push @lines, $line; } close $ifh; return @lines; } sub _slurpBinFile($fname) { # Read in file in binary mode, slurping it into a single scalar. # We have to make sure we use binmode *and* turn off the line term +ination variable completly # to work around the multiple idiosynchrasies of Perl on Windows open(my $fh, "<", $fname) or croak("$ERRNO"); local $INPUT_RECORD_SEPARATOR = undef; binmode($fh); my $data = <$fh>; close($fh); return $data; }

PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
Also check out my sisters artwork and my weekly webcomics