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   753707 use strict;
  5         12  
  5         186  
5 5     5   80 use warnings;
  5         11  
  5         296  
6              
7 5     5   2130 use Pod::Simple::Methody ();
  5         20  
  5         10328  
8             our @ISA = ('Pod::Simple::Methody');
9              
10             sub new {
11 22     22 1 753264 my $self = shift;
12 22         177 my $new = $self->SUPER::new(@_);
13              
14 22         131 $new->accept_targets('*');
15 22         97 $new->keep_encoding_directive(1);
16 22         86 $new->preserve_whitespace(1);
17 22         88 $new->complain_stderr(1);
18 22         100 $new->_output_is_for_JustPod(1);
19              
20 22         59 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 2224 my $self = shift;
31 1398         3449 while ($self->{inL}) {
32 0         0 $self->end_L(@_);
33             }
34 1398   66     7831 while ($self->{fcode_end} && @{$self->{fcode_end}}) {
  1259         4437  
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 16844 $_[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 805 my ($self, $arg) = @_;
53 409 100       881 return unless $arg;
54              
55             my $spacer = ($arg->{'~orig_spacer'})
56 387 100       924 ? $arg->{'~orig_spacer'}
57             : " ";
58 387         794 $self->handle_text($spacer);
59             }
60              
61             sub _generic_start {
62              
63             # Called from tags like =head1, etc.
64              
65 146     146   390 my ($self, $text, $arg) = @_;
66 146         459 $self->check_that_all_is_closed();
67 146         436 $self->handle_text($text);
68 146         355 $self->spacer($arg);
69             }
70              
71 22     22 0 62 sub start_Document { shift->_generic_start("=pod\n\n"); }
72 59     59 0 227 sub start_head1 { shift->_generic_start('=head1', @_); }
73 53     53 0 161 sub start_head2 { shift->_generic_start('=head2', @_); }
74 2     2 0 8 sub start_head3 { shift->_generic_start('=head3', @_); }
75 2     2 0 8 sub start_head4 { shift->_generic_start('=head4', @_); }
76 2     2 0 7 sub start_head5 { shift->_generic_start('=head5', @_); }
77 2     2 0 8 sub start_head6 { shift->_generic_start('=head6', @_); }
78 4     4 0 16 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 110 my ($self, $arg) = @_;
84 35         121 $self->check_that_all_is_closed();
85 35         123 $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       101 if (! $arg->{'~orig_content'}) {
90 2         6 $self->handle_text("\n\n");
91             }
92             else {
93 33         93 $self->spacer($arg);
94 33 100       72 if ($arg->{'~_freaky_para_hack'}) {
95              
96             # See Message Id <87y3gtcwa2.fsf@hope.eyrie.org>
97 23         35 my $item_text = $arg->{'~orig_content'};
98 23         50 my $trailing = quotemeta $arg->{'~_freaky_para_hack'};
99 23         564 $item_text =~ s/$trailing$//;
100 23         110 $self->handle_text($item_text);
101             }
102             else {
103 10         26 $self->handle_text("*\n\n");
104             }
105             }
106             }
107              
108             sub start_item_number { # Handle '=item 2'
109 4     4 0 10 my ($self, $arg) = @_;
110 4         28 $self->check_that_all_is_closed();
111 4         14 $self->handle_text("=item");
112 4         14 $self->spacer($arg);
113 4         14 $self->handle_text("$arg->{'~orig_content'}\n\n");
114             }
115              
116             sub start_item_text { # Handle '=item foo bar baz'
117 197     197 0 447 my ($self, $arg) = @_;
118 197         569 $self->check_that_all_is_closed();
119 197         642 $self->handle_text('=item');
120 197         536 $self->spacer($arg);
121             }
122              
123             sub _end_item {
124 236     236   459 my $self = shift;
125 236         685 $self->check_that_all_is_closed();
126 236         612 $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   74 my ($self, $arg) = @_;
135 32         110 $self->check_that_all_is_closed();
136 32         119 $self->handle_text("=over");
137              
138             # The =over amount is optional
139 32 100       96 if ($arg->{'~orig_content'}) {
140 29         99 $self->spacer($arg);
141 29         99 $self->handle_text("$arg->{'~orig_content'}");
142             }
143 32         96 $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   84 my $self = shift;
153 32         118 $self->check_that_all_is_closed();
154 32         98 $self->handle_text('=back');
155 32         86 $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 39 my $self = shift;
165 22         55 $self->emit; # Make sure buffer gets flushed
166 22         31 print {$self->{'output_fh'} } "=cut\n"
  22         74  
167             }
168              
169             sub _end_generic {
170 716     716   1218 my $self = shift;
171 716         1806 $self->check_that_all_is_closed();
172 716         1917 $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   1499 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       1389 ? $flags->{'~bracket_count'}
191             : 1;
192 676         2409 $self->handle_text($type . ( "<" x $bracket_count));
193              
194 676         1183 my $rspacer = "";
195 676 100       1491 if ($bracket_count > 1) {
196             my $lspacer = (exists $flags->{'~lspacer'})
197 18 100       42 ? $flags->{'~lspacer'}
198             : " ";
199 18         50 $self->handle_text($lspacer);
200              
201             $rspacer = (exists $flags->{'~rspacer'})
202 18 100       58 ? $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         981 push @{$self->{'fcode_end'}}, [ $bracket_count, $rspacer ];
  676         16608  
209             }
210              
211 60     60 0 150 sub start_B { _start_fcode('B', @_); }
212 359     359 0 805 sub start_C { _start_fcode('C', @_); }
213 22     22 0 61 sub start_E { _start_fcode('E', @_); }
214 53     53 0 152 sub start_F { _start_fcode('F', @_); }
215 85     85 0 242 sub start_I { _start_fcode('I', @_); }
216 3     3 0 12 sub start_S { _start_fcode('S', @_); }
217 0     0 0 0 sub start_U { _start_fcode('U', @_); }
218 1     1 0 5 sub start_X { _start_fcode('X', @_); }
219 4     4 0 12 sub start_Z { _start_fcode('Z', @_); }
220              
221             sub _end_fcode {
222 676     676   1143 my $self = shift;
223 676         938 my $fcode_end = pop @{$self->{'fcode_end'}};
  676         1321  
224 676         1124 my $bracket_count = 1;
225 676         1016 my $rspacer = "";
226              
227 676 50       1250 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         1142 $bracket_count = $fcode_end->[0];
233 676         1185 $rspacer = $fcode_end->[1];
234             }
235              
236 676 100       1501 $self->handle_text($rspacer) if $bracket_count > 1;
237 676         1743 $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 301 _start_fcode('L', @_);
252 89         304 $_[0]->handle_text($_[1]->{raw});
253 89         239 $_[0]->{inL}++
254             }
255              
256             sub end_L {
257 89     89 0 201 my $self = shift;
258 89         206 $self->{inL}--;
259 89 50       264 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         256 $self->_end_fcode(@_);
266             }
267              
268             sub emit {
269 1006     1006 0 1498 my $self = shift;
270              
271 1006 100       2417 if ($self->{buffer} ne "") {
272 984         1470 print { $self->{'output_fh'} } "",$self->{buffer} ,"\n\n";
  984         5080  
273              
274 984         2091 $self->{buffer} = "";
275             }
276              
277 1006         3203 return;
278             }
279              
280             1;
281              
282             __END__