2 package Pod::Simple::BlackBox;
4 # "What's in the box?" "Pain."
6 ###########################################################################
8 # This is where all the scary things happen: parsing lines into
9 # paragraphs; and then into directives, verbatims, and then also
10 # turning formatting sequences into treelets.
12 # Are you really sure you want to read this code?
14 #-----------------------------------------------------------------------------
16 # The basic work of this module Pod::Simple::BlackBox is doing the dirty work
17 # of parsing Pod into treelets (generally one per non-verbatim paragraph), and
18 # to call the proper callbacks on the treelets.
20 # Every node in a treelet is a ['name', {attrhash}, ...children...]
27 *DEBUG = \&Pod::Simple::DEBUG unless defined &DEBUG
30 #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
32 sub parse_line { shift->parse_lines(@_) } # alias
34 # - - - Turn back now! Run away! - - -
36 sub parse_lines { # Usage: $parser->parse_lines(@lines)
37 # an undef means end-of-stream
40 my $code_handler = $self->{'code_handler'};
41 my $cut_handler = $self->{'cut_handler'};
42 $self->{'line_count'} ||= 0;
47 print "# Parsing starting at line ", $self->{'line_count'}, ".\n";
50 print "# About to parse lines: ",
51 join(' ', map defined($_) ? "[$_]" : "EOF", @_), "\n";
53 my $paras = ($self->{'paras'} ||= []);
54 # paragraph buffer. Because we need to defer processing of =over
55 # directives and verbatim paragraphs. We call _ponder_paragraph_buffer
58 $self->{'pod_para_count'} ||= 0;
61 foreach my $source_line (@_) {
62 if( $self->{'source_dead'} ) {
63 DEBUG > 4 and print "# Source is dead.\n";
67 unless( defined $source_line ) {
68 DEBUG > 4 and print "# Undef-line seen.\n";
70 push @$paras, ['~end', {'start_line' => $self->{'line_count'}}];
71 push @$paras, $paras->[-1], $paras->[-1];
72 # So that it definitely fills the buffer.
73 $self->{'source_dead'} = 1;
74 $self->_ponder_paragraph_buffer;
79 if( $self->{'line_count'}++ ) {
80 ($line = $source_line) =~ tr/\n\r//d;
81 # If we don't have two vars, we'll end up with that there
82 # tr/// modding the (potentially read-only) original source line!
85 DEBUG > 2 and print "First line: [$source_line]\n";
87 if( ($line = $source_line) =~ s/^\xEF\xBB\xBF//s ) {
88 DEBUG and print "UTF-8 BOM seen. Faking a '=encode utf8'.\n";
89 $self->_handle_encoding_line( "=encode utf8" );
92 } elsif( $line =~ s/^\xFE\xFF//s ) {
93 DEBUG and print "Big-endian UTF-16 BOM seen. Aborting parsing.\n";
95 $self->{'line_count'},
96 "UTF16-BE Byte Encoding Mark found; but Pod::Simple v$Pod::Simple::VERSION doesn't implement UTF16 yet."
102 # TODO: implement somehow?
104 } elsif( $line =~ s/^\xFF\xFE//s ) {
105 DEBUG and print "Little-endian UTF-16 BOM seen. Aborting parsing.\n";
107 $self->{'line_count'},
108 "UTF16-LE Byte Encoding Mark found; but Pod::Simple v$Pod::Simple::VERSION doesn't implement UTF16 yet."
114 # TODO: implement somehow?
117 DEBUG > 2 and print "First line is BOM-less.\n";
118 ($line = $source_line) =~ tr/\n\r//d;
123 DEBUG > 5 and print "# Parsing line: [$line]\n";
125 if(!$self->{'in_pod'}) {
126 if($line =~ m/^=([a-zA-Z]+)/s) {
129 $self->{'line_count'},
130 "=cut found outside a pod block. Skipping to next block."
133 ## Before there were errata sections in the world, it was
134 ## least-pessimal to abort processing the file. But now we can
135 ## just barrel on thru (but still not start a pod block).
141 $self->{'in_pod'} = $self->{'start_of_pod_block'}
142 = $self->{'last_was_blank'} = 1;
143 # And fall thru to the pod-mode block further down
146 DEBUG > 5 and print "# It's a code-line.\n";
147 $code_handler->(map $_, $line, $self->{'line_count'}, $self)
149 # Note: this may cause code to be processed out of order relative
150 # to pods, but in order relative to cuts.
152 # Note also that we haven't yet applied the transcoding to $line
153 # by time we call $code_handler!
155 if( $line =~ m/^#\s*line\s+(\d+)\s*(?:\s"([^"]+)")?\s*$/ ) {
156 # That RE is from perlsyn, section "Plain Old Comments (Not!)",
157 #$fname = $2 if defined $2;
158 #DEBUG > 1 and defined $2 and print "# Setting fname to \"$fname\"\n";
159 DEBUG > 1 and print "# Setting nextline to $1\n";
160 $self->{'line_count'} = $1 - 1;
167 # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
168 # Else we're in pod mode:
170 # Apply any necessary transcoding:
171 $self->{'_transcoder'} && $self->{'_transcoder'}->($line);
173 # HERE WE CATCH =encoding EARLY!
174 if( $line =~ m/^=encoding\s+\S+\s*$/s ) {
175 $line = $self->_handle_encoding_line( $line );
178 if($line =~ m/^=cut/s) {
179 # here ends the pod block, and therefore the previous pod para
180 DEBUG > 1 and print "Noting =cut at line ${$self}{'line_count'}\n";
181 $self->{'in_pod'} = 0;
182 # ++$self->{'pod_para_count'};
183 $self->_ponder_paragraph_buffer();
184 # by now it's safe to consider the previous paragraph as done.
185 $cut_handler->(map $_, $line, $self->{'line_count'}, $self)
188 # TODO: add to docs: Note: this may cause cuts to be processed out
189 # of order relative to pods, but in order relative to code.
191 } elsif($line =~ m/^\s*$/s) { # it's a blank line
192 if(!$self->{'start_of_pod_block'} and @$paras and $paras->[-1][0] eq '~Verbatim') {
193 DEBUG > 1 and print "Saving blank line at line ${$self}{'line_count'}\n";
194 push @{$paras->[-1]}, $line;
195 } # otherwise it's not interesting
197 if(!$self->{'start_of_pod_block'} and !$self->{'last_was_blank'}) {
198 DEBUG > 1 and print "Noting para ends with blank line at ${$self}{'line_count'}\n";
201 $self->{'last_was_blank'} = 1;
203 } elsif($self->{'last_was_blank'}) { # A non-blank line starting a new para...
205 if($line =~ m/^(=[a-zA-Z][a-zA-Z0-9]*)(?:\s+|$)(.*)/s) {
206 # THIS IS THE ONE PLACE WHERE WE CONSTRUCT NEW DIRECTIVE OBJECTS
207 my $new = [$1, {'start_line' => $self->{'line_count'}}, $2];
208 # Note that in "=head1 foo", the WS is lost.
209 # Example: ['=head1', {'start_line' => 123}, ' foo']
211 ++$self->{'pod_para_count'};
213 $self->_ponder_paragraph_buffer();
214 # by now it's safe to consider the previous paragraph as done.
216 push @$paras, $new; # the new incipient paragraph
217 DEBUG > 1 and print "Starting new ${$paras}[-1][0] para at line ${$self}{'line_count'}\n";
219 } elsif($line =~ m/^\s/s) {
221 if(!$self->{'start_of_pod_block'} and @$paras and $paras->[-1][0] eq '~Verbatim') {
222 DEBUG > 1 and print "Resuming verbatim para at line ${$self}{'line_count'}\n";
223 push @{$paras->[-1]}, $line;
225 ++$self->{'pod_para_count'};
226 $self->_ponder_paragraph_buffer();
227 # by now it's safe to consider the previous paragraph as done.
228 DEBUG > 1 and print "Starting verbatim para at line ${$self}{'line_count'}\n";
229 push @$paras, ['~Verbatim', {'start_line' => $self->{'line_count'}}, $line];
232 ++$self->{'pod_para_count'};
233 $self->_ponder_paragraph_buffer();
234 # by now it's safe to consider the previous paragraph as done.
235 push @$paras, ['~Para', {'start_line' => $self->{'line_count'}}, $line];
236 DEBUG > 1 and print "Starting plain para at line ${$self}{'line_count'}\n";
238 $self->{'last_was_blank'} = $self->{'start_of_pod_block'} = 0;
241 # It's a non-blank line /continuing/ the current para
243 DEBUG > 2 and print "Line ${$self}{'line_count'} continues current paragraph\n";
244 push @{$paras->[-1]}, $line;
247 die "Continuing a paragraph but \@\$paras is empty?";
249 $self->{'last_was_blank'} = $self->{'start_of_pod_block'} = 0;
252 } # ends the big while loop
254 DEBUG > 1 and print(pretty(@$paras), "\n");
258 #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
260 sub _handle_encoding_line {
261 my($self, $line) = @_;
263 # The point of this routine is to set $self->{'_transcoder'} as indicated.
265 return $line unless $line =~ m/^=encoding\s+(\S+)\s*$/s;
266 DEBUG > 1 and print "Found an encoding line \"=encoding $1\"\n";
270 push @{ $self->{'encoding_command_reqs'} }, "=encoding $orig";
274 # Cf. perldoc Encode and perldoc Encode::Supported
276 require Pod::Simple::Transcode;
278 if( $self->{'encoding'} ) {
279 my $norm_current = $self->{'encoding'};
281 foreach my $that ($norm_current, $norm_e) {
285 if($norm_current eq $norm_e) {
286 DEBUG > 1 and print "The '=encoding $orig' line is ",
287 "redundant. ($norm_current eq $norm_e). Ignoring.\n";
289 # But that doesn't necessarily mean that the earlier one went okay
291 $enc_error = "Encoding is already set to " . $self->{'encoding'};
292 DEBUG > 1 and print $enc_error;
295 # OK, let's turn on the encoding
297 DEBUG > 1 and print " Setting encoding to $e\n";
298 $self->{'encoding'} = $e;
303 DEBUG and print " Putting in HACKRAW (no-op) encoding mode.\n";
305 } elsif( Pod::Simple::Transcode::->encoding_is_available($e) ) {
307 die($enc_error = "WHAT? _transcoder is already set?!")
308 if $self->{'_transcoder'}; # should never happen
309 require Pod::Simple::Transcode;
310 $self->{'_transcoder'} = Pod::Simple::Transcode::->make_transcoder($e);
312 my @x = ('', "abc", "123");
313 $self->{'_transcoder'}->(@x);
315 $@ && die( $enc_error =
316 "Really unexpected error setting up encoding $e: $@\nAborting"
320 my @supported = Pod::Simple::Transcode::->all_encodings;
322 # Note unsupported, and complain
323 DEBUG and print " Encoding [$e] is unsupported.",
324 "\nSupporteds: @supported\n";
327 # Look for a near match:
331 foreach my $enc (@supported) {
334 next unless $n eq $norm;
335 $suggestion = " (Maybe \"$e\" should be \"$enc\"?)";
338 my $encmodver = Pod::Simple::Transcode::->encmodver;
339 $enc_error = join '' =>
340 "This document probably does not appear as it should, because its ",
341 "\"=encoding $e\" line calls for an unsupported encoding.",
342 $suggestion, " [$encmodver\'s supported encodings are: @supported]"
345 $self->scream( $self->{'line_count'}, $enc_error );
347 push @{ $self->{'encoding_command_statuses'} }, $enc_error;
349 return '=encoding ALREADYDONE';
352 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
354 sub _handle_encoding_second_level {
355 # By time this is called, the encoding (if well formed) will already
356 # have been acted one.
357 my($self, $para) = @_;
359 my $content = join ' ', splice @x, 2;
360 $content =~ s/^\s+//s;
361 $content =~ s/\s+$//s;
363 DEBUG > 2 and print "Ogling encoding directive: =encoding $content\n";
365 if($content eq 'ALREADYDONE') {
366 # It's already been handled. Check for errors.
367 if(! $self->{'encoding_command_statuses'} ) {
368 DEBUG > 2 and print " CRAZY ERROR: It wasn't really handled?!\n";
369 } elsif( $self->{'encoding_command_statuses'}[-1] ) {
370 $self->whine( $para->[1]{'start_line'},
371 sprintf "Couldn't do %s: %s",
372 $self->{'encoding_command_reqs' }[-1],
373 $self->{'encoding_command_statuses'}[-1],
376 DEBUG > 2 and print " (Yup, it was successfully handled already.)\n";
380 # Otherwise it's a syntax error
381 $self->whine( $para->[1]{'start_line'},
382 "Invalid =encoding syntax: $content"
389 #~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`
392 my $m = -321; # magic line number
396 # Return 0 or more fake-o paragraphs explaining the accumulated
397 # errors on this document.
399 return() unless $self->{'errata'} and keys %{$self->{'errata'}};
403 foreach my $line (sort {$a <=> $b} keys %{$self->{'errata'}}) {
405 ['=item', {'start_line' => $m}, "Around line $line:"],
406 map( ['~Para', {'start_line' => $m, '~cooked' => 1},
407 #['~Top', {'start_line' => $m},
411 @{$self->{'errata'}{$line}}
416 # TODO: report of unknown entities? unrenderable characters?
419 ['=head1', {'start_line' => $m, 'errata' => 1}, 'POD ERRORS'],
420 ['~Para', {'start_line' => $m, '~cooked' => 1, 'errata' => 1},
423 'The above document had some coding errors, which are explained below:'
426 ['=over', {'start_line' => $m, 'errata' => 1}, ''],
430 ['=back', {'start_line' => $m, 'errata' => 1}, ''],
433 DEBUG and print "\n<<\n", pretty(\@out), "\n>>\n\n";
440 #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
442 ##############################################################################
444 ## stop reading now stop reading now stop reading now stop reading now stop
446 ## HERE IT BECOMES REALLY SCARY
448 ## stop reading now stop reading now stop reading now stop reading now stop
450 ##############################################################################
452 sub _ponder_paragraph_buffer {
454 # Para-token types as found in the buffer.
455 # ~Verbatim, ~Para, ~end, =head1..4, =for, =begin, =end,
456 # =over, =back, =item
457 # and the null =pod (to be complained about if over one line)
459 # "~data" paragraphs are something we generate at this level, depending on
460 # a currently open =over region
462 # Events fired: Begin and end for:
463 # directivename (like head1 .. head4), item, extend,
464 # for (from =begin...=end, =for),
465 # over-bullet, over-number, over-text, over-block,
466 # item-bullet, item-number, item-text,
468 # Data, Para, Verbatim
469 # B, C, longdirname (TODO -- wha?), etc. for all directives
474 return unless @{$paras = $self->{'paras'}};
475 my $curr_open = ($self->{'curr_open'} ||= []);
479 DEBUG > 10 and print "# Paragraph buffer: <<", pretty($paras), ">>\n";
481 # We have something in our buffer. So apparently the document has started.
482 unless($self->{'doc_has_started'}) {
483 $self->{'doc_has_started'} = 1;
485 my $starting_contentless;
486 $starting_contentless =
489 and @$paras and ! grep $_->[0] ne '~end', @$paras
490 # i.e., if the paras is all ~ends
493 DEBUG and print "# Starting ",
494 $starting_contentless ? 'contentless' : 'contentful',
498 $self->_handle_element_start(
499 ($scratch = 'Document'),
501 'start_line' => $paras->[0][1]{'start_line'},
502 $starting_contentless ? ( 'contentless' => 1 ) : (),
507 my($para, $para_type);
509 last if @$paras == 1 and
510 ( $paras->[0][0] eq '=over' or $paras->[0][0] eq '~Verbatim'
511 or $paras->[0][0] eq '=item' )
513 # Those're the three kinds of paragraphs that require lookahead.
514 # Actually, an "=item Foo" inside an <over type=text> region
515 # and any =item inside an <over type=block> region (rare)
516 # don't require any lookahead, but all others (bullets
519 # TODO: winge about many kinds of directives in non-resolving =for regions?
520 # TODO: many? like what? =head1 etc?
522 $para = shift @$paras;
523 $para_type = $para->[0];
525 DEBUG > 1 and print "Pondering a $para_type paragraph, given the stack: (",
526 $self->_dump_curr_open(), ")\n";
528 if($para_type eq '=for') {
529 next if $self->_ponder_for($para,$curr_open,$paras);
531 } elsif($para_type eq '=begin') {
532 next if $self->_ponder_begin($para,$curr_open,$paras);
534 } elsif($para_type eq '=end') {
535 next if $self->_ponder_end($para,$curr_open,$paras);
537 } elsif($para_type eq '~end') { # The virtual end-document signal
538 next if $self->_ponder_doc_end($para,$curr_open,$paras);
542 # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
543 #~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
544 if(grep $_->[1]{'~ignore'}, @$curr_open) {
546 print "Skipping $para_type paragraph because in ignore mode.\n";
549 #~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
550 # ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
552 if($para_type eq '=pod') {
553 $self->_ponder_pod($para,$curr_open,$paras);
555 } elsif($para_type eq '=over') {
556 next if $self->_ponder_over($para,$curr_open,$paras);
558 } elsif($para_type eq '=back') {
559 next if $self->_ponder_back($para,$curr_open,$paras);
563 # All non-magical codes!!!
565 # Here we start using $para_type for our own twisted purposes, to
566 # mean how it should get treated, not as what the element name
569 DEBUG > 1 and print "Pondering non-magical $para_type\n";
573 # Enforce some =headN discipline
574 if($para_type =~ m/^=head\d$/s
575 and ! $self->{'accept_heads_anywhere'}
577 and $curr_open->[-1][0] eq '=over'
579 DEBUG > 2 and print "'=$para_type' inside an '=over'!\n";
581 $para->[1]{'start_line'},
582 "You forgot a '=back' before '$para_type'"
584 unshift @$paras, ['=back', {}, ''], $para; # close the =over
589 if($para_type eq '=item') {
592 unless(@$curr_open and ($over = $curr_open->[-1])->[0] eq '=over') {
594 $para->[1]{'start_line'},
595 "'=item' outside of any '=over'"
598 ['=over', {'start_line' => $para->[1]{'start_line'}}, ''],
605 my $over_type = $over->[1]{'~type'};
609 die "Typeless over in stack, starting at line "
610 . $over->[1]{'start_line'};
612 } elsif($over_type eq 'block') {
613 unless($curr_open->[-1][1]{'~bitched_about'}) {
614 $curr_open->[-1][1]{'~bitched_about'} = 1;
616 $curr_open->[-1][1]{'start_line'},
617 "You can't have =items (as at line "
618 . $para->[1]{'start_line'}
619 . ") unless the first thing after the =over is an =item"
622 # Just turn it into a paragraph and reconsider it
623 $para->[0] = '~Para';
624 unshift @$paras, $para;
627 } elsif($over_type eq 'text') {
628 my $item_type = $self->_get_item_type($para);
629 # That kills the content of the item if it's a number or bullet.
630 DEBUG and print " Item is of type ", $para->[0], " under $over_type\n";
632 if($item_type eq 'text') {
633 # Nothing special needs doing for 'text'
634 } elsif($item_type eq 'number' or $item_type eq 'bullet') {
635 die "Unknown item type $item_type"
636 unless $item_type eq 'number' or $item_type eq 'bullet';
637 # Undo our clobbering:
638 push @$para, $para->[1]{'~orig_content'};
639 delete $para->[1]{'number'};
640 # Only a PROPER item-number element is allowed
641 # to have a number attribute.
643 die "Unhandled item type $item_type"; # should never happen
646 # =item-text thingies don't need any assimilation, it seems.
648 } elsif($over_type eq 'number') {
649 my $item_type = $self->_get_item_type($para);
650 # That kills the content of the item if it's a number or bullet.
651 DEBUG and print " Item is of type ", $para->[0], " under $over_type\n";
653 my $expected_value = ++ $curr_open->[-1][1]{'~counter'};
655 if($item_type eq 'bullet') {
656 # Hm, it's not numeric. Correct for this.
657 $para->[1]{'number'} = $expected_value;
659 $para->[1]{'start_line'},
660 "Expected '=item $expected_value'"
662 push @$para, $para->[1]{'~orig_content'};
663 # restore the bullet, blocking the assimilation of next para
665 } elsif($item_type eq 'text') {
666 # Hm, it's not numeric. Correct for this.
667 $para->[1]{'number'} = $expected_value;
669 $para->[1]{'start_line'},
670 "Expected '=item $expected_value'"
672 # Text content will still be there and will block next ~Para
674 } elsif($item_type ne 'number') {
675 die "Unknown item type $item_type"; # should never happen
677 } elsif($expected_value == $para->[1]{'number'}) {
678 DEBUG > 1 and print " Numeric item has the expected value of $expected_value\n";
681 DEBUG > 1 and print " Numeric item has ", $para->[1]{'number'},
682 " instead of the expected value of $expected_value\n";
684 $para->[1]{'start_line'},
685 "You have '=item " . $para->[1]{'number'} .
686 "' instead of the expected '=item $expected_value'"
688 $para->[1]{'number'} = $expected_value; # correcting!!
692 # For the cases where we /didn't/ push to @$para
693 if($paras->[0][0] eq '~Para') {
694 DEBUG and print "Assimilating following ~Para content into $over_type item\n";
695 push @$para, splice @{shift @$paras},2;
697 DEBUG and print "Can't assimilate following ", $paras->[0][0], "\n";
698 push @$para, ''; # Just so it's not contentless
703 } elsif($over_type eq 'bullet') {
704 my $item_type = $self->_get_item_type($para);
705 # That kills the content of the item if it's a number or bullet.
706 DEBUG and print " Item is of type ", $para->[0], " under $over_type\n";
708 if($item_type eq 'bullet') {
711 if( $para->[1]{'~_freaky_para_hack'} ) {
712 DEBUG and print "Accomodating '=item * Foo' tolerance hack.\n";
713 push @$para, delete $para->[1]{'~_freaky_para_hack'};
716 } elsif($item_type eq 'number') {
718 $para->[1]{'start_line'},
721 push @$para, $para->[1]{'~orig_content'};
722 # and block assimilation of the next paragraph
723 delete $para->[1]{'number'};
724 # Only a PROPER item-number element is allowed
725 # to have a number attribute.
726 } elsif($item_type eq 'text') {
728 $para->[1]{'start_line'},
731 # But doesn't need processing. But it'll block assimilation
734 die "Unhandled item type $item_type"; # should never happen
738 # For the cases where we /didn't/ push to @$para
739 if($paras->[0][0] eq '~Para') {
740 DEBUG and print "Assimilating following ~Para content into $over_type item\n";
741 push @$para, splice @{shift @$paras},2;
743 DEBUG and print "Can't assimilate following ", $paras->[0][0], "\n";
744 push @$para, ''; # Just so it's not contentless
749 die "Unhandled =over type \"$over_type\"?";
753 $para_type = 'Plain';
754 $para->[0] .= '-' . $over_type;
755 # Whew. Now fall thru and process it.
758 } elsif($para_type eq '=extend') {
759 # Well, might as well implement it here.
760 $self->_ponder_extend($para);
762 } elsif($para_type eq '=encoding') {
763 # Not actually acted on here, but we catch errors here.
764 $self->_handle_encoding_second_level($para);
767 } elsif($para_type eq '~Verbatim') {
768 $para->[0] = 'Verbatim';
769 $para_type = '?Verbatim';
770 } elsif($para_type eq '~Para') {
772 $para_type = '?Plain';
773 } elsif($para_type eq 'Data') {
775 $para_type = '?Data';
776 } elsif( $para_type =~ s/^=//s
777 and defined( $para_type = $self->{'accept_directives'}{$para_type} )
779 DEBUG > 1 and print " Pondering known directive ${$para}[0] as $para_type\n";
781 # An unknown directive!
782 DEBUG > 1 and printf "Unhandled directive %s (Handled: %s)\n",
783 $para->[0], join(' ', sort keys %{$self->{'accept_directives'}} )
786 $para->[1]{'start_line'},
787 "Unknown directive: $para->[0]"
790 # And maybe treat it as text instead of just letting it go?
794 if($para_type =~ s/^\?//s) {
795 if(! @$curr_open) { # usual case
796 DEBUG and print "Treating $para_type paragraph as such because stack is empty.\n";
798 my @fors = grep $_->[0] eq '=for', @$curr_open;
799 DEBUG > 1 and print "Containing fors: ",
800 join(',', map $_->[1]{'target'}, @fors), "\n";
803 DEBUG and print "Treating $para_type paragraph as such because stack has no =for's\n";
805 #} elsif(grep $_->[1]{'~resolve'}, @fors) {
806 #} elsif(not grep !$_->[1]{'~resolve'}, @fors) {
807 } elsif( $fors[-1][1]{'~resolve'} ) {
808 # Look to the immediately containing for
810 if($para_type eq 'Data') {
811 DEBUG and print "Treating Data paragraph as Plain/Verbatim because the containing =for ($fors[-1][1]{'target'}) is a resolver\n";
813 $para_type = 'Plain';
815 DEBUG and print "Treating $para_type paragraph as such because the containing =for ($fors[-1][1]{'target'}) is a resolver\n";
818 DEBUG and print "Treating $para_type paragraph as Data because the containing =for ($fors[-1][1]{'target'}) is a non-resolver\n";
819 $para->[0] = $para_type = 'Data';
824 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
825 if($para_type eq 'Plain') {
826 $self->_ponder_Plain($para);
827 } elsif($para_type eq 'Verbatim') {
828 $self->_ponder_Verbatim($para);
829 } elsif($para_type eq 'Data') {
830 $self->_ponder_Data($para);
832 die "\$para type is $para_type -- how did that happen?";
836 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
837 $para->[0] =~ s/^[~=]//s;
839 DEBUG and print "\n", pretty($para), "\n";
841 # traverse the treelet (which might well be just one string scalar)
842 $self->{'content_seen'} ||= 1;
843 $self->_traverse_treelet_bit(@$para);
850 ###########################################################################
851 # The sub-ponderers...
856 my ($self,$para,$curr_open,$paras) = @_;
858 # Fake it out as a begin/end
861 if(grep $_->[1]{'~ignore'}, @$curr_open) {
862 DEBUG > 1 and print "Ignoring ignorable =for\n";
866 for(my $i = 2; $i < @$para; ++$i) {
867 if($para->[$i] =~ s/^\s*(\S+)\s*//s) {
872 unless(defined $target) {
874 $para->[1]{'start_line'},
875 "=for without a target?"
880 print "Faking out a =for $target as a =begin $target / =end $target\n";
886 {'start_line' => $para->[1]{'start_line'}, '~really' => '=for'},
891 {'start_line' => $para->[1]{'start_line'}, '~really' => '=for'},
900 my ($self,$para,$curr_open,$paras) = @_;
901 my $content = join ' ', splice @$para, 2;
902 $content =~ s/^\s+//s;
903 $content =~ s/\s+$//s;
904 unless(length($content)) {
906 $para->[1]{'start_line'},
907 "=begin without a target?"
909 DEBUG and print "Ignoring targetless =begin\n";
913 my ($target, $title) = $content =~ m/^(\S+)\s*(.*)$/;
914 $para->[1]{'title'} = $title if ($title);
915 $para->[1]{'target'} = $target; # without any ':'
916 $content = $target; # strip off the title
918 $content =~ s/^:!/!:/s;
919 my $neg; # whether this is a negation-match
920 $neg = 1 if $content =~ s/^!//s;
921 my $to_resolve; # whether to process formatting codes
922 $to_resolve = 1 if $content =~ s/^://s;
924 my $dont_ignore; # whether this target matches us
926 foreach my $target_name (
927 split(',', $content, -1),
931 print " Considering whether =begin $content matches $target_name\n";
932 next unless $self->{'accept_targets'}{$target_name};
935 print " It DOES match the acceptable target $target_name!\n";
937 if $self->{'accept_targets'}{$target_name} eq 'force_resolve';
939 $para->[1]{'target_matching'} = $target_name;
940 last; # stop looking at other target names
946 delete $para->[1]{'target_matching'};
947 DEBUG > 2 and print " But the leading ! means that this is a NON-match!\n";
950 $para->[1]{'target_matching'} = '!';
951 DEBUG > 2 and print " But the leading ! means that this IS a match!\n";
955 $para->[0] = '=for'; # Just what we happen to call these, internally
956 $para->[1]{'~really'} ||= '=begin';
957 $para->[1]{'~ignore'} = (! $dont_ignore) || 0;
958 $para->[1]{'~resolve'} = $to_resolve || 0;
960 DEBUG > 1 and print " Making note to ", $dont_ignore ? 'not ' : '',
961 "ignore contents of this region\n";
962 DEBUG > 1 and $dont_ignore and print " Making note to treat contents as ",
963 ($to_resolve ? 'verbatim/plain' : 'data'), " paragraphs\n";
964 DEBUG > 1 and print " (Stack now: ", $self->_dump_curr_open(), ")\n";
966 push @$curr_open, $para;
967 if(!$dont_ignore or scalar grep $_->[1]{'~ignore'}, @$curr_open) {
968 DEBUG > 1 and print "Ignoring ignorable =begin\n";
970 $self->{'content_seen'} ||= 1;
971 $self->_handle_element_start((my $scratch='for'), $para->[1]);
978 my ($self,$para,$curr_open,$paras) = @_;
979 my $content = join ' ', splice @$para, 2;
980 $content =~ s/^\s+//s;
981 $content =~ s/\s+$//s;
982 DEBUG and print "Ogling '=end $content' directive\n";
984 unless(length($content)) {
986 $para->[1]{'start_line'},
987 "'=end' without a target?" . (
988 ( @$curr_open and $curr_open->[-1][0] eq '=for' )
989 ? ( " (Should be \"=end " . $curr_open->[-1][1]{'target'} . '")' )
993 DEBUG and print "Ignoring targetless =end\n";
997 unless($content =~ m/^\S+$/) { # i.e., unless it's one word
999 $para->[1]{'start_line'},
1000 "'=end $content' is invalid. (Stack: "
1001 . $self->_dump_curr_open() . ')'
1003 DEBUG and print "Ignoring mistargetted =end $content\n";
1007 unless(@$curr_open and $curr_open->[-1][0] eq '=for') {
1009 $para->[1]{'start_line'},
1010 "=end $content without matching =begin. (Stack: "
1011 . $self->_dump_curr_open() . ')'
1013 DEBUG and print "Ignoring mistargetted =end $content\n";
1017 unless($content eq $curr_open->[-1][1]{'target'}) {
1019 $para->[1]{'start_line'},
1020 "=end $content doesn't match =begin "
1021 . $curr_open->[-1][1]{'target'}
1023 . $self->_dump_curr_open() . ')'
1025 DEBUG and print "Ignoring mistargetted =end $content at line $para->[1]{'start_line'}\n";
1029 # Else it's okay to close...
1030 if(grep $_->[1]{'~ignore'}, @$curr_open) {
1031 DEBUG > 1 and print "Not firing any event for this =end $content because in an ignored region\n";
1032 # And that may be because of this to-be-closed =for region, or some
1033 # other one, but it doesn't matter.
1035 $curr_open->[-1][1]{'start_line'} = $para->[1]{'start_line'};
1038 $self->{'content_seen'} ||= 1;
1039 $self->_handle_element_end( my $scratch = 'for' );
1041 DEBUG > 1 and print "Popping $curr_open->[-1][0] $curr_open->[-1][1]{'target'} because of =end $content\n";
1047 sub _ponder_doc_end {
1048 my ($self,$para,$curr_open,$paras) = @_;
1049 if(@$curr_open) { # Deal with things left open
1050 DEBUG and print "Stack is nonempty at end-document: (",
1051 $self->_dump_curr_open(), ")\n";
1053 DEBUG > 9 and print "Stack: ", pretty($curr_open), "\n";
1054 unshift @$paras, $self->_closers_for_all_curr_open;
1055 # Make sure there is exactly one ~end in the parastack, at the end:
1056 @$paras = grep $_->[0] ne '~end', @$paras;
1057 push @$paras, $para, $para;
1058 # We need two -- once for the next cycle where we
1059 # generate errata, and then another to be at the end
1060 # when that loop back around to process the errata.
1064 DEBUG and print "Okay, stack is empty now.\n";
1067 # Try generating errata section, if applicable
1068 unless($self->{'~tried_gen_errata'}) {
1069 $self->{'~tried_gen_errata'} = 1;
1070 my @extras = $self->_gen_errata();
1072 unshift @$paras, @extras;
1073 DEBUG and print "Generated errata... relooping...\n";
1074 return 1; # I.e., loop around again to process these fake-o paragraphs
1078 splice @$paras; # Well, that's that for this paragraph buffer.
1079 DEBUG and print "Throwing end-document event.\n";
1081 $self->_handle_element_end( my $scratch = 'Document' );
1082 return 1; # Hasta la byebye
1086 my ($self,$para,$curr_open,$paras) = @_;
1088 $para->[1]{'start_line'},
1089 "=pod directives shouldn't be over one line long! Ignoring all "
1090 . (@$para - 2) . " lines of content"
1092 # Content is always ignored.
1097 my ($self,$para,$curr_open,$paras) = @_;
1098 return 1 unless @$paras;
1101 if($paras->[0][0] eq '=item') { # most common case
1102 $list_type = $self->_get_initial_item_type($paras->[0]);
1104 } elsif($paras->[0][0] eq '=back') {
1105 # Ignore empty lists. TODO: make this an option?
1109 } elsif($paras->[0][0] eq '~end') {
1111 $para->[1]{'start_line'},
1112 "=over is the last thing in the document?!"
1114 return 1; # But feh, ignore it.
1116 $list_type = 'block';
1118 $para->[1]{'~type'} = $list_type;
1119 push @$curr_open, $para;
1120 # yes, we reuse the paragraph as a stack item
1122 my $content = join ' ', splice @$para, 2;
1124 if($content =~ m/^\s*$/s) {
1125 $para->[1]{'indent'} = 4;
1126 } elsif($content =~ m/^\s*((?:\d*\.)?\d+)\s*$/s) {
1128 $para->[1]{'indent'} = $1;
1131 $para->[1]{'start_line'},
1132 "Can't have a 0 in =over $content"
1134 $para->[1]{'indent'} = 4;
1138 $para->[1]{'start_line'},
1139 "=over should be: '=over' or '=over positive_number'"
1141 $para->[1]{'indent'} = 4;
1143 DEBUG > 1 and print "=over found of type $list_type\n";
1145 $self->{'content_seen'} ||= 1;
1146 $self->_handle_element_start((my $scratch = 'over-' . $list_type), $para->[1]);
1152 my ($self,$para,$curr_open,$paras) = @_;
1153 # TODO: fire off </item-number> or </item-bullet> or </item-text> ??
1155 my $content = join ' ', splice @$para, 2;
1156 if($content =~ m/\S/) {
1158 $para->[1]{'start_line'},
1159 "=back doesn't take any parameters, but you said =back $content"
1163 if(@$curr_open and $curr_open->[-1][0] eq '=over') {
1164 DEBUG > 1 and print "=back happily closes matching =over\n";
1165 # Expected case: we're closing the most recently opened thing
1166 #my $over = pop @$curr_open;
1167 $self->{'content_seen'} ||= 1;
1168 $self->_handle_element_end( my $scratch =
1169 'over-' . ( (pop @$curr_open)->[1]{'~type'} )
1172 DEBUG > 1 and print "=back found without a matching =over. Stack: (",
1173 join(', ', map $_->[0], @$curr_open), ").\n";
1175 $para->[1]{'start_line'},
1176 '=back without =over'
1178 return 1; # and ignore it
1183 my ($self,$para,$curr_open,$paras) = @_;
1185 unless(@$curr_open and ($over = $curr_open->[-1])->[0] eq '=over') {
1187 $para->[1]{'start_line'},
1188 "'=item' outside of any '=over'"
1191 ['=over', {'start_line' => $para->[1]{'start_line'}}, ''],
1198 my $over_type = $over->[1]{'~type'};
1202 die "Typeless over in stack, starting at line "
1203 . $over->[1]{'start_line'};
1205 } elsif($over_type eq 'block') {
1206 unless($curr_open->[-1][1]{'~bitched_about'}) {
1207 $curr_open->[-1][1]{'~bitched_about'} = 1;
1209 $curr_open->[-1][1]{'start_line'},
1210 "You can't have =items (as at line "
1211 . $para->[1]{'start_line'}
1212 . ") unless the first thing after the =over is an =item"
1215 # Just turn it into a paragraph and reconsider it
1216 $para->[0] = '~Para';
1217 unshift @$paras, $para;
1220 } elsif($over_type eq 'text') {
1221 my $item_type = $self->_get_item_type($para);
1222 # That kills the content of the item if it's a number or bullet.
1223 DEBUG and print " Item is of type ", $para->[0], " under $over_type\n";
1225 if($item_type eq 'text') {
1226 # Nothing special needs doing for 'text'
1227 } elsif($item_type eq 'number' or $item_type eq 'bullet') {
1228 die "Unknown item type $item_type"
1229 unless $item_type eq 'number' or $item_type eq 'bullet';
1230 # Undo our clobbering:
1231 push @$para, $para->[1]{'~orig_content'};
1232 delete $para->[1]{'number'};
1233 # Only a PROPER item-number element is allowed
1234 # to have a number attribute.
1236 die "Unhandled item type $item_type"; # should never happen
1239 # =item-text thingies don't need any assimilation, it seems.
1241 } elsif($over_type eq 'number') {
1242 my $item_type = $self->_get_item_type($para);
1243 # That kills the content of the item if it's a number or bullet.
1244 DEBUG and print " Item is of type ", $para->[0], " under $over_type\n";
1246 my $expected_value = ++ $curr_open->[-1][1]{'~counter'};
1248 if($item_type eq 'bullet') {
1249 # Hm, it's not numeric. Correct for this.
1250 $para->[1]{'number'} = $expected_value;
1252 $para->[1]{'start_line'},
1253 "Expected '=item $expected_value'"
1255 push @$para, $para->[1]{'~orig_content'};
1256 # restore the bullet, blocking the assimilation of next para
1258 } elsif($item_type eq 'text') {
1259 # Hm, it's not numeric. Correct for this.
1260 $para->[1]{'number'} = $expected_value;
1262 $para->[1]{'start_line'},
1263 "Expected '=item $expected_value'"
1265 # Text content will still be there and will block next ~Para
1267 } elsif($item_type ne 'number') {
1268 die "Unknown item type $item_type"; # should never happen
1270 } elsif($expected_value == $para->[1]{'number'}) {
1271 DEBUG > 1 and print " Numeric item has the expected value of $expected_value\n";
1274 DEBUG > 1 and print " Numeric item has ", $para->[1]{'number'},
1275 " instead of the expected value of $expected_value\n";
1277 $para->[1]{'start_line'},
1278 "You have '=item " . $para->[1]{'number'} .
1279 "' instead of the expected '=item $expected_value'"
1281 $para->[1]{'number'} = $expected_value; # correcting!!
1285 # For the cases where we /didn't/ push to @$para
1286 if($paras->[0][0] eq '~Para') {
1287 DEBUG and print "Assimilating following ~Para content into $over_type item\n";
1288 push @$para, splice @{shift @$paras},2;
1290 DEBUG and print "Can't assimilate following ", $paras->[0][0], "\n";
1291 push @$para, ''; # Just so it's not contentless
1296 } elsif($over_type eq 'bullet') {
1297 my $item_type = $self->_get_item_type($para);
1298 # That kills the content of the item if it's a number or bullet.
1299 DEBUG and print " Item is of type ", $para->[0], " under $over_type\n";
1301 if($item_type eq 'bullet') {
1304 if( $para->[1]{'~_freaky_para_hack'} ) {
1305 DEBUG and print "Accomodating '=item * Foo' tolerance hack.\n";
1306 push @$para, delete $para->[1]{'~_freaky_para_hack'};
1309 } elsif($item_type eq 'number') {
1311 $para->[1]{'start_line'},
1312 "Expected '=item *'"
1314 push @$para, $para->[1]{'~orig_content'};
1315 # and block assimilation of the next paragraph
1316 delete $para->[1]{'number'};
1317 # Only a PROPER item-number element is allowed
1318 # to have a number attribute.
1319 } elsif($item_type eq 'text') {
1321 $para->[1]{'start_line'},
1322 "Expected '=item *'"
1324 # But doesn't need processing. But it'll block assimilation
1327 die "Unhandled item type $item_type"; # should never happen
1331 # For the cases where we /didn't/ push to @$para
1332 if($paras->[0][0] eq '~Para') {
1333 DEBUG and print "Assimilating following ~Para content into $over_type item\n";
1334 push @$para, splice @{shift @$paras},2;
1336 DEBUG and print "Can't assimilate following ", $paras->[0][0], "\n";
1337 push @$para, ''; # Just so it's not contentless
1342 die "Unhandled =over type \"$over_type\"?";
1345 $para->[0] .= '-' . $over_type;
1351 my ($self,$para) = @_;
1352 DEBUG and print " giving plain treatment...\n";
1353 unless( @$para == 2 or ( @$para == 3 and $para->[2] eq '' )
1354 or $para->[1]{'~cooked'}
1357 @{$self->_make_treelet(
1358 join("\n", splice(@$para, 2)),
1359 $para->[1]{'start_line'}
1362 # Empty paragraphs don't need a treelet for any reason I can see.
1363 # And precooked paragraphs already have a treelet.
1367 sub _ponder_Verbatim {
1368 my ($self,$para) = @_;
1369 DEBUG and print " giving verbatim treatment...\n";
1371 $para->[1]{'xml:space'} = 'preserve';
1372 for(my $i = 2; $i < @$para; $i++) {
1373 foreach my $line ($para->[$i]) { # just for aliasing
1375 # Sort of adapted from Text::Tabs -- yes, it's hardwired in that
1376 # tabs are at every EIGHTH column. For portability, it has to be
1377 # one setting everywhere, and 8th wins.
1378 s/^([^\t]*)(\t+)/$1.(" " x ((length($2)<<3)-(length($1)&7)))/e
1381 # TODO: whinge about (or otherwise treat) unindented or overlong lines
1386 # Now the VerbatimFormatted hoodoo...
1387 if( $self->{'accept_codes'} and
1388 $self->{'accept_codes'}{'VerbatimFormatted'}
1390 while(@$para > 3 and $para->[-1] !~ m/\S/) { pop @$para }
1391 # Kill any number of terminal newlines
1392 $self->_verbatim_format($para);
1393 } elsif ($self->{'codes_in_verbatim'}) {
1395 @{$self->_make_treelet(
1396 join("\n", splice(@$para, 2)),
1397 $para->[1]{'start_line'}, $para->[1]{'xml:space'}
1399 $para->[-1] =~ s/\n+$//s; # Kill any number of terminal newlines
1401 push @$para, join "\n", splice(@$para, 2) if @$para > 3;
1402 $para->[-1] =~ s/\n+$//s; # Kill any number of terminal newlines
1408 my ($self,$para) = @_;
1409 DEBUG and print " giving data treatment...\n";
1410 $para->[1]{'xml:space'} = 'preserve';
1411 push @$para, join "\n", splice(@$para, 2) if @$para > 3;
1418 ###########################################################################
1420 sub _traverse_treelet_bit { # for use only by the routine above
1421 my($self, $name) = splice @_,0,2;
1424 $self->_handle_element_start(($scratch=$name), shift @_);
1426 foreach my $x (@_) {
1428 &_traverse_treelet_bit($self, @$x);
1430 $self->_handle_text($x);
1434 $self->_handle_element_end($scratch=$name);
1438 #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1440 sub _closers_for_all_curr_open {
1443 foreach my $still_open (@{ $self->{'curr_open'} || return }) {
1444 my @copy = @$still_open;
1445 $copy[1] = {%{ $copy[1] }};
1446 #$copy[1]{'start_line'} = -1;
1447 if($copy[0] eq '=for') {
1449 } elsif($copy[0] eq '=over') {
1452 die "I don't know how to auto-close an open $copy[0] region";
1455 unless( @copy > 2 ) {
1456 push @copy, $copy[1]{'target'};
1457 $copy[-1] = '' unless defined $copy[-1];
1458 # since =over's don't have targets
1461 DEBUG and print "Queuing up fake-o event: ", pretty(\@copy), "\n";
1462 unshift @closers, \@copy;
1467 #--------------------------------------------------------------------------
1469 sub _verbatim_format {
1474 for(my $i = 2; $i < @$p; $i++) { # work backwards over the lines
1475 DEBUG and print "_verbatim_format appends a newline to $i: $p->[$i]\n";
1477 # Unlike with simple Verbatim blocks, we don't end up just doing
1478 # a join("\n", ...) on the contents, so we have to append a
1479 # newline to ever line, and then nix the last one later.
1484 for(my $i = $#$p; $i >= 2; $i--) { # work backwards over the lines
1485 print "_verbatim_format $i: $p->[$i]";
1490 for(my $i = $#$p; $i > 2; $i--) {
1491 # work backwards over the lines, except the first (#2)
1493 #next unless $p->[$i] =~ m{^#:([ \^\/\%]*)\n?$}s
1494 # and $p->[$i-1] !~ m{^#:[ \^\/\%]*\n?$}s;
1495 # look at a formatty line preceding a nonformatty one
1496 DEBUG > 5 and print "Scrutinizing line $i: $$p[$i]\n";
1497 if($p->[$i] =~ m{^#:([ \^\/\%]*)\n?$}s) {
1498 DEBUG > 5 and print " It's a formatty line. ",
1499 "Peeking at previous line ", $i-1, ": $$p[$i-1]: \n";
1501 if( $p->[$i-1] =~ m{^#:[ \^\/\%]*\n?$}s ) {
1502 DEBUG > 5 and print " Previous line is formatty! Skipping this one.\n";
1505 DEBUG > 5 and print " Previous line is non-formatty! Yay!\n";
1508 DEBUG > 5 and print " It's not a formatty line. Ignoring\n";
1512 # A formatty line has to have #: in the first two columns, and uses
1513 # "^" to mean bold, "/" to mean underline, and "%" to mean bold italic.
1515 # What do you want? i like pie. [or whatever]
1516 # #:^^^^^^^^^^^^^^^^^ /////////////
1519 DEBUG > 4 and print "_verbatim_format considers:\n<$p->[$i-1]>\n<$p->[$i]>\n";
1521 $formatting = ' ' . $1;
1522 $formatting =~ s/\s+$//s; # nix trailing whitespace
1523 unless(length $formatting and $p->[$i-1] =~ m/\S/) { # no-op
1524 splice @$p,$i,1; # remove this line
1525 $i--; # don't consider next line
1529 if( length($formatting) >= length($p->[$i-1]) ) {
1530 $formatting = substr($formatting, 0, length($p->[$i-1]) - 1) . ' ';
1532 $formatting .= ' ' x (length($p->[$i-1]) - length($formatting));
1534 # Make $formatting and the previous line be exactly the same length,
1535 # with $formatting having a " " as the last character.
1537 DEBUG > 4 and print "Formatting <$formatting> on <", $p->[$i-1], ">\n";
1541 while( $formatting =~ m{\G(( +)|(\^+)|(\/+)|(\%+))}g ) {
1542 #print "Format matches $1\n";
1545 #print "SKIPPING <$2>\n";
1547 substr($p->[$i-1], pos($formatting)-length($1), length($1));
1549 #print "SNARING $+\n";
1554 $5 ? 'VerbatimBI' : die("Should never get called")
1556 substr($p->[$i-1], pos($formatting)-length($1), length($1))
1558 #print "Formatting <$new_line[-1][-1]> as $new_line[-1][0]\n";
1562 splice @$p, $i-1, 2, @new_line; # replace myself and the next line
1563 DEBUG > 10 and print "Nixed count: ", scalar(@nixed), "\n";
1565 DEBUG > 6 and print "New version of the above line is these tokens (",
1566 scalar(@new_line), "):",
1567 map( ref($_)?"<@$_> ":"<$_>", @new_line ), "\n";
1568 $i--; # So the next line we scrutinize is the line before the one
1569 # that we just went and formatted
1572 $p->[0] = 'VerbatimFormatted';
1574 # Collapse adjacent text nodes, just for kicks.
1575 for( my $i = 2; $i > $#$p; $i++ ) { # work forwards over the tokens except for the last
1576 if( !ref($p->[$i]) and !ref($p->[$i + 1]) ) {
1577 DEBUG > 5 and print "_verbatim_format merges {$p->[$i]} and {$p->[$i+1]}\n";
1578 $p->[$i] .= splice @$p, $i+1, 1; # merge
1583 # Now look for the last text token, and remove the terminal newline
1584 for( my $i = $#$p; $i >= 2; $i-- ) {
1585 # work backwards over the tokens, even the first
1586 if( !ref($p->[$i]) ) {
1587 if($p->[$i] =~ s/\n$//s) {
1588 DEBUG > 5 and print "_verbatim_format killed the terminal newline on #$i: {$p->[$i]}, after {$p->[$i-1]}\n";
1591 "No terminal newline on #$i: {$p->[$i]}, after {$p->[$i-1]} !?\n";
1593 last; # we only want the next one
1601 #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1604 sub _treelet_from_formatting_codes {
1605 # Given a paragraph, returns a treelet. Full of scary tokenizing code.
1606 # Like [ '~Top', {'start_line' => $start_line},
1608 # [ 'B', {}, "pie" ],
1612 my($self, $para, $start_line, $preserve_space) = @_;
1614 my $treelet = ['~Top', {'start_line' => $start_line},];
1616 unless ($preserve_space || $self->{'preserve_whitespace'}) {
1617 $para =~ s/\. /\.\xA0 /g if $self->{'fullstop_space_harden'};
1619 $para =~ s/\s+/ /g; # collapse and trim all whitespace first.
1624 # Only apparent problem the above code is that N<< >> turns into
1625 # N<< >>. But then, word wrapping does that too! So don't do that!
1628 my @lineage = ($treelet);
1630 DEBUG > 4 and print "Paragraph:\n$para\n\n";
1632 # Here begins our frightening tokenizer RE. The following regex matches
1633 # text in four main parts:
1635 # * Start-codes. The first alternative matches C< or C<<, the latter
1636 # followed by some whitespace. $1 will hold the entire start code
1637 # (including any space following a multiple-angle-bracket delimiter),
1638 # and $2 will hold only the additional brackets past the first in a
1639 # multiple-bracket delimiter. length($2) + 1 will be the number of
1640 # closing brackets we have to find.
1642 # * Closing brackets. Match some amount of whitespace followed by
1643 # multiple close brackets. The logic to see if this closes anything
1644 # is down below. Note that in order to parse C<< >> correctly, we
1645 # have to use look-behind (?<=\s\s), since the match of the starting
1646 # code will have consumed the whitespace.
1648 # * A single closing bracket, to close a simple code like C<>.
1650 # * Something that isn't a start or end code. We have to be careful
1651 # about accepting whitespace, since perlpodspec says that any whitespace
1652 # before a multiple-bracket closing delimiter should be ignored.
1657 # Match starting codes, including the whitespace following a
1658 # multiple-delimiter start code. $1 gets the whole start code and
1659 # $2 gets all but one of the <s in the multiple-bracket case.
1660 ([A-Z]<(?:(<+)\s+)?)
1662 # Match multiple-bracket end codes. $3 gets the whitespace that
1663 # should be discarded before an end bracket but kept in other cases
1664 # and $4 gets the end brackets themselves.
1665 (\s+|(?<=\s\s))(>{2,})
1667 (\s?>) # $5: simple end-codes
1669 ( # $6: stuff containing no start-codes or end-codes
1677 # whitespace is ok, but we don't want to eat the whitespace before
1678 # a multiple-bracket end code.
1679 # NOTE: we may still have problems with e.g. S<< >>
1688 DEBUG > 4 and print "\nParagraphic tokenstack = (@stack)\n";
1691 DEBUG > 3 and print "Found complex start-text code \"$1\"\n";
1692 push @stack, length($2) + 1;
1693 # length of the necessary complex end-code string
1695 DEBUG > 3 and print "Found simple start-text code \"$1\"\n";
1696 push @stack, 0; # signal that we're looking for simple
1698 push @lineage, [ substr($1,0,1), {}, ]; # new node object
1699 push @{ $lineage[-2] }, $lineage[-1];
1701 } elsif(defined $4) {
1702 DEBUG > 3 and print "Found apparent complex end-text code \"$3$4\"\n";
1703 # This is where it gets messy...
1705 # We saw " >>>>" but needed nothing. This is ALL just stuff then.
1706 DEBUG > 4 and print " But it's really just stuff.\n";
1707 push @{ $lineage[-1] }, $3, $4;
1709 } elsif(!$stack[-1]) {
1710 # We saw " >>>>" but needed only ">". Back pos up.
1711 DEBUG > 4 and print " And that's more than we needed to close simple.\n";
1712 push @{ $lineage[-1] }, $3; # That was a for-real space, too.
1713 pos($para) = pos($para) - length($4) + 1;
1714 } elsif($stack[-1] == length($4)) {
1715 # We found " >>>>", and it was exactly what we needed. Commonest case.
1716 DEBUG > 4 and print " And that's exactly what we needed to close complex.\n";
1717 } elsif($stack[-1] < length($4)) {
1718 # We saw " >>>>" but needed only " >>". Back pos up.
1719 DEBUG > 4 and print " And that's more than we needed to close complex.\n";
1720 pos($para) = pos($para) - length($4) + $stack[-1];
1722 # We saw " >>>>" but needed " >>>>>>". So this is all just stuff!
1723 DEBUG > 4 and print " But it's really just stuff, because we needed more.\n";
1724 push @{ $lineage[-1] }, $3, $4;
1727 #print "\nHOOBOY ", scalar(@{$lineage[-1]}), "!!!\n";
1729 push @{ $lineage[-1] }, '' if 2 == @{ $lineage[-1] };
1730 # Keep the element from being childless
1735 } elsif(defined $5) {
1736 DEBUG > 3 and print "Found apparent simple end-text code \"$4\"\n";
1738 if(@stack and ! $stack[-1]) {
1739 # We're indeed expecting a simple end-code
1740 DEBUG > 4 and print " It's indeed an end-code.\n";
1742 if(length($5) == 2) { # There was a space there: " >"
1743 push @{ $lineage[-1] }, ' ';
1744 } elsif( 2 == @{ $lineage[-1] } ) { # Closing a childless element
1745 push @{ $lineage[-1] }, ''; # keep it from being really childless
1751 DEBUG > 4 and print " It's just stuff.\n";
1752 push @{ $lineage[-1] }, $5;
1755 } elsif(defined $6) {
1756 DEBUG > 3 and print "Found stuff \"$6\"\n";
1757 push @{ $lineage[-1] }, $6;
1760 # should never ever ever ever happen
1761 DEBUG and print "AYYAYAAAAA at line ", __LINE__, "\n";
1762 die "SPORK 512512!";
1766 if(@stack) { # Uhoh, some sequences weren't closed.
1769 push @{ $lineage[-1] }, '' if 2 == @{ $lineage[-1] };
1772 my $code = (pop @lineage)->[0];
1773 my $ender_length = pop @stack;
1776 $x = $code . ("<" x $ender_length) . " $x " . (">" x $ender_length);
1778 $x = $code . "<$x>";
1781 DEBUG > 1 and print "Unterminated $x sequence\n";
1782 $self->whine($start_line,
1783 "Unterminated $x sequence",
1790 #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1792 sub text_content_of_treelet { # method: $parser->text_content_of_treelet($lol)
1793 return stringify_lol($_[1]);
1796 sub stringify_lol { # function: stringify_lol($lol)
1797 my $string_form = '';
1798 _stringify_lol( $_[0] => \$string_form );
1799 return $string_form;
1802 sub _stringify_lol { # the real recursor
1805 for(my $i = 2; $i < @$lol; ++$i) {
1806 if( ref($lol->[$i] || '') and UNIVERSAL::isa($lol->[$i], 'ARRAY') ) {
1807 _stringify_lol( $lol->[$i], $to); # recurse!
1815 #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1817 sub _dump_curr_open { # return a string representation of the stack
1818 my $curr_open = $_[0]{'curr_open'};
1820 return '[empty]' unless @$curr_open;
1824 ? ( ($_->[1]{'~really'} || '=over')
1825 . ' ' . $_->[1]{'target'})
1832 ###########################################################################
1834 "\a" => '\a', # ding!
1841 "\n" => '\n', # probably overrides one of either \cm or \cj
1850 sub pretty { # adopted from Class::Classless
1851 # Not the most brilliant routine, but passable.
1852 # Don't give it a cyclic data structure!
1853 my @stuff = @_; # copy
1861 } elsif(ref($_) eq 'ARRAY' or ref($_) eq 'Pod::Simple::LinkSection') {
1862 $x = "[ " . pretty(@$_) . " ]" ;
1864 } elsif(ref($_) eq 'SCALAR') {
1865 $x = "\\" . pretty($$_) ;
1867 } elsif(ref($_) eq 'HASH') {
1869 $x = "{" . join(", ",
1870 map(pretty($_) . '=>' . pretty($hr->{$_}),
1871 sort keys %$hr ) ) . "}" ;
1873 } elsif(!length($_)) { q{''} # empty string
1875 $_ eq '0' # very common case
1877 m/^-?(?:[123456789]\d*|0)(?:\.\d+)?$/s
1878 and $_ ne '-0' # the strange case that that RE lets thru
1882 if( chr(65) eq 'A' ) {
1883 s<([^\x20\x21\x23\x27-\x3F\x41-\x5B\x5D-\x7E])>
1884 #<$pretty_form{$1} || '\\x'.(unpack("H2",$1))>eg;
1885 <$pretty_form{$1} || '\\x{'.sprintf("%x", ord($1)).'}'>eg;
1887 # We're in some crazy non-ASCII world!
1888 s<([^abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789])>
1889 #<$pretty_form{$1} || '\\x'.(unpack("H2",$1))>eg;
1890 <$pretty_form{$1} || '\\x{'.sprintf("%x", ord($1)).'}'>eg;
1895 # $out =~ s/\n */ /g if length($out) < 75;
1899 #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
1901 # A rather unsubtle method of blowing away all the state information
1902 # from a parser object so it can be reused. Provided as a utility for
1903 # backward compatibilty in Pod::Man, etc. but not recommended for
1908 foreach (qw(source_dead source_filename doc_has_started
1909 start_of_pod_block content_seen last_was_blank paras curr_open
1910 line_count pod_para_count in_pod ~tried_gen_errata errata errors_seen
1917 #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@