-
-
Save ollej/5037629 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use Modern::Perl; | |
BEGIN { | |
if (@ARGV != 2) { | |
print STDERR "usage: $0 DIR OUTPUT\n"; | |
exit 1; | |
} | |
} | |
{ | |
# a simple class that uses App::pod2pdf to create pdfs containing many pod files | |
# The hierarchical outline is generated on the fly while traversing directories | |
package MultiPDF; | |
use Moose; | |
use Path::Class; | |
use App::pod2pdf; | |
use Pod::Find qw(contains_pod); | |
has parser => ( | |
is => 'ro', | |
isa => 'App::pod2pdf', | |
lazy => 1, | |
default => sub { App::pod2pdf->new }, | |
); | |
has pdf => ( | |
is => 'ro', | |
isa => 'PDF::API2', | |
lazy => 1, | |
builder => '_build_pdf', | |
); | |
sub _build_pdf { | |
my $self = shift; | |
$self->parser->{pdf}, | |
} | |
sub process_file { | |
my $self = shift; | |
my $file = shift; | |
say "processing file: $file"; | |
$self->parser->parse_from_file($file->stringify); | |
$self->parser->formfeed; | |
return $self; | |
} | |
sub process_dir { | |
my $self = shift; | |
my $dir = Path::Class::Dir->new(shift); | |
my %structure; # { _outline => PDF::API2::Outline } | |
$dir->recurse( | |
depthfirst => 1, | |
callback => sub { | |
my $file = shift; | |
return if !-f $file || $file->basename !~ m{[.](?:pm|pod) \z}xms; | |
return if !contains_pod($file->stringify); | |
my $nr_pages = $self->pdf->pages; | |
$self->process_file($file); | |
my $name = $file->basename; | |
$name =~ s{[.]\w+ \z}{}xms; | |
my $tree = \%structure; | |
my $outline = $structure{_outline} ||= $self->pdf->outlines->outline; | |
foreach my $part (grep { $_ ne '.' } $file->relative($dir)->dir->dir_list, $name) { | |
$tree = $tree->{$part} ||= { _outline => $outline->outline }; | |
$outline = $tree->{_outline}; | |
$outline->title($part); | |
} | |
$outline->dest($self->pdf->openpage($nr_pages)); | |
} | |
); | |
return $self; | |
} | |
sub print { | |
my $self = shift; | |
$self->parser->output; | |
} | |
sub save_as { | |
my $self = shift; | |
my $path = shift; | |
$self->pdf->saveas($path); | |
} | |
} | |
MultiPDF->new | |
->process_dir($ARGV[0]) | |
->save_as($ARGV[1]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment