| 1 | # Copyrights 2003-2021 by [Mark Overmeer]. | ||
| 2 | # For other contributors see ChangeLog. | ||
| 3 | # See the manual pages for details on the licensing terms. | ||
| 4 | # Pod stripped from pm file by OODoc 2.02. | ||
| 5 | # This code is part of perl distribution OODoc. It is licensed under the | ||
| 6 | # same terms as Perl itself: https://spdx.org/licenses/Artistic-2.0.html | ||
| 7 | |||
| 8 | package OODoc::Format::Pod; | ||
| 9 | use vars '$VERSION'; | ||
| 10 | $VERSION = '2.02'; | ||
| 11 | |||
| 12 | use base 'OODoc::Format'; | ||
| 13 | |||
| 14 | use strict; | ||
| 15 | use warnings; | ||
| 16 | |||
| 17 | use Log::Report 'oodoc'; | ||
| 18 | |||
| 19 | use File::Spec (); | ||
| 20 | use List::Util qw/max/; | ||
| 21 | use Pod::Escapes qw/e2char/; | ||
| 22 | |||
| 23 | |||
| 24 | sub link($$;$) | ||
| 25 | { my ($self, $manual, $object, $text) = @_; | ||
| 26 | |||
| 27 | $object = $object->subroutine if $object->isa('OODoc::Text::Option'); | ||
| 28 | $object = $object->subroutine if $object->isa('OODoc::Text::Default'); | ||
| 29 | $object = $object->container if $object->isa('OODoc::Text::Example'); | ||
| 30 | $object = $object->container if $object->isa('OODoc::Text::Subroutine'); | ||
| 31 | $text = defined $text ? "$text|" : ''; | ||
| 32 | |||
| 33 | return "L<$text$object>" | ||
| 34 | if $object->isa('OODoc::Manual'); | ||
| 35 | |||
| 36 | $object->isa('OODoc::Text::Structure') | ||
| 37 | or error __x"cannot link to a {pkg}", pkg => ref $object; | ||
| 38 | |||
| 39 | my $manlink = defined $manual ? $object->manual.'/' : ''; | ||
| 40 | qq(L<$text$manlink"$object">); | ||
| 41 | } | ||
| 42 | |||
| 43 | |||
| 44 | sub createManual($@) | ||
| 45 | { my ($self, %args) = @_; | ||
| 46 | my $manual = $args{manual} or panic; | ||
| 47 | my $options = $args{format_options} || []; | ||
| 48 | |||
| 49 | my $podname = $manual->source; | ||
| 50 | $podname =~ s/\.pm$/.pod/; | ||
| 51 | my $tmpname = $podname . 't'; | ||
| 52 | |||
| 53 | my $tmpfile = File::Spec->catfile($self->workdir, $tmpname); | ||
| 54 | my $podfile = File::Spec->catfile($self->workdir, $podname); | ||
| 55 | |||
| 56 | my $output = IO::File->new($tmpfile, "w") | ||
| 57 | or fault __x"cannot write prelimary pod manual to {file}" | ||
| 58 | , file => $tmpfile; | ||
| 59 | |||
| 60 | $self->formatManual | ||
| 61 | ( manual => $manual | ||
| 62 | , output => $output | ||
| 63 | , append => $args{append} | ||
| 64 | , @$options | ||
| 65 | ); | ||
| 66 | |||
| 67 | $output->close; | ||
| 68 | |||
| 69 | $self->cleanupPOD($tmpfile, $podfile); | ||
| 70 | unlink $tmpfile; | ||
| 71 | |||
| 72 | $self->manifest->add($podfile); | ||
| 73 | |||
| 74 | $self; | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | sub formatManual(@) | ||
| 79 | { my $self = shift; | ||
| 80 | $self->chapterName(@_); | ||
| 81 | $self->chapterInheritance(@_); | ||
| 82 | $self->chapterSynopsis(@_); | ||
| 83 | $self->chapterDescription(@_); | ||
| 84 | $self->chapterOverloaded(@_); | ||
| 85 | $self->chapterMethods(@_); | ||
| 86 | $self->chapterExports(@_); | ||
| 87 | $self->chapterDetails(@_); | ||
| 88 | $self->chapterDiagnostics(@_); | ||
| 89 | $self->chapterReferences(@_); | ||
| 90 | $self->chapterCopyrights(@_); | ||
| 91 | $self->showAppend(@_); | ||
| 92 | $self; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub showAppend(@) | ||
| 96 | { my ($self, %args) = @_; | ||
| 97 | my $append = $args{append}; | ||
| 98 | |||
| 99 | if(!defined $append) { ; } | ||
| 100 | elsif(ref $append eq 'CODE') { $append->(formatter => $self, %args) } | ||
| 101 | else | ||
| 102 | { my $output = $args{output} or panic; | ||
| 103 | $output->print($append); | ||
| 104 | } | ||
| 105 | |||
| 106 | $self; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub showStructureExpand(@) | ||
| 110 | { my ($self, %args) = @_; | ||
| 111 | |||
| 112 | my $examples = $args{show_chapter_examples} || 'EXPAND'; | ||
| 113 | my $text = $args{structure} or panic; | ||
| 114 | |||
| 115 | my $name = $text->name; | ||
| 116 | my $level = $text->level; | ||
| 117 | my $output = $args{output} or panic; | ||
| 118 | my $manual = $args{manual} or panic; | ||
| 119 | |||
| 120 | my $descr = $self->cleanup($manual, $text->description); | ||
| 121 | $output->print("\n=head$level $name\n\n$descr"); | ||
| 122 | |||
| 123 | $self->showSubroutines(%args, subroutines => [$text->subroutines]); | ||
| 124 | $self->showExamples(%args, examples => [$text->examples]) | ||
| 125 | if $examples eq 'EXPAND'; | ||
| 126 | |||
| 127 | return $self; | ||
| 128 | } | ||
| 129 | |||
| 130 | sub showStructureRefer(@) | ||
| 131 | { my ($self, %args) = @_; | ||
| 132 | |||
| 133 | my $text = $args{structure} or panic; | ||
| 134 | |||
| 135 | my $name = $text->name; | ||
| 136 | my $level = $text->level; | ||
| 137 | my $output = $args{output} or panic; | ||
| 138 | my $manual = $args{manual} or panic; | ||
| 139 | |||
| 140 | my $link = $self->link($manual, $text); | ||
| 141 | $output->print("\n=head$level $name\n\nSee $link.\n"); | ||
| 142 | $self; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub chapterDescription(@) | ||
| 146 | { my ($self, %args) = @_; | ||
| 147 | |||
| 148 | $self->showRequiredChapter(DESCRIPTION => %args); | ||
| 149 | |||
| 150 | my $manual = $args{manual} or panic; | ||
| 151 | my $details = $manual->chapter('DETAILS'); | ||
| 152 | |||
| 153 | return $self unless defined $details; | ||
| 154 | |||
| 155 | my $output = $args{output} or panic; | ||
| 156 | $output->print("\nSee L</DETAILS> chapter below\n"); | ||
| 157 | $self->showChapterIndex($output, $details, " "); | ||
| 158 | } | ||
| 159 | |||
| 160 | sub chapterDiagnostics(@) | ||
| 161 | { my ($self, %args) = @_; | ||
| 162 | my $manual = $args{manual} or panic; | ||
| 163 | |||
| 164 | my $diags = $manual->chapter('DIAGNOSTICS'); | ||
| 165 | $self->showChapter(chapter => $diags, %args) | ||
| 166 | if defined $diags; | ||
| 167 | |||
| 168 | my @diags = map {$_->diagnostics} $manual->subroutines; | ||
| 169 | return unless @diags; | ||
| 170 | |||
| 171 | my $output = $args{output} or panic; | ||
| 172 | $diags | ||
| 173 | or $output->print("\n=head1 DIAGNOSTICS\n"); | ||
| 174 | |||
| 175 | $output->print("\n=over 4\n\n"); | ||
| 176 | $self->showDiagnostics(%args, diagnostics => \@diags); | ||
| 177 | $output->print("\n=back\n\n"); | ||
| 178 | $self; | ||
| 179 | } | ||
| 180 | |||
| 181 | |||
| 182 | sub showChapterIndex($$;$) | ||
| 183 | { my ($self, $output, $chapter, $indent) = @_; | ||
| 184 | $indent = '' unless defined $indent; | ||
| 185 | |||
| 186 | foreach my $section ($chapter->sections) | ||
| 187 | { $output->print($indent, $section->name, "\n"); | ||
| 188 | foreach my $subsection ($section->subsections) | ||
| 189 | { $output->print($indent, $indent, $subsection->name, "\n"); | ||
| 190 | } | ||
| 191 | } | ||
| 192 | $self; | ||
| 193 | } | ||
| 194 | |||
| 195 | sub showExamples(@) | ||
| 196 | { my ($self, %args) = @_; | ||
| 197 | my $examples = $args{examples} or panic; | ||
| 198 | return unless @$examples; | ||
| 199 | |||
| 200 | my $manual = $args{manual} or panic; | ||
| 201 | my $output = $args{output} or panic; | ||
| 202 | |||
| 203 | foreach my $example (@$examples) | ||
| 204 | { my $name = $self->cleanup($manual, $example->name); | ||
| 205 | $output->print("\nexample: $name\n\n"); | ||
| 206 | $output->print($self->cleanup($manual, $example->description)); | ||
| 207 | $output->print("\n"); | ||
| 208 | } | ||
| 209 | $self; | ||
| 210 | } | ||
| 211 | |||
| 212 | sub showDiagnostics(@) | ||
| 213 | { my ($self, %args) = @_; | ||
| 214 | my $diagnostics = $args{diagnostics} or panic; | ||
| 215 | return unless @$diagnostics; | ||
| 216 | |||
| 217 | my $manual = $args{manual} or panic; | ||
| 218 | my $output = $args{output} or panic; | ||
| 219 | |||
| 220 | foreach my $diag (sort @$diagnostics) | ||
| 221 | { my $name = $self->cleanup($manual, $diag->name); | ||
| 222 | my $type = $diag->type; | ||
| 223 | $output->print("\n=item $type: $name\n\n"); | ||
| 224 | $output->print($self->cleanup($manual, $diag->description)); | ||
| 225 | $output->print("\n"); | ||
| 226 | } | ||
| 227 | $self; | ||
| 228 | } | ||
| 229 | |||
| 230 | sub showSubroutines(@) | ||
| 231 | { my ($self, %args) = @_; | ||
| 232 | my $subs = $args{subroutines} || []; | ||
| 233 | @$subs or return; | ||
| 234 | |||
| 235 | my $output = $args{output} or panic; | ||
| 236 | |||
| 237 | $output->print("\n=over 4\n\n"); | ||
| 238 | $self->SUPER::showSubroutines(%args); | ||
| 239 | $output->print("\n=back\n\n"); | ||
| 240 | } | ||
| 241 | |||
| 242 | sub showSubroutine(@) | ||
| 243 | { my $self = shift; | ||
| 244 | $self->SUPER::showSubroutine(@_); | ||
| 245 | |||
| 246 | my %args = @_; | ||
| 247 | my $output = $args{output} or panic; | ||
| 248 | $output->print("\n"); | ||
| 249 | $self; | ||
| 250 | } | ||
| 251 | |||
| 252 | sub showSubroutineUse(@) | ||
| 253 | { my ($self, %args) = @_; | ||
| 254 | my $subroutine = $args{subroutine} or panic; | ||
| 255 | my $manual = $args{manual} or panic; | ||
| 256 | my $output = $args{output} or panic; | ||
| 257 | |||
| 258 | my $use = $self->subroutineUse($manual, $subroutine); | ||
| 259 | $use =~ s/(.+)/=item $1\n\n/gm; | ||
| 260 | |||
| 261 | $output->print($use); | ||
| 262 | $output->print("Inherited, see ". $self->link($manual, $subroutine)."\n\n") | ||
| 263 | if $manual->inherited($subroutine); | ||
| 264 | |||
| 265 | $self; | ||
| 266 | } | ||
| 267 | |||
| 268 | sub subroutineUse($$) | ||
| 269 | { my ($self, $manual, $subroutine) = @_; | ||
| 270 | my $type = $subroutine->type; | ||
| 271 | my $name = $self->cleanup($manual, $subroutine->name); | ||
| 272 | my $paramlist = $self->cleanup($manual, $subroutine->parameters); | ||
| 273 | my $params | ||
| 274 | = !length $paramlist ? '()' | ||
| 275 | : $paramlist =~ m/^[\[<]|[\]>]$/ ? "( $paramlist )" | ||
| 276 | : "($paramlist)"; | ||
| 277 | |||
| 278 | my $class = $manual->package; | ||
| 279 | my $use | ||
| 280 | = $type eq 'i_method' ? qq[\$obj-E<gt>B<$name>$params] | ||
| 281 | : $type eq 'c_method' ? qq[$class-E<gt>B<$name>$params] | ||
| 282 | : $type eq 'ci_method'? qq[\$obj-E<gt>B<$name>$params\n] | ||
| 283 | . qq[$class-E<gt>B<$name>$params] | ||
| 284 | : $type eq 'function' ? qq[B<$name>$params] | ||
| 285 | : $type eq 'overload' ? qq[overload: B<$name>] | ||
| 286 | : $type eq 'tie' ? qq[B<$name>$params] | ||
| 287 | : ''; | ||
| 288 | |||
| 289 | length $use | ||
| 290 | or warn "WARNING: unknown subroutine type $type for $name in $manual"; | ||
| 291 | |||
| 292 | $use; | ||
| 293 | } | ||
| 294 | |||
| 295 | sub showSubroutineName(@) | ||
| 296 | { my ($self, %args) = @_; | ||
| 297 | my $subroutine = $args{subroutine} or panic; | ||
| 298 | my $manual = $args{manual} or panic; | ||
| 299 | my $output = $args{output} or panic; | ||
| 300 | my $name = $subroutine->name; | ||
| 301 | |||
| 302 | my $url | ||
| 303 | = $manual->inherited($subroutine) | ||
| 304 | ? "M<".$subroutine->manual."::$name>" | ||
| 305 | : "M<$name>"; | ||
| 306 | |||
| 307 | $output->print | ||
| 308 | ( $self->cleanup($manual, $url) | ||
| 309 | , ($args{last} ? ".\n" : ",\n") | ||
| 310 | ); | ||
| 311 | } | ||
| 312 | |||
| 313 | sub showOptions(@) | ||
| 314 | { my ($self, %args) = @_; | ||
| 315 | my $output = $args{output} or panic; | ||
| 316 | $output->print("\n=over 2\n\n"); | ||
| 317 | $self->SUPER::showOptions(%args); | ||
| 318 | $output->print("\n=back\n\n"); | ||
| 319 | } | ||
| 320 | |||
| 321 | sub showOptionUse(@) | ||
| 322 | { my ($self, %args) = @_; | ||
| 323 | my $output = $args{output} or panic; | ||
| 324 | my $option = $args{option} or panic; | ||
| 325 | my $manual = $args{manual} or panic; | ||
| 326 | |||
| 327 | my $params = $option->parameters; | ||
| 328 | $params =~ s/\s+$//; | ||
| 329 | $params =~ s/^\s+//; | ||
| 330 | $params = " => ".$self->cleanup($manual, $params) if length $params; | ||
| 331 | |||
| 332 | $output->print("=item $option$params\n\n"); | ||
| 333 | $self; | ||
| 334 | } | ||
| 335 | |||
| 336 | sub showOptionExpand(@) | ||
| 337 | { my ($self, %args) = @_; | ||
| 338 | my $output = $args{output} or panic; | ||
| 339 | my $option = $args{option} or panic; | ||
| 340 | my $manual = $args{manual} or panic; | ||
| 341 | |||
| 342 | $self->showOptionUse(%args); | ||
| 343 | |||
| 344 | my $where = $option->findDescriptionObject or return $self; | ||
| 345 | my $descr = $self->cleanup($manual, $where->description); | ||
| 346 | $output->print("\n$descr\n\n") | ||
| 347 | if length $descr; | ||
| 348 | |||
| 349 | $self; | ||
| 350 | } | ||
| 351 | |||
| 352 | |||
| 353 | sub writeTable($@) | ||
| 354 | { my ($self, %args) = @_; | ||
| 355 | |||
| 356 | my $head = $args{header} or panic; | ||
| 357 | my $output = $args{output} or panic; | ||
| 358 | my $rows = $args{rows} or panic; | ||
| 359 | return unless @$rows; | ||
| 360 | |||
| 361 | # Convert all elements to plain text, because markup is not | ||
| 362 | # allowed in verbatim pod blocks. | ||
| 363 | my @rows; | ||
| 364 | foreach my $row (@$rows) | ||
| 365 | { push @rows, [ map {$self->removeMarkup($_)} @$row ]; | ||
| 366 | } | ||
| 367 | |||
| 368 | # Compute column widths | ||
| 369 | my @w = (0) x @$head; | ||
| 370 | |||
| 371 | foreach my $row ($head, @rows) | ||
| 372 | { $w[$_] = max $w[$_], length($row->[$_]) | ||
| 373 | foreach 0..$#$row; | ||
| 374 | } | ||
| 375 | |||
| 376 | if(my $widths = $args{widths}) | ||
| 377 | { defined $widths->[$_] && $widths->[$_] > $w[$_] && ($w[$_] = $widths->[$_]) | ||
| 378 | for 0..$#$rows; | ||
| 379 | } | ||
| 380 | |||
| 381 | pop @w; # ignore width of last column | ||
| 382 | |||
| 383 | # Table head | ||
| 384 | my $headf = " -".join("--", map { "\%-${_}s" } @w)."--%s\n"; | ||
| 385 | $output->printf($headf, @$head); | ||
| 386 | |||
| 387 | # Table body | ||
| 388 | my $format = " ".join(" ", map { "\%-${_}s" } @w)." %s\n"; | ||
| 389 | $output->printf($format, @$_) | ||
| 390 | for @rows; | ||
| 391 | } | ||
| 392 | |||
| 393 | |||
| 394 | sub removeMarkup($) | ||
| 395 | { my ($self, $string) = @_; | ||
| 396 | my $out = $self->_removeMarkup($string); | ||
| 397 | for($out) | ||
| 398 | { s/^\s+//gm; | ||
| 399 | s/\s+$//gm; | ||
| 400 | s/\s{2,}/ /g; | ||
| 401 | s/\[NB\]/ /g; | ||
| 402 | } | ||
| 403 | $out; | ||
| 404 | } | ||
| 405 | |||
| 406 | sub _removeMarkup($) | ||
| 407 | { my ($self, $string) = @_; | ||
| 408 | |||
| 409 | my $out = ''; | ||
| 410 | while($string =~ s/(.*?) # before | ||
| 411 | ([BCEFILSXZ]) # known formatting codes | ||
| 412 | ([<]+) # capture ALL starters | ||
| 413 | //x) | ||
| 414 | { $out .= $1; | ||
| 415 | my ($tag, $bracks, $brack_count) = ($2, $3, length($3)); | ||
| 416 | |||
| 417 | if($string !~ s/^(|.*?[^>]) # contained | ||
| 418 | [>]{$brack_count} | ||
| 419 | (?![>]) | ||
| 420 | //xs) | ||
| 421 | { $out .= "$tag$bracks"; | ||
| 422 | next; | ||
| 423 | } | ||
| 424 | |||
| 425 | my $container = $1; | ||
| 426 | if($tag =~ m/[XZ]/) { ; } # ignore container content | ||
| 427 | elsif($tag =~ m/[BCI]/) # cannot display, but can be nested | ||
| 428 | { $out .= $self->_removeMarkup($container); | ||
| 429 | } | ||
| 430 | elsif($tag eq 'E') { $out .= e2char($container) } | ||
| 431 | elsif($tag eq 'F') { $out .= $container } | ||
| 432 | elsif($tag eq 'L') | ||
| 433 | { if($container =~ m!^\s*([^/|]*)\|!) | ||
| 434 | { $out .= $self->_removeMarkup($1); | ||
| 435 | next; | ||
| 436 | } | ||
| 437 | |||
| 438 | my ($man, $chapter) = ($container, ''); | ||
| 439 | if($container =~ m!^\s*([^/]*)/\"([^"]*)\"\s*$!) | ||
| 440 | { ($man, $chapter) = ($1, $2); | ||
| 441 | } | ||
| 442 | elsif($container =~ m!^\s*([^/]*)/(.*?)\s*$!) | ||
| 443 | { ($man, $chapter) = ($1, $2); | ||
| 444 | } | ||
| 445 | |||
| 446 | $out .= | ||
| 447 | ( !length $man ? "section $chapter" | ||
| 448 | : !length $chapter ? $man | ||
| 449 | : "$man section $chapter" | ||
| 450 | ); | ||
| 451 | } | ||
| 452 | elsif($tag eq 'S') | ||
| 453 | { my $clean = $self->_removeMarkup($container); | ||
| 454 | $clean =~ s/ /[NB]/g; | ||
| 455 | $out .= $clean; | ||
| 456 | } | ||
| 457 | } | ||
| 458 | |||
| 459 | $out . $string; | ||
| 460 | } | ||
| 461 | |||
| 462 | sub showSubroutineDescription(@) | ||
| 463 | { my ($self, %args) = @_; | ||
| 464 | my $manual = $args{manual} or panic; | ||
| 465 | my $subroutine = $args{subroutine} or panic; | ||
| 466 | |||
| 467 | my $text = $self->cleanup($manual, $subroutine->description); | ||
| 468 | return $self unless length $text; | ||
| 469 | |||
| 470 | my $output = $args{output} or panic; | ||
| 471 | $output->print("\n", $text); | ||
| 472 | |||
| 473 | my $extends = $self->extends or return $self; | ||
| 474 | my $refer = $extends->findDescriptionObject or return $self; | ||
| 475 | $self->showSubroutineDescriptionRefer(%args, subroutine => $refer); | ||
| 476 | } | ||
| 477 | |||
| 478 | sub showSubroutineDescriptionRefer(@) | ||
| 479 | { my ($self, %args) = @_; | ||
| 480 | my $manual = $args{manual} or panic; | ||
| 481 | my $subroutine = $args{subroutine} or panic; | ||
| 482 | my $output = $args{output} or panic; | ||
| 483 | $output->print("\nInherited, see ",$self->link($manual, $subroutine),"\n"); | ||
| 484 | } | ||
| 485 | |||
| 486 | sub showSubsIndex() {;} | ||
| 487 | |||
| 488 | |||
| 489 | sub cleanupPOD($$) | ||
| 490 | { my ($self, $infn, $outfn) = @_; | ||
| 491 | my $in = IO::File->new($infn, 'r') | ||
| 492 | or fault __x"cannot read prelimary pod from {file}", file => $infn; | ||
| 493 | |||
| 494 | my $out = IO::File->new($outfn, 'w') | ||
| 495 | or fault __x"cannot write final pod to {file}", file => $outfn; | ||
| 496 | |||
| 497 | my $last_is_blank = 1; | ||
| 498 | LINE: | ||
| 499 | while(my $l = $in->getline) | ||
| 500 | { if($l =~ m/^\s*$/s) | ||
| 501 | { next LINE if $last_is_blank; | ||
| 502 | $last_is_blank = 1; | ||
| 503 | } | ||
| 504 | else | ||
| 505 | { $last_is_blank = 0; | ||
| 506 | } | ||
| 507 | |||
| 508 | $out->print($l); | ||
| 509 | } | ||
| 510 | |||
| 511 | $in->close; | ||
| 512 | $out->close | ||
| 513 | or fault __x"write to {file} failed", file => $outfn; | ||
| 514 | |||
| 515 | $self; | ||
| 516 | } | ||
| 517 | |||
| 518 | |||
| 519 | 1; |