3 # naive version of perldoc implemented in and for perl6
7 if (not defined %*ENV
<HOME
>) {
8 die "Cannot work without a HOME directory";
10 #my $dir = %*ENV<HOME> ~ ($*OS ~~ m:i/win/ ?? "/p6pod" !! "/.p6pod");
11 my $dir = %*ENV
<HOME
> ~ "/.p6pod";
13 # is there an ARGS parser already Getopt::* ?
15 if (@
*ARGS
[0] eq "--index") {
17 } elsif (@
*ARGS
[0] eq "--keyword" and defined @
*ARGS
[1]) {
19 } elsif (@
*ARGS
[0] eq "--list") {
22 display_pod
(@
*ARGS
[0]);
27 # for now assume we run in the same directory where pugs is and the docs are in
29 my $ROOT = dirname
($PROGRAM_NAME) ~ "/..";
30 my $dir = $ROOT ~ "/docs/Perl6";
31 say "processing $dir tree";
32 my $prefix_length = $dir.chars
;
34 for list_files
($dir, 1) -> $podfile {
35 @podfiles.push(substr $podfile, $prefix_length+1, -4);
37 for 0..@podfiles -> $i {
38 say "$i) @podfiles[$i]";
41 my $selection = $*IN
.get
;
42 say "selected '$selection'";
43 display_pod
("$dir/@podfiles[$selection].pod");
48 my $fh = open $podfile orelse
die "Could not open '$podfile'\n";
49 for $fh.lines
-> $line {
55 say "Should index the files now";
56 mkdir $dir unless $dir ~~ :e
;
57 # TODO: go over all the files in the standard directory, whatever the standard will be
59 #my @files = list_files(dirname($PROGRAM_NAME));
62 for list_files
(dirname
($PROGRAM_NAME)) -> $podfile {
63 say "Processing '$podfile'";
64 my $fh = open $podfile orelse
die "Could not open '$podfile'\n";
67 for $fh.lines
-> $line {
69 if ($line ~~ m
:P5
/^=head\d\ (.*)/) {
72 if ($line ~~ /X\<(.*?)\>/) {
74 #my %h = ("file" => $podfile, "row" => $row);
75 push @
(%data{$0}), "$row.$podfile"; # only one dimension work in pugs so we have this workaround
77 # always remember in which entry are we in (row number or =head? name or both)
78 # if there is one or more X<> tags in a row, remember the values and in the end save to
85 # I think File::Find does not work currently...
86 sub list_files
($dir, $full) {
88 my $dh = opendir $dir orelse
die "Could not open $dir";
90 for $dh.readdir -> $entry {
91 next if $entry eq "." or $entry eq "..";
93 if (substr($entry, -4) eq ".pod") {
94 @entries.push($full ??
"$dir/$entry" !! $entry);
96 if ("$dir/$entry" ~~ :d
) {
97 @entries.push(list_files
("$dir/$entry", $full));
103 sub lookup
($keyword) {
104 say "Now look up $keyword";
105 my %data = index_pods
();
109 if (%data{$keyword}) {
110 for %data{$keyword}[] -> $entry {
111 my ($row, $file) = split /\./, $entry, 2;
112 say "here: $file $row";
113 my $fh = open $file orelse
die "Could not open $file";
117 # TODO: I guess the display should show a few lines before and a few lines after
118 # max to the next section start, I am not sure.
127 # TODO: especailly now that pugs is a bit slow starting up, we might want an interactive
128 # help system, that onces loads itself will not go down till exiting
131 # should be improved and moved to File::Basename, or better yet we should have one module with all
132 # frequently needed filesystem related functions....
133 sub dirname
($path is copy
) {
134 $path ~~ s
:P5
{/<-[/]>*$} = '';
140 # say " $PROGRAM_NAME --index";
141 # say " $PROGRAM_NAME --keyword KEYWORD";
142 say " $PROGRAM_NAME PODFILE - display the given podfile";
143 say " $PROGRAM_NAME --list - list the available podfiles";