1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
|
package Pod::Abstract;
use strict;
use warnings;
use Pod::Abstract::Node;
use Pod::Abstract::Path;
use Pod::Abstract::Parser;
use IO::String;
our $VERSION = '0.26';
=head1 NAME
Pod::Abstract - Abstract document tree for Perl POD documents
=head1 SYNOPSIS
use Pod::Abstract;
use Pod::Abstract::BuildNode qw(node);
# Get all the first level headings, and put them in a verbatim block
# at the start of the document
my $pa = Pod::Abstract->load_filehandle(\*STDIN);
my @headings = $pa->select('/head1@heading');
my @headings_text = map { $_->pod } @headings;
my $headings_node = node->verbatim(join "\n",@headings_text);
$pa->unshift( node->cut );
$pa->unshift( $headings_node );
$pa->unshift( node->pod );
print $pa->pod;
=head1 DESCRIPTION
C<Pod::Abstract> provides a means to load a POD document without direct
reference to it's syntax, and perform manipulations on the abstract
syntax tree.
This can be used to support additional features for POD, to format
output, to compile into alternative formats, etc.
POD documents are not a natural tree, but do have a logical nesting
structure. C<Pod::Abstract> makes this explicit - C<=head*> commands
create nested sections, =over and =back create nested lists, etc.
The "paf summary" command provides easy visualisation of the created
tree.
=head2 USAGE
C<Pod::Abstract> allows easy manipulation and traversal of POD or Perl
files containing POD, without having to manually do any string
manipulation.
It allows you to easily write formatters, filters, test scripts, etc
for POD.
C<Pod::Abstract> is based on the standard L<Pod::Parser> module.
=head2 PROCESSING MODEL
C<Pod::Abstract> allows documents to be loaded, decorated, and
manupulated in multiple steps. It can also make generating a POD
formatter very simple. You can easily add features to an existing POD
formatter, since any POD abstract object can be written out as a POD
document.
Rather than write or fork a whole translator, a single inline
"decorator" can be added.
The C<paf> utility provides a good starting point, which also allows
you to hook in to an existing filter/transform library. Add a
C<Pod::Abstract::Filter> class to the namespace and it should start
working as a C<paf> command.
=head2 EXAMPLE
Suppose you are frustrated by the verbose list syntax used by regular
POD. You might reasonably want to define a simplified list format for
your own use, except POD formatters won't support it.
With Pod::Abstract you can write an inline filter to convert:
* item 1
* item 2
* item 3
into:
=over
=item *
item 1
=item *
item 2
=item *
item 3
=back
This transformation can be performed on the document tree. If your
formatter does not use Pod::Abstract, you can pipe out POD and use a
regular formatter. If your formatter supports Pod::Abstract, you can
feed in the syntax tree without having to re-serialise and parse the
document.
The source document is still valid Pod, you aren't breaking
compatibility with regular perldoc just by making Pod::Abstract
transformations.
=head2 POD SUPPORT
C<Pod::Abstract> supports all POD rules defined in perlpodspec.
=head1 COMPONENTS
Pod::Abstract is comprised of:
=over
=item *
The parser, which loads a document tree.
e.g:
my $pa = Pod::Abstract->load_filehandle(\*STDIN);
=item *
The document tree, returned from the parser. The root node (C<$pa>
above) represents the whole document. Calling B<< ->pod >> on the root node
will give you back your original document.
Note the document includes C<#cut> nodes, which are generally the Perl
code - the parts that aren't POD. These will be included in the output
of B<< ->pod >> unless you remove them, so you can modify a Perl module
as a POD document in POD abstract, and it will work the same
afterwards.
e.g
my $pod_text = $pa->pod; # $pod_text is reserialized from the tree.
See L<Pod::Abstract::Node>
=item *
L<Pod::Abstract::Path>, a node selection language. Called via C<<
$node->select(PATH_EXP) >>. Pod paths are a powerful feature allowing
declarative traversal of a document.
For example -
"Find all head2s under METHODS"
/head1[@heading=~{^METHODS$}]/head2
"Find all bold text anywhere"
//B
=item *
The node builder, L<Pod::Abstract::BuildNode>. This exports methods to
allow adding content to POD documents.
You can also combine documents -
use Pod::Abstract::BuildNode qw(node nodes);
# ...
my @nodes = nodes->from_pod($pod);
Where C<$pod> is a text with POD formatting.
=back
=head2 Using paths
The easiest way to traverse a C<$pa> tree is to use the C<select> method on the
nodes, and paths.
C<select> will accept and expression and return an array of
L<Pod::Abstract::Node>. These nodes also support the select method - for example:
my @headings = $pa->select('/head1'); # Get all heading 1
my @X = $headings[0]->select('//:X'); # Get all X (index) sequences inside that heading
my @indices = map { $_->text } @X; # Map out the contents of those as plain text.
You can combine path expressions with other methods, for example - C<children>
will give all the child nodes of a POD node, C<next>, C<previous>, C<parent> and
C<root> allow traversal from a given node.
From any node you can then call C<select> to make a declarative traversal from
there. The above methods also have comparable expressions in
L<Pod::Abstract::Path>.
=head2 Traversing for document generation
To traverse the tree for document generation, you can follow C<children> from
the first node, then examine each node type to determine what you should
generate.
The nodes will generate in a tree, so headings have nested children with
subheadings and texts. In most cases the C<body> method will give the text (or
POD nodes) next to the command, while the C<children> method will give the
contained POD.
Special types are C<:paragraph>, C<:text>, <#cut>. Interior sequences are also
started with a : for their type, like C<:L>, C<:B>, C<:I> for Link, Bold,
Italic.
Use the C<< $node->ptree >> method to see a visualised tree of a parsed document.
=head1 METHODS
=cut
=head2 load_file
my $pa = Pod::Abstract->load_file( FILENAME );
Read the POD document in the named file. Returns the root node of the
document.
=cut
sub load_file {
my $class = shift;
my $filename = shift;
my $p = Pod::Abstract::Parser->new;
$p->parse_from_file($filename);
$p->root->coalesce_body(":verbatim");
$p->root->coalesce_body(":text");
# Remove any blank verbatim nodes.
$_->detach foreach $p->root->select('//:verbatim[ . =~ {^[\s]*$}]');
return $p->root;
}
=head2 load_filehandle
my $pa = Pod::Abstract->load_file( FH );
Load a POD document from the provided filehandle reference. Returns
the root node of the document.
=cut
sub load_filehandle {
my $class = shift;
my $fh = shift;
my $p = Pod::Abstract::Parser->new;
$p->parse_from_filehandle($fh);
$p->root->coalesce_body(":verbatim");
$p->root->coalesce_body(":text");
# Remove blank verbatim nodes.
$_->detach foreach $p->root->select('//:verbatim[ . =~ {^[\s]*$}]');
return $p->root;
}
=head2 load_string
my $pa = Pod::Abstract->load_string( STRING );
Loads a POD document from a scalar string value. Returns the root node
of the document.
=cut
sub load_string {
my $class = shift;
my $str = shift;
my $fh = IO::String->new($str);
return $class->load_filehandle($fh);
}
=head1 AUTHOR
Ben Lilburne <bnej80@gmail.com>
=head1 COPYRIGHT AND LICENSE
Copyright (C) 2009-2025 Ben Lilburne
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
1;
|