xref: /openbsd-src/gnu/usr.bin/perl/cpan/Pod-Simple/lib/Pod/Simple/DumpAsXML.pm (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1
2require 5;
3package Pod::Simple::DumpAsXML;
4$VERSION = '3.28';
5use Pod::Simple ();
6BEGIN {@ISA = ('Pod::Simple')}
7
8use strict;
9
10use Carp ();
11use Text::Wrap qw(wrap);
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');
20  $new->keep_encoding_directive(1);
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);
30  DEBUG and print "++ $_[1]\n";
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 {
49  DEBUG and print "== \"$_[1]\"\n";
50  if(length $_[1]) {
51    my $indent = '  ' x $_[0]{'indent'};
52    my $text = $_[1];
53    _xml_escape($text);
54    local $Text::Wrap::huge = 'overflow';
55    $text = wrap('', $indent, $text);
56    print {$_[0]{'output_fh'}} $indent, $text, "\n";
57  }
58  return;
59}
60
61sub _handle_element_end {
62  DEBUG and print "-- $_[1]\n";
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:
73    $x =~ s/([^-\n\t !\#\$\%\(\)\*\+,\.\~\/\:\;=\?\@\[\\\]\^_\`\{\|\}abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789])/'&#'.(ord($1)).';'/eg;
74    # Yes, stipulate the list without a range, so that this can work right on
75    #  all charsets that this module happens to run under.
76    # Altho, hmm, what about that ord?  Presumably that won't work right
77    #  under non-ASCII charsets.  Something should be done about that.
78  }
79  return;
80}
81
82#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
831;
84
85__END__
86
87=head1 NAME
88
89Pod::Simple::DumpAsXML -- turn Pod into XML
90
91=head1 SYNOPSIS
92
93  perl -MPod::Simple::DumpAsXML -e \
94   "exit Pod::Simple::DumpAsXML->filter(shift)->any_errata_seen" \
95   thingy.pod
96
97=head1 DESCRIPTION
98
99Pod::Simple::DumpAsXML is a subclass of L<Pod::Simple> that parses Pod
100and turns it into indented and wrapped XML.  This class is of
101interest to people writing Pod formatters based on Pod::Simple.
102
103Pod::Simple::DumpAsXML inherits methods from
104L<Pod::Simple>.
105
106
107=head1 SEE ALSO
108
109L<Pod::Simple::XMLOutStream> is rather like this class.
110Pod::Simple::XMLOutStream's output is space-padded in a way
111that's better for sending to an XML processor (that is, it has
112no ignorable whitespace). But
113Pod::Simple::DumpAsXML's output is much more human-readable, being
114(more-or-less) one token per line, with line-wrapping.
115
116L<Pod::Simple::DumpAsText> is rather like this class,
117except that it doesn't dump with XML syntax.  Try them and see
118which one you like best!
119
120L<Pod::Simple>, L<Pod::Simple::DumpAsXML>
121
122The older libraries L<Pod::PXML>, L<Pod::XML>, L<Pod::SAX>
123
124=head1 SUPPORT
125
126Questions or discussion about POD and Pod::Simple should be sent to the
127pod-people@perl.org mail list. Send an empty email to
128pod-people-subscribe@perl.org to subscribe.
129
130This module is managed in an open GitHub repository,
131L<https://github.com/theory/pod-simple/>. Feel free to fork and contribute, or
132to clone L<git://github.com/theory/pod-simple.git> and send patches!
133
134Patches against Pod::Simple are welcome. Please send bug reports to
135<bug-pod-simple@rt.cpan.org>.
136
137=head1 COPYRIGHT AND DISCLAIMERS
138
139Copyright (c) 2002 Sean M. Burke.
140
141This library is free software; you can redistribute it and/or modify it
142under the same terms as Perl itself.
143
144This program is distributed in the hope that it will be useful, but
145without any warranty; without even the implied warranty of
146merchantability or fitness for a particular purpose.
147
148=head1 AUTHOR
149
150Pod::Simple was created by Sean M. Burke <sburke@cpan.org>.
151But don't bother him, he's retired.
152
153Pod::Simple is maintained by:
154
155=over
156
157=item * Allison Randal C<allison@perl.org>
158
159=item * Hans Dieter Pearcey C<hdp@cpan.org>
160
161=item * David E. Wheeler C<dwheeler@cpan.org>
162
163=back
164
165=cut
166