File Coverage

blib/lib/Pod/Simple/JustPod.pm
Criterion Covered Total %
statement 123 128 96.0
branch 26 28 92.8
condition 2 3 66.6
subroutine 38 38 100.0
pod 1 27 3.7
total 190 224 84.8


line stmt bran cond sub pod time code
1 4     4   131753 use 5;
  4         31  
2             package Pod::Simple::JustPod;
3             # ABSTRACT: Pod::Simple formatter that extracts POD from a file containing
4             # other things as well
5 4     4   24 use strict;
  4         8  
  4         96  
6 4     4   19 use warnings;
  4         14  
  4         154  
7              
8 4     4   1209 use Pod::Simple::Methody ();
  4         10  
  4         6846  
9             our @ISA = ('Pod::Simple::Methody');
10              
11             sub new {
12 21     21 1 16309 my $self = shift;
13 21         108 my $new = $self->SUPER::new(@_);
14              
15 21         76 $new->accept_targets('*');
16 21         67 $new->keep_encoding_directive(1);
17 21         75 $new->preserve_whitespace(1);
18 21         58 $new->complain_stderr(1);
19 21         62 $new->_output_is_for_JustPod(1);
20              
21 21         43 return $new;
22             }
23              
24             #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25              
26             sub check_that_all_is_closed {
27              
28             # Actually checks that the things we depend on being balanced in fact are,
29             # so that we can continue in spit of pod errors
30              
31 1394     1394 0 1644 my $self = shift;
32 1394         2375 while ($self->{inL}) {
33 0         0 $self->end_L(@_);
34             }
35 1394   66     2580 while ($self->{fcode_end} && @{$self->{fcode_end}}) {
  1259         3082  
36 0         0 $self->_end_fcode(@_);
37             }
38             }
39              
40             sub handle_text {
41              
42             # Add text to the output buffer. This is skipped if within a L<>, as we use
43             # the 'raw' attribute of that tag instead.
44              
45 4565 100   4565 0 11658 $_[0]{buffer} .= $_[1] unless $_[0]{inL} ;
46             }
47              
48             sub spacer {
49              
50             # Prints the white space following things like =head1. This is normally a
51             # blank, unless BlackBox has told us otherwise.
52              
53 407     407 0 587 my ($self, $arg) = @_;
54 407 100       704 return unless $arg;
55              
56             my $spacer = ($arg->{'~orig_spacer'})
57 386 100       698 ? $arg->{'~orig_spacer'}
58             : " ";
59 386         574 $self->handle_text($spacer);
60             }
61              
62             sub _generic_start {
63              
64             # Called from tags like =head1, etc.
65              
66 144     144   271 my ($self, $text, $arg) = @_;
67 144         305 $self->check_that_all_is_closed();
68 144         305 $self->handle_text($text);
69 144         246 $self->spacer($arg);
70             }
71              
72 21     21 0 59 sub start_Document { shift->_generic_start("=pod\n\n"); }
73 59     59 0 152 sub start_head1 { shift->_generic_start('=head1', @_); }
74 52     52 0 136 sub start_head2 { shift->_generic_start('=head2', @_); }
75 2     2 0 7 sub start_head3 { shift->_generic_start('=head3', @_); }
76 2     2 0 7 sub start_head4 { shift->_generic_start('=head4', @_); }
77 2     2 0 6 sub start_head5 { shift->_generic_start('=head5', @_); }
78 2     2 0 7 sub start_head6 { shift->_generic_start('=head6', @_); }
79 4     4 0 13 sub start_encoding { shift->_generic_start('=encoding', @_); }
80             # sub start_Para
81             # sub start_Verbatim
82              
83             sub start_item_bullet { # Handle =item *
84 35     35 0 65 my ($self, $arg) = @_;
85 35         80 $self->check_that_all_is_closed();
86 35         83 $self->handle_text('=item');
87              
88             # It can be that they said simply '=item', and it is inferred that it is to
89             # be a bullet.
90 35 100       63 if (! $arg->{'~orig_content'}) {
91 2         5 $self->handle_text("\n\n");
92             }
93             else {
94 33         74 $self->spacer($arg);
95 33 100       60 if ($arg->{'~_freaky_para_hack'}) {
96              
97             # See Message Id <87y3gtcwa2.fsf@hope.eyrie.org>
98 23         36 my $item_text = $arg->{'~orig_content'};
99 23         38 my $trailing = quotemeta $arg->{'~_freaky_para_hack'};
100 23         335 $item_text =~ s/$trailing$//;
101 23         60 $self->handle_text($item_text);
102             }
103             else {
104 10         16 $self->handle_text("*\n\n");
105             }
106             }
107             }
108              
109             sub start_item_number { # Handle '=item 2'
110 4     4 0 9 my ($self, $arg) = @_;
111 4         16 $self->check_that_all_is_closed();
112 4         13 $self->handle_text("=item");
113 4         12 $self->spacer($arg);
114 4         12 $self->handle_text("$arg->{'~orig_content'}\n\n");
115             }
116              
117             sub start_item_text { # Handle '=item foo bar baz'
118 197     197 0 335 my ($self, $arg) = @_;
119 197         392 $self->check_that_all_is_closed();
120 197         445 $self->handle_text('=item');
121 197         331 $self->spacer($arg);
122             }
123              
124             sub _end_item {
125 236     236   342 my $self = shift;
126 236         410 $self->check_that_all_is_closed();
127 236         448 $self->emit;
128             }
129              
130             *end_item_bullet = *_end_item;
131             *end_item_number = *_end_item;
132             *end_item_text = *_end_item;
133              
134             sub _start_over { # Handle =over
135 32     32   68 my ($self, $arg) = @_;
136 32         92 $self->check_that_all_is_closed();
137 32         85 $self->handle_text("=over");
138              
139             # The =over amount is optional
140 32 100       72 if ($arg->{'~orig_content'}) {
141 29         75 $self->spacer($arg);
142 29         75 $self->handle_text("$arg->{'~orig_content'}");
143             }
144 32         74 $self->handle_text("\n\n");
145             }
146              
147             *start_over_bullet = *_start_over;
148             *start_over_number = *_start_over;
149             *start_over_text = *_start_over;
150             *start_over_block = *_start_over;
151              
152             sub _end_over {
153 32     32   52 my $self = shift;
154 32         96 $self->check_that_all_is_closed();
155 32         123 $self->handle_text('=back');
156 32         98 $self->emit;
157             }
158              
159             *end_over_bullet = *_end_over;
160             *end_over_number = *_end_over;
161             *end_over_text = *_end_over;
162             *end_over_block = *_end_over;
163              
164             sub end_Document {
165 21     21 0 36 my $self = shift;
166 21         50 $self->emit; # Make sure buffer gets flushed
167 21         27 print {$self->{'output_fh'} } "=cut\n"
  21         63  
168             }
169              
170             sub _end_generic {
171 714     714   1035 my $self = shift;
172 714         1429 $self->check_that_all_is_closed();
173 714         1317 $self->emit;
174             }
175              
176             *end_head1 = *_end_generic;
177             *end_head2 = *_end_generic;
178             *end_head3 = *_end_generic;
179             *end_head4 = *_end_generic;
180             *end_head5 = *_end_generic;
181             *end_head6 = *_end_generic;
182             *end_encoding = *_end_generic;
183             *end_Para = *_end_generic;
184             *end_Verbatim = *_end_generic;
185              
186             sub _start_fcode {
187 676     676   1041 my ($type, $self, $flags) = @_;
188              
189             # How many brackets is set by BlackBox unless the count is 1
190             my $bracket_count = (exists $flags->{'~bracket_count'})
191 676 100       1165 ? $flags->{'~bracket_count'}
192             : 1;
193 676         1633 $self->handle_text($type . ( "<" x $bracket_count));
194              
195 676         953 my $rspacer = "";
196 676 100       1123 if ($bracket_count > 1) {
197             my $lspacer = (exists $flags->{'~lspacer'})
198 18 100       65 ? $flags->{'~lspacer'}
199             : " ";
200 18         37 $self->handle_text($lspacer);
201              
202             $rspacer = (exists $flags->{'~rspacer'})
203 18 100       49 ? $flags->{'~rspacer'}
204             : " ";
205             }
206              
207             # BlackBox doesn't output things for for the ending code callbacks, so save
208             # what we need.
209 676         754 push @{$self->{'fcode_end'}}, [ $bracket_count, $rspacer ];
  676         1654  
210             }
211              
212 60     60 0 120 sub start_B { _start_fcode('B', @_); }
213 359     359 0 619 sub start_C { _start_fcode('C', @_); }
214 22     22 0 53 sub start_E { _start_fcode('E', @_); }
215 53     53 0 98 sub start_F { _start_fcode('F', @_); }
216 85     85 0 154 sub start_I { _start_fcode('I', @_); }
217 3     3 0 11 sub start_S { _start_fcode('S', @_); }
218 1     1 0 6 sub start_X { _start_fcode('X', @_); }
219 4     4 0 16 sub start_Z { _start_fcode('Z', @_); }
220              
221             sub _end_fcode {
222 676     676   879 my $self = shift;
223 676         693 my $fcode_end = pop @{$self->{'fcode_end'}};
  676         932  
224 676         860 my $bracket_count = 1;
225 676         766 my $rspacer = "";
226              
227 676 50       930 if (! defined $fcode_end) { # If BlackBox is working, this shouldn't
228             # happen, but verify
229 0         0 $self->whine($self->{line_count}, "Extra '>'");
230             }
231             else {
232 676         864 $bracket_count = $fcode_end->[0];
233 676         823 $rspacer = $fcode_end->[1];
234             }
235              
236 676 100       1040 $self->handle_text($rspacer) if $bracket_count > 1;
237 676         1220 $self->handle_text(">" x $bracket_count);
238             }
239              
240             *end_B = *_end_fcode;
241             *end_C = *_end_fcode;
242             *end_E = *_end_fcode;
243             *end_F = *_end_fcode;
244             *end_I = *_end_fcode;
245             *end_S = *_end_fcode;
246             *end_X = *_end_fcode;
247             *end_Z = *_end_fcode;
248              
249             sub start_L {
250 89     89 0 203 _start_fcode('L', @_);
251 89         200 $_[0]->handle_text($_[1]->{raw});
252 89         149 $_[0]->{inL}++
253             }
254              
255             sub end_L {
256 89     89 0 124 my $self = shift;
257 89         125 $self->{inL}--;
258 89 50       173 if ($self->{inL} < 0) { # If BlackBox is working, this shouldn't
259             # happen, but verify
260 0         0 $self->whine($self->{line_count}, "Extra '>' ending L<>");
261 0         0 $self->{inL} = 0;
262             }
263              
264 89         168 $self->_end_fcode(@_);
265             }
266              
267             sub emit {
268 1003     1003 0 1234 my $self = shift;
269              
270 1003 100       1744 if ($self->{buffer} ne "") {
271 982         1154 print { $self->{'output_fh'} } "",$self->{buffer} ,"\n\n";
  982         3082  
272              
273 982         1606 $self->{buffer} = "";
274             }
275              
276 1003         1731 return;
277             }
278              
279             1;
280              
281             __END__