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