App/lcpan/Cmd/doc.pm

filter: Statementstmt 19.3% 19 / 98 Branchbran 0.0% 0 / 46 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 44.4% 4 / 9 Podpod 100% 1 / 1 totaltotal 15.5% 24 / 154 CC 25 SCAR 62.0
CC
25
Cov
13%
handle_cmd
650.0
__ANON__
2.0
__ANON__
2.0
CRAP 492.8
« App/lcpan/Cmd/contents.pm ↑ Summary App/lcpan/Cmd/scripts.pm »
1 package App::lcpan::Cmd::doc;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Encode qw(decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 require App::lcpan;
10  
11 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
12 2 our $DATE = '2023-09-26'; # DATE
13 2 our $DIST = 'App-lcpan'; # DIST
14 2 our $VERSION = '1.074'; # VERSION
15  
16 2 our %SPEC;
17  
18 $SPEC{'handle_cmd'} = {
19 v => 1.1,
20 summary => 'Show POD documentation of module/POD/script',
21 description => <<'_',
22  
23 This command extracts module (.pm)/.pod/script from release tarballs and render
24 its POD documentation. Since the documentation is retrieved from the release
25 tarballs in the mirror, the module/.pod/script needs not be installed.
26  
27 Note that currently this command has trouble finding documentation for core
28 modules because those are contained in perl release tarballs instead of release
29 tarballs of modules, and `lcpan` is currently not designed to work with those.
30  
31 _
32 args => {
33 %App::lcpan::common_args,
34 name => {
35 summary => 'Module or script name',
36 description => <<'_',
37  
38 If the name matches both module name and script name, the module will be chosen.
39 To choose the script, use `--script` (`-s`).
40  
41 _
42 schema => 'str*',
43 req => 1,
44 pos => 0,
45 completion => \&App::lcpan::_complete_content_package_or_script,
46 },
47 script => {
48 summary => 'Look for script first',
49 schema => ['bool', is=>1],
50 cmdline_aliases => {s=>{}},
51 },
52 format => {
53 schema => ['str*', in=>[qw/raw html man/]],
54 default => 'man',
55 cmdline_aliases => {
56 raw => {
57 summary => 'Dump raw POD instead of rendering it',
58 is_flag => 1,
59 0 ▶ code => sub { $_[0]{format} = 'raw' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 },
61 r => {
62 summary => 'Same as --raw',
63 is_flag => 1,
64 0 ▶ code => sub { $_[0]{format} = 'raw' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
65 },
66 html => {
67 summary => 'Show HTML documentation in browser instead of rendering as man',
68 is_flag => 1,
69 0 ▶ code => sub { $_[0]{format} = 'html' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
70 },
71 man => {
72 summary => 'Read as manpage (the default)',
73 is_flag => 1,
74 0 ▶ code => sub { $_[0]{format} = 'man' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 },
76 },
77 2 tags => ['category:output'],
78 },
79 },
80 args_rels => {
81 },
82 examples => [
83 {
84 summary => 'Seach module/POD/script named Rinci',
85 argv => ['Rinci'],
86 test => 0,
87 'x.doc.show_result' => 0,
88 },
89 {
90 summary => 'Specifically choose .pm file',
91 argv => ['Rinci.pm'],
92 test => 0,
93 'x.doc.show_result' => 0,
94 },
95 {
96 summary => 'Specifically choose .pod file',
97 argv => ['Rinci.pod'],
98 test => 0,
99 'x.doc.show_result' => 0,
100 },
101 {
102 summary => 'Look for script first named strict',
103 argv => ['-s', 'strict'],
104 test => 0,
105 'x.doc.show_result' => 0,
106 },
107 {
108 summary => 'Dump the raw POD instead of rendering it',
109 argv => ['--raw', 'Text::Table::Tiny'],
110 test => 0,
111 'x.doc.show_result' => 0,
112 },
113 # filter arg: rel to pick a specific release file
114 ],
115 deps => {
116 all => [
117 {prog => 'pod2man'}, # XXX only when format=man
118 {prog => 'pod2html'}, # XXX only when format=html
119 ],
120 },
121 };
122 0 ▶sub handle_cmd {
Subroutine handle_cmd: not called CC=25 SCAR=64.8
Pod documented
123 0 my %args = @_;
124  
125 0 my $state = App::lcpan::_init(\%args, 'ro');
126 0 my $dbh = $state->{dbh};
127  
128 0 my $name = $args{name};
129 0 my $ext = '';
130 0 $name =~ s!/!::!g;
131 0 ▶ $ext = $1 if $name =~ s/\.(pm|pod)\z//;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ s/\.(pm|pod)\z// 0 0 0
132  
133 0 my @look_order;
134 0 ▶ if ($args{script}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'script'}) { } 0 0 0
135 0 @look_order = ('script', 'module');
136 } else {
137 0 @look_order = ('module', 'script');
138 }
139  
140 0 my $row;
141 LOOK:
142 0 for my $look (@look_order) {
143 0 my @where;
144 0 my @bind = ($name);
145 0 ▶ if ($look eq 'module') {
Branch: 2 branches
BranchTrueFalseTotal
if ($look eq 'module') { } 0 0 0
elsif ($look eq 'script') { } 0 0 0
146  
147 0 push @where, "package=?";
148 0 ▶ if ($ext eq 'pm') {
Branch: 2 branches
BranchTrueFalseTotal
if ($ext eq 'pm') { } 0 0 0
elsif ($ext eq 'pod') { } 0 0 0
149 0 push @where, "path LIKE '%.pm'";
150 } elsif ($ext eq 'pod') {
151 0 push @where, "path LIKE '%.pod'";
152 }
153 0 push @where, ("NOT(file.name LIKE '%-Lumped-%')"); # tmp
154 0 ▶ $row = $dbh->selectrow_hashref("SELECT
Branch: 1 branch
BranchTrueFalseTotal
@where ? : 0 0 0
155 content.path content_path,
156 file.cpanid author,
157 file.name release
158 FROM content
159 LEFT JOIN file ON content.file_id=file.id
160 ".(@where ? " WHERE ".join(" AND ", @where) : "")."
161 ORDER BY content.size DESC
162 LIMIT 1", {}, @bind);
163 0 ▶ last LOOK if $row;
Branch: 1 branch
BranchTrueFalseTotal
if $row 0 0 0
164  
165 0 ▶ if ($ext eq 'pod') {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext eq "pod") 0 0 0
166 # .pod doesn't always declare package so we also try to guess
167 # from content path
168 0 $name =~ s!::!/!g; $name .= ".pod";
169 0 @where = ("content.path LIKE ?");
170 0 push @where, ("NOT(file.name LIKE '%-Lumped-%')"); # tmp
171 0 @bind = ("%$name");
172  
173 0 ▶ my $sth = $dbh->prepare("SELECT
Branch: 1 branch
BranchTrueFalseTotal
@where ? : 0 0 0
174 content.path content_path,
175 file.cpanid author,
176 file.name release
177 FROM content
178 LEFT JOIN file ON content.file_id=file.id
179 ".(@where ? " WHERE ".join(" AND ", @where) : "")."
180 ORDER BY content.size DESC");
181 0 $sth->execute(@bind);
182 0 ▶ while (my $r = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $sth->fetchrow_hashref) 0 0 0
183 0 ▶ if ($r->{content_path} =~ m!^[^/]+/\Q$name\E$!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"content_path"} =~ m[^[^/]+/\Q$name\E$]) 0 0 0
184 0 $row = $r;
185 0 last LOOK;
186 }
187 }
188 }
189  
190 } elsif ($look eq 'script') {
191  
192 0 push @where, "script.name=?";
193 0 push @where, ("NOT(file.name LIKE '%-Lumped-%')"); # tmp
194 0 ▶ $row = $dbh->selectrow_hashref("SELECT
Branch: 1 branch
BranchTrueFalseTotal
@where ? : 0 0 0
195 content.path content_path,
196 file.cpanid author,
197 file.name release
198 FROM script
199 LEFT JOIN file ON script.file_id=file.id
200 LEFT JOIN content ON script.content_id=content.id
201 ".(@where ? " WHERE ".join(" AND ", @where) : "")."
202 ORDER BY content.size DESC
203 LIMIT 1", {}, @bind);
204 0 ▶ last LOOK if $row;
Branch: 1 branch
BranchTrueFalseTotal
if $row 0 0 0
205  
206 }
207 }
208  
209 0 ▶ return [404, "No such module/POD/script"] unless $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row 0 0 0
210  
211 my $path = App::lcpan::_fullpath(
212 0 $row->{release}, $state->{cpan}, $row->{author});
213  
214 # XXX needs to be refactored into common code
215 0 my $content;
216 0 ▶ if ($path =~ /\.zip$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /\.zip$/i) { } 0 0 0
217 0 require Archive::Zip;
218 0 my $zip = Archive::Zip->new;
219 0 ▶ $zip->read($path) == Archive::Zip::AZ_OK()
Branch: 1 branch
BranchTrueFalseTotal
$zip->read($path) == Archive::Zip::AZ_OK() or return [500, "Can't read zip file '${path}'"] 0 0 0
220 or return [500, "Can't read zip file '$path'"];
221 0 $content = $zip->contents($row->{content_path});
222 } else {
223 0 require Archive::Tar;
224 0 my $tar;
225 0 eval {
226 0 $tar = Archive::Tar->new;
227 0 $content = $tar->read($path); # can still die untrapped when out of mem
228 };
229 0 ▶ return [500, "Can't read tar file '$path': $@"] if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
230 0 my ($obj) = $tar->get_files($row->{content_path});
231 0 $content = $obj->get_content;
232 }
233  
234 0 ▶ if ($content =~ /^=encoding\s+(utf-?8)/im) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /^=encoding\s+(utf-?8)/im) 0 0 0
235 # doesn't seem necessary
236 #$content = decode('utf8', $content, Encode::FB_CROAK);
237 }
238  
239 0 ▶ if ($args{format} eq 'raw') {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'format'} eq 'raw') { } 0 0 0
elsif ($args{'format'} eq 'html') { } 0 0 0
240 0 return [200, "OK", $content, {
241 "cmdline.page_result"=>1,
242 'cmdline.skip_format'=>1,
243 }];
244 } elsif ($args{format} eq 'html') {
245 0 require Browser::Open;
246 0 require File::Slurper;
247 0 require File::Temp;
248 0 require File::Util::Tempdir;
249  
250 0 my $tmpdir = File::Util::Tempdir::get_tempdir();
251 0 my $cachedir = File::Temp::tempdir(CLEANUP => 1);
252 0 my $name = $name; $name =~ s/:+/_/g;
253 0 my ($infh, $infile) = File::Temp::tempfile(
254 "$name.XXXXXXXX", DIR=>$tmpdir, SUFFIX=>".pod");
255 0 my $outfile = "$infile.html";
256 0 File::Slurper::write_binary($infile, $content);
257 0 system(
258 "pod2html",
259 "--infile", $infile,
260 "--outfile", $outfile,
261 "--cachedir", $cachedir,
262 );
263 0 ▶ return [500, "Can't pod2html: $!"] if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 0 0
264 0 my $err = Browser::Open::open_browser("file:$outfile");
265 0 ▶ return [500, "Can't open browser"] if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 0 0
266 0 [200];
267 } else {
268 0 return [200, "OK", $content, {
269 "cmdline.page_result"=>1,
270 "cmdline.pager"=>"pod2man -u | man -l -"}];
271 }
272 }
273  
274 2 1;
275 # ABSTRACT: Show POD documentation of module/POD/script
276  
277 __END__
« App/lcpan/Cmd/contents.pm ↑ Summary App/lcpan/Cmd/scripts.pm »