xref: /openbsd-src/gnu/usr.bin/perl/cpan/Pod-Simple/lib/Pod/Simple.pm (revision 3d61058aa5c692477b6d18acfbbdb653a9930ff9)
1b39c5158Smillertpackage Pod::Simple;
2b39c5158Smillertuse strict;
3*3d61058aSafresh1use warnings;
4b39c5158Smillertuse Carp ();
5b39c5158SmillertBEGIN           { *DEBUG = sub () {0} unless defined &DEBUG }
6b39c5158Smillertuse integer;
7b39c5158Smillertuse Pod::Escapes 1.04 ();
8b39c5158Smillertuse Pod::Simple::LinkSection ();
9b39c5158Smillertuse Pod::Simple::BlackBox ();
10eac174f2Safresh1use Pod::Simple::TiedOutFH;
11b39c5158Smillert#use utf8;
12b39c5158Smillert
13*3d61058aSafresh1our @ISA = ('Pod::Simple::BlackBox');
14*3d61058aSafresh1our $VERSION = '3.45';
15b39c5158Smillert
16*3d61058aSafresh1our @Known_formatting_codes = qw(I B C L E F S X Z);
17*3d61058aSafresh1our %Known_formatting_codes = map(($_=>1), @Known_formatting_codes);
18*3d61058aSafresh1our @Known_directives       = qw(head1 head2 head3 head4 head5 head6 item over back);
19*3d61058aSafresh1our %Known_directives       = map(($_=>'Plain'), @Known_directives);
20*3d61058aSafresh1our $NL = $/ unless defined $NL;
21b39c5158Smillert
22b39c5158Smillert#-----------------------------------------------------------------------------
23b39c5158Smillert# Set up some constants:
24b39c5158Smillert
25b39c5158SmillertBEGIN {
26b39c5158Smillert  if(defined &ASCII)    { }
27b39c5158Smillert  elsif(chr(65) eq 'A') { *ASCII = sub () {1}  }
28b39c5158Smillert  else                  { *ASCII = sub () {''} }
29b39c5158Smillert
30b39c5158Smillert  unless(defined &MANY_LINES) { *MANY_LINES = sub () {20} }
31b8851fccSafresh1  DEBUG > 4 and print STDERR "MANY_LINES is ", MANY_LINES(), "\n";
32b39c5158Smillert  unless(MANY_LINES() >= 1) {
33b39c5158Smillert    die "MANY_LINES is too small (", MANY_LINES(), ")!\nAborting";
34b39c5158Smillert  }
35b39c5158Smillert  if(defined &UNICODE) { }
36b39c5158Smillert  elsif($] >= 5.008)   { *UNICODE = sub() {1}  }
37b39c5158Smillert  else                 { *UNICODE = sub() {''} }
38b39c5158Smillert}
39b39c5158Smillertif(DEBUG > 2) {
40b8851fccSafresh1  print STDERR "# We are ", ASCII ? '' : 'not ', "in ASCII-land\n";
41b8851fccSafresh1  print STDERR "# We are under a Unicode-safe Perl.\n";
42b8851fccSafresh1}
43b8851fccSafresh1
44b8851fccSafresh1# The NO BREAK SPACE and SOFT HYHPEN are used in several submodules.
45b8851fccSafresh1if ($] ge 5.007_003) {  # On sufficiently modern Perls we can handle any
46b8851fccSafresh1                        # character set
47b8851fccSafresh1  $Pod::Simple::nbsp = chr utf8::unicode_to_native(0xA0);
48b8851fccSafresh1  $Pod::Simple::shy  = chr utf8::unicode_to_native(0xAD);
49b8851fccSafresh1}
50b8851fccSafresh1elsif (Pod::Simple::ASCII) {  # Hard code ASCII early Perl
51b8851fccSafresh1  $Pod::Simple::nbsp = "\xA0";
52b8851fccSafresh1  $Pod::Simple::shy  = "\xAD";
53b8851fccSafresh1}
54b8851fccSafresh1else { # EBCDIC on early Perl.  We know what the values are for the code
55b8851fccSafresh1        # pages supported then.
56b8851fccSafresh1  $Pod::Simple::nbsp = "\x41";
57b8851fccSafresh1  $Pod::Simple::shy  = "\xCA";
58b39c5158Smillert}
59b39c5158Smillert
60b39c5158Smillert# Design note:
61b39c5158Smillert# This is a parser for Pod.  It is not a parser for the set of Pod-like
62b39c5158Smillert#  languages which happens to contain Pod -- it is just for Pod, plus possibly
63b39c5158Smillert#  some extensions.
64b39c5158Smillert
65b39c5158Smillert# @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
66b39c5158Smillert#@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
67b39c5158Smillert#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
68b39c5158Smillert
69b39c5158Smillert__PACKAGE__->_accessorize(
7056d68f1eSafresh1  '_output_is_for_JustPod', # For use only by Pod::Simple::JustPod,
7156d68f1eSafresh1                       # If non-zero, don't expand Z<> E<> S<> L<>,
7256d68f1eSafresh1                       # and count how many brackets in format codes
73b39c5158Smillert  'nbsp_for_S',        # Whether to map S<...>'s to \xA0 characters
74b39c5158Smillert  'source_filename',   # Filename of the source, for use in warnings
75b39c5158Smillert  'source_dead',       # Whether to consider this parser's source dead
76b39c5158Smillert
77b39c5158Smillert  'output_fh',         # The filehandle we're writing to, if applicable.
78b39c5158Smillert                       # Used only in some derived classes.
79b39c5158Smillert
80b39c5158Smillert  'hide_line_numbers', # For some dumping subclasses: whether to pointedly
81b39c5158Smillert                       # suppress the start_line attribute
82b39c5158Smillert
83b39c5158Smillert  'line_count',        # the current line number
84b39c5158Smillert  'pod_para_count',    # count of pod paragraphs seen so far
85b39c5158Smillert
86b39c5158Smillert  'no_whining',        # whether to suppress whining
87b39c5158Smillert  'no_errata_section', # whether to suppress the errata section
88b39c5158Smillert  'complain_stderr',   # whether to complain to stderr
89b39c5158Smillert
90b39c5158Smillert  'doc_has_started',   # whether we've fired the open-Document event yet
91b39c5158Smillert
92b39c5158Smillert  'bare_output',       # For some subclasses: whether to prepend
93b39c5158Smillert                       #  header-code and postpend footer-code
94b39c5158Smillert
9591f110e0Safresh1  'keep_encoding_directive',  # whether to emit =encoding
96b39c5158Smillert  'nix_X_codes',       # whether to ignore X<...> codes
97b39c5158Smillert  'merge_text',        # whether to avoid breaking a single piece of
98b39c5158Smillert                       #  text up into several events
99b39c5158Smillert
100b39c5158Smillert  'preserve_whitespace', # whether to try to keep whitespace as-is
101b39c5158Smillert  'strip_verbatim_indent', # What indent to strip from verbatim
10256d68f1eSafresh1  'expand_verbatim_tabs',  # 0: preserve tabs in verbatim blocks
10356d68f1eSafresh1                           # n: expand tabs to stops every n columns
104b39c5158Smillert
10591f110e0Safresh1  'parse_characters',  # Whether parser should expect chars rather than octets
10691f110e0Safresh1
107b39c5158Smillert 'content_seen',      # whether we've seen any real Pod content
108b39c5158Smillert 'errors_seen',       # TODO: document.  whether we've seen any errors (fatal or not)
109b39c5158Smillert
110b39c5158Smillert 'codes_in_verbatim', # for PseudoPod extensions
111b39c5158Smillert
112b39c5158Smillert 'code_handler',      # coderef to call when a code (non-pod) line is seen
113898184e3Ssthen 'cut_handler',       # ... when a =cut line is seen
114898184e3Ssthen 'pod_handler',       # ... when a =pod line is seen
115898184e3Ssthen 'whiteline_handler', # ... when a line with only whitespace is seen
116b39c5158Smillert #Called like:
117b39c5158Smillert # $code_handler->($line, $self->{'line_count'}, $self) if $code_handler;
118b39c5158Smillert #  $cut_handler->($line, $self->{'line_count'}, $self) if $cut_handler;
119898184e3Ssthen #  $pod_handler->($line, $self->{'line_count'}, $self) if $pod_handler;
120898184e3Ssthen #   $wl_handler->($line, $self->{'line_count'}, $self) if $wl_handler;
121898184e3Ssthen 'parse_empty_lists', # whether to acknowledge empty =over/=back blocks
122b8851fccSafresh1 'raw_mode',          # to report entire raw lines instead of Pod elements
123b39c5158Smillert);
124b39c5158Smillert
125b39c5158Smillert#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
126b39c5158Smillert
127b39c5158Smillertsub any_errata_seen {  # good for using as an exit() value...
128b39c5158Smillert  return shift->{'errors_seen'} || 0;
129b39c5158Smillert}
130b39c5158Smillert
131b8851fccSafresh1sub errata_seen {
132b8851fccSafresh1  return shift->{'all_errata'} || {};
133b8851fccSafresh1}
134b8851fccSafresh1
13591f110e0Safresh1# Returns the encoding only if it was recognized as being handled and set
13691f110e0Safresh1sub detected_encoding {
13791f110e0Safresh1  return shift->{'detected_encoding'};
13891f110e0Safresh1}
13991f110e0Safresh1
14091f110e0Safresh1sub encoding {
14191f110e0Safresh1  my $this = shift;
14291f110e0Safresh1  return $this->{'encoding'} unless @_;  # GET.
14391f110e0Safresh1
14491f110e0Safresh1  $this->_handle_encoding_line("=encoding $_[0]");
14591f110e0Safresh1  if ($this->{'_processed_encoding'}) {
14691f110e0Safresh1    delete $this->{'_processed_encoding'};
14791f110e0Safresh1    if(! $this->{'encoding_command_statuses'} ) {
148b8851fccSafresh1      DEBUG > 2 and print STDERR " CRAZY ERROR: encoding wasn't really handled?!\n";
14991f110e0Safresh1    } elsif( $this->{'encoding_command_statuses'}[-1] ) {
15091f110e0Safresh1      $this->scream( "=encoding $_[0]",
15191f110e0Safresh1         sprintf "Couldn't do %s: %s",
15291f110e0Safresh1         $this->{'encoding_command_reqs'  }[-1],
15391f110e0Safresh1         $this->{'encoding_command_statuses'}[-1],
15491f110e0Safresh1      );
15591f110e0Safresh1    } else {
156b8851fccSafresh1      DEBUG > 2 and print STDERR " (encoding successfully handled.)\n";
15791f110e0Safresh1    }
15891f110e0Safresh1    return $this->{'encoding'};
15991f110e0Safresh1  } else {
16091f110e0Safresh1    return undef;
16191f110e0Safresh1  }
16291f110e0Safresh1}
16391f110e0Safresh1
164b39c5158Smillert#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
165b39c5158Smillert# Pull in some functions that, for some reason, I expect to see here too:
166b39c5158SmillertBEGIN {
167b39c5158Smillert  *pretty        = \&Pod::Simple::BlackBox::pretty;
168b39c5158Smillert  *stringify_lol = \&Pod::Simple::BlackBox::stringify_lol;
16956d68f1eSafresh1  *my_qr         = \&Pod::Simple::BlackBox::my_qr;
170b39c5158Smillert}
171b39c5158Smillert
172b39c5158Smillert#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
173b39c5158Smillert
174b39c5158Smillertsub version_report {
175b39c5158Smillert  my $class = ref($_[0]) || $_[0];
176b39c5158Smillert  if($class eq __PACKAGE__) {
177b39c5158Smillert    return "$class $VERSION";
178b39c5158Smillert  } else {
179b39c5158Smillert    my $v = $class->VERSION;
180b39c5158Smillert    return "$class $v (" . __PACKAGE__ . " $VERSION)";
181b39c5158Smillert  }
182b39c5158Smillert}
183b39c5158Smillert
184b39c5158Smillert#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
185b39c5158Smillert
186b39c5158Smillert#sub curr_open { # read-only list accessor
187b39c5158Smillert#  return @{ $_[0]{'curr_open'} || return() };
188b39c5158Smillert#}
189b39c5158Smillert#sub _curr_open_listref { $_[0]{'curr_open'} ||= [] }
190b39c5158Smillert
191b39c5158Smillert
192b39c5158Smillertsub output_string {
193b39c5158Smillert  # Works by faking out output_fh.  Simplifies our code.
194b39c5158Smillert  #
195b39c5158Smillert  my $this = shift;
196b39c5158Smillert  return $this->{'output_string'} unless @_;  # GET.
197b39c5158Smillert
198b39c5158Smillert  my $x = (defined($_[0]) and ref($_[0])) ? $_[0] : \( $_[0] );
199b39c5158Smillert  $$x = '' unless defined $$x;
200b8851fccSafresh1  DEBUG > 4 and print STDERR "# Output string set to $x ($$x)\n";
201b39c5158Smillert  $this->{'output_fh'} = Pod::Simple::TiedOutFH->handle_on($_[0]);
202b39c5158Smillert  return
203b39c5158Smillert    $this->{'output_string'} = $_[0];
204b39c5158Smillert    #${ ${ $this->{'output_fh'} } };
205b39c5158Smillert}
206b39c5158Smillert
207b39c5158Smillertsub abandon_output_string { $_[0]->abandon_output_fh; delete $_[0]{'output_string'} }
208b39c5158Smillertsub abandon_output_fh     { $_[0]->output_fh(undef) }
209b39c5158Smillert# These don't delete the string or close the FH -- they just delete our
210b39c5158Smillert#  references to it/them.
211b39c5158Smillert# TODO: document these
212b39c5158Smillert
213b39c5158Smillert#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
214b39c5158Smillert
215b39c5158Smillertsub new {
216b39c5158Smillert  # takes no parameters
217b39c5158Smillert  my $class = ref($_[0]) || $_[0];
218b39c5158Smillert  #Carp::croak(__PACKAGE__ . " is a virtual base class -- see perldoc "
219b39c5158Smillert  #  . __PACKAGE__ );
22056d68f1eSafresh1  my $obj = bless {
221b39c5158Smillert    'accept_codes'      => { map( ($_=>$_), @Known_formatting_codes ) },
222b39c5158Smillert    'accept_directives' => { %Known_directives },
223b39c5158Smillert    'accept_targets'    => {},
224b39c5158Smillert  }, $class;
22556d68f1eSafresh1
22656d68f1eSafresh1  $obj->expand_verbatim_tabs(8);
22756d68f1eSafresh1  return $obj;
228b39c5158Smillert}
229b39c5158Smillert
230b39c5158Smillert
231b39c5158Smillert
232b39c5158Smillert# TODO: an option for whether to interpolate E<...>'s, or just resolve to codes.
233b39c5158Smillert
234b39c5158Smillert#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
235b39c5158Smillert
236b39c5158Smillertsub _handle_element_start {     # OVERRIDE IN DERIVED CLASS
237b39c5158Smillert  my($self, $element_name, $attr_hash_r) = @_;
238b39c5158Smillert  return;
239b39c5158Smillert}
240b39c5158Smillert
241b39c5158Smillertsub _handle_element_end {       # OVERRIDE IN DERIVED CLASS
242b39c5158Smillert  my($self, $element_name) = @_;
243b39c5158Smillert  return;
244b39c5158Smillert}
245b39c5158Smillert
246b39c5158Smillertsub _handle_text          {     # OVERRIDE IN DERIVED CLASS
247b39c5158Smillert  my($self, $text) = @_;
248b39c5158Smillert  return;
249b39c5158Smillert}
250b39c5158Smillert
251b39c5158Smillert#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
252b39c5158Smillert#
253b39c5158Smillert# And now directives (not targets)
254b39c5158Smillert
255b39c5158Smillertsub accept_directive_as_verbatim  { shift->_accept_directives('Verbatim', @_) }
256b39c5158Smillertsub accept_directive_as_data      { shift->_accept_directives('Data',     @_) }
257b39c5158Smillertsub accept_directive_as_processed { shift->_accept_directives('Plain',    @_) }
258b39c5158Smillert
259b39c5158Smillertsub _accept_directives {
260b39c5158Smillert  my($this, $type) = splice @_,0,2;
261b39c5158Smillert  foreach my $d (@_) {
262b39c5158Smillert    next unless defined $d and length $d;
263b39c5158Smillert    Carp::croak "\"$d\" isn't a valid directive name"
264b39c5158Smillert     unless $d =~ m/^[a-zA-Z][a-zA-Z0-9]*$/s;
265b39c5158Smillert    Carp::croak "\"$d\" is already a reserved Pod directive name"
266b39c5158Smillert     if exists $Known_directives{$d};
267b39c5158Smillert    $this->{'accept_directives'}{$d} = $type;
268b8851fccSafresh1    DEBUG > 2 and print STDERR "Learning to accept \"=$d\" as directive of type $type\n";
269b39c5158Smillert  }
270b8851fccSafresh1  DEBUG > 6 and print STDERR "$this\'s accept_directives : ",
271b39c5158Smillert   pretty($this->{'accept_directives'}), "\n";
272b39c5158Smillert
273b39c5158Smillert  return sort keys %{ $this->{'accept_directives'} } if wantarray;
274b39c5158Smillert  return;
275b39c5158Smillert}
276b39c5158Smillert
277b39c5158Smillert#--------------------------------------------------------------------------
278b39c5158Smillert# TODO: document these:
279b39c5158Smillert
280b39c5158Smillertsub unaccept_directive { shift->unaccept_directives(@_) };
281b39c5158Smillert
282b39c5158Smillertsub unaccept_directives {
283b39c5158Smillert  my $this = shift;
284b39c5158Smillert  foreach my $d (@_) {
285b39c5158Smillert    next unless defined $d and length $d;
286b39c5158Smillert    Carp::croak "\"$d\" isn't a valid directive name"
287b39c5158Smillert     unless $d =~ m/^[a-zA-Z][a-zA-Z0-9]*$/s;
288b39c5158Smillert    Carp::croak "But you must accept \"$d\" directives -- it's a builtin!"
289b39c5158Smillert     if exists $Known_directives{$d};
290b39c5158Smillert    delete $this->{'accept_directives'}{$d};
291b8851fccSafresh1    DEBUG > 2 and print STDERR "OK, won't accept \"=$d\" as directive.\n";
292b39c5158Smillert  }
293b39c5158Smillert  return sort keys %{ $this->{'accept_directives'} } if wantarray;
294b39c5158Smillert  return
295b39c5158Smillert}
296b39c5158Smillert
297b39c5158Smillert#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
298b39c5158Smillert#
299b39c5158Smillert# And now targets (not directives)
300b39c5158Smillert
301b39c5158Smillertsub accept_target         { shift->accept_targets(@_)         } # alias
302b39c5158Smillertsub accept_target_as_text { shift->accept_targets_as_text(@_) } # alias
303b39c5158Smillert
304b39c5158Smillert
305b39c5158Smillertsub accept_targets         { shift->_accept_targets('1', @_) }
306b39c5158Smillert
307b39c5158Smillertsub accept_targets_as_text { shift->_accept_targets('force_resolve', @_) }
308b39c5158Smillert # forces them to be processed, even when there's no ":".
309b39c5158Smillert
310b39c5158Smillertsub _accept_targets {
311b39c5158Smillert  my($this, $type) = splice @_,0,2;
312b39c5158Smillert  foreach my $t (@_) {
313b39c5158Smillert    next unless defined $t and length $t;
314b39c5158Smillert    # TODO: enforce some limitations on what a target name can be?
315b39c5158Smillert    $this->{'accept_targets'}{$t} = $type;
316b8851fccSafresh1    DEBUG > 2 and print STDERR "Learning to accept \"$t\" as target of type $type\n";
317b39c5158Smillert  }
318b39c5158Smillert  return sort keys %{ $this->{'accept_targets'} } if wantarray;
319b39c5158Smillert  return;
320b39c5158Smillert}
321b39c5158Smillert
322b39c5158Smillert#--------------------------------------------------------------------------
323b39c5158Smillertsub unaccept_target         { shift->unaccept_targets(@_) }
324b39c5158Smillert
325b39c5158Smillertsub unaccept_targets {
326b39c5158Smillert  my $this = shift;
327b39c5158Smillert  foreach my $t (@_) {
328b39c5158Smillert    next unless defined $t and length $t;
329b39c5158Smillert    # TODO: enforce some limitations on what a target name can be?
330b39c5158Smillert    delete $this->{'accept_targets'}{$t};
331b8851fccSafresh1    DEBUG > 2 and print STDERR "OK, won't accept \"$t\" as target.\n";
332b39c5158Smillert  }
333b39c5158Smillert  return sort keys %{ $this->{'accept_targets'} } if wantarray;
334b39c5158Smillert  return;
335b39c5158Smillert}
336b39c5158Smillert
337b39c5158Smillert#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
338b39c5158Smillert#
339b39c5158Smillert# And now codes (not targets or directives)
340b39c5158Smillert
341b8851fccSafresh1# XXX Probably it is an error that the digit '9' is excluded from these re's.
342b8851fccSafresh1# Broken for early Perls on EBCDIC
34356d68f1eSafresh1my $xml_name_re = my_qr('[^-.0-8:A-Z_a-z[:^ascii:]]', '9');
34456d68f1eSafresh1$xml_name_re = qr/[\x00-\x2C\x2F\x39\x3B-\x40\x5B-\x5E\x60\x7B-\x7F]/
34556d68f1eSafresh1                                                            unless $xml_name_re;
346b8851fccSafresh1
347b39c5158Smillertsub accept_code { shift->accept_codes(@_) } # alias
348b39c5158Smillert
349b39c5158Smillertsub accept_codes {  # Add some codes
350b39c5158Smillert  my $this = shift;
351b39c5158Smillert
352b39c5158Smillert  foreach my $new_code (@_) {
353b39c5158Smillert    next unless defined $new_code and length $new_code;
354b39c5158Smillert    # A good-enough check that it's good as an XML Name symbol:
355b39c5158Smillert    Carp::croak "\"$new_code\" isn't a valid element name"
356b8851fccSafresh1      if $new_code =~ $xml_name_re
357b39c5158Smillert          # Characters under 0x80 that aren't legal in an XML Name.
358b39c5158Smillert      or $new_code =~ m/^[-\.0-9]/s
359b39c5158Smillert      or $new_code =~ m/:[-\.0-9]/s;
360b39c5158Smillert          # The legal under-0x80 Name characters that
361b39c5158Smillert          #  an XML Name still can't start with.
362b39c5158Smillert
363b39c5158Smillert    $this->{'accept_codes'}{$new_code} = $new_code;
364b39c5158Smillert
365b39c5158Smillert    # Yes, map to itself -- just so that when we
366b39c5158Smillert    #  see "=extend W [whatever] thatelementname", we say that W maps
367b39c5158Smillert    #  to whatever $this->{accept_codes}{thatelementname} is,
368b39c5158Smillert    #  i.e., "thatelementname".  Then when we go re-mapping,
369b39c5158Smillert    #  a "W" in the treelet turns into "thatelementname".  We only
370b39c5158Smillert    #  remap once.
371b39c5158Smillert    # If we say we accept "W", then a "W" in the treelet simply turns
372b39c5158Smillert    #  into "W".
373b39c5158Smillert  }
374b39c5158Smillert
375b39c5158Smillert  return;
376b39c5158Smillert}
377b39c5158Smillert
378b39c5158Smillert#--------------------------------------------------------------------------
379b39c5158Smillertsub unaccept_code { shift->unaccept_codes(@_) }
380b39c5158Smillert
381b39c5158Smillertsub unaccept_codes { # remove some codes
382b39c5158Smillert  my $this = shift;
383b39c5158Smillert
384b39c5158Smillert  foreach my $new_code (@_) {
385b39c5158Smillert    next unless defined $new_code and length $new_code;
386b39c5158Smillert    # A good-enough check that it's good as an XML Name symbol:
387b39c5158Smillert    Carp::croak "\"$new_code\" isn't a valid element name"
388b8851fccSafresh1      if $new_code =~ $xml_name_re
389b39c5158Smillert          # Characters under 0x80 that aren't legal in an XML Name.
390b39c5158Smillert      or $new_code =~ m/^[-\.0-9]/s
391b39c5158Smillert      or $new_code =~ m/:[-\.0-9]/s;
392b39c5158Smillert          # The legal under-0x80 Name characters that
393b39c5158Smillert          #  an XML Name still can't start with.
394b39c5158Smillert
395b39c5158Smillert    Carp::croak "But you must accept \"$new_code\" codes -- it's a builtin!"
396b39c5158Smillert     if grep $new_code eq $_, @Known_formatting_codes;
397b39c5158Smillert
398b39c5158Smillert    delete $this->{'accept_codes'}{$new_code};
399b39c5158Smillert
400b8851fccSafresh1    DEBUG > 2 and print STDERR "OK, won't accept the code $new_code<...>.\n";
401b39c5158Smillert  }
402b39c5158Smillert
403b39c5158Smillert  return;
404b39c5158Smillert}
405b39c5158Smillert
406b39c5158Smillert
407b39c5158Smillert#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
408b39c5158Smillert#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
409b39c5158Smillert
410b39c5158Smillertsub parse_string_document {
411b39c5158Smillert  my $self = shift;
412b39c5158Smillert  my @lines;
413b39c5158Smillert  foreach my $line_group (@_) {
414b39c5158Smillert    next unless defined $line_group and length $line_group;
415b39c5158Smillert    pos($line_group) = 0;
416b39c5158Smillert    while($line_group =~
417898184e3Ssthen      m/([^\n\r]*)(\r?\n?)/g # supports \r, \n ,\r\n
418898184e3Ssthen      #m/([^\n\r]*)((?:\r?\n)?)/g
419b39c5158Smillert    ) {
420b39c5158Smillert      #print(">> $1\n"),
421b39c5158Smillert      $self->parse_lines($1)
422b39c5158Smillert       if length($1) or length($2)
423b39c5158Smillert        or pos($line_group) != length($line_group);
424b39c5158Smillert       # I.e., unless it's a zero-length "empty line" at the very
425b39c5158Smillert       #  end of "foo\nbar\n" (i.e., between the \n and the EOS).
426b39c5158Smillert    }
427b39c5158Smillert  }
428b39c5158Smillert  $self->parse_lines(undef); # to signal EOF
429b39c5158Smillert  return $self;
430b39c5158Smillert}
431b39c5158Smillert
432b39c5158Smillertsub _init_fh_source {
433b39c5158Smillert  my($self, $source) = @_;
434b39c5158Smillert
435b8851fccSafresh1  #DEBUG > 1 and print STDERR "Declaring $source as :raw for starters\n";
436b39c5158Smillert  #$self->_apply_binmode($source, ':raw');
437b39c5158Smillert  #binmode($source, ":raw");
438b39c5158Smillert
439b39c5158Smillert  return;
440b39c5158Smillert}
441b39c5158Smillert
442b39c5158Smillert#:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.
443b39c5158Smillert#
444b39c5158Smillert
445b39c5158Smillertsub parse_file {
446b39c5158Smillert  my($self, $source) = (@_);
447b39c5158Smillert
448b39c5158Smillert  if(!defined $source) {
449b39c5158Smillert    Carp::croak("Can't use empty-string as a source for parse_file");
450b39c5158Smillert  } elsif(ref(\$source) eq 'GLOB') {
451b39c5158Smillert    $self->{'source_filename'} = '' . ($source);
452b39c5158Smillert  } elsif(ref $source) {
453b39c5158Smillert    $self->{'source_filename'} = '' . ($source);
454b39c5158Smillert  } elsif(!length $source) {
455b39c5158Smillert    Carp::croak("Can't use empty-string as a source for parse_file");
456b39c5158Smillert  } else {
457b39c5158Smillert    {
458b39c5158Smillert      local *PODSOURCE;
459b39c5158Smillert      open(PODSOURCE, "<$source") || Carp::croak("Can't open $source: $!");
460b39c5158Smillert      $self->{'source_filename'} = $source;
461b39c5158Smillert      $source = *PODSOURCE{IO};
462b39c5158Smillert    }
463b39c5158Smillert    $self->_init_fh_source($source);
464b39c5158Smillert  }
465b39c5158Smillert  # By here, $source is a FH.
466b39c5158Smillert
467b39c5158Smillert  $self->{'source_fh'} = $source;
468b39c5158Smillert
469b39c5158Smillert  my($i, @lines);
470b39c5158Smillert  until( $self->{'source_dead'} ) {
471b39c5158Smillert    splice @lines;
472898184e3Ssthen
473b39c5158Smillert    for($i = MANY_LINES; $i--;) {  # read those many lines at a time
474b39c5158Smillert      local $/ = $NL;
475b39c5158Smillert      push @lines, scalar(<$source>);  # readline
476b39c5158Smillert      last unless defined $lines[-1];
477b39c5158Smillert       # but pass thru the undef, which will set source_dead to true
478b39c5158Smillert    }
479898184e3Ssthen
480898184e3Ssthen    my $at_eof = ! $lines[-1]; # keep track of the undef
481898184e3Ssthen    pop @lines if $at_eof; # silence warnings
482898184e3Ssthen
483898184e3Ssthen    # be eol agnostic
484898184e3Ssthen    s/\r\n?/\n/g for @lines;
485898184e3Ssthen
486898184e3Ssthen    # make sure there are only one line elements for parse_lines
487898184e3Ssthen    @lines = split(/(?<=\n)/, join('', @lines));
488898184e3Ssthen
489898184e3Ssthen    # push the undef back after popping it to set source_dead to true
490898184e3Ssthen    push @lines, undef if $at_eof;
491898184e3Ssthen
492b39c5158Smillert    $self->parse_lines(@lines);
493b39c5158Smillert  }
494b39c5158Smillert  delete($self->{'source_fh'}); # so it can be GC'd
495b39c5158Smillert  return $self;
496b39c5158Smillert}
497b39c5158Smillert
498b39c5158Smillert#:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.
499b39c5158Smillert
500b39c5158Smillertsub parse_from_file {
501b39c5158Smillert  # An emulation of Pod::Parser's interface, for the sake of Perldoc.
502b39c5158Smillert  # Basically just a wrapper around parse_file.
503b39c5158Smillert
504b39c5158Smillert  my($self, $source, $to) = @_;
505b39c5158Smillert  $self = $self->new unless ref($self); # so we tolerate being a class method
506b39c5158Smillert
507b39c5158Smillert  if(!defined $source)             { $source = *STDIN{IO}
508b39c5158Smillert  } elsif(ref(\$source) eq 'GLOB') { # stet
509b39c5158Smillert  } elsif(ref($source)           ) { # stet
510b39c5158Smillert  } elsif(!length $source
511b8851fccSafresh1     or $source eq '-' or $source =~ m/^<&(?:STDIN|0)$/i
512b39c5158Smillert  ) {
513b39c5158Smillert    $source = *STDIN{IO};
514b39c5158Smillert  }
515b39c5158Smillert
516b39c5158Smillert  if(!defined $to) {             $self->output_fh( *STDOUT{IO}   );
517b39c5158Smillert  } elsif(ref(\$to) eq 'GLOB') { $self->output_fh( $to );
518b39c5158Smillert  } elsif(ref($to)) {            $self->output_fh( $to );
519b39c5158Smillert  } elsif(!length $to
520b39c5158Smillert     or $to eq '-' or $to =~ m/^>&?(?:STDOUT|1)$/i
521b39c5158Smillert  ) {
522b39c5158Smillert    $self->output_fh( *STDOUT{IO} );
523b8851fccSafresh1  } elsif($to =~ m/^>&(?:STDERR|2)$/i) {
524b8851fccSafresh1    $self->output_fh( *STDERR{IO} );
525b39c5158Smillert  } else {
526b39c5158Smillert    require Symbol;
527b39c5158Smillert    my $out_fh = Symbol::gensym();
528b8851fccSafresh1    DEBUG and print STDERR "Write-opening to $to\n";
529b39c5158Smillert    open($out_fh, ">$to")  or  Carp::croak "Can't write-open $to: $!";
530b39c5158Smillert    binmode($out_fh)
531b39c5158Smillert     if $self->can('write_with_binmode') and $self->write_with_binmode;
532b39c5158Smillert    $self->output_fh($out_fh);
533b39c5158Smillert  }
534b39c5158Smillert
535b39c5158Smillert  return $self->parse_file($source);
536b39c5158Smillert}
537b39c5158Smillert
538b39c5158Smillert#-----------------------------------------------------------------------------
539b39c5158Smillert
540b39c5158Smillertsub whine {
541b39c5158Smillert  #my($self,$line,$complaint) = @_;
542b39c5158Smillert  my $self = shift(@_);
543b39c5158Smillert  ++$self->{'errors_seen'};
544b39c5158Smillert  if($self->{'no_whining'}) {
545b8851fccSafresh1    DEBUG > 9 and print STDERR "Discarding complaint (at line $_[0]) $_[1]\n because no_whining is on.\n";
546b39c5158Smillert    return;
547b39c5158Smillert  }
548b8851fccSafresh1  push @{$self->{'all_errata'}{$_[0]}}, $_[1];
549b39c5158Smillert  return $self->_complain_warn(@_) if $self->{'complain_stderr'};
550b39c5158Smillert  return $self->_complain_errata(@_);
551b39c5158Smillert}
552b39c5158Smillert
553898184e3Ssthensub scream {    # like whine, but not suppressible
554b39c5158Smillert  #my($self,$line,$complaint) = @_;
555b39c5158Smillert  my $self = shift(@_);
556b39c5158Smillert  ++$self->{'errors_seen'};
557b8851fccSafresh1  push @{$self->{'all_errata'}{$_[0]}}, $_[1];
558b39c5158Smillert  return $self->_complain_warn(@_) if $self->{'complain_stderr'};
559b39c5158Smillert  return $self->_complain_errata(@_);
560b39c5158Smillert}
561b39c5158Smillert
562b39c5158Smillertsub _complain_warn {
563b39c5158Smillert  my($self,$line,$complaint) = @_;
564b39c5158Smillert  return printf STDERR "%s around line %s: %s\n",
565b39c5158Smillert    $self->{'source_filename'} || 'Pod input', $line, $complaint;
566b39c5158Smillert}
567b39c5158Smillert
568b39c5158Smillertsub _complain_errata {
569b39c5158Smillert  my($self,$line,$complaint) = @_;
570b39c5158Smillert  if( $self->{'no_errata_section'} ) {
571b8851fccSafresh1    DEBUG > 9 and print STDERR "Discarding erratum (at line $line) $complaint\n because no_errata_section is on.\n";
572b39c5158Smillert  } else {
573b8851fccSafresh1    DEBUG > 9 and print STDERR "Queuing erratum (at line $line) $complaint\n";
574b39c5158Smillert    push @{$self->{'errata'}{$line}}, $complaint
575b39c5158Smillert      # for a report to be generated later!
576b39c5158Smillert  }
577b39c5158Smillert  return 1;
578b39c5158Smillert}
579b39c5158Smillert
580b39c5158Smillert#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
581b39c5158Smillert
582b39c5158Smillertsub _get_initial_item_type {
583b39c5158Smillert  # A hack-wrapper here for when you have like "=over\n\n=item 456\n\n"
584b39c5158Smillert  my($self, $para) = @_;
585b39c5158Smillert  return $para->[1]{'~type'}  if $para->[1]{'~type'};
586b39c5158Smillert
587b39c5158Smillert  return $para->[1]{'~type'} = 'text'
588b39c5158Smillert   if join("\n", @{$para}[2 .. $#$para]) =~ m/^\s*(\d+)\.?\s*$/s and $1 ne '1';
589b39c5158Smillert  # Else fall thru to the general case:
590b39c5158Smillert  return $self->_get_item_type($para);
591b39c5158Smillert}
592b39c5158Smillert
593b39c5158Smillert
594b39c5158Smillert
595b39c5158Smillertsub _get_item_type {       # mutates the item!!
596b39c5158Smillert  my($self, $para) = @_;
597b39c5158Smillert  return $para->[1]{'~type'} if $para->[1]{'~type'};
598b39c5158Smillert
599b39c5158Smillert
600b39c5158Smillert  # Otherwise we haven't yet been to this node.  Maybe alter it...
601b39c5158Smillert
602b39c5158Smillert  my $content = join "\n", @{$para}[2 .. $#$para];
603b39c5158Smillert
604b39c5158Smillert  if($content =~ m/^\s*\*\s*$/s or $content =~ m/^\s*$/s) {
605b39c5158Smillert    # Like: "=item *", "=item   *   ", "=item"
606b39c5158Smillert    splice @$para, 2; # so it ends up just being ['=item', { attrhash } ]
607b39c5158Smillert    $para->[1]{'~orig_content'} = $content;
608b39c5158Smillert    return $para->[1]{'~type'} = 'bullet';
609b39c5158Smillert
610b39c5158Smillert  } elsif($content =~ m/^\s*\*\s+(.+)/s) {  # tolerance
611b39c5158Smillert
612b39c5158Smillert    # Like: "=item * Foo bar baz";
613b39c5158Smillert    $para->[1]{'~orig_content'}      = $content;
614b39c5158Smillert    $para->[1]{'~_freaky_para_hack'} = $1;
615b8851fccSafresh1    DEBUG > 2 and print STDERR " Tolerating $$para[2] as =item *\\n\\n$1\n";
616b39c5158Smillert    splice @$para, 2; # so it ends up just being ['=item', { attrhash } ]
617b39c5158Smillert    return $para->[1]{'~type'} = 'bullet';
618b39c5158Smillert
619b39c5158Smillert  } elsif($content =~ m/^\s*(\d+)\.?\s*$/s) {
620b39c5158Smillert    # Like: "=item 1.", "=item    123412"
621b39c5158Smillert
622b39c5158Smillert    $para->[1]{'~orig_content'} = $content;
623b39c5158Smillert    $para->[1]{'number'} = $1;  # Yes, stores the number there!
624b39c5158Smillert
625b39c5158Smillert    splice @$para, 2; # so it ends up just being ['=item', { attrhash } ]
626b39c5158Smillert    return $para->[1]{'~type'} = 'number';
627b39c5158Smillert
628b39c5158Smillert  } else {
629b39c5158Smillert    # It's anything else.
630b39c5158Smillert    return $para->[1]{'~type'} = 'text';
631b39c5158Smillert
632b39c5158Smillert  }
633b39c5158Smillert}
634b39c5158Smillert
635b39c5158Smillert#-----------------------------------------------------------------------------
636b39c5158Smillert
637b39c5158Smillertsub _make_treelet {
638b39c5158Smillert  my $self = shift;  # and ($para, $start_line)
639b39c5158Smillert  my $treelet;
640b39c5158Smillert  if(!@_) {
641b39c5158Smillert    return [''];
642b39c5158Smillert  } if(ref $_[0] and ref $_[0][0] and $_[0][0][0] eq '~Top') {
643b39c5158Smillert    # Hack so we can pass in fake-o pre-cooked paragraphs:
644b39c5158Smillert    #  just have the first line be a reference to a ['~Top', {}, ...]
645b39c5158Smillert    # We use this feechure in gen_errata and stuff.
646b39c5158Smillert
647b8851fccSafresh1    DEBUG and print STDERR "Applying precooked treelet hack to $_[0][0]\n";
648b39c5158Smillert    $treelet = $_[0][0];
649b39c5158Smillert    splice @$treelet, 0, 2;  # lop the top off
650b39c5158Smillert    return $treelet;
651b39c5158Smillert  } else {
652b39c5158Smillert    $treelet = $self->_treelet_from_formatting_codes(@_);
653b39c5158Smillert  }
654b39c5158Smillert
65556d68f1eSafresh1  if( ! $self->{'_output_is_for_JustPod'}   # Retain these as-is for pod output
65656d68f1eSafresh1     && $self->_remap_sequences($treelet) )
65756d68f1eSafresh1  {
658b39c5158Smillert    $self->_treat_Zs($treelet);  # Might as well nix these first
659b39c5158Smillert    $self->_treat_Ls($treelet);  # L has to precede E and S
660b39c5158Smillert    $self->_treat_Es($treelet);
661b39c5158Smillert    $self->_treat_Ss($treelet);  # S has to come after E
662b39c5158Smillert    $self->_wrap_up($treelet); # Nix X's and merge texties
663b39c5158Smillert
664b39c5158Smillert  } else {
665b8851fccSafresh1    DEBUG and print STDERR "Formatless treelet gets fast-tracked.\n";
666b39c5158Smillert     # Very common case!
667b39c5158Smillert  }
668b39c5158Smillert
669b39c5158Smillert  splice @$treelet, 0, 2;  # lop the top off
670b39c5158Smillert
671b39c5158Smillert  return $treelet;
672b39c5158Smillert}
673b39c5158Smillert
674b39c5158Smillert#:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.
675b39c5158Smillert
676b39c5158Smillertsub _wrap_up {
677b39c5158Smillert  my($self, @stack) = @_;
678b39c5158Smillert  my $nixx  = $self->{'nix_X_codes'};
679b39c5158Smillert  my $merge = $self->{'merge_text' };
680b39c5158Smillert  return unless $nixx or $merge;
681b39c5158Smillert
682b8851fccSafresh1  DEBUG > 2 and print STDERR "\nStarting _wrap_up traversal.\n",
683b39c5158Smillert   $merge ? (" Merge mode on\n") : (),
684b39c5158Smillert   $nixx  ? (" Nix-X mode on\n") : (),
685b39c5158Smillert  ;
686b39c5158Smillert
687b39c5158Smillert
688b39c5158Smillert  my($i, $treelet);
689b39c5158Smillert  while($treelet = shift @stack) {
690b8851fccSafresh1    DEBUG > 3 and print STDERR " Considering children of this $treelet->[0] node...\n";
691b39c5158Smillert    for($i = 2; $i < @$treelet; ++$i) { # iterate over children
692b8851fccSafresh1      DEBUG > 3 and print STDERR " Considering child at $i ", pretty($treelet->[$i]), "\n";
693b39c5158Smillert      if($nixx and ref $treelet->[$i] and $treelet->[$i][0] eq 'X') {
694b8851fccSafresh1        DEBUG > 3 and print STDERR "   Nixing X node at $i\n";
695b39c5158Smillert        splice(@$treelet, $i, 1); # just nix this node (and its descendants)
696b39c5158Smillert        # no need to back-update the counter just yet
697b39c5158Smillert        redo;
698b39c5158Smillert
699b39c5158Smillert      } elsif($merge and $i != 2 and  # non-initial
700b39c5158Smillert         !ref $treelet->[$i] and !ref $treelet->[$i - 1]
701b39c5158Smillert      ) {
702b8851fccSafresh1        DEBUG > 3 and print STDERR "   Merging ", $i-1,
703b39c5158Smillert         ":[$treelet->[$i-1]] and $i\:[$treelet->[$i]]\n";
704b39c5158Smillert        $treelet->[$i-1] .= ( splice(@$treelet, $i, 1) )[0];
705b8851fccSafresh1        DEBUG > 4 and print STDERR "    Now: ", $i-1, ":[$treelet->[$i-1]]\n";
706b39c5158Smillert        --$i;
707b39c5158Smillert        next;
708b39c5158Smillert        # since we just pulled the possibly last node out from under
709b39c5158Smillert        #  ourselves, we can't just redo()
710b39c5158Smillert
711b39c5158Smillert      } elsif( ref $treelet->[$i] ) {
712b8851fccSafresh1        DEBUG > 4 and print STDERR "  Enqueuing ", pretty($treelet->[$i]), " for traversal.\n";
713b39c5158Smillert        push @stack, $treelet->[$i];
714b39c5158Smillert
715b39c5158Smillert        if($treelet->[$i][0] eq 'L') {
716b39c5158Smillert          my $thing;
717b39c5158Smillert          foreach my $attrname ('section', 'to') {
718b39c5158Smillert            if(defined($thing = $treelet->[$i][1]{$attrname}) and ref $thing) {
719b39c5158Smillert              unshift @stack, $thing;
720b8851fccSafresh1              DEBUG > 4 and print STDERR "  +Enqueuing ",
721b39c5158Smillert               pretty( $treelet->[$i][1]{$attrname} ),
722b39c5158Smillert               " as an attribute value to tweak.\n";
723b39c5158Smillert            }
724b39c5158Smillert          }
725b39c5158Smillert        }
726b39c5158Smillert      }
727b39c5158Smillert    }
728b39c5158Smillert  }
729b8851fccSafresh1  DEBUG > 2 and print STDERR "End of _wrap_up traversal.\n\n";
730b39c5158Smillert
731b39c5158Smillert  return;
732b39c5158Smillert}
733b39c5158Smillert
734b39c5158Smillert#:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.
735b39c5158Smillert
736b39c5158Smillertsub _remap_sequences {
737b39c5158Smillert  my($self,@stack) = @_;
738b39c5158Smillert
739b39c5158Smillert  if(@stack == 1 and @{ $stack[0] } == 3 and !ref $stack[0][2]) {
740b39c5158Smillert    # VERY common case: abort it.
741b8851fccSafresh1    DEBUG and print STDERR "Skipping _remap_sequences: formatless treelet.\n";
742b39c5158Smillert    return 0;
743b39c5158Smillert  }
744b39c5158Smillert
745b39c5158Smillert  my $map = ($self->{'accept_codes'} || die "NO accept_codes in $self?!?");
746b39c5158Smillert
747b39c5158Smillert  my $start_line = $stack[0][1]{'start_line'};
748b39c5158Smillert  DEBUG > 2 and printf
749b39c5158Smillert   "\nAbout to start _remap_sequences on treelet from line %s.\n",
750b39c5158Smillert   $start_line || '[?]'
751b39c5158Smillert  ;
752b8851fccSafresh1  DEBUG > 3 and print STDERR " Map: ",
753b39c5158Smillert    join('; ', map "$_=" . (
754b39c5158Smillert        ref($map->{$_}) ? join(",", @{$map->{$_}}) : $map->{$_}
755b39c5158Smillert      ),
756b39c5158Smillert      sort keys %$map ),
757b39c5158Smillert    ("B~C~E~F~I~L~S~X~Z" eq join '~', sort keys %$map)
758b39c5158Smillert     ? "  (all normal)\n" : "\n"
759b39c5158Smillert  ;
760b39c5158Smillert
761b39c5158Smillert  # A recursive algorithm implemented iteratively!  Whee!
762b39c5158Smillert
763b39c5158Smillert  my($is, $was, $i, $treelet); # scratch
764b39c5158Smillert  while($treelet = shift @stack) {
765b8851fccSafresh1    DEBUG > 3 and print STDERR " Considering children of this $treelet->[0] node...\n";
766b39c5158Smillert    for($i = 2; $i < @$treelet; ++$i) { # iterate over children
767b39c5158Smillert      next unless ref $treelet->[$i];  # text nodes are uninteresting
768b39c5158Smillert
769b8851fccSafresh1      DEBUG > 4 and print STDERR "  Noting child $i : $treelet->[$i][0]<...>\n";
770b39c5158Smillert
771b39c5158Smillert      $is = $treelet->[$i][0] = $map->{ $was = $treelet->[$i][0] };
772b39c5158Smillert      if( DEBUG > 3 ) {
773b39c5158Smillert        if(!defined $is) {
774b8851fccSafresh1          print STDERR "   Code $was<> is UNKNOWN!\n";
775b39c5158Smillert        } elsif($is eq $was) {
776b8851fccSafresh1          DEBUG > 4 and print STDERR "   Code $was<> stays the same.\n";
777b39c5158Smillert        } else  {
778b8851fccSafresh1          print STDERR "   Code $was<> maps to ",
779b39c5158Smillert           ref($is)
780b39c5158Smillert            ? ( "tags ", map("$_<", @$is), '...', map('>', @$is), "\n" )
781b39c5158Smillert            : "tag $is<...>.\n";
782b39c5158Smillert        }
783b39c5158Smillert      }
784b39c5158Smillert
785b39c5158Smillert      if(!defined $is) {
786b39c5158Smillert        $self->whine($start_line, "Deleting unknown formatting code $was<>");
787b39c5158Smillert        $is = $treelet->[$i][0] = '1';  # But saving the children!
788b39c5158Smillert        # I could also insert a leading "$was<" and tailing ">" as
789b39c5158Smillert        # children of this node, but something about that seems icky.
790b39c5158Smillert      }
791b39c5158Smillert      if(ref $is) {
792b39c5158Smillert        my @dynasty = @$is;
793b8851fccSafresh1        DEBUG > 4 and print STDERR "    Renaming $was node to $dynasty[-1]\n";
794b39c5158Smillert        $treelet->[$i][0] = pop @dynasty;
795b39c5158Smillert        my $nugget;
796b39c5158Smillert        while(@dynasty) {
797b39c5158Smillert          DEBUG > 4 and printf
798b39c5158Smillert           "    Grafting a new %s node between %s and %s\n",
799b39c5158Smillert           $dynasty[-1], $treelet->[0], $treelet->[$i][0],
800b39c5158Smillert          ;
801b39c5158Smillert
802b39c5158Smillert          #$nugget = ;
803b39c5158Smillert          splice @$treelet, $i, 1, [pop(@dynasty), {}, $treelet->[$i]];
804b39c5158Smillert            # relace node with a new parent
805b39c5158Smillert        }
806b39c5158Smillert      } elsif($is eq '0') {
807b39c5158Smillert        splice(@$treelet, $i, 1); # just nix this node (and its descendants)
808b39c5158Smillert        --$i;  # back-update the counter
809b39c5158Smillert      } elsif($is eq '1') {
810b39c5158Smillert        splice(@$treelet, $i, 1 # replace this node with its children!
811b39c5158Smillert          => splice @{ $treelet->[$i] },2
812b39c5158Smillert              # (not catching its first two (non-child) items)
813b39c5158Smillert        );
814b39c5158Smillert        --$i;  # back up for new stuff
815b39c5158Smillert      } else {
816b39c5158Smillert        # otherwise it's unremarkable
817b39c5158Smillert        unshift @stack, $treelet->[$i];  # just recurse
818b39c5158Smillert      }
819b39c5158Smillert    }
820b39c5158Smillert  }
821b39c5158Smillert
822b8851fccSafresh1  DEBUG > 2 and print STDERR "End of _remap_sequences traversal.\n\n";
823b39c5158Smillert
824b39c5158Smillert  if(@_ == 2 and @{ $_[1] } == 3 and !ref $_[1][2]) {
825b8851fccSafresh1    DEBUG and print STDERR "Noting that the treelet is now formatless.\n";
826b39c5158Smillert    return 0;
827b39c5158Smillert  }
828b39c5158Smillert  return 1;
829b39c5158Smillert}
830b39c5158Smillert
831b39c5158Smillert# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
832b39c5158Smillert
833b39c5158Smillertsub _ponder_extend {
834b39c5158Smillert
835b39c5158Smillert  # "Go to an extreme, move back to a more comfortable place"
836b39c5158Smillert  #  -- /Oblique Strategies/,  Brian Eno and Peter Schmidt
837b39c5158Smillert
838b39c5158Smillert  my($self, $para) = @_;
839b39c5158Smillert  my $content = join ' ', splice @$para, 2;
840b39c5158Smillert  $content =~ s/^\s+//s;
841b39c5158Smillert  $content =~ s/\s+$//s;
842b39c5158Smillert
843b8851fccSafresh1  DEBUG > 2 and print STDERR "Ogling extensor: =extend $content\n";
844b39c5158Smillert
845b39c5158Smillert  if($content =~
846b39c5158Smillert    m/^
847b39c5158Smillert      (\S+)         # 1 : new item
848b39c5158Smillert      \s+
849b39c5158Smillert      (\S+)         # 2 : fallback(s)
850b39c5158Smillert      (?:\s+(\S+))? # 3 : element name(s)
851b39c5158Smillert      \s*
852b39c5158Smillert      $
853b39c5158Smillert    /xs
854b39c5158Smillert  ) {
855b39c5158Smillert    my $new_letter = $1;
856b39c5158Smillert    my $fallbacks_one = $2;
857b39c5158Smillert    my $elements_one;
858b39c5158Smillert    $elements_one = defined($3) ? $3 : $1;
859b39c5158Smillert
860b8851fccSafresh1    DEBUG > 2 and print STDERR "Extensor has good syntax.\n";
861b39c5158Smillert
862b39c5158Smillert    unless($new_letter =~ m/^[A-Z]$/s or $new_letter) {
863b8851fccSafresh1      DEBUG > 2 and print STDERR " $new_letter isn't a valid thing to entend.\n";
864b39c5158Smillert      $self->whine(
865b39c5158Smillert        $para->[1]{'start_line'},
866b39c5158Smillert        "You can extend only formatting codes A-Z, not like \"$new_letter\""
867b39c5158Smillert      );
868b39c5158Smillert      return;
869b39c5158Smillert    }
870b39c5158Smillert
871b39c5158Smillert    if(grep $new_letter eq $_, @Known_formatting_codes) {
872b8851fccSafresh1      DEBUG > 2 and print STDERR " $new_letter isn't a good thing to extend, because known.\n";
873b39c5158Smillert      $self->whine(
874b39c5158Smillert        $para->[1]{'start_line'},
875b39c5158Smillert        "You can't extend an established code like \"$new_letter\""
876b39c5158Smillert      );
877b39c5158Smillert
878b39c5158Smillert      #TODO: or allow if last bit is same?
879b39c5158Smillert
880b39c5158Smillert      return;
881b39c5158Smillert    }
882b39c5158Smillert
883b39c5158Smillert    unless($fallbacks_one =~ m/^[A-Z](,[A-Z])*$/s  # like "B", "M,I", etc.
884b39c5158Smillert      or $fallbacks_one eq '0' or $fallbacks_one eq '1'
885b39c5158Smillert    ) {
886b39c5158Smillert      $self->whine(
887b39c5158Smillert        $para->[1]{'start_line'},
888b39c5158Smillert        "Format for second =extend parameter must be like"
889b39c5158Smillert        . " M or 1 or 0 or M,N or M,N,O but you have it like "
890b39c5158Smillert        . $fallbacks_one
891b39c5158Smillert      );
892b39c5158Smillert      return;
893b39c5158Smillert    }
894b39c5158Smillert
895b39c5158Smillert    unless($elements_one =~ m/^[^ ,]+(,[^ ,]+)*$/s) { # like "B", "M,I", etc.
896b39c5158Smillert      $self->whine(
897b39c5158Smillert        $para->[1]{'start_line'},
898b39c5158Smillert        "Format for third =extend parameter: like foo or bar,Baz,qu:ux but not like "
899b39c5158Smillert        . $elements_one
900b39c5158Smillert      );
901b39c5158Smillert      return;
902b39c5158Smillert    }
903b39c5158Smillert
904b39c5158Smillert    my @fallbacks  = split ',', $fallbacks_one,  -1;
905b39c5158Smillert    my @elements   = split ',', $elements_one, -1;
906b39c5158Smillert
907b39c5158Smillert    foreach my $f (@fallbacks) {
908b39c5158Smillert      next if exists $Known_formatting_codes{$f} or $f eq '0' or $f eq '1';
909b8851fccSafresh1      DEBUG > 2 and print STDERR "  Can't fall back on unknown code $f\n";
910b39c5158Smillert      $self->whine(
911b39c5158Smillert        $para->[1]{'start_line'},
912b39c5158Smillert        "Can't use unknown formatting code '$f' as a fallback for '$new_letter'"
913b39c5158Smillert      );
914b39c5158Smillert      return;
915b39c5158Smillert    }
916b39c5158Smillert
917b8851fccSafresh1    DEBUG > 3 and printf STDERR "Extensor: Fallbacks <%s> Elements <%s>.\n",
918b39c5158Smillert     @fallbacks, @elements;
919b39c5158Smillert
920b39c5158Smillert    my $canonical_form;
921b39c5158Smillert    foreach my $e (@elements) {
922b39c5158Smillert      if(exists $self->{'accept_codes'}{$e}) {
923b8851fccSafresh1        DEBUG > 1 and print STDERR " Mapping '$new_letter' to known extension '$e'\n";
924b39c5158Smillert        $canonical_form = $e;
925b39c5158Smillert        last; # first acceptable elementname wins!
926b39c5158Smillert      } else {
927b8851fccSafresh1        DEBUG > 1 and print STDERR " Can't map '$new_letter' to unknown extension '$e'\n";
928b39c5158Smillert      }
929b39c5158Smillert    }
930b39c5158Smillert
931b39c5158Smillert
932b39c5158Smillert    if( defined $canonical_form ) {
933b39c5158Smillert      # We found a good N => elementname mapping
934b39c5158Smillert      $self->{'accept_codes'}{$new_letter} = $canonical_form;
935b39c5158Smillert      DEBUG > 2 and print
936b39c5158Smillert       "Extensor maps $new_letter => known element $canonical_form.\n";
937b39c5158Smillert    } else {
938b39c5158Smillert      # We have to use the fallback(s), which might be '0', or '1'.
939b39c5158Smillert      $self->{'accept_codes'}{$new_letter}
940b39c5158Smillert        = (@fallbacks == 1) ? $fallbacks[0] : \@fallbacks;
941b39c5158Smillert      DEBUG > 2 and print
942b39c5158Smillert       "Extensor maps $new_letter => fallbacks @fallbacks.\n";
943b39c5158Smillert    }
944b39c5158Smillert
945b39c5158Smillert  } else {
946b8851fccSafresh1    DEBUG > 2 and print STDERR "Extensor has bad syntax.\n";
947b39c5158Smillert    $self->whine(
948b39c5158Smillert      $para->[1]{'start_line'},
949b39c5158Smillert      "Unknown =extend syntax: $content"
950b39c5158Smillert    )
951b39c5158Smillert  }
952b39c5158Smillert  return;
953b39c5158Smillert}
954b39c5158Smillert
955b39c5158Smillert
956b39c5158Smillert#:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.
957b39c5158Smillert
958b39c5158Smillertsub _treat_Zs {  # Nix Z<...>'s
959b39c5158Smillert  my($self,@stack) = @_;
960b39c5158Smillert
961b39c5158Smillert  my($i, $treelet);
962b39c5158Smillert  my $start_line = $stack[0][1]{'start_line'};
963b39c5158Smillert
964b39c5158Smillert  # A recursive algorithm implemented iteratively!  Whee!
965b39c5158Smillert
966b39c5158Smillert  while($treelet = shift @stack) {
967b39c5158Smillert    for($i = 2; $i < @$treelet; ++$i) { # iterate over children
968b39c5158Smillert      next unless ref $treelet->[$i];  # text nodes are uninteresting
969b39c5158Smillert      unless($treelet->[$i][0] eq 'Z') {
970b39c5158Smillert        unshift @stack, $treelet->[$i]; # recurse
971b39c5158Smillert        next;
972b39c5158Smillert      }
973b39c5158Smillert
974b8851fccSafresh1      DEBUG > 1 and print STDERR "Nixing Z node @{$treelet->[$i]}\n";
975b39c5158Smillert
976b39c5158Smillert      # bitch UNLESS it's empty
977b39c5158Smillert      unless(  @{$treelet->[$i]} == 2
978b39c5158Smillert           or (@{$treelet->[$i]} == 3 and $treelet->[$i][2] eq '')
979b39c5158Smillert      ) {
980b39c5158Smillert        $self->whine( $start_line, "A non-empty Z<>" );
981b39c5158Smillert      }      # but kill it anyway
982b39c5158Smillert
983b39c5158Smillert      splice(@$treelet, $i, 1); # thereby just nix this node.
984b39c5158Smillert      --$i;
985b39c5158Smillert
986b39c5158Smillert    }
987b39c5158Smillert  }
988b39c5158Smillert
989b39c5158Smillert  return;
990b39c5158Smillert}
991b39c5158Smillert
992b39c5158Smillert# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
993b39c5158Smillert
994b39c5158Smillert# Quoting perlpodspec:
995b39c5158Smillert
996b39c5158Smillert# In parsing an L<...> code, Pod parsers must distinguish at least four
997b39c5158Smillert# attributes:
998b39c5158Smillert
999b39c5158Smillert############# Not used.  Expressed via the element children plus
1000b39c5158Smillert#############  the value of the "content-implicit" flag.
1001b39c5158Smillert# First:
1002b39c5158Smillert# The link-text. If there is none, this must be undef. (E.g., in "L<Perl
1003b39c5158Smillert# Functions|perlfunc>", the link-text is "Perl Functions". In
1004b39c5158Smillert# "L<Time::HiRes>" and even "L<|Time::HiRes>", there is no link text. Note
1005b39c5158Smillert# that link text may contain formatting.)
1006b39c5158Smillert#
1007b39c5158Smillert
1008b39c5158Smillert############# The element children
1009b39c5158Smillert# Second:
1010b39c5158Smillert# The possibly inferred link-text -- i.e., if there was no real link text,
1011b39c5158Smillert# then this is the text that we'll infer in its place. (E.g., for
1012b39c5158Smillert# "L<Getopt::Std>", the inferred link text is "Getopt::Std".)
1013b39c5158Smillert#
1014b39c5158Smillert
1015b39c5158Smillert############# The "to" attribute (which might be text, or a treelet)
1016b39c5158Smillert# Third:
1017b39c5158Smillert# The name or URL, or undef if none. (E.g., in "L<Perl
1018b39c5158Smillert# Functions|perlfunc>", the name -- also sometimes called the page -- is
1019b39c5158Smillert# "perlfunc". In "L</CAVEATS>", the name is undef.)
1020b39c5158Smillert#
1021b39c5158Smillert
1022b39c5158Smillert############# The "section" attribute (which might be next, or a treelet)
1023b39c5158Smillert# Fourth:
1024b39c5158Smillert# The section (AKA "item" in older perlpods), or undef if none. E.g., in
1025b39c5158Smillert# Getopt::Std/DESCRIPTION, "DESCRIPTION" is the section. (Note that this
1026b39c5158Smillert# is not the same as a manpage section like the "5" in "man 5 crontab".
1027b39c5158Smillert# "Section Foo" in the Pod sense means the part of the text that's
1028b39c5158Smillert# introduced by the heading or item whose text is "Foo".)
1029b39c5158Smillert#
1030b39c5158Smillert# Pod parsers may also note additional attributes including:
1031b39c5158Smillert#
1032b39c5158Smillert
1033b39c5158Smillert############# The "type" attribute.
1034b39c5158Smillert# Fifth:
1035b39c5158Smillert# A flag for whether item 3 (if present) is a URL (like
1036b39c5158Smillert# "http://lists.perl.org" is), in which case there should be no section
1037b39c5158Smillert# attribute; a Pod name (like "perldoc" and "Getopt::Std" are); or
1038b39c5158Smillert# possibly a man page name (like "crontab(5)" is).
1039b39c5158Smillert#
1040b39c5158Smillert
1041898184e3Ssthen############# The "raw" attribute that is already there.
1042b39c5158Smillert# Sixth:
1043b39c5158Smillert# The raw original L<...> content, before text is split on "|", "/", etc,
1044b39c5158Smillert# and before E<...> codes are expanded.
1045b39c5158Smillert
1046b39c5158Smillert
1047b39c5158Smillert# For L<...> codes without a "name|" part, only E<...> and Z<> codes may
1048b39c5158Smillert# occur -- no other formatting codes. That is, authors should not use
1049b39c5158Smillert# "L<B<Foo::Bar>>".
1050b39c5158Smillert#
1051b39c5158Smillert# Note, however, that formatting codes and Z<>'s can occur in any and all
1052b39c5158Smillert# parts of an L<...> (i.e., in name, section, text, and url).
1053b39c5158Smillert
1054b39c5158Smillertsub _treat_Ls {  # Process our dear dear friends, the L<...> sequences
1055b39c5158Smillert
1056b39c5158Smillert  # L<name>
1057b39c5158Smillert  # L<name/"sec"> or L<name/sec>
1058b39c5158Smillert  # L</"sec"> or L</sec> or L<"sec">
1059b39c5158Smillert  # L<text|name>
1060b39c5158Smillert  # L<text|name/"sec"> or L<text|name/sec>
1061b39c5158Smillert  # L<text|/"sec"> or L<text|/sec> or L<text|"sec">
1062b39c5158Smillert  # L<scheme:...>
1063b39c5158Smillert  # L<text|scheme:...>
1064b39c5158Smillert
1065b39c5158Smillert  my($self,@stack) = @_;
1066b39c5158Smillert
1067b39c5158Smillert  my($i, $treelet);
1068b39c5158Smillert  my $start_line = $stack[0][1]{'start_line'};
1069b39c5158Smillert
1070b39c5158Smillert  # A recursive algorithm implemented iteratively!  Whee!
1071b39c5158Smillert
1072b39c5158Smillert  while($treelet = shift @stack) {
1073b39c5158Smillert    for(my $i = 2; $i < @$treelet; ++$i) {
1074b39c5158Smillert      # iterate over children of current tree node
1075b39c5158Smillert      next unless ref $treelet->[$i];  # text nodes are uninteresting
1076b39c5158Smillert      unless($treelet->[$i][0] eq 'L') {
1077b39c5158Smillert        unshift @stack, $treelet->[$i]; # recurse
1078b39c5158Smillert        next;
1079b39c5158Smillert      }
1080b39c5158Smillert
1081b39c5158Smillert
1082b39c5158Smillert      # By here, $treelet->[$i] is definitely an L node
1083b39c5158Smillert      my $ell = $treelet->[$i];
108456d68f1eSafresh1      DEBUG > 1 and print STDERR "Ogling L node " . pretty($ell) . "\n";
1085b39c5158Smillert
108656d68f1eSafresh1      # bitch if it's empty or is just '/'
108756d68f1eSafresh1      if (@{$ell} == 3 and $ell->[2] =~ m!\A\s*/\s*\z!) {
108856d68f1eSafresh1        $self->whine( $start_line, "L<> contains only '/'" );
108956d68f1eSafresh1        $treelet->[$i] = 'L</>';  # just make it a text node
109056d68f1eSafresh1        next;  # and move on
109156d68f1eSafresh1      }
1092b39c5158Smillert      if(  @{$ell} == 2
1093b39c5158Smillert       or (@{$ell} == 3 and $ell->[2] eq '')
1094b39c5158Smillert      ) {
1095b39c5158Smillert        $self->whine( $start_line, "An empty L<>" );
1096b39c5158Smillert        $treelet->[$i] = 'L<>';  # just make it a text node
1097b39c5158Smillert        next;  # and move on
1098b39c5158Smillert      }
1099b39c5158Smillert
110091f110e0Safresh1      if( (! ref $ell->[2]  && $ell->[2] =~ /\A\s/)
110191f110e0Safresh1        ||(! ref $ell->[-1] && $ell->[-1] =~ /\s\z/)
110291f110e0Safresh1      ) {
110391f110e0Safresh1        $self->whine( $start_line, "L<> starts or ends with whitespace" );
110491f110e0Safresh1      }
110591f110e0Safresh1
1106b39c5158Smillert      # Catch URLs:
1107b39c5158Smillert
1108b39c5158Smillert      # there are a number of possible cases:
1109b39c5158Smillert      # 1) text node containing url: http://foo.com
1110b39c5158Smillert      #   -> [ 'http://foo.com' ]
1111b39c5158Smillert      # 2) text node containing url and text: foo|http://foo.com
1112b39c5158Smillert      #   -> [ 'foo|http://foo.com' ]
1113b39c5158Smillert      # 3) text node containing url start: mailto:xE<at>foo.com
1114b39c5158Smillert      #   -> [ 'mailto:x', [ E ... ], 'foo.com' ]
1115b39c5158Smillert      # 4) text node containing url start and text: foo|mailto:xE<at>foo.com
1116b39c5158Smillert      #   -> [ 'foo|mailto:x', [ E ... ], 'foo.com' ]
1117b39c5158Smillert      # 5) other nodes containing text and url start: OE<39>Malley|http://foo.com
1118b39c5158Smillert      #   -> [ 'O', [ E ... ], 'Malley', '|http://foo.com' ]
1119b39c5158Smillert      # ... etc.
1120b39c5158Smillert
1121b39c5158Smillert      # anything before the url is part of the text.
1122b39c5158Smillert      # anything after it is part of the url.
1123b39c5158Smillert      # the url text node itself may contain parts of both.
1124b39c5158Smillert
1125b39c5158Smillert      if (my ($url_index, $text_part, $url_part) =
1126b39c5158Smillert        # grep is no good here; we want to bail out immediately so that we can
1127b39c5158Smillert        # use $1, $2, etc. without having to do the match twice.
1128b39c5158Smillert        sub {
1129b39c5158Smillert          for (2..$#$ell) {
1130b39c5158Smillert            next if ref $ell->[$_];
1131b39c5158Smillert            next unless $ell->[$_] =~ m/^(?:([^|]*)\|)?(\w+:[^:\s]\S*)$/s;
1132b39c5158Smillert            return ($_, $1, $2);
1133b39c5158Smillert          }
1134b39c5158Smillert          return;
1135b39c5158Smillert        }->()
1136b39c5158Smillert      ) {
1137b39c5158Smillert        $ell->[1]{'type'} = 'url';
1138b39c5158Smillert
1139b39c5158Smillert        my @text = @{$ell}[2..$url_index-1];
1140b39c5158Smillert        push @text, $text_part if defined $text_part;
1141b39c5158Smillert
1142b39c5158Smillert        my @url  = @{$ell}[$url_index+1..$#$ell];
1143b39c5158Smillert        unshift @url, $url_part;
1144b39c5158Smillert
1145b39c5158Smillert        unless (@text) {
1146b39c5158Smillert          $ell->[1]{'content-implicit'} = 'yes';
1147b39c5158Smillert          @text = @url;
1148b39c5158Smillert        }
1149b39c5158Smillert
1150b39c5158Smillert        $ell->[1]{to} = Pod::Simple::LinkSection->new(
1151b39c5158Smillert          @url == 1
1152b39c5158Smillert          ? $url[0]
1153b39c5158Smillert          : [ '', {}, @url ],
1154b39c5158Smillert        );
1155b39c5158Smillert
1156b39c5158Smillert        splice @$ell, 2, $#$ell, @text;
1157b39c5158Smillert
1158b39c5158Smillert        next;
1159b39c5158Smillert      }
1160b39c5158Smillert
1161b39c5158Smillert      # Catch some very simple and/or common cases
1162b39c5158Smillert      if(@{$ell} == 3 and ! ref $ell->[2]) {
1163b39c5158Smillert        my $it = $ell->[2];
116491f110e0Safresh1        if($it =~ m{^[^/|]+[(][-a-zA-Z0-9]+[)]$}s) { # man sections
1165b39c5158Smillert          # Hopefully neither too broad nor too restrictive a RE
1166b8851fccSafresh1          DEBUG > 1 and print STDERR "Catching \"$it\" as manpage link.\n";
1167b39c5158Smillert          $ell->[1]{'type'} = 'man';
1168b39c5158Smillert          # This's the only place where man links can get made.
1169b39c5158Smillert          $ell->[1]{'content-implicit'} = 'yes';
1170b39c5158Smillert          $ell->[1]{'to'  } =
1171b39c5158Smillert            Pod::Simple::LinkSection->new( $it ); # treelet!
1172b39c5158Smillert
1173b39c5158Smillert          next;
1174b39c5158Smillert        }
1175b39c5158Smillert        if($it =~ m/^[^\/\|,\$\%\@\ \"\<\>\:\#\&\*\{\}\[\]\(\)]+(\:\:[^\/\|,\$\%\@\ \"\<\>\:\#\&\*\{\}\[\]\(\)]+)*$/s) {
1176b39c5158Smillert          # Extremely forgiving idea of what constitutes a bare
1177b39c5158Smillert          #  modulename link like L<Foo::Bar> or even L<Thing::1.0::Docs::Tralala>
1178b8851fccSafresh1          DEBUG > 1 and print STDERR "Catching \"$it\" as ho-hum L<Modulename> link.\n";
1179b39c5158Smillert          $ell->[1]{'type'} = 'pod';
1180b39c5158Smillert          $ell->[1]{'content-implicit'} = 'yes';
1181b39c5158Smillert          $ell->[1]{'to'  } =
1182b39c5158Smillert            Pod::Simple::LinkSection->new( $it ); # treelet!
1183b39c5158Smillert          next;
1184b39c5158Smillert        }
1185b39c5158Smillert        # else fall thru...
1186b39c5158Smillert      }
1187b39c5158Smillert
1188b39c5158Smillert
1189b39c5158Smillert
1190b39c5158Smillert      # ...Uhoh, here's the real L<...> parsing stuff...
1191b39c5158Smillert      # "With the ill behavior, with the ill behavior, with the ill behavior..."
1192b39c5158Smillert
1193b8851fccSafresh1      DEBUG > 1 and print STDERR "Running a real parse on this non-trivial L\n";
1194b39c5158Smillert
1195b39c5158Smillert
1196b39c5158Smillert      my $link_text; # set to an arrayref if found
1197b39c5158Smillert      my @ell_content = @$ell;
1198b39c5158Smillert      splice @ell_content,0,2; # Knock off the 'L' and {} bits
1199b39c5158Smillert
1200b8851fccSafresh1      DEBUG > 3 and print STDERR " Ell content to start: ",
1201b39c5158Smillert       pretty(@ell_content), "\n";
1202b39c5158Smillert
1203b39c5158Smillert
1204b39c5158Smillert      # Look for the "|" -- only in CHILDREN (not all underlings!)
1205b39c5158Smillert      # Like L<I like the strictness|strict>
1206b39c5158Smillert      DEBUG > 3 and
1207b8851fccSafresh1         print STDERR "  Peering at L content for a '|' ...\n";
1208b39c5158Smillert      for(my $j = 0; $j < @ell_content; ++$j) {
1209b39c5158Smillert        next if ref $ell_content[$j];
1210b39c5158Smillert        DEBUG > 3 and
1211b8851fccSafresh1         print STDERR "    Peering at L-content text bit \"$ell_content[$j]\" for a '|'.\n";
1212b39c5158Smillert
1213b39c5158Smillert        if($ell_content[$j] =~ m/^([^\|]*)\|(.*)$/s) {
1214b39c5158Smillert          my @link_text = ($1);   # might be 0-length
1215b39c5158Smillert          $ell_content[$j] = $2;  # might be 0-length
1216b39c5158Smillert
1217b39c5158Smillert          DEBUG > 3 and
1218b8851fccSafresh1           print STDERR "     FOUND a '|' in it.  Splitting into [$1] + [$2]\n";
1219b39c5158Smillert
122091f110e0Safresh1          if ($link_text[0] =~ m{[|/]}) {
122191f110e0Safresh1            $self->whine(
122291f110e0Safresh1              $start_line,
122391f110e0Safresh1              "alternative text '$link_text[0]' contains non-escaped | or /"
122491f110e0Safresh1            );
122591f110e0Safresh1          }
122691f110e0Safresh1
1227b39c5158Smillert          unshift @link_text, splice @ell_content, 0, $j;
1228b39c5158Smillert            # leaving only things at J and after
1229b39c5158Smillert          @ell_content =  grep ref($_)||length($_), @ell_content ;
1230b39c5158Smillert          $link_text   = [grep ref($_)||length($_), @link_text  ];
1231b39c5158Smillert          DEBUG > 3 and printf
1232b39c5158Smillert           "  So link text is %s\n  and remaining ell content is %s\n",
1233b39c5158Smillert            pretty($link_text), pretty(@ell_content);
1234b39c5158Smillert          last;
1235b39c5158Smillert        }
1236b39c5158Smillert      }
1237b39c5158Smillert
1238b39c5158Smillert
1239b39c5158Smillert      # Now look for the "/" -- only in CHILDREN (not all underlings!)
1240b39c5158Smillert      # And afterward, anything left in @ell_content will be the raw name
1241b39c5158Smillert      # Like L<Foo::Bar/Object Methods>
1242b39c5158Smillert      my $section_name;  # set to arrayref if found
1243b8851fccSafresh1      DEBUG > 3 and print STDERR "  Peering at L-content for a '/' ...\n";
1244b39c5158Smillert      for(my $j = 0; $j < @ell_content; ++$j) {
1245b39c5158Smillert        next if ref $ell_content[$j];
1246b39c5158Smillert        DEBUG > 3 and
1247b8851fccSafresh1         print STDERR "    Peering at L-content text bit \"$ell_content[$j]\" for a '/'.\n";
1248b39c5158Smillert
1249b39c5158Smillert        if($ell_content[$j] =~ m/^([^\/]*)\/(.*)$/s) {
1250b39c5158Smillert          my @section_name = ($2); # might be 0-length
1251b39c5158Smillert          $ell_content[$j] =  $1;  # might be 0-length
1252b39c5158Smillert
1253b39c5158Smillert          DEBUG > 3 and
1254b8851fccSafresh1           print STDERR "     FOUND a '/' in it.",
1255b39c5158Smillert             "  Splitting to page [...$1] + section [$2...]\n";
1256b39c5158Smillert
1257b39c5158Smillert          push @section_name, splice @ell_content, 1+$j;
1258b39c5158Smillert            # leaving only things before and including J
1259b39c5158Smillert
1260b39c5158Smillert          @ell_content  = grep ref($_)||length($_), @ell_content  ;
1261b39c5158Smillert          @section_name = grep ref($_)||length($_), @section_name ;
1262b39c5158Smillert
1263b39c5158Smillert          # Turn L<.../"foo"> into L<.../foo>
1264b39c5158Smillert          if(@section_name
1265b39c5158Smillert            and !ref($section_name[0]) and !ref($section_name[-1])
1266b39c5158Smillert            and $section_name[ 0] =~ m/^\"/s
1267b39c5158Smillert            and $section_name[-1] =~ m/\"$/s
1268b39c5158Smillert            and !( # catch weird degenerate case of L<"> !
1269b39c5158Smillert              @section_name == 1 and $section_name[0] eq '"'
1270b39c5158Smillert            )
1271b39c5158Smillert          ) {
1272b39c5158Smillert            $section_name[ 0] =~ s/^\"//s;
1273b39c5158Smillert            $section_name[-1] =~ s/\"$//s;
1274b39c5158Smillert            DEBUG > 3 and
1275b8851fccSafresh1             print STDERR "     Quotes removed: ", pretty(@section_name), "\n";
1276b39c5158Smillert          } else {
1277b39c5158Smillert            DEBUG > 3 and
1278b8851fccSafresh1             print STDERR "     No need to remove quotes in ", pretty(@section_name), "\n";
1279b39c5158Smillert          }
1280b39c5158Smillert
1281b39c5158Smillert          $section_name = \@section_name;
1282b39c5158Smillert          last;
1283b39c5158Smillert        }
1284b39c5158Smillert      }
1285b39c5158Smillert
1286b39c5158Smillert      # Turn L<"Foo Bar"> into L</Foo Bar>
1287b39c5158Smillert      if(!$section_name and @ell_content
1288b39c5158Smillert         and !ref($ell_content[0]) and !ref($ell_content[-1])
1289b39c5158Smillert         and $ell_content[ 0] =~ m/^\"/s
1290b39c5158Smillert         and $ell_content[-1] =~ m/\"$/s
1291b39c5158Smillert         and !( # catch weird degenerate case of L<"> !
1292b39c5158Smillert           @ell_content == 1 and $ell_content[0] eq '"'
1293b39c5158Smillert         )
1294b39c5158Smillert      ) {
1295b39c5158Smillert        $section_name = [splice @ell_content];
1296b39c5158Smillert        $section_name->[ 0] =~ s/^\"//s;
1297b39c5158Smillert        $section_name->[-1] =~ s/\"$//s;
129856d68f1eSafresh1        $ell->[1]{'~tolerated'} = 1;
1299b39c5158Smillert      }
1300b39c5158Smillert
1301b39c5158Smillert      # Turn L<Foo Bar> into L</Foo Bar>.
1302b39c5158Smillert      if(!$section_name and !$link_text and @ell_content
1303b39c5158Smillert         and grep !ref($_) && m/ /s, @ell_content
1304b39c5158Smillert      ) {
1305b39c5158Smillert        $section_name = [splice @ell_content];
130656d68f1eSafresh1        $ell->[1]{'~deprecated'} = 1;
1307b39c5158Smillert        # That's support for the now-deprecated syntax.
1308b39c5158Smillert        # Note that it deliberately won't work on L<...|Foo Bar>
1309b39c5158Smillert      }
1310b39c5158Smillert
1311b39c5158Smillert
1312b39c5158Smillert      # Now make up the link_text
1313b39c5158Smillert      # L<Foo>     -> L<Foo|Foo>
1314b39c5158Smillert      # L</Bar>    -> L<"Bar"|Bar>
1315b39c5158Smillert      # L<Foo/Bar> -> L<"Bar" in Foo/Foo>
1316b39c5158Smillert      unless($link_text) {
1317b39c5158Smillert        $ell->[1]{'content-implicit'} = 'yes';
1318b39c5158Smillert        $link_text = [];
1319b39c5158Smillert        push @$link_text, '"', @$section_name, '"' if $section_name;
1320b39c5158Smillert
1321b39c5158Smillert        if(@ell_content) {
1322b39c5158Smillert          $link_text->[-1] .= ' in ' if $section_name;
1323b39c5158Smillert          push @$link_text, @ell_content;
1324b39c5158Smillert        }
1325b39c5158Smillert      }
1326b39c5158Smillert
1327b39c5158Smillert
1328b39c5158Smillert      # And the E resolver will have to deal with all our treeletty things:
1329b39c5158Smillert
1330b39c5158Smillert      if(@ell_content == 1 and !ref($ell_content[0])
133191f110e0Safresh1         and $ell_content[0] =~ m{^[^/]+[(][-a-zA-Z0-9]+[)]$}s
1332b39c5158Smillert      ) {
1333b39c5158Smillert        $ell->[1]{'type'}    = 'man';
1334b8851fccSafresh1        DEBUG > 3 and print STDERR "Considering this ($ell_content[0]) a man link.\n";
1335b39c5158Smillert      } else {
1336b39c5158Smillert        $ell->[1]{'type'}    = 'pod';
1337b8851fccSafresh1        DEBUG > 3 and print STDERR "Considering this a pod link (not man or url).\n";
1338b39c5158Smillert      }
1339b39c5158Smillert
1340b39c5158Smillert      if( defined $section_name ) {
1341b39c5158Smillert        $ell->[1]{'section'} = Pod::Simple::LinkSection->new(
1342b39c5158Smillert          ['', {}, @$section_name]
1343b39c5158Smillert        );
1344b8851fccSafresh1        DEBUG > 3 and print STDERR "L-section content: ", pretty($ell->[1]{'section'}), "\n";
1345b39c5158Smillert      }
1346b39c5158Smillert
1347b39c5158Smillert      if( @ell_content ) {
1348b39c5158Smillert        $ell->[1]{'to'} = Pod::Simple::LinkSection->new(
1349b39c5158Smillert          ['', {}, @ell_content]
1350b39c5158Smillert        );
1351b8851fccSafresh1        DEBUG > 3 and print STDERR "L-to content: ", pretty($ell->[1]{'to'}), "\n";
1352b39c5158Smillert      }
1353b39c5158Smillert
1354b39c5158Smillert      # And update children to be the link-text:
1355b39c5158Smillert      @$ell = (@$ell[0,1], defined($link_text) ? splice(@$link_text) : '');
1356b39c5158Smillert
135756d68f1eSafresh1      DEBUG > 2 and print STDERR "End of L-parsing for this node " . pretty($treelet->[$i]) . "\n";
1358b39c5158Smillert
1359b39c5158Smillert      unshift @stack, $treelet->[$i]; # might as well recurse
1360b39c5158Smillert    }
1361b39c5158Smillert  }
1362b39c5158Smillert
1363b39c5158Smillert  return;
1364b39c5158Smillert}
1365b39c5158Smillert
1366b39c5158Smillert# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1367b39c5158Smillert
1368b39c5158Smillertsub _treat_Es {
1369b39c5158Smillert  my($self,@stack) = @_;
1370b39c5158Smillert
1371b39c5158Smillert  my($i, $treelet, $content, $replacer, $charnum);
1372b39c5158Smillert  my $start_line = $stack[0][1]{'start_line'};
1373b39c5158Smillert
1374b39c5158Smillert  # A recursive algorithm implemented iteratively!  Whee!
1375b39c5158Smillert
1376b39c5158Smillert
1377b39c5158Smillert  # Has frightening side effects on L nodes' attributes.
1378b39c5158Smillert
1379b39c5158Smillert  #my @ells_to_tweak;
1380b39c5158Smillert
1381b39c5158Smillert  while($treelet = shift @stack) {
1382b39c5158Smillert    for(my $i = 2; $i < @$treelet; ++$i) { # iterate over children
1383b39c5158Smillert      next unless ref $treelet->[$i];  # text nodes are uninteresting
1384b39c5158Smillert      if($treelet->[$i][0] eq 'L') {
1385b39c5158Smillert        # SPECIAL STUFF for semi-processed L<>'s
1386b39c5158Smillert
1387b39c5158Smillert        my $thing;
1388b39c5158Smillert        foreach my $attrname ('section', 'to') {
1389b39c5158Smillert          if(defined($thing = $treelet->[$i][1]{$attrname}) and ref $thing) {
1390b39c5158Smillert            unshift @stack, $thing;
1391b8851fccSafresh1            DEBUG > 2 and print STDERR "  Enqueuing ",
1392b39c5158Smillert             pretty( $treelet->[$i][1]{$attrname} ),
1393b39c5158Smillert             " as an attribute value to tweak.\n";
1394b39c5158Smillert          }
1395b39c5158Smillert        }
1396b39c5158Smillert
1397b39c5158Smillert        unshift @stack, $treelet->[$i]; # recurse
1398b39c5158Smillert        next;
1399b39c5158Smillert      } elsif($treelet->[$i][0] ne 'E') {
1400b39c5158Smillert        unshift @stack, $treelet->[$i]; # recurse
1401b39c5158Smillert        next;
1402b39c5158Smillert      }
1403b39c5158Smillert
1404b8851fccSafresh1      DEBUG > 1 and print STDERR "Ogling E node ", pretty($treelet->[$i]), "\n";
1405b39c5158Smillert
1406b39c5158Smillert      # bitch if it's empty
1407b39c5158Smillert      if(  @{$treelet->[$i]} == 2
1408b39c5158Smillert       or (@{$treelet->[$i]} == 3 and $treelet->[$i][2] eq '')
1409b39c5158Smillert      ) {
1410b39c5158Smillert        $self->whine( $start_line, "An empty E<>" );
1411b39c5158Smillert        $treelet->[$i] = 'E<>'; # splice in a literal
1412b39c5158Smillert        next;
1413b39c5158Smillert      }
1414b39c5158Smillert
1415b39c5158Smillert      # bitch if content is weird
1416b39c5158Smillert      unless(@{$treelet->[$i]} == 3 and !ref($content = $treelet->[$i][2])) {
1417b39c5158Smillert        $self->whine( $start_line, "An E<...> surrounding strange content" );
1418b39c5158Smillert        $replacer = $treelet->[$i]; # scratch
1419b39c5158Smillert        splice(@$treelet, $i, 1,   # fake out a literal
1420b39c5158Smillert          'E<',
1421b39c5158Smillert          splice(@$replacer,2), # promote its content
1422b39c5158Smillert          '>'
1423b39c5158Smillert        );
1424b39c5158Smillert        # Don't need to do --$i, as the 'E<' we just added isn't interesting.
1425b39c5158Smillert        next;
1426b39c5158Smillert      }
1427b39c5158Smillert
1428b8851fccSafresh1      DEBUG > 1 and print STDERR "Ogling E<$content>\n";
1429b39c5158Smillert
1430898184e3Ssthen      # XXX E<>'s contents *should* be a valid char in the scope of the current
1431898184e3Ssthen      # =encoding directive. Defaults to iso-8859-1, I believe. Fix this in the
1432898184e3Ssthen      # future sometime.
1433898184e3Ssthen
1434b39c5158Smillert      $charnum  = Pod::Escapes::e2charnum($content);
1435b8851fccSafresh1      DEBUG > 1 and print STDERR " Considering E<$content> with char ",
1436b39c5158Smillert        defined($charnum) ? $charnum : "undef", ".\n";
1437b39c5158Smillert
1438b39c5158Smillert      if(!defined( $charnum )) {
1439b8851fccSafresh1        DEBUG > 1 and print STDERR "I don't know how to deal with E<$content>.\n";
1440b39c5158Smillert        $self->whine( $start_line, "Unknown E content in E<$content>" );
1441b39c5158Smillert        $replacer = "E<$content>"; # better than nothing
1442b39c5158Smillert      } elsif($charnum >= 255 and !UNICODE) {
1443b39c5158Smillert        $replacer = ASCII ? "\xA4" : "?";
1444b8851fccSafresh1        DEBUG > 1 and print STDERR "This Perl version can't handle ",
1445b39c5158Smillert          "E<$content> (chr $charnum), so replacing with $replacer\n";
1446b39c5158Smillert      } else {
1447b39c5158Smillert        $replacer = Pod::Escapes::e2char($content);
1448b8851fccSafresh1        DEBUG > 1 and print STDERR " Replacing E<$content> with $replacer\n";
1449b39c5158Smillert      }
1450b39c5158Smillert
1451b39c5158Smillert      splice(@$treelet, $i, 1, $replacer); # no need to back up $i, tho
1452b39c5158Smillert    }
1453b39c5158Smillert  }
1454b39c5158Smillert
1455b39c5158Smillert  return;
1456b39c5158Smillert}
1457b39c5158Smillert
1458b39c5158Smillert
1459b39c5158Smillert# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1460b39c5158Smillert
1461b39c5158Smillertsub _treat_Ss {
1462b39c5158Smillert  my($self,$treelet) = @_;
1463b39c5158Smillert
1464b39c5158Smillert  _change_S_to_nbsp($treelet,0) if $self->{'nbsp_for_S'};
1465b39c5158Smillert
1466b39c5158Smillert  # TODO: or a change_nbsp_to_S
1467b39c5158Smillert  #  Normalizing nbsp's to S is harder: for each text node, make S content
1468b39c5158Smillert  #  out of anything matching m/([^ \xA0]*(?:\xA0+[^ \xA0]*)+)/
1469b39c5158Smillert
1470b39c5158Smillert
1471b39c5158Smillert  return;
1472b39c5158Smillert}
1473b39c5158Smillert
1474b39c5158Smillertsub _change_S_to_nbsp { #  a recursive function
1475b39c5158Smillert  # Sanely assumes that the top node in the excursion won't be an S node.
1476b39c5158Smillert  my($treelet, $in_s) = @_;
1477b39c5158Smillert
1478b39c5158Smillert  my $is_s = ('S' eq $treelet->[0]);
1479b39c5158Smillert  $in_s ||= $is_s; # So in_s is on either by this being an S element,
1480b39c5158Smillert                   #  or by an ancestor being an S element.
1481b39c5158Smillert
1482b39c5158Smillert  for(my $i = 2; $i < @$treelet; ++$i) {
1483b39c5158Smillert    if(ref $treelet->[$i]) {
1484b39c5158Smillert      if( _change_S_to_nbsp( $treelet->[$i], $in_s ) ) {
1485b39c5158Smillert        my $to_pull_up = $treelet->[$i];
1486b39c5158Smillert        splice @$to_pull_up,0,2;   # ...leaving just its content
1487b39c5158Smillert        splice @$treelet, $i, 1, @$to_pull_up;  # Pull up content
1488b39c5158Smillert        $i +=  @$to_pull_up - 1;   # Make $i skip the pulled-up stuff
1489b39c5158Smillert      }
1490b39c5158Smillert    } else {
1491b8851fccSafresh1      $treelet->[$i] =~ s/\s/$Pod::Simple::nbsp/g if $in_s;
1492b39c5158Smillert
1493b39c5158Smillert       # Note that if you apply nbsp_for_S to text, and so turn
1494b39c5158Smillert       # "foo S<bar baz> quux" into "foo bar&#160;faz quux", you
1495b39c5158Smillert       # end up with something that fails to say "and don't hyphenate
1496b39c5158Smillert       # any part of 'bar baz'".  However, hyphenation is such a vexing
1497b39c5158Smillert       # problem anyway, that most Pod renderers just don't render it
1498b39c5158Smillert       # at all.  But if you do want to implement hyphenation, I guess
1499b39c5158Smillert       # that you'd better have nbsp_for_S off.
1500b39c5158Smillert    }
1501b39c5158Smillert  }
1502b39c5158Smillert
1503b39c5158Smillert  return $is_s;
1504b39c5158Smillert}
1505b39c5158Smillert
1506b39c5158Smillert#-----------------------------------------------------------------------------
1507b39c5158Smillert
1508b39c5158Smillertsub _accessorize {  # A simple-minded method-maker
1509b39c5158Smillert  no strict 'refs';
1510b39c5158Smillert  foreach my $attrname (@_) {
1511b39c5158Smillert    next if $attrname =~ m/::/; # a hack
1512b39c5158Smillert    *{caller() . '::' . $attrname} = sub {
1513b39c5158Smillert      use strict;
1514b39c5158Smillert      $Carp::CarpLevel = 1,  Carp::croak(
1515b39c5158Smillert       "Accessor usage: \$obj->$attrname() or \$obj->$attrname(\$new_value)"
1516b39c5158Smillert      ) unless (@_ == 1 or @_ == 2) and ref $_[0];
151756d68f1eSafresh1
1518b39c5158Smillert      (@_ == 1) ?  $_[0]->{$attrname}
1519b39c5158Smillert                : ($_[0]->{$attrname} = $_[1]);
1520b39c5158Smillert    };
1521b39c5158Smillert  }
1522b39c5158Smillert  # Ya know, they say accessories make the ensemble!
1523b39c5158Smillert  return;
1524b39c5158Smillert}
1525b39c5158Smillert
1526b39c5158Smillert# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1527b39c5158Smillert# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1528b39c5158Smillert#=============================================================================
1529b39c5158Smillert
1530b39c5158Smillertsub filter {
1531b39c5158Smillert  my($class, $source) = @_;
1532b39c5158Smillert  my $new = $class->new;
1533b39c5158Smillert  $new->output_fh(*STDOUT{IO});
1534b39c5158Smillert
1535b39c5158Smillert  if(ref($source || '') eq 'SCALAR') {
1536b39c5158Smillert    $new->parse_string_document( $$source );
1537b39c5158Smillert  } elsif(ref($source)) {  # it's a file handle
1538b39c5158Smillert    $new->parse_file($source);
1539b39c5158Smillert  } else {  # it's a filename
1540b39c5158Smillert    $new->parse_file($source);
1541b39c5158Smillert  }
1542b39c5158Smillert
1543b39c5158Smillert  return $new;
1544b39c5158Smillert}
1545b39c5158Smillert
1546b39c5158Smillert
1547b39c5158Smillert#-----------------------------------------------------------------------------
1548b39c5158Smillert
1549b39c5158Smillertsub _out {
1550b39c5158Smillert  # For use in testing: Class->_out($source)
1551b39c5158Smillert  #  returns the transformation of $source
1552b39c5158Smillert
1553b39c5158Smillert  my $class = shift(@_);
1554b39c5158Smillert
1555b39c5158Smillert  my $mutor = shift(@_) if @_ and ref($_[0] || '') eq 'CODE';
1556b39c5158Smillert
1557b8851fccSafresh1  DEBUG and print STDERR "\n\n", '#' x 76,
1558b39c5158Smillert   "\nAbout to parse source: {{\n$_[0]\n}}\n\n";
1559b39c5158Smillert
1560b39c5158Smillert
1561b39c5158Smillert  my $parser = ref $class && $class->isa(__PACKAGE__) ? $class : $class->new;
1562b39c5158Smillert  $parser->hide_line_numbers(1);
1563b39c5158Smillert
1564b39c5158Smillert  my $out = '';
1565b39c5158Smillert  $parser->output_string( \$out );
1566b8851fccSafresh1  DEBUG and print STDERR " _out to ", \$out, "\n";
1567b39c5158Smillert
1568b39c5158Smillert  $mutor->($parser) if $mutor;
1569b39c5158Smillert
1570b39c5158Smillert  $parser->parse_string_document( $_[0] );
1571b8851fccSafresh1  # use Data::Dumper; print STDERR Dumper($parser), "\n";
1572b39c5158Smillert  return $out;
1573b39c5158Smillert}
1574b39c5158Smillert
1575b39c5158Smillert
1576b39c5158Smillertsub _duo {
1577b39c5158Smillert  # For use in testing: Class->_duo($source1, $source2)
1578b39c5158Smillert  #  returns the parse trees of $source1 and $source2.
1579b39c5158Smillert  # Good in things like: &ok( Class->duo(... , ...) );
1580b39c5158Smillert
1581b39c5158Smillert  my $class = shift(@_);
1582b39c5158Smillert
1583b39c5158Smillert  Carp::croak "But $class->_duo is useful only in list context!"
1584b39c5158Smillert   unless wantarray;
1585b39c5158Smillert
1586b39c5158Smillert  my $mutor = shift(@_) if @_ and ref($_[0] || '') eq 'CODE';
1587b39c5158Smillert
1588b39c5158Smillert  Carp::croak "But $class->_duo takes two parameters, not: @_"
1589b39c5158Smillert   unless @_ == 2;
1590b39c5158Smillert
1591b39c5158Smillert  my(@out);
1592b39c5158Smillert
1593b39c5158Smillert  while( @_ ) {
1594b39c5158Smillert    my $parser = $class->new;
1595b39c5158Smillert
1596b39c5158Smillert    push @out, '';
1597b39c5158Smillert    $parser->output_string( \( $out[-1] ) );
1598b39c5158Smillert
1599b8851fccSafresh1    DEBUG and print STDERR " _duo out to ", $parser->output_string(),
1600b39c5158Smillert      " = $parser->{'output_string'}\n";
1601b39c5158Smillert
1602b39c5158Smillert    $parser->hide_line_numbers(1);
1603b39c5158Smillert    $mutor->($parser) if $mutor;
1604b39c5158Smillert    $parser->parse_string_document( shift( @_ ) );
1605b8851fccSafresh1    # use Data::Dumper; print STDERR Dumper($parser), "\n";
1606b39c5158Smillert  }
1607b39c5158Smillert
1608b39c5158Smillert  return @out;
1609b39c5158Smillert}
1610b39c5158Smillert
1611b39c5158Smillert
1612b39c5158Smillert
1613b39c5158Smillert#-----------------------------------------------------------------------------
1614b39c5158Smillert1;
1615b39c5158Smillert__END__
1616b39c5158Smillert
1617b39c5158SmillertTODO:
1618b39c5158SmillertA start_formatting_code and end_formatting_code methods, which in the
1619b39c5158Smillertbase class call start_L, end_L, start_C, end_C, etc., if they are
1620b39c5158Smillertdefined.
1621b39c5158Smillert
1622b39c5158Smillerthave the POD FORMATTING ERRORS section note the localtime, and the
1623b39c5158Smillertversion of Pod::Simple.
1624b39c5158Smillert
1625b39c5158Smillertoption to delete all E<shy>s?
1626b39c5158Smillertoption to scream if under-0x20 literals are found in the input, or
1627b39c5158Smillertunder-E<32> E codes are found in the tree. And ditto \x7f-\x9f
1628b39c5158Smillert
1629b39c5158SmillertOption to turn highbit characters into their compromised form? (applies
1630b39c5158Smillertto E parsing too)
1631b39c5158Smillert
1632b39c5158SmillertTODO: BOM/encoding things.
1633b39c5158Smillert
1634b39c5158SmillertTODO: ascii-compat things in the XML classes?
1635b39c5158Smillert
1636