forked from jonallen/perldoc.perl.org
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild-perldoc-pdf.pl
executable file
·112 lines (82 loc) · 2.9 KB
/
build-perldoc-pdf.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#! /usr/bin/perl
use strict;
use warnings;
use File::Basename;
use File::Path;
use FindBin qw/$Bin/;
use Getopt::Long;
use lib "$Bin/lib";
use Perldoc::Config;
#--Set config options-----------------------------------------------------
my %specifiers = (
'output-path' => '=s',
'perl' => '=s',
);
my %options;
GetOptions( \%options, optionspec(%specifiers) );
#--Check mandatory options have been given--------------------------------
my @mandatory_options = qw/ output-path /;
foreach (@mandatory_options) {
(my $option = $_) =~ tr/-/_/;
unless ($options{$option}) {
die "Option '$_' must be specified!\n";
}
}
#--Check the output path exists-------------------------------------------
unless (-d $options{output_path}) {
die "Output path '$options{output_path}' does not exist!\n";
}
$Perldoc::Config::option{output_path} = $options{output_path};
#--Check if we are using a different perl----------------------------------
if ($options{perl}) {
#warn "Setting perl to $options{perl}\n";
my $version_cmd = 'printf("%vd",$^V)';
my $perl_version = `$options{perl} -e '$version_cmd'`;
my $inc_cmd = 'print "$_\n" foreach @INC';
my $perl_inc = `$options{perl} -e '$inc_cmd'`;
my $bin_cmd = 'use Config; print $Config{bin}';
my $perl_bin = `$options{perl} -e '$bin_cmd'`;
$Perldoc::Config::option{perl_version} = $perl_version;
$Perldoc::Config::option{perl5_version} = substr($perl_version,2);
$Perldoc::Config::option{inc} = [split /\n/,$perl_inc];
$Perldoc::Config::option{bin} = $perl_bin;
#warn Dumper(\%Perldoc::Config::option);
}
eval <<EOT;
use Perldoc::Page;
use Perldoc::Page::Convert;
use Perldoc::Section;
EOT
die $@ if $@;
#--Convert pages to PDF---------------------------------------------------
my @pages = grep {exists $Perldoc::Page::CoreList{$_}} Perldoc::Page::list();
foreach my $section (Perldoc::Section::list()) {
push @pages,Perldoc::Section::pages($section);
}
my %pages = map {$_,1} @pages;
@pages = keys %pages;
foreach my $page (@pages) {
(my $filename = "$Perldoc::Config::option{output_path}/$page.pdf") =~ s!::!/!g;
check_filepath($filename);
#print "$page\n";
warn("Converting $page to PDF as $filename\n");
open OUTPUT,'>',$filename or die("Cannot open file '$filename': $!\n");
print OUTPUT Perldoc::Page::Convert::pdf($page);
close OUTPUT;
}
sub check_filepath {
my $filename = shift;
my $directory = dirname($filename);
mkpath $directory unless (-d $directory);
}
sub optionspec {
my %option_specs = @_;
my @getopt_list;
while (my ($option_name,$spec) = each %option_specs) {
(my $variable_name = $option_name) =~ tr/-/_/;
(my $nospace_name = $option_name) =~ s/-//g;
my $getopt_name = ($variable_name ne $option_name) ? "$variable_name|$option_name|$nospace_name" : $option_name;
push @getopt_list,"$getopt_name$spec";
}
return @getopt_list;
}