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