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