Skip to content

Commit 67120d9

Browse files
committed
basic implementation of -f
1 parent 03bfa61 commit 67120d9

File tree

1 file changed

+67
-11
lines changed

1 file changed

+67
-11
lines changed

bin/p6doc

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
#!/usr/bin/env perl6
22

3+
# die with printing a backtrace
4+
my class X::P6doc is Exception {
5+
has $.message;
6+
multi method gist(X::P6doc:D:) {
7+
self.message;
8+
}
9+
}
10+
311
constant DEBUG = %*ENV<P6DOC_DEBUG>;
412
sub findbin() returns Str {
513
$*PROGRAM_NAME.subst(rx{<-[/]>+$}, '');
614
}
15+
sub tempfile() {
16+
my $tempfile = %*ENV<TEMP> // %*ENV<TMP> // '/tmp';
17+
$tempfile ~= '/';
18+
$tempfile ~= join '', ('a'..'z', 0..9).roll(5);
19+
$tempfile ~= '.tmp';
20+
nextsame if $tempfile.IO.e;
21+
return $tempfile;
22+
}
23+
724
sub search-paths() {
825
findbin() ~ '../doc/perl6/lib/',
926
findbin() ~ '../lib/',
@@ -16,7 +33,47 @@ sub module-names(Str $modulename) {
1633
}
1734

1835
sub locate-module(Str $modulename) {
19-
return (search-paths() X~ module-names($modulename)).first: *.IO.f;
36+
my $m = (search-paths() X~ module-names($modulename)).first: *.IO.f;
37+
unless $m.defined {
38+
my $message = join "\n",
39+
"Cannot locate $modulename in any of the following paths:",
40+
search-paths.map({" $_"});
41+
X::P6doc.new(:$message).throw;
42+
}
43+
return $m;
44+
}
45+
46+
sub extract-section-from-file(Str $filename, Str $section) {
47+
my $f = open :r, $filename;
48+
49+
my $temp-filename = tempfile();
50+
LEAVE $f.close;
51+
52+
my $temp-fh = open :w, $temp-filename;
53+
$temp-fh.say: '=begin pod';
54+
$temp-fh.say: '';
55+
LEAVE $temp-fh.close;
56+
UNDO unlink $temp-fh;
57+
58+
my $found-start = False;
59+
my $start-re = rx/ ^ '=head' (\d+) \s+ $section >>/;
60+
my $heading-level;
61+
my $stop-re = rx/ ^ [ '=head' (\d+) <?{ $0 <= $heading-level }> | '=end pod' ] /;
62+
for $f.lines {
63+
last if $found-start && ($_ ~~ $stop-re);
64+
if $found-start {
65+
$temp-fh.say: $_;
66+
}
67+
elsif $_ ~~ $start-re {
68+
$temp-fh.say: $_;
69+
$found-start = True;
70+
$heading-level = +$0;
71+
}
72+
}
73+
$temp-fh.say: '';
74+
$temp-fh.say: '=end pod';
75+
X::P6doc.new(message => "No documentation found for '$section' in file '$filename'").throw unless $found-start;
76+
return $temp-filename;
2077
}
2178
2279
sub show-docs(Str $path) {
@@ -30,20 +87,19 @@ multi sub MAIN() {
3087
3188
multi sub MAIN($docee) {
3289
return MAIN($docee, :f) if defined $docee.index('.') ;
33-
my $m = locate-module($docee);
34-
if $m.defined {
35-
show-docs($m);
36-
}
37-
else {
38-
say "Cannot locate $docee in any of the following paths:";
39-
say " $_" for search-paths();
40-
exit 1;
41-
}
90+
show-docs(locate-module($docee));
4291
}
4392
4493
multi sub MAIN($docee, Bool :$f!) {
4594
my ($package, $method) = $docee.split('.');
46-
say '-f is not yet implemented, sorry';
95+
unless $method {
96+
say 'unqualified sub and method names are not yet implemented';
97+
say 'For example use "p6doc -f Str.split" instead of "p6doc -f split" for now';
98+
exit 2;
99+
}
100+
my $m = locate-module($package);
101+
$m = extract-section-from-file($m, $method);
102+
show-docs($m);
47103
}
48104
49105

0 commit comments

Comments
 (0)