-
Notifications
You must be signed in to change notification settings - Fork 21
/
perldoc-browser.pl
executable file
·168 lines (143 loc) · 6.47 KB
/
perldoc-browser.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env perl
# This software is Copyright (c) 2018 by Dan Book <[email protected]>.
# This is free software, licensed under:
# The Artistic License 2.0 (GPL Compatible)
use 5.020;
my @current_inc;
BEGIN { @current_inc = grep { !ref and $_ ne '.' } @INC }
use Mojolicious::Lite;
use Config;
use File::Spec;
use IPC::Run3;
use Mojo::File 'path';
use Mojo::Util 'dumper';
use Sort::Versions;
use version;
use experimental 'signatures';
use lib::relative 'lib';
app->attr('search_backend');
push @{app->commands->namespaces}, 'PerldocBrowser::Command';
push @{app->plugins->namespaces}, 'PerldocBrowser::Plugin';
plugin Config => {file => 'perldoc-browser.conf', default => {}};
if (defined(my $logfile = app->config->{logfile})) {
app->log->with_roles('+Clearable')->path($logfile);
}
my %inc_dirs;
helper warmup_inc_dirs => sub ($c, $perl_version) {
my $bin = $c->perls_dir->child($perl_version, 'bin', 'perl');
local $ENV{PERLLIB} = '';
local $ENV{PERL5LIB} = '';
local $ENV{PERL5OPT} = '';
# Regular @INC dirs, $installprivlib/pod will be included by Pod::Simple::Search
# $installprivlib/pods is used on some architectures
# scriptdir is not automatically included when specifying dirs
run3 [$bin, '-MConfig', '-MFile::Spec', '-e',
'print "$_\n" for @INC, File::Spec->catdir($Config{installprivlib}, "pods"), $Config{scriptdir}'], undef, \my @output;
my $exit = $? >> 8;
die "Failed to retrieve include directories for $bin (exit $exit)\n" if $exit;
chomp @output;
return $inc_dirs{$perl_version} = [grep { length $_ && $_ ne '.' } @output];
};
helper inc_dirs => sub ($c, $perl_version) { $inc_dirs{$perl_version} // [] };
my %version_objects;
helper warmup_version_object => sub ($c, $perl_version) {
my $bin = $c->perls_dir->child($perl_version, 'bin', 'perl');
local $ENV{PERL5OPT} = '';
run3 [$bin, '-e', 'print "$]\n"'], undef, \my $output;
chomp $output;
return $version_objects{$perl_version} = version->parse($output);
};
helper perl_version_object => sub ($c, $perl_version) { $version_objects{$perl_version} };
my %function_descriptions;
helper warmup_function_descs => sub ($c, $perl_version) {
my $bin = $c->perls_dir->child($perl_version, 'bin', 'perl');
local $ENV{PERL5OPT} = '';
run3 [$bin, '-MPod::Functions', '-e', 'print "$_ $Flavor{$_}\n" for sort keys %Flavor'], undef, \my @output;
chomp @output;
my (@function_names, %descriptions);
foreach my $line (@output) {
my ($name, $desc) = split ' ', $line, 2;
push @function_names, $name;
$descriptions{$name} = $desc;
}
return $function_descriptions{$perl_version} = {names => \@function_names, descriptions => \%descriptions};
};
helper function_names => sub ($c, $perl_version) { $function_descriptions{$perl_version}{names} };
helper function_descriptions => sub ($c, $perl_version) { $function_descriptions{$perl_version}{descriptions} };
helper function_description => sub ($c, $perl_version, $name) { $function_descriptions{$perl_version}{descriptions}{$name} };
my $perls_dir = path(app->config->{perls_dir} // app->home->child('perls'));
helper perls_dir => sub ($c) { $perls_dir };
my (@all_versions, @perl_versions, @dev_versions, %version_is_dev, $latest_version);
helper warmup_perl_versions => sub ($c) {
@all_versions = -d $c->perls_dir ? $c->perls_dir->list({dir => 1})
->grep(sub { -d && -x path($_)->child('bin', 'perl') })
->map(sub { $_->basename })
->sort(sub { versioncmp($b, $a) })->each : ();
(@perl_versions, @dev_versions, %version_is_dev) = ();
$latest_version = app->config->{latest_perl_version};
if (@all_versions) {
foreach my $perl_version (@all_versions) {
my $v = app->warmup_version_object($perl_version);
if ($perl_version eq 'blead' or $perl_version =~ m/-RC[0-9]+$/
or ($v < version->parse('v5.6.0') and ($v->{version}[2] // 0) >= 500)
or ($v >= version->parse('v5.6.0') and ($v->{version}[1] // 0) % 2)) {
push @dev_versions, $perl_version;
$version_is_dev{$perl_version} = 1;
} else {
push @perl_versions, $perl_version;
$version_is_dev{$perl_version} = 0;
$latest_version //= $perl_version if defined $v;
}
app->warmup_inc_dirs($perl_version);
app->warmup_function_descs($perl_version);
}
$latest_version //= $all_versions[0];
} else {
my $current_version = $Config{version};
if ($Config{PERL_VERSION} % 2) {
push @dev_versions, $current_version;
$version_is_dev{$current_version} = 1;
} else {
push @perl_versions, $current_version;
$version_is_dev{$current_version} = 0;
}
@all_versions = $current_version;
$latest_version //= $current_version;
$inc_dirs{$current_version} = [@current_inc, File::Spec->catdir($Config{installprivlib}, 'pods'), $Config{scriptdir}];
if (eval { require Pod::Functions; 1 }) {
my @function_names = sort keys %Pod::Functions::Flavor;
my %descriptions = map { ($_ => $Pod::Functions::Flavor{$_}) } @function_names;
$function_descriptions{$current_version} = {names => \@function_names, descriptions => \%descriptions};
}
}
};
helper all_perl_versions => sub ($c) { [@all_versions] };
helper perl_versions => sub ($c) { [@perl_versions] };
helper dev_versions => sub ($c) { [@dev_versions] };
helper latest_perl_version => sub ($c) { $latest_version };
helper perl_version_is_dev => sub ($c, $perl_version) { $version_is_dev{$perl_version} };
app->warmup_perl_versions;
my $csp = join '; ',
q{default-src 'self'},
q{connect-src 'self' *.google-analytics.com},
q{img-src 'self' data: www.google-analytics.com www.googletagmanager.com},
q{script-src 'self' 'unsafe-inline' cdnjs.cloudflare.com code.jquery.com stackpath.bootstrapcdn.com www.google-analytics.com www.googletagmanager.com},
q{style-src 'self' 'unsafe-inline' cdnjs.cloudflare.com stackpath.bootstrapcdn.com},
q{report-uri /csp-reports};
hook after_render => sub ($c, @) { $c->res->headers->content_security_policy($csp) };
post '/csp-reports' => sub ($c) {
if (defined(my $violation = $c->req->json)) {
my $serialized = dumper $violation;
$c->app->log->error("CSP violation: $serialized");
}
$c->render(data => '');
};
any '/opensearch' => [format => ['xml']];
plugin 'PerldocSearch';
plugin 'PerldocRenderer';
plugin 'PerldocInstall';
# needs renderer helpers available
my $footer_src = join "\n\n", '=encoding utf8', @{app->config->{footer_pod} // app->config->{contact_pod} // []};
my $footer_html = app->pod_to_html($footer_src);
helper footer_html => sub ($c) { $footer_html };
app->start;