1
1
# !/usr/bin/env perl6
2
- use MONKEY-SEE-NO-EVAL ; # until we have a better serialisation
2
+
3
+ use v6 ;
3
4
use JSON::Fast;
5
+ use File::Find;
6
+ use MONKEY-SEE-NO-EVAL ; # until we have a better serialisation
4
7
5
8
my $ PROGRAM-NAME = " p6doc" ;
6
9
@@ -71,7 +74,17 @@ sub show-docs(Str $path, :$section, :$no-pager) {
71
74
}
72
75
73
76
sub USAGE () {
74
- say ' What documentation do you want to read?' ;
77
+ say " You want to maintain the index?" ;
78
+ say " To build an index for '$ PROGRAM-NAME -f'" ;
79
+ say " $ PROGRAM-NAME build" ;
80
+ say " \n To list the index keys" ;
81
+ say " $ PROGRAM-NAME list" ;
82
+ say " \n To display module name(s) containing key" ;
83
+ say " $ PROGRAM-NAME lookup" ;
84
+ say " \n To show where the index file lives" ;
85
+ say " $ PROGRAM-NAME path-to-index" ;
86
+
87
+ say " \n What documentation do you want to read?" ;
75
88
say " Examples: $ PROGRAM-NAME Str" ;
76
89
say " $ PROGRAM-NAME Str.split" ;
77
90
say " $ PROGRAM-NAME faq" ;
@@ -85,7 +98,7 @@ sub USAGE() {
85
98
say " $ PROGRAM-NAME -f Type::Array.push" ;
86
99
87
100
say " \n You can bypass the pager and print straight to stdout:" ;
88
- say " $ PROGRAM-NAME -n Str"
101
+ say " $ PROGRAM-NAME -n Str" ;
89
102
}
90
103
91
104
multi sub MAIN ($ docee , Bool : $ n ) {
@@ -120,7 +133,7 @@ multi sub MAIN($docee, Bool :$f!, Bool :$n) {
120
133
show-docs($ m , : section($ method ), : no-pager($ n ));
121
134
} else {
122
135
say ' In order to use unqualified sub and method names like "p6doc -f say"' ;
123
- say ' you will need to run "p6doc-index build" to build index.data.' ;
136
+ say ' you will need to run "p6doc build" to build index.data.' ;
124
137
say ' Otherwise use "p6doc -f Type::Str.split" instead of "p6doc -f split" for now.' ;
125
138
exit 2 ;
126
139
}
@@ -134,6 +147,67 @@ multi sub MAIN(Str $file where $file.IO ~~ :e, Bool :$n) {
134
147
show-docs($ file , : no-pager($ n ));
135
148
}
136
149
150
+ # index related
151
+
152
+ multi sub MAIN (' path-to-index' ) {
153
+ say INDEX if INDEX. IO . e ;
154
+ }
155
+
156
+ multi sub MAIN (' build' ) {
157
+ my % words ;
158
+
159
+ # XXX should index more than this - currently only core pod
160
+ for ($ * REPO . repo-chain()>>. Str X ~ " { $ * SPEC . dir-sep} doc{ $ * SPEC . dir-sep} " ). grep : *. IO . d -> $ lib_path is copy {
161
+
162
+ # for p6doc -f only looking under "Type" directory is useful (and faster)
163
+ my @ files = find(: dir($ lib_path ~ " Type" ),: type(' file' )). map ({. IO });
164
+
165
+ for @ files -> $ f {
166
+ my $ file = $ f . path ;
167
+ next if $ file ! ~~ /\. pod6? $ /;
168
+ my $ pod = substr ($ file . Str , 0 , $ file . Str . chars -4 );
169
+ $ pod .= subst ($ lib_path ," " );
170
+ $ pod .= subst (/"{$*SPEC.dir-sep}" /,' ::' ,: g);
171
+ my $ section = ' ' ;
172
+ for open ( $ file . Str ). lines -> $ row {
173
+ if $ row ~~ /^ \=(item| head\d ) \s + (.*? ) \s * $ / {
174
+ $ section = $1 . Str if $1 . defined ;
175
+ % words {$ section }. push ([$ pod , $ section ]) if $ section ~~ m /^ ("method " | "sub " | "routine " ) /;
176
+ }
177
+ }
178
+ }
179
+ }
180
+
181
+ my $ out = open (INDEX, : w);
182
+ $ out . print (% words . perl );
183
+ $ out . close ;
184
+ }
185
+
186
+ multi sub MAIN (' list' ) {
187
+ if INDEX. IO ~~ : e {
188
+ my % data = EVAL slurp INDEX;
189
+ for % data . keys . sort -> $ name {
190
+ say $ name
191
+ # my $newdoc = %data{$docee}[0][0] ~ "." ~ %data{$docee}[0][1];
192
+ # return MAIN($newdoc, :f);
193
+ }
194
+ } else {
195
+ say " First run $ * PROGRAM-NAME build to create the index" ;
196
+ exit ;
197
+ }
198
+ }
199
+
200
+ multi sub MAIN (' lookup' , $ key ) {
201
+ if INDEX. IO ~~ : e {
202
+ my % data = EVAL slurp INDEX;
203
+ die " not found" unless % data {$ key };
204
+ say % data {$ key }. split (" " ). [0 ];
205
+ } else {
206
+ say " First run $ * PROGRAM-NAME build to create the index" ;
207
+ exit ;
208
+ }
209
+ }
210
+
137
211
sub disambiguate-f-search ($ docee , % data ) {
138
212
my % found ;
139
213
0 commit comments