xref: /openbsd-src/gnu/usr.bin/perl/cpan/Pod-Simple/lib/Pod/Simple/XMLOutStream.pm (revision 3d61058aa5c692477b6d18acfbbdb653a9930ff9)
1b39c5158Smillertpackage Pod::Simple::XMLOutStream;
2b39c5158Smillertuse strict;
3*3d61058aSafresh1use warnings;
4b39c5158Smillertuse Carp ();
5b39c5158Smillertuse Pod::Simple ();
6*3d61058aSafresh1our $VERSION = '3.45';
7b39c5158SmillertBEGIN {
8*3d61058aSafresh1  our @ISA = ('Pod::Simple');
9b39c5158Smillert  *DEBUG = \&Pod::Simple::DEBUG unless defined &DEBUG;
10b39c5158Smillert}
11b39c5158Smillert
12*3d61058aSafresh1our $ATTR_PAD;
13b39c5158Smillert$ATTR_PAD = "\n" unless defined $ATTR_PAD;
14b39c5158Smillert # Don't mess with this unless you know what you're doing.
15b39c5158Smillert
16*3d61058aSafresh1our $SORT_ATTRS;
17b39c5158Smillert$SORT_ATTRS = 0 unless defined $SORT_ATTRS;
18b39c5158Smillert
19b39c5158Smillertsub new {
20b39c5158Smillert  my $self = shift;
21b39c5158Smillert  my $new = $self->SUPER::new(@_);
22b39c5158Smillert  $new->{'output_fh'} ||= *STDOUT{IO};
2391f110e0Safresh1  $new->keep_encoding_directive(1);
24b39c5158Smillert  #$new->accept_codes('VerbatimFormatted');
25b39c5158Smillert  return $new;
26b39c5158Smillert}
27b39c5158Smillert
28b39c5158Smillert#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29b39c5158Smillert
30b39c5158Smillertsub _handle_element_start {
31b39c5158Smillert  # ($self, $element_name, $attr_hash_r)
32b39c5158Smillert  my $fh = $_[0]{'output_fh'};
33b39c5158Smillert  my($key, $value);
34b8851fccSafresh1  DEBUG and print STDERR "++ $_[1]\n";
35b39c5158Smillert  print $fh "<", $_[1];
36b39c5158Smillert  if($SORT_ATTRS) {
37b39c5158Smillert    foreach my $key (sort keys %{$_[2]}) {
38b39c5158Smillert      unless($key =~ m/^~/s) {
39b39c5158Smillert        next if $key eq 'start_line' and $_[0]{'hide_line_numbers'};
40b39c5158Smillert        _xml_escape($value = $_[2]{$key});
41b39c5158Smillert        print $fh $ATTR_PAD, $key, '="', $value, '"';
42b39c5158Smillert      }
43b39c5158Smillert    }
44b39c5158Smillert  } else { # faster
45b39c5158Smillert    while(($key,$value) = each %{$_[2]}) {
46b39c5158Smillert      unless($key =~ m/^~/s) {
47b39c5158Smillert        next if $key eq 'start_line' and $_[0]{'hide_line_numbers'};
48b39c5158Smillert        _xml_escape($value);
49b39c5158Smillert        print $fh $ATTR_PAD, $key, '="', $value, '"';
50b39c5158Smillert      }
51b39c5158Smillert    }
52b39c5158Smillert  }
53b39c5158Smillert  print $fh ">";
54b39c5158Smillert  return;
55b39c5158Smillert}
56b39c5158Smillert
57b39c5158Smillertsub _handle_text {
58b8851fccSafresh1  DEBUG and print STDERR "== \"$_[1]\"\n";
59b39c5158Smillert  if(length $_[1]) {
60b39c5158Smillert    my $text = $_[1];
61b39c5158Smillert    _xml_escape($text);
62b39c5158Smillert    print {$_[0]{'output_fh'}} $text;
63b39c5158Smillert  }
64b39c5158Smillert  return;
65b39c5158Smillert}
66b39c5158Smillert
67b39c5158Smillertsub _handle_element_end {
68b8851fccSafresh1  DEBUG and print STDERR "-- $_[1]\n";
69b39c5158Smillert  print {$_[0]{'output_fh'}} "</", $_[1], ">";
70b39c5158Smillert  return;
71b39c5158Smillert}
72b39c5158Smillert
73b39c5158Smillert# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
74b39c5158Smillert#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
75b39c5158Smillert
76b39c5158Smillertsub _xml_escape {
77b39c5158Smillert  foreach my $x (@_) {
78b39c5158Smillert    # Escape things very cautiously:
79b8851fccSafresh1    if ($] ge 5.007_003) {
80b8851fccSafresh1      $x =~ s/([^-\n\t !\#\$\%\(\)\*\+,\.\~\/\:\;=\?\@\[\\\]\^_\`\{\|\}abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789])/'&#'.(utf8::native_to_unicode(ord($1))).';'/eg;
81b8851fccSafresh1    } else { # Is broken for non-ASCII platforms on early perls
82b39c5158Smillert      $x =~ s/([^-\n\t !\#\$\%\(\)\*\+,\.\~\/\:\;=\?\@\[\\\]\^_\`\{\|\}abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789])/'&#'.(ord($1)).';'/eg;
83b8851fccSafresh1    }
84b39c5158Smillert    # Yes, stipulate the list without a range, so that this can work right on
85b39c5158Smillert    #  all charsets that this module happens to run under.
86b39c5158Smillert  }
87b39c5158Smillert  return;
88b39c5158Smillert}
89b39c5158Smillert
90b39c5158Smillert#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
91b39c5158Smillert1;
92b39c5158Smillert
93b39c5158Smillert__END__
94b39c5158Smillert
95b39c5158Smillert=head1 NAME
96b39c5158Smillert
97b39c5158SmillertPod::Simple::XMLOutStream -- turn Pod into XML
98b39c5158Smillert
99b39c5158Smillert=head1 SYNOPSIS
100b39c5158Smillert
101b39c5158Smillert  perl -MPod::Simple::XMLOutStream -e \
102b39c5158Smillert   "exit Pod::Simple::XMLOutStream->filter(shift)->any_errata_seen" \
103b39c5158Smillert   thingy.pod
104b39c5158Smillert
105b39c5158Smillert=head1 DESCRIPTION
106b39c5158Smillert
107b39c5158SmillertPod::Simple::XMLOutStream is a subclass of L<Pod::Simple> that parses
108b39c5158SmillertPod and turns it into XML.
109b39c5158Smillert
110b39c5158SmillertPod::Simple::XMLOutStream inherits methods from
111b39c5158SmillertL<Pod::Simple>.
112b39c5158Smillert
113b39c5158Smillert
114b39c5158Smillert=head1 SEE ALSO
115b39c5158Smillert
116b39c5158SmillertL<Pod::Simple::DumpAsXML> is rather like this class; see its
117b39c5158Smillertdocumentation for a discussion of the differences.
118b39c5158Smillert
119b39c5158SmillertL<Pod::Simple>, L<Pod::Simple::DumpAsXML>, L<Pod::SAX>
120b39c5158Smillert
121b39c5158SmillertL<Pod::Simple::Subclassing>
122b39c5158Smillert
123b39c5158SmillertThe older (and possibly obsolete) libraries L<Pod::PXML>, L<Pod::XML>
124b39c5158Smillert
125b39c5158Smillert
126b39c5158Smillert=head1 ABOUT EXTENDING POD
127b39c5158Smillert
128b39c5158SmillertTODO: An example or two of =extend, then point to Pod::Simple::Subclassing
129b39c5158Smillert
130b39c5158Smillert=head1 SEE ALSO
131b39c5158Smillert
132b39c5158SmillertL<Pod::Simple>, L<Pod::Simple::Text>, L<Pod::Spell>
133b39c5158Smillert
134b39c5158Smillert=head1 SUPPORT
135b39c5158Smillert
136b39c5158SmillertQuestions or discussion about POD and Pod::Simple should be sent to the
137b39c5158Smillertpod-people@perl.org mail list. Send an empty email to
138b39c5158Smillertpod-people-subscribe@perl.org to subscribe.
139b39c5158Smillert
140b39c5158SmillertThis module is managed in an open GitHub repository,
141b8851fccSafresh1L<https://github.com/perl-pod/pod-simple/>. Feel free to fork and contribute, or
142*3d61058aSafresh1to clone L<https://github.com/perl-pod/pod-simple.git> and send patches!
143b39c5158Smillert
144b39c5158SmillertPatches against Pod::Simple are welcome. Please send bug reports to
145b39c5158Smillert<bug-pod-simple@rt.cpan.org>.
146b39c5158Smillert
147b39c5158Smillert=head1 COPYRIGHT AND DISCLAIMERS
148b39c5158Smillert
149b39c5158SmillertCopyright (c) 2002-2004 Sean M. Burke.
150b39c5158Smillert
151b39c5158SmillertThis library is free software; you can redistribute it and/or modify it
152b39c5158Smillertunder the same terms as Perl itself.
153b39c5158Smillert
154b39c5158SmillertThis program is distributed in the hope that it will be useful, but
155b39c5158Smillertwithout any warranty; without even the implied warranty of
156b39c5158Smillertmerchantability or fitness for a particular purpose.
157b39c5158Smillert
158b39c5158Smillert=head1 AUTHOR
159b39c5158Smillert
160b39c5158SmillertPod::Simple was created by Sean M. Burke <sburke@cpan.org>.
161b39c5158SmillertBut don't bother him, he's retired.
162b39c5158Smillert
163b39c5158SmillertPod::Simple is maintained by:
164b39c5158Smillert
165b39c5158Smillert=over
166b39c5158Smillert
167b39c5158Smillert=item * Allison Randal C<allison@perl.org>
168b39c5158Smillert
169b39c5158Smillert=item * Hans Dieter Pearcey C<hdp@cpan.org>
170b39c5158Smillert
171b39c5158Smillert=item * David E. Wheeler C<dwheeler@cpan.org>
172b39c5158Smillert
173b39c5158Smillert=back
174b39c5158Smillert
175b39c5158Smillert=cut
176