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