| 1 | # the filter for the PDL pod format (which is a valid general perl | ||
| 2 | # pod format but with special interpretation of some =for directives) | ||
| 3 | |||
| 4 | package PDL::PodParser; | ||
| 5 | use PDL::Core ''; | ||
| 6 | use Pod::Select; | ||
| 7 | use File::Spec; | ||
| 8 | use File::Basename; | ||
| 9 | |||
| 10 | @ISA = qw(Pod::Select); | ||
| 11 | |||
| 12 | %Title = ('Example' => 'Example', | ||
| 13 | 'Ref' => 'Reference', | ||
| 14 | 'Sig' => 'Signature', | ||
| 15 | 'Opt' => 'Options', | ||
| 16 | 'Usage' => 'Usage', | ||
| 17 | 'Bad' => 'Bad value support', | ||
| 18 | ); | ||
| 19 | |||
| 20 | |||
| 21 | |||
| 22 | sub new { | ||
| 23 | my $class = shift; | ||
| 24 | my $parser = $class->SUPER::new(@_); | ||
| 25 | bless $parser,$class; # just in case | ||
| 26 | |||
| 27 | $parser->select("METHODS|OPERATORS|CONTRUCTORS|FUNCTIONS|NAME"); | ||
| 28 | $parser->{CURFUNC} = undef; | ||
| 29 | $parser->{SYMHASH} = {}; | ||
| 30 | $parser->{INBLOCK} = 0; | ||
| 31 | $parser->{Mode} = ""; | ||
| 32 | $parser->{verbose} = 0; | ||
| 33 | $parser->{NAME} = 'UNKNOWN'; | ||
| 34 | return $parser; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub command { | ||
| 38 | my ($this,$cmd,$txt,$line_num,$pod_para) = @_; | ||
| 39 | $this->{Parmode} = 'Body'; | ||
| 40 | |||
| 41 | if ($cmd eq 'head1') { | ||
| 42 | $this->{Mode} = $txt; | ||
| 43 | $this->{Parmode} = 'Body'; | ||
| 44 | $this->{Parmode} = 'NAME' if $txt =~ /NAME/; | ||
| 45 | } elsif ($this->{Mode} =~ /NAME/) { | ||
| 46 | # do nothing (was 'last' but that was probably a mistake) | ||
| 47 | } elsif ($cmd eq 'head2') { | ||
| 48 | # A function can have multiple names (ex: zeros and zeroes), | ||
| 49 | # so split at the commas | ||
| 50 | my @funcs = split(',',$txt); | ||
| 51 | # Remove parentheses (so myfunc and myfunc() both work) | ||
| 52 | my @names = map {$1 if m/\s*([^\s(]+)\s*/} @funcs; | ||
| 53 | barf "error parsing function list '$txt'" | ||
| 54 | unless $#funcs == $#names; | ||
| 55 | # check for signatures | ||
| 56 | my $sym = $this->{SYMHASH}; | ||
| 57 | for (@funcs) { | ||
| 58 | $sym->{$1}->{Module} = $this->{NAME} if m/\s*([^\s(]+)\s*/; | ||
| 59 | $sym->{$1}->{Sig} = $2 if m/\s*([^\s(]+)\s*\(\s*(.+)\s*\)\s*$/; | ||
| 60 | } | ||
| 61 | # make the first one the current function | ||
| 62 | $sym->{$names[0]}->{Names} = join(',',@names) if $#names > 0; | ||
| 63 | my $name = shift @names; | ||
| 64 | # Make the other names cross-reference the first name | ||
| 65 | $sym->{$_}->{Crossref} = $name for (@names); | ||
| 66 | my $sig = $sym->{$name}->{Sig}; | ||
| 67 | # diagnostic output | ||
| 68 | print "\nFunction '".join(',',($name,@names))."'\n" if $this->{verbose}; | ||
| 69 | print "\n\tSignature: $sig\n" if defined $sig && $this->{verbose}; | ||
| 70 | $this->{CURFUNC} = $name; | ||
| 71 | } elsif ($cmd eq 'for') { | ||
| 72 | $this->check_for_mode($txt,$pod_para) if $cmd eq 'for'; | ||
| 73 | } | ||
| 74 | local $this->{Parmode} = 'Body'; | ||
| 75 | $this->SUPER::command($cmd,$txt,$line_num,$pod_para); | ||
| 76 | } | ||
| 77 | |||
| 78 | sub check_for_mode { | ||
| 79 | my ($this,$txt,$pod_para) = @_; | ||
| 80 | if ($txt =~ /^(sig|example|ref|opt|usage|bad|body)/i) { | ||
| 81 | $this->{Parmode} = ucfirst lc $1; | ||
| 82 | print "switched now to '$1' mode\n" if $this->{VERBOSE}; | ||
| 83 | print "\n\t$Title{$this->{Parmode}}\n" | ||
| 84 | unless $this->{Parmode} =~ /Body/ || !$this->{verbose}; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | sub textblock { | ||
| 89 | my $this = shift; | ||
| 90 | my $txt = shift; | ||
| 91 | $this->checkmode($txt); | ||
| 92 | local $this->{INBLOCK} = 1; | ||
| 93 | $this->SUPER::textblock($txt,@_); | ||
| 94 | $this->{Parmode} = 'Body'; # and reset parmode | ||
| 95 | } | ||
| 96 | |||
| 97 | sub checkmode { | ||
| 98 | my ($this,$txt,$verbatim) = @_; | ||
| 99 | if ($this->{Mode} =~ /NAME/ && $this->{Parmode} =~ /NAME/) { | ||
| 100 | $this->{NAME} = $1 if $this->trim($txt) =~ /^\s*(\S+)\s*/; | ||
| 101 | print "\nNAME\t$this->{NAME}\n" if $this->{verbose}; | ||
| 102 | $this->{Parmode} = 'Body'; | ||
| 103 | return; | ||
| 104 | } | ||
| 105 | unless ($this->{Parmode} =~ /Body/ || $this->{INBLOCK}) { | ||
| 106 | my $func = $this->{CURFUNC}; | ||
| 107 | barf "no function defined" unless defined $func; | ||
| 108 | local $this->{INBLOCK} = 1; # can interpolate call textblock? | ||
| 109 | my $itxt = $verbatim ? $txt : $this->interpolate($txt); | ||
| 110 | $this->{SYMHASH}->{$func}->{$this->{Parmode}} .= | ||
| 111 | $this->trim($itxt,$verbatim); | ||
| 112 | my $cr = ($verbatim && $this->{Parmode} ne 'Sig') ? "\n" : ""; | ||
| 113 | my $out = "\n\t\t$cr".$this->trim($itxt,$verbatim); | ||
| 114 | print "$out\n$cr" if $this->{verbose}; | ||
| 115 | } | ||
| 116 | $this->{Parmode} = 'Body'; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub verbatim { | ||
| 120 | my $this = shift; | ||
| 121 | my $txt = shift; | ||
| 122 | $this->checkmode($txt,1); | ||
| 123 | $this->SUPER::verbatim($txt,@_); | ||
| 124 | } | ||
| 125 | |||
| 126 | # this needs improvement | ||
| 127 | # and any formatting information should be removed? | ||
| 128 | # it probably depends | ||
| 129 | sub trim { | ||
| 130 | my ($this,$txt,$verbatim) = @_; | ||
| 131 | my $ntxt = ""; | ||
| 132 | $txt =~ s/(signature|usage):\s*//i if $this->{Parmode} eq 'Sig' || | ||
| 133 | $this->{Parmode} eq 'Usage'; | ||
| 134 | if ($this->{Parmode} eq 'Sig') { | ||
| 135 | |||
| 136 | $txt =~ s/^\s*//; | ||
| 137 | $txt =~ s/\s*$//; | ||
| 138 | while( $txt =~ s/^\((.*)\)$/$1/ ) {}; # Strip BALANCED brackets | ||
| 139 | |||
| 140 | } | ||
| 141 | for (split "\n", $txt) { | ||
| 142 | s/^\s*(.*)\s*$/$1/ unless $verbatim; | ||
| 143 | $ntxt .= "$_\n" unless m/^\s*$/; | ||
| 144 | } | ||
| 145 | # $txt =~ s/^\s*(.*)\s*$/$1/; | ||
| 146 | chomp $ntxt; | ||
| 147 | return $ntxt; | ||
| 148 | } | ||
| 149 | |||
| 150 | |||
| 151 | =head1 NAME | ||
| 152 | |||
| 153 | PDL::Doc - support for PDL online documentation | ||
| 154 | |||
| 155 | =head1 SYNOPSIS | ||
| 156 | |||
| 157 | use PDL::Doc; | ||
| 158 | $onlinedc = new PDL::Doc ($docfile); | ||
| 159 | @match = $onlinedc->search('m/slice|clump/'); | ||
| 160 | |||
| 161 | =head1 DESCRIPTION | ||
| 162 | |||
| 163 | An implementation of online docs for PDL. | ||
| 164 | |||
| 165 | =head1 Using PDL documentation | ||
| 166 | |||
| 167 | PDL::Doc's main use is in the "help" (synonym "?") and "apropos" | ||
| 168 | (synonym "??") commands in the perldl shell. PDL:Doc provides the | ||
| 169 | infrastrucure to index and access PDL's documentation through these | ||
| 170 | commands. There is also an API for direct access to the documentation | ||
| 171 | database (see below). | ||
| 172 | |||
| 173 | The PDL doc system is built on Perl's pod (Plain Old Documentation), | ||
| 174 | included inline with each module. The PDL core modules are | ||
| 175 | automatically indexed when PDL is built and installed, and there is | ||
| 176 | provision for indexing external modules as well. | ||
| 177 | |||
| 178 | To include your module's pod into the Perl::Doc index, you should | ||
| 179 | follow the documentation conventions below. | ||
| 180 | |||
| 181 | =head1 PDL documentation conventions | ||
| 182 | |||
| 183 | For a package like PDL that has I<a lot> of functions it | ||
| 184 | is very desirable to have some form of online help to | ||
| 185 | make it easy for the user to remind himself of names, | ||
| 186 | calling conventions and typical usage of the multitude | ||
| 187 | of functions at his disposal. To make it straightforward | ||
| 188 | to extract the relevant information from the POD documentation | ||
| 189 | in source files that make up the PDL distribution | ||
| 190 | certain conventions have been adopted in formatting this | ||
| 191 | documentation. | ||
| 192 | |||
| 193 | The first convention says that all documentation for | ||
| 194 | PDL functions appears in the POD section introduced | ||
| 195 | by one of the following: | ||
| 196 | |||
| 197 | =head1 FUNCTIONS | ||
| 198 | =head1 OPERATORS | ||
| 199 | =head1 METHODS | ||
| 200 | =head1 CONSTRUCTORS | ||
| 201 | |||
| 202 | If you're documenting an object-oriented interface to a class | ||
| 203 | that your module defines, you should use METHODS and CONSTRUCTORS | ||
| 204 | as appropriate. If you are simply adding functions to PDL, | ||
| 205 | use FUNCTIONS and OPERATORS as appropriate. | ||
| 206 | |||
| 207 | Individual functions or methods in these section are introduced by | ||
| 208 | |||
| 209 | =head2 funcname | ||
| 210 | |||
| 211 | where signature is the argumentlist for a PP defined function as | ||
| 212 | explained in L<PDL::PP>. Generally, PDL documentation is in valid POD | ||
| 213 | format (see L<perlpod|perlpod>) but uses the C<=for> directive in a | ||
| 214 | special way. The C<=for> directive is used to flag to the PDL Pod | ||
| 215 | parser that information is following that will be used to generate | ||
| 216 | online help. | ||
| 217 | |||
| 218 | The PDL Pod parser recognises the following C<=for> directives: | ||
| 219 | |||
| 220 | =over 5 | ||
| 221 | |||
| 222 | =item Ref | ||
| 223 | |||
| 224 | indicates that the one line reference for this function follows, | ||
| 225 | e.g., | ||
| 226 | |||
| 227 | =for ref | ||
| 228 | |||
| 229 | Returns a piddle of lags to parent. | ||
| 230 | |||
| 231 | =item Sig | ||
| 232 | |||
| 233 | the signature for the current function follows, e.g., | ||
| 234 | |||
| 235 | =for sig | ||
| 236 | |||
| 237 | Signature: (a(n), [o]b(), [t]tmp(n)) | ||
| 238 | |||
| 239 | =item Usage | ||
| 240 | |||
| 241 | an indication of the possible calling conventions for the current | ||
| 242 | function, e.g., | ||
| 243 | |||
| 244 | =for usage | ||
| 245 | |||
| 246 | wpic($pdl,$filename[,{ options... }]) | ||
| 247 | |||
| 248 | =item Opt | ||
| 249 | |||
| 250 | lists options for the current function, e.g., | ||
| 251 | |||
| 252 | =for options | ||
| 253 | |||
| 254 | CONVERTER => 'ppmtogif', # explicitly specify pbm converter | ||
| 255 | FLAGS => '-interlaced -transparent 0', # flags for converter | ||
| 256 | IFORM => 'PGM', # explicitly specify intermediate format | ||
| 257 | XTRAFLAGS => '-imagename iris', # additional flags to defaultflags | ||
| 258 | FORMAT => 'PCX', # explicitly specify output image format | ||
| 259 | COLOR => 'bw', # specify color conversion | ||
| 260 | LUT => $lut, # use color table information | ||
| 261 | |||
| 262 | |||
| 263 | =item Example | ||
| 264 | |||
| 265 | gives examples of typical usage for the current function: | ||
| 266 | |||
| 267 | =for example | ||
| 268 | |||
| 269 | wpic $pdl, $file; | ||
| 270 | $im->wpic('web.gif',{LUT => $lut}); | ||
| 271 | for (@images) { | ||
| 272 | $_->wpic($name[0],{CONVERTER => 'ppmtogif'}) | ||
| 273 | } | ||
| 274 | |||
| 275 | =item Bad | ||
| 276 | |||
| 277 | provides information on how the function handles bad values (if | ||
| 278 | C<$PDL:Config{WITH_BADVAL}> is set to 1). The documentation under | ||
| 279 | this directive should indicate if this function accepts piddles | ||
| 280 | with bad values and under what circumstances this function might | ||
| 281 | return piddles with bad values. | ||
| 282 | |||
| 283 | =back | ||
| 284 | |||
| 285 | The PDL podparser is implemented as a simple state machine. Any of | ||
| 286 | the above C<=for> statements switches the podparser into a state | ||
| 287 | where the following paragraph is accepted as information for the | ||
| 288 | respective field (C<Ref>, C<Usage>, C<Opt>, C<Example> or C<Bad>). | ||
| 289 | Only the text up to | ||
| 290 | the end of the current paragraph is accepted, for example: | ||
| 291 | |||
| 292 | =for example | ||
| 293 | |||
| 294 | ($x,$y) = $a->func(1,3); # this is part of the accepted info | ||
| 295 | $x = func($a,0,1); # this as well | ||
| 296 | |||
| 297 | $x = func($a,$b); # but this isn't | ||
| 298 | |||
| 299 | To make the resulting pod documentation also easily digestible for the | ||
| 300 | existing pod filters (pod2man, pod2text, pod2html, etc) the actual | ||
| 301 | textblock of information must be separated from the C<=for> directive | ||
| 302 | by at least one blank line. Otherwise, the textblock will be lost in | ||
| 303 | the translation process when the "normal" podformatters are used. The | ||
| 304 | general idea behind this format is that it should be easy to extract | ||
| 305 | the information for online documentation, automatic generation of a | ||
| 306 | reference card, etc but at the same time the documentation should be | ||
| 307 | translated by the standard podformatters without loss of contents | ||
| 308 | (and without requiring any changes in the existing POD format). | ||
| 309 | |||
| 310 | The preceding explanations should be further explained by the | ||
| 311 | following example (extracted from PDL/IO/Misc/misc.pd): | ||
| 312 | |||
| 313 | =head2 rcols() | ||
| 314 | |||
| 315 | =for ref | ||
| 316 | |||
| 317 | Read ASCII whitespaced cols from file into piddles efficiently. | ||
| 318 | |||
| 319 | If no columns are specified all are assumed | ||
| 320 | Will optionally only process lines matching a pattern. | ||
| 321 | Can take file name or *HANDLE. | ||
| 322 | |||
| 323 | =for usage | ||
| 324 | |||
| 325 | Usage: ($x,$y,...) = rcols(*HANDLE|"filename", ["/pattern/",$col1, $col2,] ...) | ||
| 326 | |||
| 327 | e.g., | ||
| 328 | |||
| 329 | =for example | ||
| 330 | |||
| 331 | ($x,$y) = rcols 'file1' | ||
| 332 | ($x,$y,$z) = rcols 'file2', "/foo/",3,4 | ||
| 333 | $x = PDL->rcols 'file1'; | ||
| 334 | |||
| 335 | Note: currently quotes are required on the pattern. | ||
| 336 | |||
| 337 | |||
| 338 | which is translated by, e.g, the standard C<pod2text> converter into: | ||
| 339 | |||
| 340 | rcols() | ||
| 341 | |||
| 342 | Read ASCII whitespaced cols from file into piddles efficiently. | ||
| 343 | |||
| 344 | If no columns are specified all are assumed Will optionally only | ||
| 345 | process lines matching a pattern. Can take file name or *HANDLE. | ||
| 346 | |||
| 347 | Usage: ($x,$y,...) = rcols(*HANDLE|"filename", ["/pattern/",$col1, $col2,] ...) | ||
| 348 | |||
| 349 | e.g., | ||
| 350 | |||
| 351 | ($x,$y) = rcols 'file1' | ||
| 352 | ($x,$y,$z) = rcols 'file2', "/foo/",3,4 | ||
| 353 | $x = PDL->rcols 'file1'; | ||
| 354 | |||
| 355 | Note: currently quotes are required on the pattern. | ||
| 356 | |||
| 357 | It should be clear from the preceding example that readable output | ||
| 358 | can be obtained from this format using the standard converters and | ||
| 359 | the reader will hopefully get a feeling how he can easily intersperse | ||
| 360 | the special C<=for> directives with the normal POD documentation. | ||
| 361 | |||
| 362 | =head2 Which directives should be contained in the documentation | ||
| 363 | |||
| 364 | The module documentation should | ||
| 365 | start with the | ||
| 366 | |||
| 367 | =head1 NAME | ||
| 368 | |||
| 369 | PDL::Modulename -- do something with piddles | ||
| 370 | |||
| 371 | section (as anyway required by C<pod2man>) since the PDL podparser | ||
| 372 | extracts the name of the module this function belongs to from | ||
| 373 | that section. | ||
| 374 | |||
| 375 | Each function that is I<not> only for internal use by the module | ||
| 376 | should be documented, introduced with the C<=head2> directive | ||
| 377 | in the C<=head1 FUNCTIONS> section. The only field that every function | ||
| 378 | documented along these lines should have is the I<Ref> field preceding | ||
| 379 | a one line description of its intended functionality (suitable for | ||
| 380 | inclusion in a concise reference card). PP defined functions (see L<PDL::PP>) | ||
| 381 | should have a I<Sig> field stating their signature. To facilitate | ||
| 382 | maintenance of this documentation for such functions the 'Doc' field | ||
| 383 | has been introduced into the definition of C<pp_def> (see again L<PDL::PP>) | ||
| 384 | which will take care that name and signature of the so defined function | ||
| 385 | are documented in this way (for examples of this usage see, for example, | ||
| 386 | the PDL::Slices module, especially F<slices.pd> and the resulting | ||
| 387 | F<Slices.pm>). Similarly, the 'BadDoc' field provides a means of | ||
| 388 | specifying information on how the routine handles the presence of | ||
| 389 | bad values: this will be autpmatically created if | ||
| 390 | C<BadDoc> is not supplied, or set to C<undef>. | ||
| 391 | |||
| 392 | Furthermore, the documentation for each function should contain | ||
| 393 | at least one of the I<Usage> or I<Examples> fields. Depending on the | ||
| 394 | calling conventions for the function under consideration presence | ||
| 395 | of both fields may be warranted. | ||
| 396 | |||
| 397 | If a function has options that should be given as a hash reference in | ||
| 398 | the form | ||
| 399 | |||
| 400 | {Option => Value, ...} | ||
| 401 | |||
| 402 | then the possible options (and aproppriate values) should be explained | ||
| 403 | in the textblock following the C<=for Opt> directive (see example above | ||
| 404 | and, e.g., PDL::IO::Pic). | ||
| 405 | |||
| 406 | It is well possible that some of these conventions appear to be clumsy | ||
| 407 | at times and the author is keen to hear of any suggestions for better | ||
| 408 | alternatives. | ||
| 409 | |||
| 410 | =cut | ||
| 411 | |||
| 412 | package PDL::Doc; | ||
| 413 | use PDL::Core ''; | ||
| 414 | use IO::File; # for file handles | ||
| 415 | use File::Basename; | ||
| 416 | use PDL::Doc::Config; | ||
| 417 | |||
| 418 | =head1 INSTANCE METHODS | ||
| 419 | |||
| 420 | =head2 new | ||
| 421 | |||
| 422 | $onlinedc = new PDL::Doc ('file.pdl',[more files]); | ||
| 423 | |||
| 424 | =cut | ||
| 425 | |||
| 426 | sub new { | ||
| 427 | my ($type,@files) = @_; | ||
| 428 | my $this = bless {},$type; | ||
| 429 | $this->{File} = [@files]; | ||
| 430 | $this->{Scanned} = []; | ||
| 431 | $this->{Outfile} = $files[0]; | ||
| 432 | return $this; | ||
| 433 | } | ||
| 434 | |||
| 435 | =head2 addfiles | ||
| 436 | |||
| 437 | add another file to the online database associated with this object. | ||
| 438 | |||
| 439 | =cut | ||
| 440 | |||
| 441 | sub addfiles { | ||
| 442 | my ($this,@files) = @_; | ||
| 443 | push @{$this->{File}}, @files; | ||
| 444 | } | ||
| 445 | |||
| 446 | =head2 outfile | ||
| 447 | |||
| 448 | set the name of the output file for this online db | ||
| 449 | |||
| 450 | =cut | ||
| 451 | |||
| 452 | sub outfile { | ||
| 453 | my ($this,$file) = @_; | ||
| 454 | $this->{Outfile} = $file if defined $file; | ||
| 455 | return $this->{Outfile}; | ||
| 456 | } | ||
| 457 | |||
| 458 | =head2 ensuredb | ||
| 459 | |||
| 460 | Make sure that the database is slurped in | ||
| 461 | |||
| 462 | =cut | ||
| 463 | |||
| 464 | sub ensuredb { | ||
| 465 | my ($this) = @_; | ||
| 466 | while (my $fi = pop @{$this->{File}}) { | ||
| 467 | open IN, $fi or | ||
| 468 | barf "can't open database $fi, scan docs first"; | ||
| 469 | binmode IN; | ||
| 470 | my ($plen,$txt); | ||
| 471 | while (read IN, $plen,2) { | ||
| 472 | my ($len) = unpack "S", $plen; | ||
| 473 | read IN, $txt, $len; | ||
| 474 | my (@a) = split chr(0), $txt; | ||
| 475 | push(@a, "") unless(@a % 2); # Add null string at end if necessary -- solves bug with missing REF section. | ||
| 476 | my ($sym, %hash) = @a; | ||
| 477 | |||
| 478 | $hash{Dbfile} = $fi; # keep the origin pdldoc.db path | ||
| 479 | $this->{SYMS}->{$sym} = {%hash}; | ||
| 480 | } | ||
| 481 | close IN; | ||
| 482 | push @{$this->{Scanned}}, $fi; | ||
| 483 | } | ||
| 484 | return $this->{SYMS}; | ||
| 485 | } | ||
| 486 | |||
| 487 | =head2 savedb | ||
| 488 | |||
| 489 | save the database (i.e., the hash of PDL symbols) to the file associated | ||
| 490 | with this object. | ||
| 491 | |||
| 492 | =cut | ||
| 493 | |||
| 494 | sub savedb { | ||
| 495 | my ($this) = @_; | ||
| 496 | my $hash = $this->ensuredb(); | ||
| 497 | open OUT, ">$this->{Outfile}" or barf "can't write to symdb $this->{Outfile}"; | ||
| 498 | binmode OUT; | ||
| 499 | while (my ($key,$val) = each %$hash) { | ||
| 500 | next if 0 == scalar(%$val); | ||
| 501 | my $fi = $val->{File}; | ||
| 502 | if (File::Spec->file_name_is_absolute($fi) && -f $fi) { | ||
| 503 | #store paths to *.pm files relative to pdldoc.db | ||
| 504 | $val->{File} = File::Spec->abs2rel($fi, dirname($this->{Outfile})) ; | ||
| 505 | } | ||
| 506 | delete $val->{Dbfile}; # no need to store Dbfile | ||
| 507 | my $txt = "$key".chr(0).join(chr(0),%$val); | ||
| 508 | print OUT pack("S",length($txt)).$txt; | ||
| 509 | } | ||
| 510 | } | ||
| 511 | |||
| 512 | |||
| 513 | =head2 gethash | ||
| 514 | |||
| 515 | Return the PDL symhash (e.g. for custom search operations) | ||
| 516 | |||
| 517 | The symhash is a multiply nested hash with the following structure: | ||
| 518 | |||
| 519 | $symhash = { | ||
| 520 | function_name => { | ||
| 521 | Module => 'module::name', | ||
| 522 | Sig => 'signature string', | ||
| 523 | Bad => 'bad documentation string', | ||
| 524 | ... | ||
| 525 | }, | ||
| 526 | function_name => { | ||
| 527 | Module => 'module::name', | ||
| 528 | Sig => 'signature string', | ||
| 529 | Bad => 'bad documentation string', | ||
| 530 | ... | ||
| 531 | }, | ||
| 532 | }; | ||
| 533 | |||
| 534 | The possible keys for each function include: | ||
| 535 | |||
| 536 | Module - module name | ||
| 537 | Sig - signature | ||
| 538 | Crossref - the function name for the documentation, if it has multiple | ||
| 539 | names (ex: the documentation for zeros is under zeroes) | ||
| 540 | Names - a comma-separated string of the all the function's names | ||
| 541 | Example - example text (optional) | ||
| 542 | Ref - one-line reference string | ||
| 543 | Opt - options | ||
| 544 | Usage - short usage explanation | ||
| 545 | Bad - explanation of behavior when it encounters bad values | ||
| 546 | |||
| 547 | =cut | ||
| 548 | |||
| 549 | sub gethash { | ||
| 550 | return $_[0]->ensuredb(); | ||
| 551 | } | ||
| 552 | |||
| 553 | =head2 search | ||
| 554 | |||
| 555 | Search a PDL symhash | ||
| 556 | |||
| 557 | =for usage | ||
| 558 | |||
| 559 | $onldc->search($regex, $fields [, $sort]) | ||
| 560 | |||
| 561 | Searching is by default case insensitive. Other flags can be | ||
| 562 | given by specifying the regexp in the form C<m/regex/ismx> | ||
| 563 | where C</> can be replaced with any other non-alphanumeric | ||
| 564 | character. $fields is an array reference for all hash fields | ||
| 565 | (or simply a string if you only want to search one field) | ||
| 566 | that should be matched against the regex. Valid fields are | ||
| 567 | |||
| 568 | Name, # name of the function | ||
| 569 | Module, # module the function belongs to | ||
| 570 | Ref, # the one-line reference description | ||
| 571 | Example, # the example for this function | ||
| 572 | Opt, # options | ||
| 573 | File, # the path to the source file these docs have been extracted from | ||
| 574 | |||
| 575 | If you wish to have your results sorted by function name, pass a true | ||
| 576 | value for C<$sort>. | ||
| 577 | |||
| 578 | The results will be returned as an array of pairs in the form | ||
| 579 | |||
| 580 | @results = ( | ||
| 581 | [funcname, {SYMHASH_ENTRY}], | ||
| 582 | [funcname, {SYMHASH_ENTRY}], | ||
| 583 | ... | ||
| 584 | ); | ||
| 585 | |||
| 586 | See the example at the end of the documentation to see how you might | ||
| 587 | use this. | ||
| 588 | |||
| 589 | =cut | ||
| 590 | |||
| 591 | |||
| 592 | sub search { | ||
| 593 | my ($this,$pattern,$fields,$sort) = @_; | ||
| 594 | $sort = 0 unless defined $sort; | ||
| 595 | my $hash = $this->ensuredb; | ||
| 596 | my @match = (); | ||
| 597 | |||
| 598 | # Make a single scalar $fields work | ||
| 599 | $fields = [$fields] if ref($fields) eq ''; | ||
| 600 | |||
| 601 | $pattern = $this->checkregex($pattern); | ||
| 602 | |||
| 603 | while (my ($key,$val) = each %$hash) { | ||
| 604 | FIELD: for (@$fields) { | ||
| 605 | if ($_ eq 'Name' and $key =~ /$pattern/i | ||
| 606 | or defined $val->{$_} and $val->{$_} =~ /$pattern/i) { | ||
| 607 | $val = $hash->{$val->{Crossref}} | ||
| 608 | if defined $val->{Crossref} && defined $hash->{$val->{Crossref}}; | ||
| 609 | push @match, [$key,$val]; | ||
| 610 | last FIELD; | ||
| 611 | } | ||
| 612 | } | ||
| 613 | } | ||
| 614 | @match = sort {$a->[0] cmp $b->[0]} @match if (@match && $sort); | ||
| 615 | return @match; | ||
| 616 | } | ||
| 617 | |||
| 618 | |||
| 619 | # parse a regexp in the form | ||
| 620 | # m/^[a-z]+/ismx | ||
| 621 | # where the pairs of '/' can be replaced by any other pair of matching | ||
| 622 | # characters | ||
| 623 | # if the expression doesn't start with 'm' followed by a nonalphanumeric | ||
| 624 | # character, return as-is | ||
| 625 | sub checkregex { | ||
| 626 | my ($this,$regex) = @_; | ||
| 627 | return "(?i)$regex" unless $regex =~ /^m[^a-z,A-Z,0-9]/; | ||
| 628 | my $sep = substr($regex,1,1); | ||
| 629 | substr($regex,0,2) = ''; | ||
| 630 | $sep = '(?<!\\\\)\\'.$sep; # Avoid '\' before the separator | ||
| 631 | |||
| 632 | my ($pattern,$mod) = split($sep,$regex,2); | ||
| 633 | barf "unknown regex modifiers '$mod'" if $mod && $mod !~ /[imsx]+/; | ||
| 634 | $pattern = "(?$mod)$pattern" if $mod; | ||
| 635 | return $pattern; | ||
| 636 | } | ||
| 637 | |||
| 638 | =head2 scan | ||
| 639 | |||
| 640 | Scan a source file using the PDL podparser to extract information | ||
| 641 | for online documentation | ||
| 642 | |||
| 643 | =cut | ||
| 644 | |||
| 645 | sub scan { | ||
| 646 | my ($this,$file,$verbose) = @_; | ||
| 647 | $verbose = 0 unless defined $verbose; | ||
| 648 | barf "can't find file '$file'" unless -f $file; | ||
| 649 | |||
| 650 | # First HTMLify file in the tree | ||
| 651 | |||
| 652 | # Does not work yet | ||
| 653 | |||
| 654 | #if (system ("pod2html $file")!=0) { | ||
| 655 | # warn "Failed to execute command: pod2html $file2\n"; | ||
| 656 | #} | ||
| 657 | #else{ # Rename result (crummy pod2html) | ||
| 658 | # rename ("$file.html","$1.html") if $file =~ /^(.*)\.pm$/; | ||
| 659 | #} | ||
| 660 | |||
| 661 | # Now parse orig pm/pod | ||
| 662 | |||
| 663 | my $infile = new IO::File $file; | ||
| 664 | # XXXX convert to absolute path | ||
| 665 | # my $outfile = '/tmp/'.basename($file).'.pod'; | ||
| 666 | open my $outfile, '>', \(my $outfile_text); | ||
| 667 | |||
| 668 | # Handle RPM etc. case where we are building away from the final | ||
| 669 | # location. Alright it's a hack - KGB | ||
| 670 | my $file2 = $file; | ||
| 671 | $file2 =~ s/^$ENV{BUILDROOTPREFIX}// if $ENV{BUILDROOTPREFIX} ne ""; | ||
| 672 | |||
| 673 | my $parser = new PDL::PodParser; | ||
| 674 | $parser->{verbose} = $verbose; | ||
| 675 | eval { $parser->parse_from_filehandle($infile,$outfile) }; | ||
| 676 | warn "cannot parse '$file'" if $@; | ||
| 677 | |||
| 678 | $this->{SYMS} = {} unless defined $this->{SYMS}; | ||
| 679 | my $hash = $this->{SYMS}; | ||
| 680 | my @stats = stat $file; | ||
| 681 | $this->{FTIME}->{$file2} = $stats[9]; # store last mod time | ||
| 682 | # print "mtime of $file: $stats[9]\n"; | ||
| 683 | my $phash = $parser->{SYMHASH}; | ||
| 684 | my $n = 0; | ||
| 685 | while (my ($key,$val) = each %$phash) { | ||
| 686 | #print "adding '$key'\n"; | ||
| 687 | $n++; | ||
| 688 | |||
| 689 | $val->{File} = $file2; | ||
| 690 | $hash->{$key} = $val | ||
| 691 | } | ||
| 692 | |||
| 693 | # KGB pass2 - scan for module name and function | ||
| 694 | # alright I admit this is kludgy but it works | ||
| 695 | # and one can now find modules with 'apropos' | ||
| 696 | |||
| 697 | $infile = new IO::File $file; | ||
| 698 | $outfile_text = ''; | ||
| 699 | $parser = new PDL::PodParser; | ||
| 700 | $parser->select('NAME'); | ||
| 701 | eval { $parser->parse_from_filehandle($infile,$outfile) }; | ||
| 702 | warn "cannot parse '$file'" if $@; | ||
| 703 | |||
| 704 | my @namelines = split("\n",$outfile_text); | ||
| 705 | my ($name,$does); | ||
| 706 | for (@namelines) { | ||
| 707 | if (/^(PDL) (-) (.*)/ or /^\s*(Inline::Pdlpp)\s*(-*)?\s*(.*)\s*$/ or /\s*(PDL::[\w:]*)\s*(-*)?\s*(.*)\s*$/) { | ||
| 708 | $name = $1; $does = $3; | ||
| 709 | } | ||
| 710 | if (/^\s*([a-z][a-z0-9]*) (-+) (.*)/) { # lowercase shell script name | ||
| 711 | $name = $1; $does = $3; | ||
| 712 | ($name,$does) = (undef,undef) unless $does =~ /shell|script/i; | ||
| 713 | } | ||
| 714 | } | ||
| 715 | $does = 'Hmmm ????' if $does =~ /^\s*$/; | ||
| 716 | my $type = ($file =~ /\.pod$/ ? | ||
| 717 | ($does =~ /shell|script/i && $name =~ /^[a-z][a-z0-9]*$/) ? 'Script:' | ||
| 718 | : 'Manual:' | ||
| 719 | : 'Module:'); | ||
| 720 | $hash->{$name} = {Ref=>"$type $does",File=>$file2} if $name !~ /^\s*$/; | ||
| 721 | return $n; | ||
| 722 | } | ||
| 723 | |||
| 724 | =head2 scantree | ||
| 725 | |||
| 726 | Scan whole directory trees for online documentation in | ||
| 727 | C<.pm> (module definition) and C<*.pod> (general | ||
| 728 | documentation) files (using the File::Find module). | ||
| 729 | |||
| 730 | =cut | ||
| 731 | |||
| 732 | sub scantree { | ||
| 733 | my ($this,$dir,$verbose) = @_; | ||
| 734 | $verbose = 0 unless defined $verbose; | ||
| 735 | require File::Find; | ||
| 736 | print "Scanning $dir ... \n\n"; | ||
| 737 | my $ntot = 0; | ||
| 738 | my $sub = sub { if (($File::Find::name =~ /[.]pm$/ && | ||
| 739 | $File::Find::name !~ /PP.pm/ && | ||
| 740 | $File::Find::name !~ m|Pod/Parser.pm| && | ||
| 741 | $File::Find::dir !~ m#/PP|/Gen#) or ( | ||
| 742 | $File::Find::name =~ /[.]pod$/ && | ||
| 743 | $File::Find::name !~ /Index[.]pod$/)){ | ||
| 744 | printf "%-20s", $_.'...'; | ||
| 745 | my $n = $this->scan($File::Find::name,$verbose); # bind $this lexically | ||
| 746 | print "\t$n functions\n"; | ||
| 747 | $ntot += $n; | ||
| 748 | } | ||
| 749 | }; | ||
| 750 | File::Find::find($sub,$dir); | ||
| 751 | print "\n\nfound $ntot functions\n"; | ||
| 752 | } | ||
| 753 | |||
| 754 | |||
| 755 | =head2 funcdocs | ||
| 756 | |||
| 757 | extract the complete documentation about a function from its | ||
| 758 | source file using the PDL::Pod::Parser filter. | ||
| 759 | |||
| 760 | =cut | ||
| 761 | |||
| 762 | sub funcdocs { | ||
| 763 | my ($this,$func,$fout) = @_; | ||
| 764 | my $hash = $this->ensuredb; | ||
| 765 | barf "unknown function '$func'" unless defined($hash->{$func}); | ||
| 766 | my $file = $hash->{$func}->{File}; | ||
| 767 | my $dbf = $hash->{$func}->{Dbfile}; | ||
| 768 | if (!File::Spec->file_name_is_absolute($file) && $dbf) { | ||
| 769 | $file = File::Spec->rel2abs($file, dirname($dbf)); | ||
| 770 | } | ||
| 771 | funcdocs_fromfile($func,$file,$fout); | ||
| 772 | } | ||
| 773 | |||
| 774 | =head1 FUNCTIONS | ||
| 775 | |||
| 776 | =cut | ||
| 777 | |||
| 778 | sub funcdocs_fromfile { | ||
| 779 | my ($func,$file) = @_; | ||
| 780 | barf "can't find file '$file'" unless -f $file; | ||
| 781 | local $SIG{PIPE}= sub {}; # Prevent crashing if user exits the pager | ||
| 782 | my $in = new IO::File $file; | ||
| 783 | my $out = ($#_ > 1 && defined($_[2])) ? $_[2] : | ||
| 784 | new IO::File "| pod2text | $PDL::Doc::pager"; | ||
| 785 | barf "can't open file $file" unless $in; | ||
| 786 | barf "can't open output handle" unless $out; | ||
| 787 | getfuncdocs($func,$in,$out); | ||
| 788 | |||
| 789 | if (ref $out eq 'GLOB') { | ||
| 790 | print $out "Docs from $file\n\n"; } else { | ||
| 791 | $out->print("Docs from $file\n\n"); } | ||
| 792 | } | ||
| 793 | |||
| 794 | sub extrdoc { | ||
| 795 | my ($func,$file) = @_; | ||
| 796 | open my $out, '>', \(my $out_text); | ||
| 797 | funcdocs_fromfile($func,$file,$out); | ||
| 798 | return $out_text; | ||
| 799 | } | ||
| 800 | |||
| 801 | sub getfuncdocs { | ||
| 802 | my ($func,$in,$out) = @_; | ||
| 803 | my $parser = Pod::Select->new; | ||
| 804 | # $parser->select("\\(METHODS\\|OPERATORS\\|CONSTRUCTORS\\|FUNCTIONS\\|METHODS\\)/$func(\\(.*\\)*\\s*"); | ||
| 805 | foreach my $foo(qw/FUNCTIONS OPERATORS CONSTRUCTORS METHODS/) { | ||
| 806 | seek $in,0,0; | ||
| 807 | $parser->select("$foo/$func(\\(.*\\))*\\s*"); | ||
| 808 | $parser->parse_from_filehandle($in,$out); | ||
| 809 | } | ||
| 810 | } | ||
| 811 | |||
| 812 | |||
| 813 | =head2 add_module | ||
| 814 | |||
| 815 | =for usage | ||
| 816 | |||
| 817 | use PDL::Doc; PDL::Doc::add_module("my::module"); | ||
| 818 | |||
| 819 | =for ref | ||
| 820 | |||
| 821 | The C<add_module> function allows you to add POD from a particular Perl | ||
| 822 | module that you've installed somewhere in @INC. It searches for the | ||
| 823 | active PDL document database and the module's .pod and .pm files, and | ||
| 824 | scans and indexes the module into the database. | ||
| 825 | |||
| 826 | C<add_module> is meant to be added to your module's Makefile as part of the | ||
| 827 | installation script. | ||
| 828 | |||
| 829 | =cut | ||
| 830 | |||
| 831 | package PDL::Doc; | ||
| 832 | sub add_module { | ||
| 833 | my($module) = shift; | ||
| 834 | |||
| 835 | use File::Copy qw{copy}; | ||
| 836 | |||
| 837 | my($dir, $file, $pdldoc); | ||
| 838 | local($_); | ||
| 839 | |||
| 840 | DIRECTORY: | ||
| 841 | for(@INC){ | ||
| 842 | $dir = $_; | ||
| 843 | $file = $dir."/PDL/pdldoc.db"; | ||
| 844 | if( -f $file) { | ||
| 845 | if(! -w "$dir/PDL") { | ||
| 846 | die "No write permission at $dir/PDL - not updating docs database.\n"; | ||
| 847 | } | ||
| 848 | |||
| 849 | print "Found docs database $file\n"; | ||
| 850 | $pdldoc = new ("PDL::Doc",($file)); | ||
| 851 | last DIRECTORY; | ||
| 852 | } | ||
| 853 | } | ||
| 854 | |||
| 855 | die "Unable to find docs database - therefore not updating it.\n" unless($pdldoc); | ||
| 856 | |||
| 857 | my $mfile = $module; | ||
| 858 | $mfile =~ s/\:\:/\//g; | ||
| 859 | for(@INC){ | ||
| 860 | my $postfix; | ||
| 861 | my $hit = 0; | ||
| 862 | for $postfix(".pm",".pod") { | ||
| 863 | my $f = "$_/$mfile$postfix"; | ||
| 864 | if( -e $f ){ | ||
| 865 | $pdldoc->ensuredb(); | ||
| 866 | $pdldoc->scan($f); | ||
| 867 | eval { $pdldoc->savedb(); }; | ||
| 868 | warn $@ if $@; | ||
| 869 | print "PDL docs database updated - added $f.\n"; | ||
| 870 | $hit = 1; | ||
| 871 | } | ||
| 872 | } | ||
| 873 | return if($hit); | ||
| 874 | } | ||
| 875 | |||
| 876 | die "Unable to find a .pm or .pod file in \@INC for module $module\n"; | ||
| 877 | } | ||
| 878 | |||
| 879 | 1; | ||
| 880 | |||
| 881 | =head1 PDL::DOC EXAMPLE | ||
| 882 | |||
| 883 | Here's an example of how you might use the PDL Doc database in your | ||
| 884 | own code. | ||
| 885 | |||
| 886 | use PDL::Doc; | ||
| 887 | # Find the pdl documentation | ||
| 888 | my ($dir,$file,$pdldoc); | ||
| 889 | DIRECTORY: for $dir (@INC) { | ||
| 890 | $file = $dir."/PDL/pdldoc.db"; | ||
| 891 | if (-f $file) { | ||
| 892 | print "Found docs database $file\n"; | ||
| 893 | $pdldoc = new PDL::Doc ($file); | ||
| 894 | last DIRECTORY; | ||
| 895 | } | ||
| 896 | } | ||
| 897 | |||
| 898 | die ("Unable to find docs database!\n") unless $pdldoc; | ||
| 899 | |||
| 900 | # Print the reference line for zeroes: | ||
| 901 | print $pdldoc->gethash->{zeroes}->{Ref}; | ||
| 902 | |||
| 903 | # See which examples use zeroes | ||
| 904 | $pdldoc->search('zeroes', 'Example', 1); | ||
| 905 | |||
| 906 | # All the functions that use zeroes in their example: | ||
| 907 | my @entries = $pdldoc->search('zeroes', 'Example', 1); | ||
| 908 | print "Functions that use 'zeroes' in their examples include:\n"; | ||
| 909 | foreach my $entry (@entries) { | ||
| 910 | # Unpack the entry | ||
| 911 | my ($func_name, $sym_hash) = @$entry; | ||
| 912 | print "$func_name\n"; | ||
| 913 | } | ||
| 914 | |||
| 915 | print "\n"; | ||
| 916 | |||
| 917 | # Let's look at the function 'mpdl' | ||
| 918 | @entries = $pdldoc->search('mpdl', 'Name'); | ||
| 919 | # I know there's only one: | ||
| 920 | my $entry = $entries[0]; | ||
| 921 | my ($func_name, $sym_hash) = @$entry; | ||
| 922 | print "mpdl info:\n"; | ||
| 923 | foreach my $key (keys %$sym_hash) { | ||
| 924 | # Unpack the entry | ||
| 925 | print "---$key---\n$sym_hash->{$key}\n"; | ||
| 926 | } | ||
| 927 | |||
| 928 | =head2 Finding Modules | ||
| 929 | |||
| 930 | How can you tell if you've gotten a module for one of your entries? | ||
| 931 | The Ref entry will begin with 'Module:' if it's a module. In code: | ||
| 932 | |||
| 933 | # Prints: | ||
| 934 | # Module: fundamental PDL functionality | ||
| 935 | my $sym_hash = $pdldoc->gethash; | ||
| 936 | print $pdldoc->gethash->{'PDL::Core'}->{Ref}, "\n" | ||
| 937 | |||
| 938 | =head1 BUGS | ||
| 939 | |||
| 940 | Quite a few shortcomings which will hopefully be fixed following | ||
| 941 | discussions on the pdl-devel mailing list. | ||
| 942 | |||
| 943 | =head1 AUTHOR | ||
| 944 | |||
| 945 | Copyright 1997 Christian Soeller E<lt>c.soeller@auckland.ac.nzE<gt> | ||
| 946 | and Karl Glazebrook E<lt>kgb@aaoepp.aao.gov.auE<gt> | ||
| 947 | |||
| 948 | Further contributions copyright 2010 David Mertens | ||
| 949 | E<lt>dcmertens.perl@gmail.comE<gt> | ||
| 950 | |||
| 951 | All rights reserved. There is no warranty. You are allowed | ||
| 952 | to redistribute this software / documentation under certain | ||
| 953 | conditions. For details, see the file COPYING in the PDL | ||
| 954 | distribution. If this file is separated from the PDL distribution, | ||
| 955 | the copyright notice should be included in the file. | ||
| 956 | |||
| 957 | =cut | ||
| 958 | |||
| 959 | 1; |