FunctionalPerl

 view release on metacpan or  search on metacpan

examples/pdf-to-html  view on Meta::CPAN

#!/usr/bin/env perl

# Copyright (c) 2015-2020 Christian Jaeger, copying@christianjaeger.ch
# This is free software. See the file COPYING.md that came bundled
# with this file.

use strict;
use warnings;
use warnings FATAL => 'uninitialized';
use experimental "signatures";

# find modules from functional-perl working directory (not installed)
use Cwd 'abs_path';
our ($mydir, $myname);

BEGIN {
    my $location = (-l $0) ? abs_path($0) : $0;
    $location =~ /(.*?)([^\/]+?)_?\z/s or die "?";
    ($mydir, $myname) = ($1, $2);
}
use lib "$mydir/../lib";

sub usage {
    print "usage: $myname file.pdf [file2.pdf ..]

   Convert a pdf file to SVG images (by way of `pdf2svg`) and a set of
   html pages embedding them.

   Options:
    --single   create a single html page with all pages (default: one
               page per html file)
    --outdir   default: file path with .pdf suffix stripped
";
    exit 1;
}

use Getopt::Long;
my $verbose = 0;
my $opt_single;
my $opt_outdir;
GetOptions(
    "verbose"     => \$verbose,
    "help"        => sub {usage},
    "single-page" => \$opt_single,
    "outdir=s"    => \$opt_outdir,
) or exit 1;

use FP::IOStream qw(xdirectory_paths);
use FP::List qw(list cons);
use FP::Stream qw(Keep);
use Chj::xperlfunc qw(xstat xxsystem_safe xunlink basename dirname);
use FP::Combinators qw(compose_scalar);
use FP::Ops qw(the_method real_cmp regex_match regex_xsubstitute);
use PXML::XHTML ':all';
use PXML::Serialize qw(puthtmlfile);
use FP::Array_sort qw(on);
use Chj::xIOUtil qw(xputfile_utf8);
use Chj::TEST ":all";
use FP::Div qw(min max);
use Chj::singlequote qw(quote_javascript);

sub note {
    print STDERR "$myname: note: ", @_, "\n";
}

sub css_link($src) {
    LINK({ rel => "stylesheet", href => $src, type => "text/css" })
}

# svgfile and html paths

our $svgfile_template = 'page-%02d.svg';
our $svgpath_re       = qr{(^|.*/)page-(\d+)\.svg$}s;
*svgpath_to_htmlpath = regex_xsubstitute($svgpath_re, sub {"$1/page-$2.html"});
*svgpath_to_pageno   = regex_xsubstitute($svgpath_re, sub { $2 +0 });

our $css_src = "$myname.css";

# CSS contents

my $css_code = '
ul.menu {
  border: 1px solid #000;
  background-color: #eee;
  padding: 5px;
  list-style: none;
  padding-left: 0.5em;
}
li.menu {
  border-right: 1px solid #000;
  list-style: none;
  padding-left: 0.5em;
  padding-right: 0.3em;
  display: inline;
}
li.menu_last {
  list-style: none;
  padding-left: 0.5em;
  padding-right: 0.3em;
  display: inline;
}
';

sub svgpaths($dir) {
    xdirectory_paths($dir)->filter(regex_match $svgpath_re)
        ->sort(on \&svgpath_to_pageno, \&real_cmp)
}

# ------------------------------------------------------------------
# file conversion

sub possibly_symlink ($old, $new) {
    symlink $old, $new or note "could not add symlink at '$new': $!";
}



( run in 0.631 second using v1.01-cache-2.11-cpan-501a3233654 )