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