--- /dev/null
+#############################################################################\r
+# Pod/Parser.pm -- package which defines a base class for parsing POD docs.\r
+#\r
+# Copyright (C) 1996-2000 by Bradford Appleton. All rights reserved.\r
+# This file is part of "PodParser". PodParser is free software;\r
+# you can redistribute it and/or modify it under the same terms\r
+# as Perl itself.\r
+#############################################################################\r
+\r
+package Pod::Parser;\r
+use strict;\r
+\r
+## These "variables" are used as local "glob aliases" for performance\r
+use vars qw($VERSION @ISA %myData %myOpts @input_stack);\r
+$VERSION = '1.60'; ## Current version of this package\r
+require 5.005; ## requires this Perl version or later\r
+\r
+#############################################################################\r
+\r
+=head1 NAME\r
+\r
+Pod::Parser - base class for creating POD filters and translators\r
+\r
+=head1 SYNOPSIS\r
+\r
+ use Pod::Parser;\r
+\r
+ package MyParser;\r
+ @ISA = qw(Pod::Parser);\r
+\r
+ sub command { \r
+ my ($parser, $command, $paragraph, $line_num) = @_;\r
+ ## Interpret the command and its text; sample actions might be:\r
+ if ($command eq 'head1') { ... }\r
+ elsif ($command eq 'head2') { ... }\r
+ ## ... other commands and their actions\r
+ my $out_fh = $parser->output_handle();\r
+ my $expansion = $parser->interpolate($paragraph, $line_num);\r
+ print $out_fh $expansion;\r
+ }\r
+\r
+ sub verbatim { \r
+ my ($parser, $paragraph, $line_num) = @_;\r
+ ## Format verbatim paragraph; sample actions might be:\r
+ my $out_fh = $parser->output_handle();\r
+ print $out_fh $paragraph;\r
+ }\r
+\r
+ sub textblock { \r
+ my ($parser, $paragraph, $line_num) = @_;\r
+ ## Translate/Format this block of text; sample actions might be:\r
+ my $out_fh = $parser->output_handle();\r
+ my $expansion = $parser->interpolate($paragraph, $line_num);\r
+ print $out_fh $expansion;\r
+ }\r
+\r
+ sub interior_sequence { \r
+ my ($parser, $seq_command, $seq_argument) = @_;\r
+ ## Expand an interior sequence; sample actions might be:\r
+ return "*$seq_argument*" if ($seq_command eq 'B');\r
+ return "`$seq_argument'" if ($seq_command eq 'C');\r
+ return "_${seq_argument}_'" if ($seq_command eq 'I');\r
+ ## ... other sequence commands and their resulting text\r
+ }\r
+\r
+ package main;\r
+\r
+ ## Create a parser object and have it parse file whose name was\r
+ ## given on the command-line (use STDIN if no files were given).\r
+ $parser = new MyParser();\r
+ $parser->parse_from_filehandle(\*STDIN) if (@ARGV == 0);\r
+ for (@ARGV) { $parser->parse_from_file($_); }\r
+\r
+=head1 REQUIRES\r
+\r
+perl5.005, Pod::InputObjects, Exporter, Symbol, Carp\r
+\r
+=head1 EXPORTS\r
+\r
+Nothing.\r
+\r
+=head1 DESCRIPTION\r
+\r
+B<Pod::Parser> is a base class for creating POD filters and translators.\r
+It handles most of the effort involved with parsing the POD sections\r
+from an input stream, leaving subclasses free to be concerned only with\r
+performing the actual translation of text.\r
+\r
+B<Pod::Parser> parses PODs, and makes method calls to handle the various\r
+components of the POD. Subclasses of B<Pod::Parser> override these methods\r
+to translate the POD into whatever output format they desire.\r
+\r
+Note: This module is considered as legacy; modern Perl releases (5.18 and\r
+higher) are going to remove Pod::Parser from core and use L<Pod::Simple>\r
+for all things POD.\r
+\r
+=head1 QUICK OVERVIEW\r
+\r
+To create a POD filter for translating POD documentation into some other\r
+format, you create a subclass of B<Pod::Parser> which typically overrides\r
+just the base class implementation for the following methods:\r
+\r
+=over 2\r
+\r
+=item *\r
+\r
+B<command()>\r
+\r
+=item *\r
+\r
+B<verbatim()>\r
+\r
+=item *\r
+\r
+B<textblock()>\r
+\r
+=item *\r
+\r
+B<interior_sequence()>\r
+\r
+=back\r
+\r
+You may also want to override the B<begin_input()> and B<end_input()>\r
+methods for your subclass (to perform any needed per-file and/or\r
+per-document initialization or cleanup).\r
+\r
+If you need to perform any preprocessing of input before it is parsed\r
+you may want to override one or more of B<preprocess_line()> and/or\r
+B<preprocess_paragraph()>.\r
+\r
+Sometimes it may be necessary to make more than one pass over the input\r
+files. If this is the case you have several options. You can make the\r
+first pass using B<Pod::Parser> and override your methods to store the\r
+intermediate results in memory somewhere for the B<end_pod()> method to\r
+process. You could use B<Pod::Parser> for several passes with an\r
+appropriate state variable to control the operation for each pass. If\r
+your input source can't be reset to start at the beginning, you can\r
+store it in some other structure as a string or an array and have that\r
+structure implement a B<getline()> method (which is all that\r
+B<parse_from_filehandle()> uses to read input).\r
+\r
+Feel free to add any member data fields you need to keep track of things\r
+like current font, indentation, horizontal or vertical position, or\r
+whatever else you like. Be sure to read L<"PRIVATE METHODS AND DATA">\r
+to avoid name collisions.\r
+\r
+For the most part, the B<Pod::Parser> base class should be able to\r
+do most of the input parsing for you and leave you free to worry about\r
+how to interpret the commands and translate the result.\r
+\r
+Note that all we have described here in this quick overview is the\r
+simplest most straightforward use of B<Pod::Parser> to do stream-based\r
+parsing. It is also possible to use the B<Pod::Parser::parse_text> function\r
+to do more sophisticated tree-based parsing. See L<"TREE-BASED PARSING">.\r
+\r
+=head1 PARSING OPTIONS\r
+\r
+A I<parse-option> is simply a named option of B<Pod::Parser> with a\r
+value that corresponds to a certain specified behavior. These various\r
+behaviors of B<Pod::Parser> may be enabled/disabled by setting\r
+or unsetting one or more I<parse-options> using the B<parseopts()> method.\r
+The set of currently accepted parse-options is as follows:\r
+\r
+=over 3\r
+\r
+=item B<-want_nonPODs> (default: unset)\r
+\r
+Normally (by default) B<Pod::Parser> will only provide access to\r
+the POD sections of the input. Input paragraphs that are not part\r
+of the POD-format documentation are not made available to the caller\r
+(not even using B<preprocess_paragraph()>). Setting this option to a\r
+non-empty, non-zero value will allow B<preprocess_paragraph()> to see\r
+non-POD sections of the input as well as POD sections. The B<cutting()>\r
+method can be used to determine if the corresponding paragraph is a POD\r
+paragraph, or some other input paragraph.\r
+\r
+=item B<-process_cut_cmd> (default: unset)\r
+\r
+Normally (by default) B<Pod::Parser> handles the C<=cut> POD directive\r
+by itself and does not pass it on to the caller for processing. Setting\r
+this option to a non-empty, non-zero value will cause B<Pod::Parser> to\r
+pass the C<=cut> directive to the caller just like any other POD command\r
+(and hence it may be processed by the B<command()> method).\r
+\r
+B<Pod::Parser> will still interpret the C<=cut> directive to mean that\r
+"cutting mode" has been (re)entered, but the caller will get a chance\r
+to capture the actual C<=cut> paragraph itself for whatever purpose\r
+it desires.\r
+\r
+=item B<-warnings> (default: unset)\r
+\r
+Normally (by default) B<Pod::Parser> recognizes a bare minimum of\r
+pod syntax errors and warnings and issues diagnostic messages\r
+for errors, but not for warnings. (Use B<Pod::Checker> to do more\r
+thorough checking of POD syntax.) Setting this option to a non-empty,\r
+non-zero value will cause B<Pod::Parser> to issue diagnostics for\r
+the few warnings it recognizes as well as the errors.\r
+\r
+=back\r
+\r
+Please see L<"parseopts()"> for a complete description of the interface\r
+for the setting and unsetting of parse-options.\r
+\r
+=cut\r
+\r
+#############################################################################\r
+\r
+#use diagnostics;\r
+use Pod::InputObjects;\r
+use Carp;\r
+use Exporter;\r
+BEGIN {\r
+ if ($] < 5.006) {\r
+ require Symbol;\r
+ import Symbol;\r
+ }\r
+}\r
+@ISA = qw(Exporter);\r
+\r
+#############################################################################\r
+\r
+=head1 RECOMMENDED SUBROUTINE/METHOD OVERRIDES\r
+\r
+B<Pod::Parser> provides several methods which most subclasses will probably\r
+want to override. These methods are as follows:\r
+\r
+=cut\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<command()>\r
+\r
+ $parser->command($cmd,$text,$line_num,$pod_para);\r
+\r
+This method should be overridden by subclasses to take the appropriate\r
+action when a POD command paragraph (denoted by a line beginning with\r
+"=") is encountered. When such a POD directive is seen in the input,\r
+this method is called and is passed:\r
+\r
+=over 3\r
+\r
+=item C<$cmd>\r
+\r
+the name of the command for this POD paragraph\r
+\r
+=item C<$text>\r
+\r
+the paragraph text for the given POD paragraph command.\r
+\r
+=item C<$line_num>\r
+\r
+the line-number of the beginning of the paragraph\r
+\r
+=item C<$pod_para>\r
+\r
+a reference to a C<Pod::Paragraph> object which contains further\r
+information about the paragraph command (see L<Pod::InputObjects>\r
+for details).\r
+\r
+=back\r
+\r
+B<Note> that this method I<is> called for C<=pod> paragraphs.\r
+\r
+The base class implementation of this method simply treats the raw POD\r
+command as normal block of paragraph text (invoking the B<textblock()>\r
+method with the command paragraph).\r
+\r
+=cut\r
+\r
+sub command {\r
+ my ($self, $cmd, $text, $line_num, $pod_para) = @_;\r
+ ## Just treat this like a textblock\r
+ $self->textblock($pod_para->raw_text(), $line_num, $pod_para);\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<verbatim()>\r
+\r
+ $parser->verbatim($text,$line_num,$pod_para);\r
+\r
+This method may be overridden by subclasses to take the appropriate\r
+action when a block of verbatim text is encountered. It is passed the\r
+following parameters:\r
+\r
+=over 3\r
+\r
+=item C<$text>\r
+\r
+the block of text for the verbatim paragraph\r
+\r
+=item C<$line_num>\r
+\r
+the line-number of the beginning of the paragraph\r
+\r
+=item C<$pod_para>\r
+\r
+a reference to a C<Pod::Paragraph> object which contains further\r
+information about the paragraph (see L<Pod::InputObjects>\r
+for details).\r
+\r
+=back\r
+\r
+The base class implementation of this method simply prints the textblock\r
+(unmodified) to the output filehandle.\r
+\r
+=cut\r
+\r
+sub verbatim {\r
+ my ($self, $text, $line_num, $pod_para) = @_;\r
+ my $out_fh = $self->{_OUTPUT};\r
+ print $out_fh $text;\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<textblock()>\r
+\r
+ $parser->textblock($text,$line_num,$pod_para);\r
+\r
+This method may be overridden by subclasses to take the appropriate\r
+action when a normal block of POD text is encountered (although the base\r
+class method will usually do what you want). It is passed the following\r
+parameters:\r
+\r
+=over 3\r
+\r
+=item C<$text>\r
+\r
+the block of text for the a POD paragraph\r
+\r
+=item C<$line_num>\r
+\r
+the line-number of the beginning of the paragraph\r
+\r
+=item C<$pod_para>\r
+\r
+a reference to a C<Pod::Paragraph> object which contains further\r
+information about the paragraph (see L<Pod::InputObjects>\r
+for details).\r
+\r
+=back\r
+\r
+In order to process interior sequences, subclasses implementations of\r
+this method will probably want to invoke either B<interpolate()> or\r
+B<parse_text()>, passing it the text block C<$text>, and the corresponding\r
+line number in C<$line_num>, and then perform any desired processing upon\r
+the returned result.\r
+\r
+The base class implementation of this method simply prints the text block\r
+as it occurred in the input stream).\r
+\r
+=cut\r
+\r
+sub textblock {\r
+ my ($self, $text, $line_num, $pod_para) = @_;\r
+ my $out_fh = $self->{_OUTPUT};\r
+ print $out_fh $self->interpolate($text, $line_num);\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<interior_sequence()>\r
+\r
+ $parser->interior_sequence($seq_cmd,$seq_arg,$pod_seq);\r
+\r
+This method should be overridden by subclasses to take the appropriate\r
+action when an interior sequence is encountered. An interior sequence is\r
+an embedded command within a block of text which appears as a command\r
+name (usually a single uppercase character) followed immediately by a\r
+string of text which is enclosed in angle brackets. This method is\r
+passed the sequence command C<$seq_cmd> and the corresponding text\r
+C<$seq_arg>. It is invoked by the B<interpolate()> method for each interior\r
+sequence that occurs in the string that it is passed. It should return\r
+the desired text string to be used in place of the interior sequence.\r
+The C<$pod_seq> argument is a reference to a C<Pod::InteriorSequence>\r
+object which contains further information about the interior sequence.\r
+Please see L<Pod::InputObjects> for details if you need to access this\r
+additional information.\r
+\r
+Subclass implementations of this method may wish to invoke the \r
+B<nested()> method of C<$pod_seq> to see if it is nested inside\r
+some other interior-sequence (and if so, which kind).\r
+\r
+The base class implementation of the B<interior_sequence()> method\r
+simply returns the raw text of the interior sequence (as it occurred\r
+in the input) to the caller.\r
+\r
+=cut\r
+\r
+sub interior_sequence {\r
+ my ($self, $seq_cmd, $seq_arg, $pod_seq) = @_;\r
+ ## Just return the raw text of the interior sequence\r
+ return $pod_seq->raw_text();\r
+}\r
+\r
+#############################################################################\r
+\r
+=head1 OPTIONAL SUBROUTINE/METHOD OVERRIDES\r
+\r
+B<Pod::Parser> provides several methods which subclasses may want to override\r
+to perform any special pre/post-processing. These methods do I<not> have to\r
+be overridden, but it may be useful for subclasses to take advantage of them.\r
+\r
+=cut\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<new()>\r
+\r
+ my $parser = Pod::Parser->new();\r
+\r
+This is the constructor for B<Pod::Parser> and its subclasses. You\r
+I<do not> need to override this method! It is capable of constructing\r
+subclass objects as well as base class objects, provided you use\r
+any of the following constructor invocation styles:\r
+\r
+ my $parser1 = MyParser->new();\r
+ my $parser2 = new MyParser();\r
+ my $parser3 = $parser2->new();\r
+\r
+where C<MyParser> is some subclass of B<Pod::Parser>.\r
+\r
+Using the syntax C<MyParser::new()> to invoke the constructor is I<not>\r
+recommended, but if you insist on being able to do this, then the\r
+subclass I<will> need to override the B<new()> constructor method. If\r
+you do override the constructor, you I<must> be sure to invoke the\r
+B<initialize()> method of the newly blessed object.\r
+\r
+Using any of the above invocations, the first argument to the\r
+constructor is always the corresponding package name (or object\r
+reference). No other arguments are required, but if desired, an\r
+associative array (or hash-table) my be passed to the B<new()>\r
+constructor, as in:\r
+\r
+ my $parser1 = MyParser->new( MYDATA => $value1, MOREDATA => $value2 );\r
+ my $parser2 = new MyParser( -myflag => 1 );\r
+\r
+All arguments passed to the B<new()> constructor will be treated as\r
+key/value pairs in a hash-table. The newly constructed object will be\r
+initialized by copying the contents of the given hash-table (which may\r
+have been empty). The B<new()> constructor for this class and all of its\r
+subclasses returns a blessed reference to the initialized object (hash-table).\r
+\r
+=cut\r
+\r
+sub new {\r
+ ## Determine if we were called via an object-ref or a classname\r
+ my ($this,%params) = @_;\r
+ my $class = ref($this) || $this;\r
+ ## Any remaining arguments are treated as initial values for the\r
+ ## hash that is used to represent this object.\r
+ my $self = { %params };\r
+ ## Bless ourselves into the desired class and perform any initialization\r
+ bless $self, $class;\r
+ $self->initialize();\r
+ return $self;\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<initialize()>\r
+\r
+ $parser->initialize();\r
+\r
+This method performs any necessary object initialization. It takes no\r
+arguments (other than the object instance of course, which is typically\r
+copied to a local variable named C<$self>). If subclasses override this\r
+method then they I<must> be sure to invoke C<$self-E<gt>SUPER::initialize()>.\r
+\r
+=cut\r
+\r
+sub initialize {\r
+ #my $self = shift;\r
+ #return;\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<begin_pod()>\r
+\r
+ $parser->begin_pod();\r
+\r
+This method is invoked at the beginning of processing for each POD\r
+document that is encountered in the input. Subclasses should override\r
+this method to perform any per-document initialization.\r
+\r
+=cut\r
+\r
+sub begin_pod {\r
+ #my $self = shift;\r
+ #return;\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<begin_input()>\r
+\r
+ $parser->begin_input();\r
+\r
+This method is invoked by B<parse_from_filehandle()> immediately I<before>\r
+processing input from a filehandle. The base class implementation does\r
+nothing, however, subclasses may override it to perform any per-file\r
+initializations.\r
+\r
+Note that if multiple files are parsed for a single POD document\r
+(perhaps the result of some future C<=include> directive) this method\r
+is invoked for every file that is parsed. If you wish to perform certain\r
+initializations once per document, then you should use B<begin_pod()>.\r
+\r
+=cut\r
+\r
+sub begin_input {\r
+ #my $self = shift;\r
+ #return;\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<end_input()>\r
+\r
+ $parser->end_input();\r
+\r
+This method is invoked by B<parse_from_filehandle()> immediately I<after>\r
+processing input from a filehandle. The base class implementation does\r
+nothing, however, subclasses may override it to perform any per-file\r
+cleanup actions.\r
+\r
+Please note that if multiple files are parsed for a single POD document\r
+(perhaps the result of some kind of C<=include> directive) this method\r
+is invoked for every file that is parsed. If you wish to perform certain\r
+cleanup actions once per document, then you should use B<end_pod()>.\r
+\r
+=cut\r
+\r
+sub end_input {\r
+ #my $self = shift;\r
+ #return;\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<end_pod()>\r
+\r
+ $parser->end_pod();\r
+\r
+This method is invoked at the end of processing for each POD document\r
+that is encountered in the input. Subclasses should override this method\r
+to perform any per-document finalization.\r
+\r
+=cut\r
+\r
+sub end_pod {\r
+ #my $self = shift;\r
+ #return;\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<preprocess_line()>\r
+\r
+ $textline = $parser->preprocess_line($text, $line_num);\r
+\r
+This method should be overridden by subclasses that wish to perform\r
+any kind of preprocessing for each I<line> of input (I<before> it has\r
+been determined whether or not it is part of a POD paragraph). The\r
+parameter C<$text> is the input line; and the parameter C<$line_num> is\r
+the line number of the corresponding text line.\r
+\r
+The value returned should correspond to the new text to use in its\r
+place. If the empty string or an undefined value is returned then no\r
+further processing will be performed for this line.\r
+\r
+Please note that the B<preprocess_line()> method is invoked I<before>\r
+the B<preprocess_paragraph()> method. After all (possibly preprocessed)\r
+lines in a paragraph have been assembled together and it has been\r
+determined that the paragraph is part of the POD documentation from one\r
+of the selected sections, then B<preprocess_paragraph()> is invoked.\r
+\r
+The base class implementation of this method returns the given text.\r
+\r
+=cut\r
+\r
+sub preprocess_line {\r
+ my ($self, $text, $line_num) = @_;\r
+ return $text;\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<preprocess_paragraph()>\r
+\r
+ $textblock = $parser->preprocess_paragraph($text, $line_num);\r
+\r
+This method should be overridden by subclasses that wish to perform any\r
+kind of preprocessing for each block (paragraph) of POD documentation\r
+that appears in the input stream. The parameter C<$text> is the POD\r
+paragraph from the input file; and the parameter C<$line_num> is the\r
+line number for the beginning of the corresponding paragraph.\r
+\r
+The value returned should correspond to the new text to use in its\r
+place If the empty string is returned or an undefined value is\r
+returned, then the given C<$text> is ignored (not processed).\r
+\r
+This method is invoked after gathering up all the lines in a paragraph\r
+and after determining the cutting state of the paragraph,\r
+but before trying to further parse or interpret them. After\r
+B<preprocess_paragraph()> returns, the current cutting state (which\r
+is returned by C<$self-E<gt>cutting()>) is examined. If it evaluates\r
+to true then input text (including the given C<$text>) is cut (not\r
+processed) until the next POD directive is encountered.\r
+\r
+Please note that the B<preprocess_line()> method is invoked I<before>\r
+the B<preprocess_paragraph()> method. After all (possibly preprocessed)\r
+lines in a paragraph have been assembled together and either it has been\r
+determined that the paragraph is part of the POD documentation from one\r
+of the selected sections or the C<-want_nonPODs> option is true,\r
+then B<preprocess_paragraph()> is invoked.\r
+\r
+The base class implementation of this method returns the given text.\r
+\r
+=cut\r
+\r
+sub preprocess_paragraph {\r
+ my ($self, $text, $line_num) = @_;\r
+ return $text;\r
+}\r
+\r
+#############################################################################\r
+\r
+=head1 METHODS FOR PARSING AND PROCESSING\r
+\r
+B<Pod::Parser> provides several methods to process input text. These\r
+methods typically won't need to be overridden (and in some cases they\r
+can't be overridden), but subclasses may want to invoke them to exploit\r
+their functionality.\r
+\r
+=cut\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<parse_text()>\r
+\r
+ $ptree1 = $parser->parse_text($text, $line_num);\r
+ $ptree2 = $parser->parse_text({%opts}, $text, $line_num);\r
+ $ptree3 = $parser->parse_text(\%opts, $text, $line_num);\r
+\r
+This method is useful if you need to perform your own interpolation \r
+of interior sequences and can't rely upon B<interpolate> to expand\r
+them in simple bottom-up order.\r
+\r
+The parameter C<$text> is a string or block of text to be parsed\r
+for interior sequences; and the parameter C<$line_num> is the\r
+line number corresponding to the beginning of C<$text>.\r
+\r
+B<parse_text()> will parse the given text into a parse-tree of "nodes."\r
+and interior-sequences. Each "node" in the parse tree is either a\r
+text-string, or a B<Pod::InteriorSequence>. The result returned is a\r
+parse-tree of type B<Pod::ParseTree>. Please see L<Pod::InputObjects>\r
+for more information about B<Pod::InteriorSequence> and B<Pod::ParseTree>.\r
+\r
+If desired, an optional hash-ref may be specified as the first argument\r
+to customize certain aspects of the parse-tree that is created and\r
+returned. The set of recognized option keywords are:\r
+\r
+=over 3\r
+\r
+=item B<-expand_seq> =E<gt> I<code-ref>|I<method-name>\r
+\r
+Normally, the parse-tree returned by B<parse_text()> will contain an\r
+unexpanded C<Pod::InteriorSequence> object for each interior-sequence\r
+encountered. Specifying B<-expand_seq> tells B<parse_text()> to "expand"\r
+every interior-sequence it sees by invoking the referenced function\r
+(or named method of the parser object) and using the return value as the\r
+expanded result.\r
+\r
+If a subroutine reference was given, it is invoked as:\r
+\r
+ &$code_ref( $parser, $sequence )\r
+\r
+and if a method-name was given, it is invoked as:\r
+\r
+ $parser->method_name( $sequence )\r
+\r
+where C<$parser> is a reference to the parser object, and C<$sequence>\r
+is a reference to the interior-sequence object.\r
+[I<NOTE>: If the B<interior_sequence()> method is specified, then it is\r
+invoked according to the interface specified in L<"interior_sequence()">].\r
+\r
+=item B<-expand_text> =E<gt> I<code-ref>|I<method-name>\r
+\r
+Normally, the parse-tree returned by B<parse_text()> will contain a\r
+text-string for each contiguous sequence of characters outside of an\r
+interior-sequence. Specifying B<-expand_text> tells B<parse_text()> to\r
+"preprocess" every such text-string it sees by invoking the referenced\r
+function (or named method of the parser object) and using the return value\r
+as the preprocessed (or "expanded") result. [Note that if the result is\r
+an interior-sequence, then it will I<not> be expanded as specified by the\r
+B<-expand_seq> option; Any such recursive expansion needs to be handled by\r
+the specified callback routine.]\r
+\r
+If a subroutine reference was given, it is invoked as:\r
+\r
+ &$code_ref( $parser, $text, $ptree_node )\r
+\r
+and if a method-name was given, it is invoked as:\r
+\r
+ $parser->method_name( $text, $ptree_node )\r
+\r
+where C<$parser> is a reference to the parser object, C<$text> is the\r
+text-string encountered, and C<$ptree_node> is a reference to the current\r
+node in the parse-tree (usually an interior-sequence object or else the\r
+top-level node of the parse-tree).\r
+\r
+=item B<-expand_ptree> =E<gt> I<code-ref>|I<method-name>\r
+\r
+Rather than returning a C<Pod::ParseTree>, pass the parse-tree as an\r
+argument to the referenced subroutine (or named method of the parser\r
+object) and return the result instead of the parse-tree object.\r
+\r
+If a subroutine reference was given, it is invoked as:\r
+\r
+ &$code_ref( $parser, $ptree )\r
+\r
+and if a method-name was given, it is invoked as:\r
+\r
+ $parser->method_name( $ptree )\r
+\r
+where C<$parser> is a reference to the parser object, and C<$ptree>\r
+is a reference to the parse-tree object.\r
+\r
+=back\r
+\r
+=cut\r
+\r
+sub parse_text {\r
+ my $self = shift;\r
+ local $_ = '';\r
+\r
+ ## Get options and set any defaults\r
+ my %opts = (ref $_[0]) ? %{ shift() } : ();\r
+ my $expand_seq = $opts{'-expand_seq'} || undef;\r
+ my $expand_text = $opts{'-expand_text'} || undef;\r
+ my $expand_ptree = $opts{'-expand_ptree'} || undef;\r
+\r
+ my $text = shift;\r
+ my $line = shift;\r
+ my $file = $self->input_file();\r
+ my $cmd = "";\r
+\r
+ ## Convert method calls into closures, for our convenience\r
+ my $xseq_sub = $expand_seq;\r
+ my $xtext_sub = $expand_text;\r
+ my $xptree_sub = $expand_ptree;\r
+ if (defined $expand_seq and $expand_seq eq 'interior_sequence') {\r
+ ## If 'interior_sequence' is the method to use, we have to pass\r
+ ## more than just the sequence object, we also need to pass the\r
+ ## sequence name and text.\r
+ $xseq_sub = sub {\r
+ my ($sself, $iseq) = @_;\r
+ my $args = join('', $iseq->parse_tree->children);\r
+ return $sself->interior_sequence($iseq->name, $args, $iseq);\r
+ };\r
+ }\r
+ ref $xseq_sub or $xseq_sub = sub { shift()->$expand_seq(@_) };\r
+ ref $xtext_sub or $xtext_sub = sub { shift()->$expand_text(@_) };\r
+ ref $xptree_sub or $xptree_sub = sub { shift()->$expand_ptree(@_) };\r
+\r
+ ## Keep track of the "current" interior sequence, and maintain a stack\r
+ ## of "in progress" sequences.\r
+ ##\r
+ ## NOTE that we push our own "accumulator" at the very beginning of the\r
+ ## stack. It's really a parse-tree, not a sequence; but it implements\r
+ ## the methods we need so we can use it to gather-up all the sequences\r
+ ## and strings we parse. Thus, by the end of our parsing, it should be\r
+ ## the only thing left on our stack and all we have to do is return it!\r
+ ##\r
+ my $seq = Pod::ParseTree->new();\r
+ my @seq_stack = ($seq);\r
+ my ($ldelim, $rdelim) = ('', '');\r
+\r
+ ## Iterate over all sequence starts text (NOTE: split with\r
+ ## capturing parens keeps the delimiters)\r
+ $_ = $text;\r
+ my @tokens = split /([A-Z]<(?:<+(?:\r?\n|[ \t]))?)/;\r
+ while ( @tokens ) {\r
+ $_ = shift @tokens;\r
+ ## Look for the beginning of a sequence\r
+ if ( /^([A-Z])(<(?:<+(?:\r?\n|[ \t]))?)$/ ) {\r
+ ## Push a new sequence onto the stack of those "in-progress"\r
+ my $ldelim_orig;\r
+ ($cmd, $ldelim_orig) = ($1, $2);\r
+ ($ldelim = $ldelim_orig) =~ s/\s+$//;\r
+ ($rdelim = $ldelim) =~ tr/</>/;\r
+ $seq = Pod::InteriorSequence->new(\r
+ -name => $cmd,\r
+ -ldelim => $ldelim_orig, -rdelim => $rdelim,\r
+ -file => $file, -line => $line\r
+ );\r
+ (@seq_stack > 1) and $seq->nested($seq_stack[-1]);\r
+ push @seq_stack, $seq;\r
+ }\r
+ ## Look for sequence ending\r
+ elsif ( @seq_stack > 1 ) {\r
+ ## Make sure we match the right kind of closing delimiter\r
+ my ($seq_end, $post_seq) = ('', '');\r
+ if ( ($ldelim eq '<' and /\A(.*?)(>)/s)\r
+ or /\A(.*?)(\s+$rdelim)/s )\r
+ {\r
+ ## Found end-of-sequence, capture the interior and the\r
+ ## closing the delimiter, and put the rest back on the\r
+ ## token-list\r
+ $post_seq = substr($_, length($1) + length($2));\r
+ ($_, $seq_end) = ($1, $2);\r
+ (length $post_seq) and unshift @tokens, $post_seq;\r
+ }\r
+ if (length) {\r
+ ## In the middle of a sequence, append this text to it, and\r
+ ## dont forget to "expand" it if that's what the caller wanted\r
+ $seq->append($expand_text ? &$xtext_sub($self,$_,$seq) : $_);\r
+ $_ .= $seq_end;\r
+ }\r
+ if (length $seq_end) {\r
+ ## End of current sequence, record terminating delimiter\r
+ $seq->rdelim($seq_end);\r
+ ## Pop it off the stack of "in progress" sequences\r
+ pop @seq_stack;\r
+ ## Append result to its parent in current parse tree\r
+ $seq_stack[-1]->append($expand_seq ? &$xseq_sub($self,$seq)\r
+ : $seq);\r
+ ## Remember the current cmd-name and left-delimiter\r
+ if(@seq_stack > 1) {\r
+ $cmd = $seq_stack[-1]->name;\r
+ $ldelim = $seq_stack[-1]->ldelim;\r
+ $rdelim = $seq_stack[-1]->rdelim;\r
+ } else {\r
+ $cmd = $ldelim = $rdelim = '';\r
+ }\r
+ }\r
+ }\r
+ elsif (length) {\r
+ ## In the middle of a sequence, append this text to it, and\r
+ ## dont forget to "expand" it if that's what the caller wanted\r
+ $seq->append($expand_text ? &$xtext_sub($self,$_,$seq) : $_);\r
+ }\r
+ ## Keep track of line count\r
+ $line += /\n/;\r
+ ## Remember the "current" sequence\r
+ $seq = $seq_stack[-1];\r
+ }\r
+\r
+ ## Handle unterminated sequences\r
+ my $errorsub = (@seq_stack > 1) ? $self->errorsub() : undef;\r
+ while (@seq_stack > 1) {\r
+ ($cmd, $file, $line) = ($seq->name, $seq->file_line);\r
+ $ldelim = $seq->ldelim;\r
+ ($rdelim = $ldelim) =~ tr/</>/;\r
+ $rdelim =~ s/^(\S+)(\s*)$/$2$1/;\r
+ pop @seq_stack;\r
+ my $errmsg = "*** ERROR: unterminated ${cmd}${ldelim}...${rdelim}".\r
+ " at line $line in file $file\n";\r
+ (ref $errorsub) and &{$errorsub}($errmsg)\r
+ or (defined $errorsub) and $self->$errorsub($errmsg)\r
+ or carp($errmsg);\r
+ $seq_stack[-1]->append($expand_seq ? &$xseq_sub($self,$seq) : $seq);\r
+ $seq = $seq_stack[-1];\r
+ }\r
+\r
+ ## Return the resulting parse-tree\r
+ my $ptree = (pop @seq_stack)->parse_tree;\r
+ return $expand_ptree ? &$xptree_sub($self, $ptree) : $ptree;\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<interpolate()>\r
+\r
+ $textblock = $parser->interpolate($text, $line_num);\r
+\r
+This method translates all text (including any embedded interior sequences)\r
+in the given text string C<$text> and returns the interpolated result. The\r
+parameter C<$line_num> is the line number corresponding to the beginning\r
+of C<$text>.\r
+\r
+B<interpolate()> merely invokes a private method to recursively expand\r
+nested interior sequences in bottom-up order (innermost sequences are\r
+expanded first). If there is a need to expand nested sequences in\r
+some alternate order, use B<parse_text> instead.\r
+\r
+=cut\r
+\r
+sub interpolate {\r
+ my($self, $text, $line_num) = @_;\r
+ my %parse_opts = ( -expand_seq => 'interior_sequence' );\r
+ my $ptree = $self->parse_text( \%parse_opts, $text, $line_num );\r
+ return join '', $ptree->children();\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=begin __PRIVATE__\r
+\r
+=head1 B<parse_paragraph()>\r
+\r
+ $parser->parse_paragraph($text, $line_num);\r
+\r
+This method takes the text of a POD paragraph to be processed, along\r
+with its corresponding line number, and invokes the appropriate method\r
+(one of B<command()>, B<verbatim()>, or B<textblock()>).\r
+\r
+For performance reasons, this method is invoked directly without any\r
+dynamic lookup; Hence subclasses may I<not> override it!\r
+\r
+=end __PRIVATE__\r
+\r
+=cut\r
+\r
+sub parse_paragraph {\r
+ my ($self, $text, $line_num) = @_;\r
+ local *myData = $self; ## alias to avoid deref-ing overhead\r
+ local *myOpts = ($myData{_PARSEOPTS} ||= {}); ## get parse-options\r
+ local $_;\r
+\r
+ ## See if we want to preprocess nonPOD paragraphs as well as POD ones.\r
+ my $wantNonPods = $myOpts{'-want_nonPODs'};\r
+\r
+ ## Update cutting status\r
+ $myData{_CUTTING} = 0 if $text =~ /^={1,2}\S/;\r
+\r
+ ## Perform any desired preprocessing if we wanted it this early\r
+ $wantNonPods and $text = $self->preprocess_paragraph($text, $line_num);\r
+\r
+ ## Ignore up until next POD directive if we are cutting\r
+ return if $myData{_CUTTING};\r
+\r
+ ## Now we know this is block of text in a POD section!\r
+\r
+ ##-----------------------------------------------------------------\r
+ ## This is a hook (hack ;-) for Pod::Select to do its thing without\r
+ ## having to override methods, but also without Pod::Parser assuming\r
+ ## $self is an instance of Pod::Select (if the _SELECTED_SECTIONS\r
+ ## field exists then we assume there is an is_selected() method for\r
+ ## us to invoke (calling $self->can('is_selected') could verify this\r
+ ## but that is more overhead than I want to incur)\r
+ ##-----------------------------------------------------------------\r
+\r
+ ## Ignore this block if it isnt in one of the selected sections\r
+ if (exists $myData{_SELECTED_SECTIONS}) {\r
+ $self->is_selected($text) or return ($myData{_CUTTING} = 1);\r
+ }\r
+\r
+ ## If we havent already, perform any desired preprocessing and\r
+ ## then re-check the "cutting" state\r
+ unless ($wantNonPods) {\r
+ $text = $self->preprocess_paragraph($text, $line_num);\r
+ return 1 unless ((defined $text) and (length $text));\r
+ return 1 if ($myData{_CUTTING});\r
+ }\r
+\r
+ ## Look for one of the three types of paragraphs\r
+ my ($pfx, $cmd, $arg, $sep) = ('', '', '', '');\r
+ my $pod_para = undef;\r
+ if ($text =~ /^(={1,2})(?=\S)/) {\r
+ ## Looks like a command paragraph. Capture the command prefix used\r
+ ## ("=" or "=="), as well as the command-name, its paragraph text,\r
+ ## and whatever sequence of characters was used to separate them\r
+ $pfx = $1;\r
+ $_ = substr($text, length $pfx);\r
+ ($cmd, $sep, $text) = split /(\s+)/, $_, 2;\r
+ $sep = '' unless defined $sep;\r
+ $text = '' unless defined $text;\r
+ ## If this is a "cut" directive then we dont need to do anything\r
+ ## except return to "cutting" mode.\r
+ if ($cmd eq 'cut') {\r
+ $myData{_CUTTING} = 1;\r
+ return unless $myOpts{'-process_cut_cmd'};\r
+ }\r
+ }\r
+ ## Save the attributes indicating how the command was specified.\r
+ $pod_para = new Pod::Paragraph(\r
+ -name => $cmd,\r
+ -text => $text,\r
+ -prefix => $pfx,\r
+ -separator => $sep,\r
+ -file => $myData{_INFILE},\r
+ -line => $line_num\r
+ );\r
+ # ## Invoke appropriate callbacks\r
+ # if (exists $myData{_CALLBACKS}) {\r
+ # ## Look through the callback list, invoke callbacks,\r
+ # ## then see if we need to do the default actions\r
+ # ## (invoke_callbacks will return true if we do).\r
+ # return 1 unless $self->invoke_callbacks($cmd, $text, $line_num, $pod_para);\r
+ # }\r
+\r
+ # If the last paragraph ended in whitespace, and we're not between verbatim blocks, carp\r
+ if ($myData{_WHITESPACE} and $myOpts{'-warnings'}\r
+ and not ($text =~ /^\s+/ and ($myData{_PREVIOUS}||"") eq "verbatim")) {\r
+ my $errorsub = $self->errorsub();\r
+ my $line = $line_num - 1;\r
+ my $errmsg = "*** WARNING: line containing nothing but whitespace".\r
+ " in paragraph at line $line in file $myData{_INFILE}\n";\r
+ (ref $errorsub) and &{$errorsub}($errmsg)\r
+ or (defined $errorsub) and $self->$errorsub($errmsg)\r
+ or carp($errmsg);\r
+ }\r
+\r
+ if (length $cmd) {\r
+ ## A command paragraph\r
+ $self->command($cmd, $text, $line_num, $pod_para);\r
+ $myData{_PREVIOUS} = $cmd;\r
+ }\r
+ elsif ($text =~ /^\s+/) {\r
+ ## Indented text - must be a verbatim paragraph\r
+ $self->verbatim($text, $line_num, $pod_para);\r
+ $myData{_PREVIOUS} = "verbatim";\r
+ }\r
+ else {\r
+ ## Looks like an ordinary block of text\r
+ $self->textblock($text, $line_num, $pod_para);\r
+ $myData{_PREVIOUS} = "textblock";\r
+ }\r
+\r
+ # Update the whitespace for the next time around\r
+ #$myData{_WHITESPACE} = $text =~ /^[^\S\r\n]+\Z/m ? 1 : 0;\r
+ $myData{_WHITESPACE} = $text =~ /^[^\S\r\n]+\r*\Z/m ? 1 : 0;\r
+\r
+ return 1;\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<parse_from_filehandle()>\r
+\r
+ $parser->parse_from_filehandle($in_fh,$out_fh);\r
+\r
+This method takes an input filehandle (which is assumed to already be\r
+opened for reading) and reads the entire input stream looking for blocks\r
+(paragraphs) of POD documentation to be processed. If no first argument\r
+is given the default input filehandle C<STDIN> is used.\r
+\r
+The C<$in_fh> parameter may be any object that provides a B<getline()>\r
+method to retrieve a single line of input text (hence, an appropriate\r
+wrapper object could be used to parse PODs from a single string or an\r
+array of strings).\r
+\r
+Using C<$in_fh-E<gt>getline()>, input is read line-by-line and assembled\r
+into paragraphs or "blocks" (which are separated by lines containing\r
+nothing but whitespace). For each block of POD documentation\r
+encountered it will invoke a method to parse the given paragraph.\r
+\r
+If a second argument is given then it should correspond to a filehandle where\r
+output should be sent (otherwise the default output filehandle is\r
+C<STDOUT> if no output filehandle is currently in use).\r
+\r
+B<NOTE:> For performance reasons, this method caches the input stream at\r
+the top of the stack in a local variable. Any attempts by clients to\r
+change the stack contents during processing when in the midst executing\r
+of this method I<will not affect> the input stream used by the current\r
+invocation of this method.\r
+\r
+This method does I<not> usually need to be overridden by subclasses.\r
+\r
+=cut\r
+\r
+sub parse_from_filehandle {\r
+ my $self = shift;\r
+ my %opts = (ref $_[0] eq 'HASH') ? %{ shift() } : ();\r
+ my ($in_fh, $out_fh) = @_;\r
+ $in_fh = \*STDIN unless ($in_fh);\r
+ local *myData = $self; ## alias to avoid deref-ing overhead\r
+ local *myOpts = ($myData{_PARSEOPTS} ||= {}); ## get parse-options\r
+ local $_;\r
+\r
+ ## Put this stream at the top of the stack and do beginning-of-input\r
+ ## processing. NOTE that $in_fh might be reset during this process.\r
+ my $topstream = $self->_push_input_stream($in_fh, $out_fh);\r
+ (exists $opts{-cutting}) and $self->cutting( $opts{-cutting} );\r
+\r
+ ## Initialize line/paragraph\r
+ my ($textline, $paragraph) = ('', '');\r
+ my ($nlines, $plines) = (0, 0);\r
+\r
+ ## Use <$fh> instead of $fh->getline where possible (for speed)\r
+ $_ = ref $in_fh;\r
+ my $tied_fh = (/^(?:GLOB|FileHandle|IO::\w+)$/ or tied $in_fh);\r
+\r
+ ## Read paragraphs line-by-line\r
+ while (defined ($textline = $tied_fh ? <$in_fh> : $in_fh->getline)) {\r
+ $textline = $self->preprocess_line($textline, ++$nlines);\r
+ next unless ((defined $textline) && (length $textline));\r
+\r
+ if ((! length $paragraph) && ($textline =~ /^==/)) {\r
+ ## '==' denotes a one-line command paragraph\r
+ $paragraph = $textline;\r
+ $plines = 1;\r
+ $textline = '';\r
+ } else {\r
+ ## Append this line to the current paragraph\r
+ $paragraph .= $textline;\r
+ ++$plines;\r
+ }\r
+\r
+ ## See if this line is blank and ends the current paragraph.\r
+ ## If it isnt, then keep iterating until it is.\r
+ next unless (($textline =~ /^[^\S\r\n]*[\r\n]*$/)\r
+ && (length $paragraph));\r
+\r
+ ## Now process the paragraph\r
+ parse_paragraph($self, $paragraph, ($nlines - $plines) + 1);\r
+ $paragraph = '';\r
+ $plines = 0;\r
+ }\r
+ ## Dont forget about the last paragraph in the file\r
+ if (length $paragraph) {\r
+ parse_paragraph($self, $paragraph, ($nlines - $plines) + 1)\r
+ }\r
+\r
+ ## Now pop the input stream off the top of the input stack.\r
+ $self->_pop_input_stream();\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<parse_from_file()>\r
+\r
+ $parser->parse_from_file($filename,$outfile);\r
+\r
+This method takes a filename and does the following:\r
+\r
+=over 2\r
+\r
+=item *\r
+\r
+opens the input and output files for reading\r
+(creating the appropriate filehandles)\r
+\r
+=item *\r
+\r
+invokes the B<parse_from_filehandle()> method passing it the\r
+corresponding input and output filehandles.\r
+\r
+=item *\r
+\r
+closes the input and output files.\r
+\r
+=back\r
+\r
+If the special input filename "-" or "<&STDIN" is given then the STDIN\r
+filehandle is used for input (and no open or close is performed). If no\r
+input filename is specified then "-" is implied. Filehandle references,\r
+or objects that support the regular IO operations (like C<E<lt>$fhE<gt>>\r
+or C<$fh-<Egt>getline>) are also accepted; the handles must already be \r
+opened.\r
+\r
+If a second argument is given then it should be the name of the desired\r
+output file. If the special output filename "-" or ">&STDOUT" is given\r
+then the STDOUT filehandle is used for output (and no open or close is\r
+performed). If the special output filename ">&STDERR" is given then the\r
+STDERR filehandle is used for output (and no open or close is\r
+performed). If no output filehandle is currently in use and no output\r
+filename is specified, then "-" is implied.\r
+Alternatively, filehandle references or objects that support the regular\r
+IO operations (like C<print>, e.g. L<IO::String>) are also accepted;\r
+the object must already be opened.\r
+\r
+This method does I<not> usually need to be overridden by subclasses.\r
+\r
+=cut\r
+\r
+sub parse_from_file {\r
+ my $self = shift;\r
+ my %opts = (ref $_[0] eq 'HASH') ? %{ shift() } : ();\r
+ my ($infile, $outfile) = @_;\r
+ my ($in_fh, $out_fh);\r
+ if ($] < 5.006) {\r
+ ($in_fh, $out_fh) = (gensym(), gensym());\r
+ }\r
+ my ($close_input, $close_output) = (0, 0);\r
+ local *myData = $self;\r
+ local *_;\r
+\r
+ ## Is $infile a filename or a (possibly implied) filehandle\r
+ if (defined $infile && ref $infile) {\r
+ if (ref($infile) =~ /^(SCALAR|ARRAY|HASH|CODE|REF)$/) {\r
+ croak "Input from $1 reference not supported!\n";\r
+ }\r
+ ## Must be a filehandle-ref (or else assume its a ref to an object\r
+ ## that supports the common IO read operations).\r
+ $myData{_INFILE} = ${$infile};\r
+ $in_fh = $infile;\r
+ }\r
+ elsif (!defined($infile) || !length($infile) || ($infile eq '-')\r
+ || ($infile =~ /^<&(?:STDIN|0)$/i))\r
+ {\r
+ ## Not a filename, just a string implying STDIN\r
+ $infile ||= '-';\r
+ $myData{_INFILE} = '<standard input>';\r
+ $in_fh = \*STDIN;\r
+ }\r
+ else {\r
+ ## We have a filename, open it for reading\r
+ $myData{_INFILE} = $infile;\r
+ open($in_fh, "< $infile") or\r
+ croak "Can't open $infile for reading: $!\n";\r
+ $close_input = 1;\r
+ }\r
+\r
+ ## NOTE: we need to be *very* careful when "defaulting" the output\r
+ ## file. We only want to use a default if this is the beginning of\r
+ ## the entire document (but *not* if this is an included file). We\r
+ ## determine this by seeing if the input stream stack has been set-up\r
+ ## already\r
+\r
+ ## Is $outfile a filename, a (possibly implied) filehandle, maybe a ref?\r
+ if (ref $outfile) {\r
+ ## we need to check for ref() first, as other checks involve reading\r
+ if (ref($outfile) =~ /^(ARRAY|HASH|CODE)$/) {\r
+ croak "Output to $1 reference not supported!\n";\r
+ }\r
+ elsif (ref($outfile) eq 'SCALAR') {\r
+# # NOTE: IO::String isn't a part of the perl distribution,\r
+# # so probably we shouldn't support this case...\r
+# require IO::String;\r
+# $myData{_OUTFILE} = "$outfile";\r
+# $out_fh = IO::String->new($outfile);\r
+ croak "Output to SCALAR reference not supported!\n";\r
+ }\r
+ else {\r
+ ## Must be a filehandle-ref (or else assume its a ref to an\r
+ ## object that supports the common IO write operations).\r
+ $myData{_OUTFILE} = ${$outfile};\r
+ $out_fh = $outfile;\r
+ }\r
+ }\r
+ elsif (!defined($outfile) || !length($outfile) || ($outfile eq '-')\r
+ || ($outfile =~ /^>&?(?:STDOUT|1)$/i))\r
+ {\r
+ if (defined $myData{_TOP_STREAM}) {\r
+ $out_fh = $myData{_OUTPUT};\r
+ }\r
+ else {\r
+ ## Not a filename, just a string implying STDOUT\r
+ $outfile ||= '-';\r
+ $myData{_OUTFILE} = '<standard output>';\r
+ $out_fh = \*STDOUT;\r
+ }\r
+ }\r
+ elsif ($outfile =~ /^>&(STDERR|2)$/i) {\r
+ ## Not a filename, just a string implying STDERR\r
+ $myData{_OUTFILE} = '<standard error>';\r
+ $out_fh = \*STDERR;\r
+ }\r
+ else {\r
+ ## We have a filename, open it for writing\r
+ $myData{_OUTFILE} = $outfile;\r
+ (-d $outfile) and croak "$outfile is a directory, not POD input!\n";\r
+ open($out_fh, "> $outfile") or\r
+ croak "Can't open $outfile for writing: $!\n";\r
+ $close_output = 1;\r
+ }\r
+\r
+ ## Whew! That was a lot of work to set up reasonably/robust behavior\r
+ ## in the case of a non-filename for reading and writing. Now we just\r
+ ## have to parse the input and close the handles when we're finished.\r
+ $self->parse_from_filehandle(\%opts, $in_fh, $out_fh);\r
+\r
+ $close_input and\r
+ close($in_fh) || croak "Can't close $infile after reading: $!\n";\r
+ $close_output and\r
+ close($out_fh) || croak "Can't close $outfile after writing: $!\n";\r
+}\r
+\r
+#############################################################################\r
+\r
+=head1 ACCESSOR METHODS\r
+\r
+Clients of B<Pod::Parser> should use the following methods to access\r
+instance data fields:\r
+\r
+=cut\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<errorsub()>\r
+\r
+ $parser->errorsub("method_name");\r
+ $parser->errorsub(\&warn_user);\r
+ $parser->errorsub(sub { print STDERR, @_ });\r
+\r
+Specifies the method or subroutine to use when printing error messages\r
+about POD syntax. The supplied method/subroutine I<must> return TRUE upon\r
+successful printing of the message. If C<undef> is given, then the B<carp>\r
+builtin is used to issue error messages (this is the default behavior).\r
+\r
+ my $errorsub = $parser->errorsub()\r
+ my $errmsg = "This is an error message!\n"\r
+ (ref $errorsub) and &{$errorsub}($errmsg)\r
+ or (defined $errorsub) and $parser->$errorsub($errmsg)\r
+ or carp($errmsg);\r
+\r
+Returns a method name, or else a reference to the user-supplied subroutine\r
+used to print error messages. Returns C<undef> if the B<carp> builtin\r
+is used to issue error messages (this is the default behavior).\r
+\r
+=cut\r
+\r
+sub errorsub {\r
+ return (@_ > 1) ? ($_[0]->{_ERRORSUB} = $_[1]) : $_[0]->{_ERRORSUB};\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<cutting()>\r
+\r
+ $boolean = $parser->cutting();\r
+\r
+Returns the current C<cutting> state: a boolean-valued scalar which\r
+evaluates to true if text from the input file is currently being "cut"\r
+(meaning it is I<not> considered part of the POD document).\r
+\r
+ $parser->cutting($boolean);\r
+\r
+Sets the current C<cutting> state to the given value and returns the\r
+result.\r
+\r
+=cut\r
+\r
+sub cutting {\r
+ return (@_ > 1) ? ($_[0]->{_CUTTING} = $_[1]) : $_[0]->{_CUTTING};\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<parseopts()>\r
+\r
+When invoked with no additional arguments, B<parseopts> returns a hashtable\r
+of all the current parsing options.\r
+\r
+ ## See if we are parsing non-POD sections as well as POD ones\r
+ my %opts = $parser->parseopts();\r
+ $opts{'-want_nonPODs}' and print "-want_nonPODs\n";\r
+\r
+When invoked using a single string, B<parseopts> treats the string as the\r
+name of a parse-option and returns its corresponding value if it exists\r
+(returns C<undef> if it doesn't).\r
+\r
+ ## Did we ask to see '=cut' paragraphs?\r
+ my $want_cut = $parser->parseopts('-process_cut_cmd');\r
+ $want_cut and print "-process_cut_cmd\n";\r
+\r
+When invoked with multiple arguments, B<parseopts> treats them as\r
+key/value pairs and the specified parse-option names are set to the\r
+given values. Any unspecified parse-options are unaffected.\r
+\r
+ ## Set them back to the default\r
+ $parser->parseopts(-warnings => 0);\r
+\r
+When passed a single hash-ref, B<parseopts> uses that hash to completely\r
+reset the existing parse-options, all previous parse-option values\r
+are lost.\r
+\r
+ ## Reset all options to default \r
+ $parser->parseopts( { } );\r
+\r
+See L<"PARSING OPTIONS"> for more information on the name and meaning of each\r
+parse-option currently recognized.\r
+\r
+=cut\r
+\r
+sub parseopts {\r
+ local *myData = shift;\r
+ local *myOpts = ($myData{_PARSEOPTS} ||= {});\r
+ return %myOpts if (@_ == 0);\r
+ if (@_ == 1) {\r
+ local $_ = shift;\r
+ return ref($_) ? $myData{_PARSEOPTS} = $_ : $myOpts{$_};\r
+ }\r
+ my @newOpts = (%myOpts, @_);\r
+ $myData{_PARSEOPTS} = { @newOpts };\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<output_file()>\r
+\r
+ $fname = $parser->output_file();\r
+\r
+Returns the name of the output file being written.\r
+\r
+=cut\r
+\r
+sub output_file {\r
+ return $_[0]->{_OUTFILE};\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<output_handle()>\r
+\r
+ $fhandle = $parser->output_handle();\r
+\r
+Returns the output filehandle object.\r
+\r
+=cut\r
+\r
+sub output_handle {\r
+ return $_[0]->{_OUTPUT};\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<input_file()>\r
+\r
+ $fname = $parser->input_file();\r
+\r
+Returns the name of the input file being read.\r
+\r
+=cut\r
+\r
+sub input_file {\r
+ return $_[0]->{_INFILE};\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=head1 B<input_handle()>\r
+\r
+ $fhandle = $parser->input_handle();\r
+\r
+Returns the current input filehandle object.\r
+\r
+=cut\r
+\r
+sub input_handle {\r
+ return $_[0]->{_INPUT};\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=begin __PRIVATE__\r
+\r
+=head1 B<input_streams()>\r
+\r
+ $listref = $parser->input_streams();\r
+\r
+Returns a reference to an array which corresponds to the stack of all\r
+the input streams that are currently in the middle of being parsed.\r
+\r
+While parsing an input stream, it is possible to invoke\r
+B<parse_from_file()> or B<parse_from_filehandle()> to parse a new input\r
+stream and then return to parsing the previous input stream. Each input\r
+stream to be parsed is pushed onto the end of this input stack\r
+before any of its input is read. The input stream that is currently\r
+being parsed is always at the end (or top) of the input stack. When an\r
+input stream has been exhausted, it is popped off the end of the\r
+input stack.\r
+\r
+Each element on this input stack is a reference to C<Pod::InputSource>\r
+object. Please see L<Pod::InputObjects> for more details.\r
+\r
+This method might be invoked when printing diagnostic messages, for example,\r
+to obtain the name and line number of the all input files that are currently\r
+being processed.\r
+\r
+=end __PRIVATE__\r
+\r
+=cut\r
+\r
+sub input_streams {\r
+ return $_[0]->{_INPUT_STREAMS};\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=begin __PRIVATE__\r
+\r
+=head1 B<top_stream()>\r
+\r
+ $hashref = $parser->top_stream();\r
+\r
+Returns a reference to the hash-table that represents the element\r
+that is currently at the top (end) of the input stream stack\r
+(see L<"input_streams()">). The return value will be the C<undef>\r
+if the input stack is empty.\r
+\r
+This method might be used when printing diagnostic messages, for example,\r
+to obtain the name and line number of the current input file.\r
+\r
+=end __PRIVATE__\r
+\r
+=cut\r
+\r
+sub top_stream {\r
+ return $_[0]->{_TOP_STREAM} || undef;\r
+}\r
+\r
+#############################################################################\r
+\r
+=head1 PRIVATE METHODS AND DATA\r
+\r
+B<Pod::Parser> makes use of several internal methods and data fields\r
+which clients should not need to see or use. For the sake of avoiding\r
+name collisions for client data and methods, these methods and fields\r
+are briefly discussed here. Determined hackers may obtain further\r
+information about them by reading the B<Pod::Parser> source code.\r
+\r
+Private data fields are stored in the hash-object whose reference is\r
+returned by the B<new()> constructor for this class. The names of all\r
+private methods and data-fields used by B<Pod::Parser> begin with a\r
+prefix of "_" and match the regular expression C</^_\w+$/>.\r
+\r
+=cut\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=begin _PRIVATE_\r
+\r
+=head1 B<_push_input_stream()>\r
+\r
+ $hashref = $parser->_push_input_stream($in_fh,$out_fh);\r
+\r
+This method will push the given input stream on the input stack and\r
+perform any necessary beginning-of-document or beginning-of-file\r
+processing. The argument C<$in_fh> is the input stream filehandle to\r
+push, and C<$out_fh> is the corresponding output filehandle to use (if\r
+it is not given or is undefined, then the current output stream is used,\r
+which defaults to standard output if it doesnt exist yet).\r
+\r
+The value returned will be reference to the hash-table that represents\r
+the new top of the input stream stack. I<Please Note> that it is\r
+possible for this method to use default values for the input and output\r
+file handles. If this happens, you will need to look at the C<INPUT>\r
+and C<OUTPUT> instance data members to determine their new values.\r
+\r
+=end _PRIVATE_\r
+\r
+=cut\r
+\r
+sub _push_input_stream {\r
+ my ($self, $in_fh, $out_fh) = @_;\r
+ local *myData = $self;\r
+\r
+ ## Initialize stuff for the entire document if this is *not*\r
+ ## an included file.\r
+ ##\r
+ ## NOTE: we need to be *very* careful when "defaulting" the output\r
+ ## filehandle. We only want to use a default value if this is the\r
+ ## beginning of the entire document (but *not* if this is an included\r
+ ## file).\r
+ unless (defined $myData{_TOP_STREAM}) {\r
+ $out_fh = \*STDOUT unless (defined $out_fh);\r
+ $myData{_CUTTING} = 1; ## current "cutting" state\r
+ $myData{_INPUT_STREAMS} = []; ## stack of all input streams\r
+ }\r
+\r
+ ## Initialize input indicators\r
+ $myData{_OUTFILE} = '(unknown)' unless (defined $myData{_OUTFILE});\r
+ $myData{_OUTPUT} = $out_fh if (defined $out_fh);\r
+ $in_fh = \*STDIN unless (defined $in_fh);\r
+ $myData{_INFILE} = '(unknown)' unless (defined $myData{_INFILE});\r
+ $myData{_INPUT} = $in_fh;\r
+ my $input_top = $myData{_TOP_STREAM}\r
+ = new Pod::InputSource(\r
+ -name => $myData{_INFILE},\r
+ -handle => $in_fh,\r
+ -was_cutting => $myData{_CUTTING}\r
+ );\r
+ local *input_stack = $myData{_INPUT_STREAMS};\r
+ push(@input_stack, $input_top);\r
+\r
+ ## Perform beginning-of-document and/or beginning-of-input processing\r
+ $self->begin_pod() if (@input_stack == 1);\r
+ $self->begin_input();\r
+\r
+ return $input_top;\r
+}\r
+\r
+##---------------------------------------------------------------------------\r
+\r
+=begin _PRIVATE_\r
+\r
+=head1 B<_pop_input_stream()>\r
+\r
+ $hashref = $parser->_pop_input_stream();\r
+\r
+This takes no arguments. It will perform any necessary end-of-file or\r
+end-of-document processing and then pop the current input stream from\r
+the top of the input stack.\r
+\r
+The value returned will be reference to the hash-table that represents\r
+the new top of the input stream stack.\r
+\r
+=end _PRIVATE_\r
+\r
+=cut\r
+\r
+sub _pop_input_stream {\r
+ my ($self) = @_;\r
+ local *myData = $self;\r
+ local *input_stack = $myData{_INPUT_STREAMS};\r
+\r
+ ## Perform end-of-input and/or end-of-document processing\r
+ $self->end_input() if (@input_stack > 0);\r
+ $self->end_pod() if (@input_stack == 1);\r
+\r
+ ## Restore cutting state to whatever it was before we started\r
+ ## parsing this file.\r
+ my $old_top = pop(@input_stack);\r
+ $myData{_CUTTING} = $old_top->was_cutting();\r
+\r
+ ## Dont forget to reset the input indicators\r
+ my $input_top = undef;\r
+ if (@input_stack > 0) {\r
+ $input_top = $myData{_TOP_STREAM} = $input_stack[-1];\r
+ $myData{_INFILE} = $input_top->name();\r
+ $myData{_INPUT} = $input_top->handle();\r
+ } else {\r
+ delete $myData{_TOP_STREAM};\r
+ delete $myData{_INPUT_STREAMS};\r
+ }\r
+\r
+ return $input_top;\r
+}\r
+\r
+#############################################################################\r
+\r
+=head1 TREE-BASED PARSING\r
+\r
+If straightforward stream-based parsing wont meet your needs (as is\r
+likely the case for tasks such as translating PODs into structured\r
+markup languages like HTML and XML) then you may need to take the\r
+tree-based approach. Rather than doing everything in one pass and\r
+calling the B<interpolate()> method to expand sequences into text, it\r
+may be desirable to instead create a parse-tree using the B<parse_text()>\r
+method to return a tree-like structure which may contain an ordered\r
+list of children (each of which may be a text-string, or a similar\r
+tree-like structure).\r
+\r
+Pay special attention to L<"METHODS FOR PARSING AND PROCESSING"> and\r
+to the objects described in L<Pod::InputObjects>. The former describes\r
+the gory details and parameters for how to customize and extend the\r
+parsing behavior of B<Pod::Parser>. B<Pod::InputObjects> provides\r
+several objects that may all be used interchangeably as parse-trees. The\r
+most obvious one is the B<Pod::ParseTree> object. It defines the basic\r
+interface and functionality that all things trying to be a POD parse-tree\r
+should do. A B<Pod::ParseTree> is defined such that each "node" may be a\r
+text-string, or a reference to another parse-tree. Each B<Pod::Paragraph>\r
+object and each B<Pod::InteriorSequence> object also supports the basic\r
+parse-tree interface.\r
+\r
+The B<parse_text()> method takes a given paragraph of text, and\r
+returns a parse-tree that contains one or more children, each of which\r
+may be a text-string, or an InteriorSequence object. There are also\r
+callback-options that may be passed to B<parse_text()> to customize\r
+the way it expands or transforms interior-sequences, as well as the\r
+returned result. These callbacks can be used to create a parse-tree\r
+with custom-made objects (which may or may not support the parse-tree\r
+interface, depending on how you choose to do it).\r
+\r
+If you wish to turn an entire POD document into a parse-tree, that process\r
+is fairly straightforward. The B<parse_text()> method is the key to doing\r
+this successfully. Every paragraph-callback (i.e. the polymorphic methods\r
+for B<command()>, B<verbatim()>, and B<textblock()> paragraphs) takes\r
+a B<Pod::Paragraph> object as an argument. Each paragraph object has a\r
+B<parse_tree()> method that can be used to get or set a corresponding\r
+parse-tree. So for each of those paragraph-callback methods, simply call\r
+B<parse_text()> with the options you desire, and then use the returned\r
+parse-tree to assign to the given paragraph object.\r
+\r
+That gives you a parse-tree for each paragraph - so now all you need is\r
+an ordered list of paragraphs. You can maintain that yourself as a data\r
+element in the object/hash. The most straightforward way would be simply\r
+to use an array-ref, with the desired set of custom "options" for each\r
+invocation of B<parse_text>. Let's assume the desired option-set is\r
+given by the hash C<%options>. Then we might do something like the\r
+following:\r
+\r
+ package MyPodParserTree;\r
+\r
+ @ISA = qw( Pod::Parser );\r
+\r
+ ...\r
+\r
+ sub begin_pod {\r
+ my $self = shift;\r
+ $self->{'-paragraphs'} = []; ## initialize paragraph list\r
+ }\r
+\r
+ sub command { \r
+ my ($parser, $command, $paragraph, $line_num, $pod_para) = @_;\r
+ my $ptree = $parser->parse_text({%options}, $paragraph, ...);\r
+ $pod_para->parse_tree( $ptree );\r
+ push @{ $self->{'-paragraphs'} }, $pod_para;\r
+ }\r
+\r
+ sub verbatim { \r
+ my ($parser, $paragraph, $line_num, $pod_para) = @_;\r
+ push @{ $self->{'-paragraphs'} }, $pod_para;\r
+ }\r
+\r
+ sub textblock { \r
+ my ($parser, $paragraph, $line_num, $pod_para) = @_;\r
+ my $ptree = $parser->parse_text({%options}, $paragraph, ...);\r
+ $pod_para->parse_tree( $ptree );\r
+ push @{ $self->{'-paragraphs'} }, $pod_para;\r
+ }\r
+\r
+ ...\r
+\r
+ package main;\r
+ ...\r
+ my $parser = new MyPodParserTree(...);\r
+ $parser->parse_from_file(...);\r
+ my $paragraphs_ref = $parser->{'-paragraphs'};\r
+\r
+Of course, in this module-author's humble opinion, I'd be more inclined to\r
+use the existing B<Pod::ParseTree> object than a simple array. That way\r
+everything in it, paragraphs and sequences, all respond to the same core\r
+interface for all parse-tree nodes. The result would look something like:\r
+\r
+ package MyPodParserTree2;\r
+\r
+ ...\r
+\r
+ sub begin_pod {\r
+ my $self = shift;\r
+ $self->{'-ptree'} = new Pod::ParseTree; ## initialize parse-tree\r
+ }\r
+\r
+ sub parse_tree {\r
+ ## convenience method to get/set the parse-tree for the entire POD\r
+ (@_ > 1) and $_[0]->{'-ptree'} = $_[1];\r
+ return $_[0]->{'-ptree'};\r
+ }\r
+\r
+ sub command { \r
+ my ($parser, $command, $paragraph, $line_num, $pod_para) = @_;\r
+ my $ptree = $parser->parse_text({<<options>>}, $paragraph, ...);\r
+ $pod_para->parse_tree( $ptree );\r
+ $parser->parse_tree()->append( $pod_para );\r
+ }\r
+\r
+ sub verbatim { \r
+ my ($parser, $paragraph, $line_num, $pod_para) = @_;\r
+ $parser->parse_tree()->append( $pod_para );\r
+ }\r
+\r
+ sub textblock { \r
+ my ($parser, $paragraph, $line_num, $pod_para) = @_;\r
+ my $ptree = $parser->parse_text({<<options>>}, $paragraph, ...);\r
+ $pod_para->parse_tree( $ptree );\r
+ $parser->parse_tree()->append( $pod_para );\r
+ }\r
+\r
+ ...\r
+\r
+ package main;\r
+ ...\r
+ my $parser = new MyPodParserTree2(...);\r
+ $parser->parse_from_file(...);\r
+ my $ptree = $parser->parse_tree;\r
+ ...\r
+\r
+Now you have the entire POD document as one great big parse-tree. You\r
+can even use the B<-expand_seq> option to B<parse_text> to insert\r
+whole different kinds of objects. Just don't expect B<Pod::Parser>\r
+to know what to do with them after that. That will need to be in your\r
+code. Or, alternatively, you can insert any object you like so long as\r
+it conforms to the B<Pod::ParseTree> interface.\r
+\r
+One could use this to create subclasses of B<Pod::Paragraphs> and\r
+B<Pod::InteriorSequences> for specific commands (or to create your own\r
+custom node-types in the parse-tree) and add some kind of B<emit()>\r
+method to each custom node/subclass object in the tree. Then all you'd\r
+need to do is recursively walk the tree in the desired order, processing\r
+the children (most likely from left to right) by formatting them if\r
+they are text-strings, or by calling their B<emit()> method if they\r
+are objects/references.\r
+\r
+=head1 CAVEATS\r
+\r
+Please note that POD has the notion of "paragraphs": this is something\r
+starting I<after> a blank (read: empty) line, with the single exception\r
+of the file start, which is also starting a paragraph. That means that\r
+especially a command (e.g. C<=head1>) I<must> be preceded with a blank\r
+line; C<__END__> is I<not> a blank line.\r
+\r
+=head1 SEE ALSO\r
+\r
+L<Pod::InputObjects>, L<Pod::Select>\r
+\r
+B<Pod::InputObjects> defines POD input objects corresponding to\r
+command paragraphs, parse-trees, and interior-sequences.\r
+\r
+B<Pod::Select> is a subclass of B<Pod::Parser> which provides the ability\r
+to selectively include and/or exclude sections of a POD document from being\r
+translated based upon the current heading, subheading, subsubheading, etc.\r
+\r
+=for __PRIVATE__\r
+B<Pod::Callbacks> is a subclass of B<Pod::Parser> which gives its users\r
+the ability the employ I<callback functions> instead of, or in addition\r
+to, overriding methods of the base class.\r
+\r
+=for __PRIVATE__\r
+B<Pod::Select> and B<Pod::Callbacks> do not override any\r
+methods nor do they define any new methods with the same name. Because\r
+of this, they may I<both> be used (in combination) as a base class of\r
+the same subclass in order to combine their functionality without\r
+causing any namespace clashes due to multiple inheritance.\r
+\r
+=head1 AUTHOR\r
+\r
+Please report bugs using L<http://rt.cpan.org>.\r
+\r
+Brad Appleton E<lt>bradapp@enteract.comE<gt>\r
+\r
+Based on code for B<Pod::Text> written by\r
+Tom Christiansen E<lt>tchrist@mox.perl.comE<gt>\r
+\r
+=head1 LICENSE\r
+\r
+Pod-Parser is free software; you can redistribute it and/or modify it\r
+under the terms of the Artistic License distributed with Perl version\r
+5.000 or (at your option) any later version. Please refer to the\r
+Artistic License that came with your Perl distribution for more\r
+details. If your version of Perl was not distributed under the\r
+terms of the Artistic License, than you may distribute PodParser\r
+under the same terms as Perl itself.\r
+\r
+=cut\r
+\r
+1;\r
+# vim: ts=4 sw=4 et\r