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