File Coverage

blib/lib/Pod/Simple/DumpAsText.pm
Criterion Covered Total %
statement 44 49 89.8
branch 4 8 50.0
condition 4 9 44.4
subroutine 10 10 100.0
pod 1 1 100.0
total 63 77 81.8


line stmt bran cond sub pod time code
1             package Pod::Simple::DumpAsText;
2 2     2   1875 use strict;
  2         5  
  2         103  
3             our $VERSION = '3.47';
4 2     2   11 use Pod::Simple ();
  2         3  
  2         86  
5 2     2   87 BEGIN { our @ISA = ('Pod::Simple')}
6              
7 2     2   11 use Carp ();
  2         3  
  2         66  
8              
9 2 50   2   1546 BEGIN { *DEBUG = \&Pod::Simple::DEBUG unless defined &DEBUG }
10              
11             sub new {
12 2     2 1 4 my $self = shift;
13 2         8 my $new = $self->SUPER::new(@_);
14 2   50     9 $new->{'output_fh'} ||= *STDOUT{IO};
15 2         8 $new->accept_codes('VerbatimFormatted');
16 2         4 $new->keep_encoding_directive(1);
17 2         3 return $new;
18             }
19              
20             #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
21              
22             sub _handle_element_start {
23             # ($self, $element_name, $attr_hash_r)
24 2     2   4 my $fh = $_[0]{'output_fh'};
25 2         3 my($key, $value);
26 2         3 DEBUG and print STDERR "++ $_[1]\n";
27              
28 2   100     12 print $fh ' ' x ($_[0]{'indent'} || 0), "++", $_[1], "\n";
29 2         4 $_[0]{'indent'}++;
30 2         3 while(($key,$value) = each %{$_[2]}) {
  4         10  
31 2 50       5 unless($key =~ m/^~/s) {
32 2 50 33     8 next if $key eq 'start_line' and $_[0]{'hide_line_numbers'};
33 0         0 _perly_escape($key);
34 0         0 _perly_escape($value);
35             printf $fh qq{%s \\ "%s" => "%s"\n},
36 0   0     0 ' ' x ($_[0]{'indent'} || 0), $key, $value;
37             }
38             }
39 2         4 return;
40             }
41              
42             sub _handle_text {
43 1     1   2 DEBUG and print STDERR "== \"$_[1]\"\n";
44              
45 1 50       3 if(length $_[1]) {
46 1         3 my $indent = ' ' x $_[0]{'indent'};
47 1         2 my $text = $_[1];
48 1         3 _perly_escape($text);
49 1         2 $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 1         1 print {$_[0]{'output_fh'}} $indent, '* "', $text, "\"\n";
  1         3  
59             }
60 1         2 return;
61             }
62              
63             sub _handle_element_end {
64 2     2   2 DEBUG and print STDERR "-- $_[1]\n";
65 2         6 print {$_[0]{'output_fh'}}
66 2         3 ' ' x --$_[0]{'indent'}, "--", $_[1], "\n";
67 2         3 return;
68             }
69              
70             # . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
71              
72             sub _perly_escape {
73 1     1   2 foreach my $x (@_) {
74 1         2 $x =~ s/([^\x00-\xFF])/sprintf'\x{%X}',ord($1)/eg;
  0         0  
75             # Escape things very cautiously:
76 1         3 $x =~ s/([^-\n\t \&\<\>\'!\#\%\(\)\*\+,\.\/\:\;=\?\~\[\]\^_\`\{\|\}abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789])/sprintf'\x%02X',ord($1)/eg;
  0         0  
77             }
78 1         1 return;
79             }
80              
81             #@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
82             1;
83              
84              
85             __END__