File Coverage

blib/lib/Pod/Simple/JustPod.pm
Criterion Covered Total %
statement 121 127 95.2
branch 26 28 92.8
condition 2 3 66.6
subroutine 37 38 97.3
pod 1 28 3.5
total 187 224 83.4


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