Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^4: PDL Book as epub?

by cavac (Parson)
on Aug 12, 2024 at 15:28 UTC ( [id://11161018]=note: print w/replies, xml ) Need Help??


in reply to Re^3: PDL Book as epub?
in thread PDL Book as epub?

I hacked together a first, unfinished, largely untested prototype converter. This goes into the base directory of https://sourceforge.net/p/pdl/pdl-book/ci/master/tree/ as make_epub.pl:

#!/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; $subfile .= '.pod'; if(!-f $subfile) { warn("$subfile does not exist"); } 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; } 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 on the line termi +nation 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; }

Basically, it concatenates all chapters, and inlines all images. There's a lot more to do to make it pretty and more readable (especially the code sections) and fix some encoding issues. But i do get a mostly usable book with chapters, subchapters, pretty pictures and about 226 pages (default font size on my Boox ereader), including a chapter called "POD ERRORS" at the very end ;-)

Also, i still need to add all the .pl files as their own chapter at the very end of the book, so it's a COMPLETE reference.

Edit: Forgot to say, this is a work in progress, and more posts will follow. But it's a good start for what is, for me, equivalent to a "My First Epub generator" picture book ("with simple, large letters for a first forray into the world of written words")...

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

Replies are listed 'Best First'.
Re^5: PDL Book as epub?
by etj (Curate) on Aug 13, 2024 at 14:22 UTC
    A great start! Keep the updates coming. If you don't do this as part of your work, I'll make the .pls get incorporated inline as a "verbatim" section with suitable "figure" notes before, rather than as a chapter at the end.

    Also, when it's ready-ish, I'll ask you to make a PR (so you get credit!). The authoritative source for anything I can think of PDL-related that may have a presence on Sourceforge is under https://github.com/PDLPorters, including "the book" at https://github.com/PDLPorters/pdl-book which you'll see has recent updates from me.

      Still working on that. But until end of October, this will probably be on the back burner. I've got the biggest company event of the year coming up, and as every year, it's a doozy.

      In simple terms, my company not only creates cash register/point of sales software, we also provide on-premise cash register services for events(¹). In a couple of weeks, i'm doing the yearly three-weekend-Oktoberfest near Vienna. I've got two delivery vans worth of equipment to set up (boss put me in charge of the whole setup for the third year in a row), including a couple of kilometers of network cabling.

      Got i tight schedule, too. Can't start too early, because the tent wouldn't be up. Can't start too late, cause other equipment would prevent me from reaching the spots i need. For example: Next Tuesday afternoon the floor goes in for the main tent, on Thursday morning the beer tables get set up. If we can't mount all the wireless antennas on Wednesday, i'm in very big doodoo. That's the only time we can use the scissor lift to reach the 10 meter tent ceiling (yup, it's a big f...ing tent for like 3000 people). And when i say "we", it's mostly me and our new company apprentice...

      So yeah, work on the ePub project will probably resume in November, after the usual burn-out and sprouting-more-grey-hair period.


      (¹) So if you ever host an event in Austria and you need cash registers (sales, gastronomy, etc), give me a holler.

      (²) Forget to mess up your work that ONE time, and now you have to do it every time... ;-)

      PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP
      Also check out my sisters artwork and my weekly webcomics
        Sounds like quite a challenge - good luck!

        One thing that might help with the tight window when you can do stuff, is to prepare with your apprentice, laying stuff out so you can pack it in reverse order of when you need it. But you probably already know this well :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11161018]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-09-13 17:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (21 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.