| 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::Parser::Markov; | ||
| 9 | use vars '$VERSION'; | ||
| 10 | $VERSION = '2.02'; | ||
| 11 | |||
| 12 | use base 'OODoc::Parser'; | ||
| 13 | |||
| 14 | use strict; | ||
| 15 | use warnings; | ||
| 16 | |||
| 17 | use Log::Report 'oodoc'; | ||
| 18 | |||
| 19 | use OODoc::Text::Chapter; | ||
| 20 | use OODoc::Text::Section; | ||
| 21 | use OODoc::Text::SubSection; | ||
| 22 | use OODoc::Text::SubSubSection; | ||
| 23 | use OODoc::Text::Subroutine; | ||
| 24 | use OODoc::Text::Option; | ||
| 25 | use OODoc::Text::Default; | ||
| 26 | use OODoc::Text::Diagnostic; | ||
| 27 | use OODoc::Text::Example; | ||
| 28 | use OODoc::Manual; | ||
| 29 | |||
| 30 | use File::Spec; | ||
| 31 | use IO::File; | ||
| 32 | |||
| 33 | my $url_modsearch = "http://search.cpan.org/perldoc?"; | ||
| 34 | my $url_coderoot = 'CODE'; | ||
| 35 | |||
| 36 | |||
| 37 | #------------------------------------------- | ||
| 38 | |||
| 39 | my @default_rules = | ||
| 40 | ( [ '=cut' => 'docCut' ] | ||
| 41 | , [ '=chapter' => 'docChapter' ] | ||
| 42 | , [ '=section' => 'docSection' ] | ||
| 43 | , [ '=subsection' => 'docSubSection' ] | ||
| 44 | , [ '=subsubsection' => 'docSubSubSection' ] | ||
| 45 | , [ '=method' => 'docSubroutine' ] | ||
| 46 | , [ '=i_method' => 'docSubroutine' ] | ||
| 47 | , [ '=c_method' => 'docSubroutine' ] | ||
| 48 | , [ '=ci_method' => 'docSubroutine' ] | ||
| 49 | , [ '=function' => 'docSubroutine' ] | ||
| 50 | , [ '=tie' => 'docSubroutine' ] | ||
| 51 | , [ '=overload' => 'docSubroutine' ] | ||
| 52 | , [ '=option' => 'docOption' ] | ||
| 53 | , [ '=default' => 'docDefault' ] | ||
| 54 | , [ '=requires' => 'docRequires' ] | ||
| 55 | , [ '=example' => 'docExample' ] | ||
| 56 | , [ '=examples' => 'docExample' ] | ||
| 57 | , [ '=error' => 'docDiagnostic' ] | ||
| 58 | , [ '=warning' => 'docDiagnostic' ] | ||
| 59 | , [ '=notice' => 'docDiagnostic' ] | ||
| 60 | , [ '=debug' => 'docDiagnostic' ] | ||
| 61 | |||
| 62 | # deprecated | ||
| 63 | , [ '=head1' => 'docChapter' ] | ||
| 64 | , [ '=head2' => 'docSection' ] | ||
| 65 | , [ '=head3' => 'docSubSection' ] | ||
| 66 | |||
| 67 | # problem spotter | ||
| 68 | , [ qr/^(warn|die|carp|confess|croak)\s/ => 'debugRemains' ] | ||
| 69 | , [ qr/^( sub \s+ \w | ||
| 70 | | (?:my|our) \s+ [\($@%] | ||
| 71 | | (?:package|use) \s+ \w+\: | ||
| 72 | ) | ||
| 73 | /x => 'forgotCut' ] | ||
| 74 | ); | ||
| 75 | |||
| 76 | |||
| 77 | sub init($) | ||
| 78 | { my ($self, $args) = @_; | ||
| 79 | $self->SUPER::init($args) or return; | ||
| 80 | |||
| 81 | my @rules = @default_rules; | ||
| 82 | unshift @rules, @{delete $args->{additional_rules}} | ||
| 83 | if exists $args->{additional_rules}; | ||
| 84 | |||
| 85 | $self->{OP_rules} = []; | ||
| 86 | $self->rule(@$_) for @rules; | ||
| 87 | $self; | ||
| 88 | } | ||
| 89 | |||
| 90 | #------------------------------------------- | ||
| 91 | |||
| 92 | |||
| 93 | sub rule($$) | ||
| 94 | { my ($self, $match, $action) = @_; | ||
| 95 | push @{$self->{OP_rules}}, [$match, $action]; | ||
| 96 | $self; | ||
| 97 | } | ||
| 98 | |||
| 99 | #------------------------------------------- | ||
| 100 | |||
| 101 | |||
| 102 | sub findMatchingRule($) | ||
| 103 | { my ($self, $line) = @_; | ||
| 104 | |||
| 105 | foreach ( @{$self->{OP_rules}} ) | ||
| 106 | { my ($match, $action) = @$_; | ||
| 107 | if(ref $match) | ||
| 108 | { return ($&, $action) if $line =~ $match; | ||
| 109 | } | ||
| 110 | elsif(substr($line, 0, length($match)) eq $match) | ||
| 111 | { return ($match, $action); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | (); | ||
| 116 | } | ||
| 117 | |||
| 118 | |||
| 119 | sub parse(@) | ||
| 120 | { my ($self, %args) = @_; | ||
| 121 | |||
| 122 | my $input = $args{input} | ||
| 123 | or error __x"no input file to parse specified"; | ||
| 124 | |||
| 125 | my $output = $args{output} || File::Spec->devnull; | ||
| 126 | my $version = $args{version} or panic; | ||
| 127 | my $distr = $args{distribution} or panic; | ||
| 128 | |||
| 129 | my $in = IO::File->new($input, 'r') | ||
| 130 | or die "ERROR: cannot read document from $input: $!\n"; | ||
| 131 | |||
| 132 | my $out = IO::File->new($output, 'w') | ||
| 133 | or die "ERROR: cannot write stripped code to $output: $!\n"; | ||
| 134 | |||
| 135 | # pure doc files have no package statement included, so it shall | ||
| 136 | # be created beforehand. | ||
| 137 | |||
| 138 | my ($manual, @manuals); | ||
| 139 | |||
| 140 | my $pure_pod = $input =~ m/\.pod$/; | ||
| 141 | if($pure_pod) | ||
| 142 | { $manual = OODoc::Manual->new | ||
| 143 | ( package => $self->filenameToPackage($input) | ||
| 144 | , pure_pod => 1 | ||
| 145 | , source => $input | ||
| 146 | , parser => $self | ||
| 147 | |||
| 148 | , distribution => $distr | ||
| 149 | , version => $version | ||
| 150 | ); | ||
| 151 | |||
| 152 | push @manuals, $manual; | ||
| 153 | $self->currentManual($manual); | ||
| 154 | $self->inDoc(1); | ||
| 155 | } | ||
| 156 | else | ||
| 157 | { $out->print($args{notice}) if $args{notice}; | ||
| 158 | $self->inDoc(0); | ||
| 159 | } | ||
| 160 | |||
| 161 | # Read through the file. | ||
| 162 | |||
| 163 | while(my $line = $in->getline) | ||
| 164 | { my $ln = $in->input_line_number; | ||
| 165 | |||
| 166 | if( !$self->inDoc | ||
| 167 | && $line !~ m/^\s*package\s*DB\s*;/ | ||
| 168 | && $line =~ s/^(\s*package\s*([\w\-\:]+)\s*\;)// | ||
| 169 | ) | ||
| 170 | { $out->print($1); | ||
| 171 | my $package = $2; | ||
| 172 | |||
| 173 | # I would like to use 'our' here, but in some cases, that will | ||
| 174 | # cause compaints about double declaration with our. | ||
| 175 | $out->print("\nuse vars '\$VERSION';\n\$VERSION = '$version';\n"); | ||
| 176 | $out->print($line); | ||
| 177 | |||
| 178 | $manual = OODoc::Manual->new | ||
| 179 | ( package => $package | ||
| 180 | , source => $input | ||
| 181 | , stripped => $output | ||
| 182 | , parser => $self | ||
| 183 | |||
| 184 | , distribution => $distr | ||
| 185 | , version => $version | ||
| 186 | ); | ||
| 187 | push @manuals, $manual; | ||
| 188 | $self->currentManual($manual); | ||
| 189 | } | ||
| 190 | elsif(!$self->inDoc && $line =~ m/^=package\s*([\w\-\:]+)\s*$/) | ||
| 191 | { my $package = $1; | ||
| 192 | $manual = OODoc::Manual->new | ||
| 193 | ( package => $package | ||
| 194 | , source => $input | ||
| 195 | , stripped => $output | ||
| 196 | , parser => $self | ||
| 197 | , distribution => $distr | ||
| 198 | , version => $version | ||
| 199 | ); | ||
| 200 | push @manuals, $manual; | ||
| 201 | $self->currentManual($manual); | ||
| 202 | } | ||
| 203 | elsif(my($match, $action) = $self->findMatchingRule($line)) | ||
| 204 | { | ||
| 205 | |||
| 206 | if(ref $action) | ||
| 207 | { $action->($self, $match, $line, $input, $ln) | ||
| 208 | or $out->print($line); | ||
| 209 | } | ||
| 210 | else | ||
| 211 | { no strict 'refs'; | ||
| 212 | $self->$action($match, $line, $input, $ln) | ||
| 213 | or $out->print($line); | ||
| 214 | } | ||
| 215 | } | ||
| 216 | elsif($line =~ m/^=(over|back|item|for|pod|begin|end|head4|encoding)\b/) | ||
| 217 | { ${$self->{OPM_block}} .= "\n". $line; | ||
| 218 | $self->inDoc(1); | ||
| 219 | } | ||
| 220 | elsif(substr($line, 0, 1) eq '=') | ||
| 221 | { warn "WARNING: unknown markup in $input line $ln:\n $line"; | ||
| 222 | ${$self->{OPM_block}} .= $line; | ||
| 223 | $self->inDoc(1); | ||
| 224 | } | ||
| 225 | elsif($pure_pod || $self->inDoc) | ||
| 226 | { # add the line to the currently open text block | ||
| 227 | my $block = $self->{OPM_block}; | ||
| 228 | unless($block) | ||
| 229 | { warn "WARNING: no block for line $ln in file $input\n $line"; | ||
| 230 | my $dummy = ''; | ||
| 231 | $block = $self->setBlock(\$dummy); | ||
| 232 | } | ||
| 233 | $$block .= $line; | ||
| 234 | } | ||
| 235 | elsif($line eq "__DATA__\n") # flush rest file | ||
| 236 | { $out->print($line, $in->getlines); | ||
| 237 | } | ||
| 238 | else | ||
| 239 | { $out->print($line); | ||
| 240 | } | ||
| 241 | } | ||
| 242 | |||
| 243 | warn "WARNING: doc did not end in $input.\n" | ||
| 244 | if $self->inDoc && ! $pure_pod; | ||
| 245 | |||
| 246 | $self->closeChapter; | ||
| 247 | $in->close && $out->close; | ||
| 248 | |||
| 249 | @manuals; | ||
| 250 | } | ||
| 251 | |||
| 252 | #------------------------------------------- | ||
| 253 | |||
| 254 | |||
| 255 | sub setBlock($) | ||
| 256 | { my ($self, $ref) = @_; | ||
| 257 | $self->{OPM_block} = $ref; | ||
| 258 | $self->inDoc(1); | ||
| 259 | $self; | ||
| 260 | } | ||
| 261 | |||
| 262 | #------------------------------------------- | ||
| 263 | |||
| 264 | |||
| 265 | sub inDoc(;$) | ||
| 266 | { my $self = shift; | ||
| 267 | $self->{OPM_in_pod} = shift if @_; | ||
| 268 | $self->{OPM_in_pod}; | ||
| 269 | } | ||
| 270 | |||
| 271 | #------------------------------------------- | ||
| 272 | |||
| 273 | |||
| 274 | sub currentManual(;$) | ||
| 275 | { my $self = shift; | ||
| 276 | @_ ? $self->{OPM_manual} = shift : $self->{OPM_manual}; | ||
| 277 | } | ||
| 278 | |||
| 279 | #------------------------------------------- | ||
| 280 | |||
| 281 | |||
| 282 | sub docCut($$$$) | ||
| 283 | { my ($self, $match, $line, $fn, $ln) = @_; | ||
| 284 | |||
| 285 | if($self->currentManual->isPurePod) | ||
| 286 | { warn "The whole file $fn is pod, so =cut in line $ln is useless.\n"; | ||
| 287 | return; | ||
| 288 | } | ||
| 289 | |||
| 290 | warn "WARNING: $match does not terminate any doc in $fn line $ln.\n" | ||
| 291 | unless $self->inDoc; | ||
| 292 | |||
| 293 | $self->inDoc(0); | ||
| 294 | 1; | ||
| 295 | } | ||
| 296 | |||
| 297 | #------------------------------------------- | ||
| 298 | # CHAPTER | ||
| 299 | |||
| 300 | |||
| 301 | sub docChapter($$$$) | ||
| 302 | { my ($self, $match, $line, $fn, $ln) = @_; | ||
| 303 | $line =~ s/^\=(chapter|head1)\s+//; | ||
| 304 | $line =~ s/\s+$//; | ||
| 305 | |||
| 306 | $self->closeChapter; | ||
| 307 | |||
| 308 | my $manual = $self->currentManual; | ||
| 309 | die "ERROR: chapter $line before package statement in $fn line $ln\n" | ||
| 310 | unless defined $manual; | ||
| 311 | |||
| 312 | my $chapter = $self->{OPM_chapter} = OODoc::Text::Chapter->new | ||
| 313 | ( name => $line | ||
| 314 | , manual => $manual | ||
| 315 | , linenr => $ln | ||
| 316 | ); | ||
| 317 | |||
| 318 | $self->setBlock($chapter->openDescription); | ||
| 319 | $manual->chapter($chapter); | ||
| 320 | $chapter; | ||
| 321 | } | ||
| 322 | |||
| 323 | sub closeChapter() | ||
| 324 | { my $self = shift; | ||
| 325 | my $chapter = delete $self->{OPM_chapter} or return; | ||
| 326 | $self->closeSection()->closeSubroutine(); | ||
| 327 | } | ||
| 328 | |||
| 329 | #------------------------------------------- | ||
| 330 | # SECTION | ||
| 331 | |||
| 332 | |||
| 333 | sub docSection($$$$) | ||
| 334 | { my ($self, $match, $line, $fn, $ln) = @_; | ||
| 335 | $line =~ s/^\=(section|head2)\s+//; | ||
| 336 | $line =~ s/\s+$//; | ||
| 337 | |||
| 338 | $self->closeSection; | ||
| 339 | |||
| 340 | my $chapter = $self->{OPM_chapter}; | ||
| 341 | die "ERROR: section `$line' outside chapter in $fn line $ln\n" | ||
| 342 | unless defined $chapter; | ||
| 343 | |||
| 344 | my $section = $self->{OPM_section} = OODoc::Text::Section->new | ||
| 345 | ( name => $line | ||
| 346 | , chapter => $chapter | ||
| 347 | , linenr => $ln | ||
| 348 | ); | ||
| 349 | |||
| 350 | $chapter->section($section); | ||
| 351 | $self->setBlock($section->openDescription); | ||
| 352 | $section; | ||
| 353 | } | ||
| 354 | |||
| 355 | sub closeSection() | ||
| 356 | { my $self = shift; | ||
| 357 | my $section = delete $self->{OPM_section} or return $self; | ||
| 358 | $self->closeSubSection(); | ||
| 359 | } | ||
| 360 | |||
| 361 | #------------------------------------------- | ||
| 362 | # SUBSECTION | ||
| 363 | |||
| 364 | |||
| 365 | sub docSubSection($$$$) | ||
| 366 | { my ($self, $match, $line, $fn, $ln) = @_; | ||
| 367 | $line =~ s/^\=(subsection|head3)\s+//; | ||
| 368 | $line =~ s/\s+$//; | ||
| 369 | |||
| 370 | $self->closeSubSection; | ||
| 371 | |||
| 372 | my $section = $self->{OPM_section}; | ||
| 373 | defined $section | ||
| 374 | or die "ERROR: subsection `$line' outside section in $fn line $ln\n"; | ||
| 375 | |||
| 376 | my $subsection = $self->{OPM_subsection} = OODoc::Text::SubSection->new | ||
| 377 | ( name => $line | ||
| 378 | , section => $section | ||
| 379 | , linenr => $ln | ||
| 380 | ); | ||
| 381 | |||
| 382 | $section->subsection($subsection); | ||
| 383 | $self->setBlock($subsection->openDescription); | ||
| 384 | $subsection; | ||
| 385 | } | ||
| 386 | |||
| 387 | sub closeSubSection() | ||
| 388 | { my $self = shift; | ||
| 389 | my $subsection = delete $self->{OPM_subsection}; | ||
| 390 | $self; | ||
| 391 | } | ||
| 392 | |||
| 393 | |||
| 394 | #------------------------------------------- | ||
| 395 | # SUBSECTION | ||
| 396 | |||
| 397 | |||
| 398 | sub docSubSubSection($$$$) | ||
| 399 | { my ($self, $match, $line, $fn, $ln) = @_; | ||
| 400 | $line =~ s/^\=(subsubsection|head4)\s+//; | ||
| 401 | $line =~ s/\s+$//; | ||
| 402 | |||
| 403 | $self->closeSubSubSection; | ||
| 404 | |||
| 405 | my $subsection = $self->{OPM_subsection}; | ||
| 406 | defined $subsection | ||
| 407 | or die "ERROR: subsubsection `$line' outside subsection in $fn line $ln\n"; | ||
| 408 | |||
| 409 | my $subsubsection | ||
| 410 | = $self->{OPM_subsubsection} = OODoc::Text::SubSubSection->new | ||
| 411 | ( name => $line | ||
| 412 | , subsection => $subsection | ||
| 413 | , linenr => $ln | ||
| 414 | ); | ||
| 415 | |||
| 416 | $subsection->subsubsection($subsubsection); | ||
| 417 | $self->setBlock($subsubsection->openDescription); | ||
| 418 | $subsubsection; | ||
| 419 | } | ||
| 420 | |||
| 421 | sub closeSubSubSection() | ||
| 422 | { my $self = shift; | ||
| 423 | delete $self->{OPM_subsubsection}; | ||
| 424 | $self; | ||
| 425 | } | ||
| 426 | |||
| 427 | #------------------------------------------- | ||
| 428 | # SUBROUTINES | ||
| 429 | |||
| 430 | |||
| 431 | sub docSubroutine($$$$) | ||
| 432 | { my ($self, $match, $line, $fn, $ln) = @_; | ||
| 433 | |||
| 434 | chomp $line; | ||
| 435 | $line =~ s/^\=(\w+)\s+//; | ||
| 436 | my $type = $1; | ||
| 437 | |||
| 438 | my ($name, $params) | ||
| 439 | = $type eq 'overload' ? ($line, '') | ||
| 440 | : $line =~ m/^(\w*)\s*(.*?)\s*$/; | ||
| 441 | |||
| 442 | my $container = $self->{OPM_subsection} | ||
| 443 | || $self->{OPM_section} | ||
| 444 | || $self->{OPM_chapter}; | ||
| 445 | |||
| 446 | die "ERROR: subroutine $name outside chapter in $fn line $ln\n" | ||
| 447 | unless defined $container; | ||
| 448 | |||
| 449 | $type = 'i_method' if $type eq 'method'; | ||
| 450 | my $sub = $self->{OPM_subroutine} = OODoc::Text::Subroutine->new | ||
| 451 | ( type => $type | ||
| 452 | , name => $name | ||
| 453 | , parameters => $params | ||
| 454 | , linenr => $ln | ||
| 455 | , container => $container | ||
| 456 | ); | ||
| 457 | |||
| 458 | $self->setBlock($sub->openDescription); | ||
| 459 | $container->addSubroutine($sub); | ||
| 460 | $sub; | ||
| 461 | } | ||
| 462 | |||
| 463 | sub closeSubroutine() | ||
| 464 | { my $self = shift; | ||
| 465 | delete $self->{OPM_subroutine}; | ||
| 466 | $self; | ||
| 467 | } | ||
| 468 | |||
| 469 | #------------------------------------------- | ||
| 470 | # SUBROUTINE ADDITIONALS | ||
| 471 | |||
| 472 | |||
| 473 | sub docOption($$$$) | ||
| 474 | { my ($self, $match, $line, $fn, $ln) = @_; | ||
| 475 | |||
| 476 | unless($line =~ m/^\=option\s+(\S+)\s+(.+?)\s*$/ ) | ||
| 477 | { warn "WARNING: option line incorrect in $fn line $ln:\n$line"; | ||
| 478 | return; | ||
| 479 | } | ||
| 480 | my ($name, $parameters) = ($1, $2); | ||
| 481 | |||
| 482 | my $sub = $self->{OPM_subroutine}; | ||
| 483 | die "ERROR: option $name outside subroutine in $fn line $ln\n" | ||
| 484 | unless defined $sub; | ||
| 485 | |||
| 486 | my $option = OODoc::Text::Option->new | ||
| 487 | ( name => $name | ||
| 488 | , parameters => $parameters | ||
| 489 | , linenr => $ln | ||
| 490 | , subroutine => $sub | ||
| 491 | ); | ||
| 492 | |||
| 493 | $self->setBlock($option->openDescription); | ||
| 494 | $sub->option($option); | ||
| 495 | $sub; | ||
| 496 | } | ||
| 497 | |||
| 498 | #------------------------------------------- | ||
| 499 | |||
| 500 | |||
| 501 | sub docDefault($$$$) | ||
| 502 | { my ($self, $match, $line, $fn, $ln) = @_; | ||
| 503 | |||
| 504 | unless($line =~ m/^\=default\s+(\S+)\s+(.+?)\s*$/ ) | ||
| 505 | { warn "WARNING: default line incorrect in $fn line $ln:\n$line"; | ||
| 506 | return; | ||
| 507 | } | ||
| 508 | |||
| 509 | my ($name, $value) = ($1, $2); | ||
| 510 | |||
| 511 | my $sub = $self->{OPM_subroutine}; | ||
| 512 | die "ERROR: default for option $name outside subroutine in $fn line $ln\n" | ||
| 513 | unless defined $sub; | ||
| 514 | |||
| 515 | my $default = OODoc::Text::Default->new | ||
| 516 | ( name => $name | ||
| 517 | , value => $value | ||
| 518 | , linenr => $ln | ||
| 519 | , subroutine => $sub | ||
| 520 | ); | ||
| 521 | |||
| 522 | $sub->default($default); | ||
| 523 | $sub; | ||
| 524 | } | ||
| 525 | |||
| 526 | sub docRequires($$$$) | ||
| 527 | { my ($self, $match, $line, $fn, $ln) = @_; | ||
| 528 | |||
| 529 | unless($line =~ m/^\=requires\s+(\w+)\s+(.+?)\s*$/ ) | ||
| 530 | { warn "WARNING: requires line incorrect in $fn line $ln:\n$line"; | ||
| 531 | return; | ||
| 532 | } | ||
| 533 | |||
| 534 | my ($name, $param) = ($1, $2); | ||
| 535 | $self->docOption ($match, "=option $name $param", $fn, $ln); | ||
| 536 | $self->docDefault($match, "=default $name <required>", $fn, $ln); | ||
| 537 | } | ||
| 538 | |||
| 539 | #------------------------------------------- | ||
| 540 | # DIAGNOSTICS | ||
| 541 | |||
| 542 | |||
| 543 | sub docDiagnostic($$$$) | ||
| 544 | { my ($self, $match, $line, $fn, $ln) = @_; | ||
| 545 | |||
| 546 | $line =~ s/^\=(\w+)\s*//; | ||
| 547 | my $type = $1; | ||
| 548 | |||
| 549 | $line =~ s/\s*$//; | ||
| 550 | unless(length $line) | ||
| 551 | { warn "WARNING: no diagnostic message supplied in $fn line $ln"; | ||
| 552 | return; | ||
| 553 | } | ||
| 554 | |||
| 555 | my $sub = $self->{OPM_subroutine}; | ||
| 556 | die "ERROR: diagnostic $type outside subroutine in $fn line $ln\n" | ||
| 557 | unless defined $sub; | ||
| 558 | |||
| 559 | my $diag = OODoc::Text::Diagnostic->new | ||
| 560 | ( type => ucfirst($type) | ||
| 561 | , name => $line | ||
| 562 | , linenr => $ln | ||
| 563 | , subroutine => $sub | ||
| 564 | ); | ||
| 565 | |||
| 566 | $self->setBlock($diag->openDescription); | ||
| 567 | $sub->diagnostic($diag); | ||
| 568 | $sub; | ||
| 569 | } | ||
| 570 | |||
| 571 | #------------------------------------------- | ||
| 572 | # EXAMPLE | ||
| 573 | |||
| 574 | |||
| 575 | sub docExample($$$$) | ||
| 576 | { my ($self, $match, $line, $fn, $ln) = @_; | ||
| 577 | |||
| 578 | $line =~ s/^=examples?\s*//; | ||
| 579 | $line =~ s/^\#.*//; | ||
| 580 | |||
| 581 | my $container = $self->{OPM_subroutine} | ||
| 582 | || $self->{OPM_subsubsection} | ||
| 583 | || $self->{OPM_subsection} | ||
| 584 | || $self->{OPM_section} | ||
| 585 | || $self->{OPM_chapter}; | ||
| 586 | |||
| 587 | die "ERROR: example outside chapter in $fn line $ln\n" | ||
| 588 | unless defined $container; | ||
| 589 | |||
| 590 | my $example = OODoc::Text::Example->new | ||
| 591 | ( name => ($line || '') | ||
| 592 | , linenr => $ln | ||
| 593 | , container => $container | ||
| 594 | ); | ||
| 595 | |||
| 596 | $self->setBlock($example->openDescription); | ||
| 597 | $container->example($example); | ||
| 598 | $example; | ||
| 599 | } | ||
| 600 | |||
| 601 | #------------------------------------------- | ||
| 602 | |||
| 603 | |||
| 604 | sub debugRemains($$$$) | ||
| 605 | { my ($self, $match, $line, $fn, $ln) = @_; | ||
| 606 | |||
| 607 | warn "WARNING: Debugging remains in $fn line $ln\n" | ||
| 608 | unless $self->inDoc || $self->currentManual->isPurePod; | ||
| 609 | |||
| 610 | undef; | ||
| 611 | } | ||
| 612 | |||
| 613 | #------------------------------------------- | ||
| 614 | |||
| 615 | |||
| 616 | sub forgotCut($$$$) | ||
| 617 | { my ($self, $match, $line, $fn, $ln) = @_; | ||
| 618 | |||
| 619 | warn "WARNING: You may have accidentally captured code in doc $fn line $ln\n" | ||
| 620 | if $self->inDoc && ! $self->currentManual->isPurePod; | ||
| 621 | |||
| 622 | undef; | ||
| 623 | } | ||
| 624 | |||
| 625 | #------------------------------------------- | ||
| 626 | |||
| 627 | |||
| 628 | sub decomposeM($$) | ||
| 629 | { my ($self, $manual, $link) = @_; | ||
| 630 | |||
| 631 | my ($subroutine, $option) | ||
| 632 | = $link =~ s/(?:^|\:\:) (\w+) \( (.*?) \)$//x ? ($1, $2) | ||
| 633 | : ('', ''); | ||
| 634 | |||
| 635 | my $man; | ||
| 636 | if(not length($link)) { $man = $manual } | ||
| 637 | elsif(defined($man = $self->manual($link))) { ; } | ||
| 638 | else | ||
| 639 | { eval "no warnings; require $link"; | ||
| 640 | if( ! $@ | ||
| 641 | || $@ =~ m/attempt to reload/i | ||
| 642 | || $self->skipManualLink($link) | ||
| 643 | ) { ; } | ||
| 644 | elsif($@ =~ m/Can't locate/ ) | ||
| 645 | { warn "WARNING: module $link is not on your system, found in $manual\n"; | ||
| 646 | } | ||
| 647 | else | ||
| 648 | { $@ =~ s/ at \(eval.*//; | ||
| 649 | warn "WARNING: use problem for module $link in $manual;\n$@"; | ||
| 650 | warn " Did you use an 'M' tag on something which is not a module?\n"; | ||
| 651 | } | ||
| 652 | $man = $link; | ||
| 653 | } | ||
| 654 | |||
| 655 | unless(ref $man) | ||
| 656 | { return ( $manual | ||
| 657 | , $man | ||
| 658 | . (length($subroutine) ? " subroutine $subroutine" : '') | ||
| 659 | . (length($option) ? " option $option" : '') | ||
| 660 | ); | ||
| 661 | } | ||
| 662 | |||
| 663 | return (undef, $man) | ||
| 664 | unless defined $subroutine && length $subroutine; | ||
| 665 | |||
| 666 | my $package = $self->manual($man->package); | ||
| 667 | unless(defined $package) | ||
| 668 | { my $want = $man->package; | ||
| 669 | warn "WARNING: no manual for $want (correct casing?)\n"; | ||
| 670 | return (undef, "$want subroutine $subroutine"); | ||
| 671 | } | ||
| 672 | |||
| 673 | my $sub = $package->subroutine($subroutine); | ||
| 674 | unless(defined $sub) | ||
| 675 | { warn "WARNING: subroutine $subroutine() is not defined by $package, but linked to in $manual\n"; | ||
| 676 | return ($package, "$package subroutine $subroutine"); | ||
| 677 | } | ||
| 678 | |||
| 679 | my $location = $sub->manual; | ||
| 680 | return ($location, $sub) | ||
| 681 | unless defined $option && length $option; | ||
| 682 | |||
| 683 | my $opt = $sub->findOption($option); | ||
| 684 | unless(defined $opt) | ||
| 685 | { warn "WARNING: option \"$option\" unknown for $subroutine() in $location, found in $manual\n"; | ||
| 686 | return ($location, "$package subroutine $subroutine option $option"); | ||
| 687 | } | ||
| 688 | |||
| 689 | ($location, $opt); | ||
| 690 | } | ||
| 691 | |||
| 692 | |||
| 693 | sub decomposeL($$) | ||
| 694 | { my ($self, $manual, $link) = @_; | ||
| 695 | my $text = $link =~ s/^([^|]*)\|// ? $1 : undef; | ||
| 696 | |||
| 697 | unless(length $link) | ||
| 698 | { warn "WARNING: empty L link in $manual"; | ||
| 699 | return (); | ||
| 700 | } | ||
| 701 | |||
| 702 | if($link =~ m/^[a-z]+\:[^:]/ ) | ||
| 703 | { $text = $link unless defined $text; | ||
| 704 | return (undef, undef, $link, $text); | ||
| 705 | } | ||
| 706 | |||
| 707 | my ($name, $item) = $link =~ m[(.*?)(?:/(.*))?$]; | ||
| 708 | |||
| 709 | ($name, $item) = (undef, $name) if $name =~ m/^".*"$/; | ||
| 710 | $item =~ s/^"(.*)"$/$1/ if defined $item; | ||
| 711 | |||
| 712 | my $man = length $name ? ($self->manual($name) || $name) : $manual; | ||
| 713 | |||
| 714 | my $dest; | ||
| 715 | if(!ref $man) | ||
| 716 | { unless(defined $text && length $text) | ||
| 717 | { $text = "manual $man"; | ||
| 718 | $text .= " entry $item" if defined $item && length $item; | ||
| 719 | } | ||
| 720 | |||
| 721 | if($man !~ m/\(\d.*\)\s*$/) | ||
| 722 | { (my $escaped = $man) =~ s/\W+/_/g; | ||
| 723 | $dest = "$url_modsearch$escaped"; | ||
| 724 | } | ||
| 725 | } | ||
| 726 | elsif(!defined $item) | ||
| 727 | { $dest = $man; | ||
| 728 | $text = $man->name unless defined $text; | ||
| 729 | } | ||
| 730 | elsif(my @obj = $man->all(findEntry => $item)) | ||
| 731 | { $dest = shift @obj; | ||
| 732 | $text = $item unless defined $text; | ||
| 733 | } | ||
| 734 | else | ||
| 735 | { warn "WARNING: Manual $manual links to unknown entry \"$item\" in $man\n"; | ||
| 736 | $dest = $man; | ||
| 737 | $text = "$man" unless defined $text; | ||
| 738 | } | ||
| 739 | |||
| 740 | ($man, $dest, undef, $text); | ||
| 741 | } | ||
| 742 | |||
| 743 | |||
| 744 | sub cleanupPod($$$) | ||
| 745 | { my ($self, $formatter, $manual, $string) = @_; | ||
| 746 | return '' unless defined $string && length $string; | ||
| 747 | |||
| 748 | my @lines = split /^/, $string; | ||
| 749 | my $protect; | ||
| 750 | |||
| 751 | for(my $i=0; $i < @lines; $i++) | ||
| 752 | { $protect = $1 if $lines[$i] =~ m/^=(for|begin)\s+\w/; | ||
| 753 | |||
| 754 | undef $protect if $lines[$i] =~ m/^=end/; | ||
| 755 | |||
| 756 | undef $protect if $lines[$i] =~ m/^\s*$/ | ||
| 757 | && $protect && $protect eq 'for'; | ||
| 758 | |||
| 759 | next if $protect; | ||
| 760 | |||
| 761 | $lines[$i] =~ | ||
| 762 | s/\bM\<([^>]*)\>/$self->cleanupPodM($formatter,$manual,$1)/ge; | ||
| 763 | |||
| 764 | $lines[$i] =~ | ||
| 765 | s/\bL\<([^>]*)\>/$self->cleanupPodL($formatter,$manual,$1)/ge | ||
| 766 | if substr($lines[$i], 0, 1) eq ' '; | ||
| 767 | |||
| 768 | # permit losing blank lines around pod statements. | ||
| 769 | if(substr($lines[$i], 0, 1) eq '=') | ||
| 770 | { if($i > 0 && $lines[$i-1] ne "\n") | ||
| 771 | { splice @lines, $i-1, 0, "\n"; | ||
| 772 | $i++; | ||
| 773 | } | ||
| 774 | elsif($i < $#lines && $lines[$i+1] ne "\n" | ||
| 775 | && substr($lines[$i], 0, 5) ne "=for ") | ||
| 776 | { splice @lines, $i+1, 0, "\n"; | ||
| 777 | } | ||
| 778 | } | ||
| 779 | else | ||
| 780 | { $lines[$i] =~ s/^\\\=/\=/; | ||
| 781 | } | ||
| 782 | |||
| 783 | # Remove superfluous blanks | ||
| 784 | if($i < $#lines && $lines[$i] eq "\n" && $lines[$i+1] eq "\n") | ||
| 785 | { splice @lines, $i+1, 1; | ||
| 786 | } | ||
| 787 | } | ||
| 788 | |||
| 789 | # remove leading and trailing blank lines | ||
| 790 | shift @lines while @lines && $lines[0] eq "\n"; | ||
| 791 | pop @lines while @lines && $lines[-1] eq "\n"; | ||
| 792 | |||
| 793 | @lines ? join('', @lines) : ''; | ||
| 794 | } | ||
| 795 | |||
| 796 | |||
| 797 | sub cleanupPodM($$$) | ||
| 798 | { my ($self, $formatter, $manual, $link) = @_; | ||
| 799 | my ($toman, $to) = $self->decomposeM($manual, $link); | ||
| 800 | ref $to ? $formatter->link($toman, $to, $link) : $to; | ||
| 801 | } | ||
| 802 | |||
| 803 | |||
| 804 | sub cleanupPodL($$$) | ||
| 805 | { my ($self, $formatter, $manual, $link) = @_; | ||
| 806 | my ($toman, $to, $href, $text) = $self->decomposeL($manual, $link); | ||
| 807 | $text; | ||
| 808 | } | ||
| 809 | |||
| 810 | #------------------------------------------- | ||
| 811 | |||
| 812 | |||
| 813 | sub cleanupHtml($$$;$) | ||
| 814 | { my ($self, $formatter, $manual, $string, $is_html) = @_; | ||
| 815 | return '' unless defined $string && length $string; | ||
| 816 | |||
| 817 | if($string =~ m/(?:\A|\n) # start of line | ||
| 818 | \=begin\s+(:?\w+)\s* # begin statement | ||
| 819 | (.*?) # encapsulated | ||
| 820 | \n\=end\s+\1\s* # related end statement | ||
| 821 | /xs | ||
| 822 | || $string =~ m/(?:\A|\n) # start of line | ||
| 823 | \=for\s+(:?\w+)\b # for statement | ||
| 824 | (.*?)\n # encapsulated | ||
| 825 | (\n|\Z) # end of paragraph | ||
| 826 | /xs | ||
| 827 | ) | ||
| 828 | { my ($before, $type, $capture, $after) = ($`, lc($1), $2, $'); | ||
| 829 | if($type =~ m/^\:(text|pod)\b/ ) | ||
| 830 | { $type = 'text'; | ||
| 831 | $capture = $self->cleanupPod($formatter, $manual, $capture); | ||
| 832 | } | ||
| 833 | elsif($type =~ m/^\:?html\b/ ) | ||
| 834 | { $type = 'html'; | ||
| 835 | $capture = $self->cleanupHtml($formatter, $manual, $capture, 1); | ||
| 836 | } | ||
| 837 | |||
| 838 | my $take = $type eq 'text' ? "<pre>\n". $capture . "</pre>\n" | ||
| 839 | : $type eq 'html' ? $capture | ||
| 840 | : ''; # ignore | ||
| 841 | |||
| 842 | return $self->cleanupHtml($formatter, $manual, $before) | ||
| 843 | . $take | ||
| 844 | . $self->cleanupHtml($formatter, $manual, $after); | ||
| 845 | } | ||
| 846 | |||
| 847 | for($string) | ||
| 848 | { unless($is_html) | ||
| 849 | { s#\&#\&#g; | ||
| 850 | s#(?<!\b[LFCIBEM])\<#<#g; | ||
| 851 | s#\-\>#-\>#g; | ||
| 852 | } | ||
| 853 | s/\bM\<([^>]*)\>/$self->cleanupHtmlM($formatter, $manual, $1)/ge; | ||
| 854 | s/\bL\<([^>]*)\>/$self->cleanupHtmlL($formatter, $manual, $1)/ge; | ||
| 855 | s#\bC\<([^>]*)\>#<code>$1</code>#g; | ||
| 856 | s#\bI\<([^>]*)\>#<em>$1</em>#g; | ||
| 857 | s#\bB\<([^>]*)\>#<b>$1</b>#g; | ||
| 858 | s#\bE\<([^>]*)\>#\&$1;#g; | ||
| 859 | s#^\=over\s+\d+\s*#\n<ul>\n#gms; | ||
| 860 | s#(?:\A|\n)\=item\s*(?:\*\s*)?([^\n]*)#\n<li><b>$1</b><br />#gms; | ||
| 861 | s#(?:\A|\s*)\=back\b#\n</ul>#gms; | ||
| 862 | s#^=pod\b##gm; | ||
| 863 | |||
| 864 | # when F<> contains a URL, it will be used. However, when it | ||
| 865 | # contains a file, we cannot do anything with it yet. | ||
| 866 | s#\bF\<(\w+\://[^>]*)\>#<a href="$1">$1</a>#g; | ||
| 867 | s#\bF\<([^>]*)\>#<tt>$1</tt>#g; | ||
| 868 | |||
| 869 | my ($label, $level, $title); | ||
| 870 | s#^\=head([1-6])\s*([^\n]*)# | ||
| 871 | ($title, $level) = ($1, $2); | ||
| 872 | $label = $title; | ||
| 873 | $label =~ s/\W+/_/g; | ||
| 874 | qq[<h$level class="$title"><a name="$label">$title</a></h$level>]; | ||
| 875 | #ge; | ||
| 876 | |||
| 877 | next if $is_html; | ||
| 878 | |||
| 879 | s!(?:(?:^|\n) | ||
| 880 | [^\ \t\n]+[^\n]* # line starting with blank: para | ||
| 881 | )+ | ||
| 882 | !<p>$&</p>!gsx; | ||
| 883 | |||
| 884 | s!(?:(?:^|\n) # start of line | ||
| 885 | [\ \t]+[^\n]+ # line starting with blank: pre | ||
| 886 | )+ | ||
| 887 | !<pre>$&\n</pre>!gsx; | ||
| 888 | |||
| 889 | s#</pre>\n<pre>##gs; | ||
| 890 | s#<p>\n#\n<p>#gs; | ||
| 891 | } | ||
| 892 | |||
| 893 | $string; | ||
| 894 | } | ||
| 895 | |||
| 896 | |||
| 897 | sub cleanupHtmlM($$$) | ||
| 898 | { my ($self, $formatter, $manual, $link) = @_; | ||
| 899 | my ($toman, $to) = $self->decomposeM($manual, $link); | ||
| 900 | ref $to ? $formatter->link($toman, $to, $link) : $to; | ||
| 901 | } | ||
| 902 | |||
| 903 | |||
| 904 | sub cleanupHtmlL($$$) | ||
| 905 | { my ($self, $formatter, $manual, $link) = @_; | ||
| 906 | my ($toman, $to, $href, $text) = $self->decomposeL($manual, $link); | ||
| 907 | |||
| 908 | defined $href ? qq[<a href="$href" target="_blank">$text</a>] | ||
| 909 | : !defined $to ? $text | ||
| 910 | : ref $to ? $formatter->link($toman, $to, $text) | ||
| 911 | : qq[<a href="$to">$text</a>] | ||
| 912 | } | ||
| 913 | |||
| 914 | #------------------------------------------- | ||
| 915 | |||
| 916 | |||
| 917 | 1; |