This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update Pod::Simple from version 3.38 to 3.39
[perl5.git] / cpan / Pod-Simple / lib / Pod / Simple / DumpAsXML.pm
CommitLineData
351625bd
SP
1
2require 5;
3package Pod::Simple::DumpAsXML;
f2667158 4$VERSION = '3.39';
351625bd
SP
5use Pod::Simple ();
6BEGIN {@ISA = ('Pod::Simple')}
7
8use strict;
9
10use Carp ();
0ace302a 11use Text::Wrap qw(wrap);
351625bd
SP
12
13BEGIN { *DEBUG = \&Pod::Simple::DEBUG unless defined &DEBUG }
14
15sub new {
16 my $self = shift;
17 my $new = $self->SUPER::new(@_);
18 $new->{'output_fh'} ||= *STDOUT{IO};
19 $new->accept_codes('VerbatimFormatted');
b5ae6e74 20 $new->keep_encoding_directive(1);
351625bd
SP
21 return $new;
22}
23
24#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
25
26sub _handle_element_start {
27 # ($self, $element_name, $attr_hash_r)
28 my $fh = $_[0]{'output_fh'};
29 my($key, $value);
a4a12576 30 DEBUG and print STDERR "++ $_[1]\n";
351625bd
SP
31
32 print $fh ' ' x ($_[0]{'indent'} || 0), "<", $_[1];
33
34 foreach my $key (sort keys %{$_[2]}) {
35 unless($key =~ m/^~/s) {
36 next if $key eq 'start_line' and $_[0]{'hide_line_numbers'};
37 _xml_escape($value = $_[2]{$key});
38 print $fh ' ', $key, '="', $value, '"';
39 }
40 }
41
42
43 print $fh ">\n";
44 $_[0]{'indent'}++;
45 return;
46}
47
48sub _handle_text {
a4a12576 49 DEBUG and print STDERR "== \"$_[1]\"\n";
351625bd
SP
50 if(length $_[1]) {
51 my $indent = ' ' x $_[0]{'indent'};
52 my $text = $_[1];
53 _xml_escape($text);
0ace302a
SH
54 local $Text::Wrap::huge = 'overflow';
55 $text = wrap('', $indent, $text);
351625bd
SP
56 print {$_[0]{'output_fh'}} $indent, $text, "\n";
57 }
58 return;
59}
60
61sub _handle_element_end {
a4a12576 62 DEBUG and print STDERR "-- $_[1]\n";
351625bd
SP
63 print {$_[0]{'output_fh'}}
64 ' ' x --$_[0]{'indent'}, "</", $_[1], ">\n";
65 return;
66}
67
68# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
69
70sub _xml_escape {
71 foreach my $x (@_) {
72 # Escape things very cautiously:
08d70c44
CBW
73 if ($] ge 5.007_003) {
74 $x =~ s/([^-\n\t !\#\$\%\(\)\*\+,\.\~\/\:\;=\?\@\[\\\]\^_\`\{\|\}abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789])/'&#'.(utf8::native_to_unicode(ord($1))).';'/eg;
75 } else { # Is broken for non-ASCII platforms on early perls
76 $x =~ s/([^-\n\t !\#\$\%\(\)\*\+,\.\~\/\:\;=\?\@\[\\\]\^_\`\{\|\}abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789])/'&#'.(ord($1)).';'/eg;
77 }
351625bd
SP
78 # Yes, stipulate the list without a range, so that this can work right on
79 # all charsets that this module happens to run under.
351625bd
SP
80 }
81 return;
82}
83
84#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
851;
86
87__END__
88
89=head1 NAME
90
91Pod::Simple::DumpAsXML -- turn Pod into XML
92
93=head1 SYNOPSIS
94
95 perl -MPod::Simple::DumpAsXML -e \
96 "exit Pod::Simple::DumpAsXML->filter(shift)->any_errata_seen" \
97 thingy.pod
98
99=head1 DESCRIPTION
100
101Pod::Simple::DumpAsXML is a subclass of L<Pod::Simple> that parses Pod
102and turns it into indented and wrapped XML. This class is of
103interest to people writing Pod formatters based on Pod::Simple.
104
105Pod::Simple::DumpAsXML inherits methods from
106L<Pod::Simple>.
107
108
109=head1 SEE ALSO
110
111L<Pod::Simple::XMLOutStream> is rather like this class.
112Pod::Simple::XMLOutStream's output is space-padded in a way
113that's better for sending to an XML processor (that is, it has
c9989a74 114no ignorable whitespace). But
351625bd
SP
115Pod::Simple::DumpAsXML's output is much more human-readable, being
116(more-or-less) one token per line, with line-wrapping.
117
118L<Pod::Simple::DumpAsText> is rather like this class,
119except that it doesn't dump with XML syntax. Try them and see
120which one you like best!
121
122L<Pod::Simple>, L<Pod::Simple::DumpAsXML>
123
124The older libraries L<Pod::PXML>, L<Pod::XML>, L<Pod::SAX>
125
a242eeb4
JV
126=head1 SUPPORT
127
128Questions or discussion about POD and Pod::Simple should be sent to the
129pod-people@perl.org mail list. Send an empty email to
130pod-people-subscribe@perl.org to subscribe.
131
132This module is managed in an open GitHub repository,
a4a12576
CBW
133L<https://github.com/perl-pod/pod-simple/>. Feel free to fork and contribute, or
134to clone L<git://github.com/perl-pod/pod-simple.git> and send patches!
a242eeb4
JV
135
136Patches against Pod::Simple are welcome. Please send bug reports to
137<bug-pod-simple@rt.cpan.org>.
351625bd
SP
138
139=head1 COPYRIGHT AND DISCLAIMERS
140
433cf6b4 141Copyright (c) 2002 Sean M. Burke.
351625bd
SP
142
143This library is free software; you can redistribute it and/or modify it
144under the same terms as Perl itself.
145
146This program is distributed in the hope that it will be useful, but
147without any warranty; without even the implied warranty of
148merchantability or fitness for a particular purpose.
149
150=head1 AUTHOR
151
a242eeb4
JV
152Pod::Simple was created by Sean M. Burke <sburke@cpan.org>.
153But don't bother him, he's retired.
351625bd 154
a242eeb4
JV
155Pod::Simple is maintained by:
156
157=over
351625bd 158
a242eeb4
JV
159=item * Allison Randal C<allison@perl.org>
160
161=item * Hans Dieter Pearcey C<hdp@cpan.org>
162
163=item * David E. Wheeler C<dwheeler@cpan.org>
164
165=back
166
167=cut