xref: /openbsd-src/gnu/usr.bin/perl/cpan/Pod-Simple/lib/Pod/Simple/DumpAsText.pm (revision 3d61058aa5c692477b6d18acfbbdb653a9930ff9)
1package Pod::Simple::DumpAsText;
2use strict;
3our $VERSION = '3.45';
4use Pod::Simple ();
5BEGIN { our @ISA = ('Pod::Simple')}
6
7use Carp ();
8
9BEGIN { *DEBUG = \&Pod::Simple::DEBUG unless defined &DEBUG }
10
11sub new {
12  my $self = shift;
13  my $new = $self->SUPER::new(@_);
14  $new->{'output_fh'} ||= *STDOUT{IO};
15  $new->accept_codes('VerbatimFormatted');
16  $new->keep_encoding_directive(1);
17  return $new;
18}
19
20#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
21
22sub _handle_element_start {
23  # ($self, $element_name, $attr_hash_r)
24  my $fh = $_[0]{'output_fh'};
25  my($key, $value);
26  DEBUG and print STDERR "++ $_[1]\n";
27
28  print $fh   '  ' x ($_[0]{'indent'} || 0),  "++", $_[1], "\n";
29  $_[0]{'indent'}++;
30  while(($key,$value) = each %{$_[2]}) {
31    unless($key =~ m/^~/s) {
32      next if $key eq 'start_line' and $_[0]{'hide_line_numbers'};
33      _perly_escape($key);
34      _perly_escape($value);
35      printf $fh qq{%s \\ "%s" => "%s"\n},
36        '  ' x ($_[0]{'indent'} || 0), $key, $value;
37    }
38  }
39  return;
40}
41
42sub _handle_text {
43  DEBUG and print STDERR "== \"$_[1]\"\n";
44
45  if(length $_[1]) {
46    my $indent = '  ' x $_[0]{'indent'};
47    my $text = $_[1];
48    _perly_escape($text);
49    $text =~  # A not-totally-brilliant wrapping algorithm:
50      s/(
51         [^\n]{55}         # Snare some characters from a line
52         [^\n\ ]{0,50}     #  and finish any current word
53        )
54        \ {1,10}(?!\n)     # capture some spaces not at line-end
55       /$1"\n$indent . "/gx     # => line-break here
56    ;
57
58    print {$_[0]{'output_fh'}} $indent, '* "', $text, "\"\n";
59  }
60  return;
61}
62
63sub _handle_element_end {
64  DEBUG and print STDERR "-- $_[1]\n";
65  print {$_[0]{'output_fh'}}
66   '  ' x --$_[0]{'indent'}, "--", $_[1], "\n";
67  return;
68}
69
70# . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
71
72sub _perly_escape {
73  foreach my $x (@_) {
74    $x =~ s/([^\x00-\xFF])/sprintf'\x{%X}',ord($1)/eg;
75    # Escape things very cautiously:
76    $x =~ s/([^-\n\t \&\<\>\'!\#\%\(\)\*\+,\.\/\:\;=\?\~\[\]\^_\`\{\|\}abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789])/sprintf'\x%02X',ord($1)/eg;
77  }
78  return;
79}
80
81#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
821;
83
84
85__END__
86
87=head1 NAME
88
89Pod::Simple::DumpAsText -- dump Pod-parsing events as text
90
91=head1 SYNOPSIS
92
93  perl -MPod::Simple::DumpAsText -e \
94   "exit Pod::Simple::DumpAsText->filter(shift)->any_errata_seen" \
95   thingy.pod
96
97=head1 DESCRIPTION
98
99This class is for dumping, as text, the events gotten from parsing a Pod
100document.  This class is of interest to people writing Pod formatters
101based on Pod::Simple. It is useful for seeing exactly what events you
102get out of some Pod that you feed in.
103
104This is a subclass of L<Pod::Simple> and inherits all its methods.
105
106=head1 SEE ALSO
107
108L<Pod::Simple::DumpAsXML>
109
110L<Pod::Simple>
111
112=head1 SUPPORT
113
114Questions or discussion about POD and Pod::Simple should be sent to the
115pod-people@perl.org mail list. Send an empty email to
116pod-people-subscribe@perl.org to subscribe.
117
118This module is managed in an open GitHub repository,
119L<https://github.com/perl-pod/pod-simple/>. Feel free to fork and contribute, or
120to clone L<https://github.com/perl-pod/pod-simple.git> and send patches!
121
122Patches against Pod::Simple are welcome. Please send bug reports to
123<bug-pod-simple@rt.cpan.org>.
124
125=head1 COPYRIGHT AND DISCLAIMERS
126
127Copyright (c) 2002 Sean M. Burke.
128
129This library is free software; you can redistribute it and/or modify it
130under the same terms as Perl itself.
131
132This program is distributed in the hope that it will be useful, but
133without any warranty; without even the implied warranty of
134merchantability or fitness for a particular purpose.
135
136=head1 AUTHOR
137
138Pod::Simple was created by Sean M. Burke <sburke@cpan.org>.
139But don't bother him, he's retired.
140
141Pod::Simple is maintained by:
142
143=over
144
145=item * Allison Randal C<allison@perl.org>
146
147=item * Hans Dieter Pearcey C<hdp@cpan.org>
148
149=item * David E. Wheeler C<dwheeler@cpan.org>
150
151=back
152
153=cut
154use warnings;
155