|
| 1 | +#!/usr/bin/env perl6 |
| 2 | +use v6; |
| 3 | +use File::Find; |
| 4 | + |
| 5 | +# XXX this script probably should be refactored into p6doc itself |
| 6 | + |
| 7 | +sub findbin() returns Str { |
| 8 | + $*PROGRAM_NAME.subst(rx{<-[/\\]>+$}, ''); |
| 9 | +} |
| 10 | + |
| 11 | +constant INDEX = findbin() ~ 'index.data'; |
| 12 | + |
| 13 | +multi sub MAIN() { |
| 14 | + say "Usage: $*PROGRAM_NAME build to build index the docs for use with 'p6doc -f'"; |
| 15 | + say "Usage: $*PROGRAM_NAME list to list the names"; |
| 16 | + say "Usage: $*PROGRAM_NAME lookup <key> to display module name containing key"; |
| 17 | +} |
| 18 | + |
| 19 | +multi sub MAIN('build') { |
| 20 | + my %words; |
| 21 | + |
| 22 | + # XXX p6doc probably uses another path to @*INC which is probably incomplete |
| 23 | + |
| 24 | + for ( @*INC ) -> $lib_path is copy { |
| 25 | + |
| 26 | + $lib_path = $lib_path.Str; |
| 27 | + my @files := find(:dir($lib_path),:type('file')); |
| 28 | + |
| 29 | + for @files -> $f { |
| 30 | + my $file = $f.path; |
| 31 | + next if $file !~~ /\.pod$/; |
| 32 | + my $pod = substr($file.Str, 0 , $file.Str.chars -4); |
| 33 | + $pod.=subst(/$lib_path\//,""); |
| 34 | + $pod.=subst(/\//,'::',:g); |
| 35 | + my $section = ''; |
| 36 | + for open( $file.Str).lines -> $row { |
| 37 | + if $row ~~ /^\=(item|head\d) \s+ (.*?) \s*$/ { |
| 38 | + $section = $1.Str; |
| 39 | + %words{$section}.push([$pod, $section]); |
| 40 | + } |
| 41 | + if $row ~~ /X\<(.*?)\>/ and $section { |
| 42 | + my $x = $0.Str; |
| 43 | + %words{$x}.push([$pod, $section]); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + my $out = open(INDEX, :w); |
| 50 | + $out.print(%words.perl); |
| 51 | + $out.close; |
| 52 | +} |
| 53 | + |
| 54 | +multi sub MAIN('list') { |
| 55 | + if INDEX.IO ~~ :e { |
| 56 | + my %data = EVAL slurp INDEX; |
| 57 | + for %data.keys.sort -> $name { |
| 58 | + say $name |
| 59 | + # my $newdoc = %data{$docee}[0][0] ~ "." ~ %data{$docee}[0][1]; |
| 60 | + # return MAIN($newdoc, :f); |
| 61 | + } |
| 62 | + } else { |
| 63 | + say "First run $*PROGRAM_NAME index to create the index"; |
| 64 | + exit; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +multi sub MAIN('lookup', $key) { |
| 69 | + if INDEX.IO ~~ :e { |
| 70 | + my %data = EVAL slurp INDEX; |
| 71 | + die "not found" unless %data{$key}; |
| 72 | + say %data{$key}.split(" ").[0]; |
| 73 | + } else { |
| 74 | + say "First run $*PROGRAM_NAME index to create the index"; |
| 75 | + exit; |
| 76 | + } |
| 77 | +} |
| 78 | + |
0 commit comments