File Coverage

blib/lib/App/PodLinkCheck/ParseSections.pm
Criterion Covered Total %
statement 44 44 100.0
branch 9 10 90.0
condition n/a
subroutine 11 11 100.0
pod 1 2 50.0
total 65 67 97.0


line stmt bran cond sub pod time code
1             # Copyright 2010, 2011, 2012 Kevin Ryde
2              
3             # This file is part of PodLinkCheck.
4              
5             # PodLinkCheck is free software; you can redistribute it and/or modify it
6             # under the terms of the GNU General Public License as published by the Free
7             # Software Foundation; either version 3, or (at your option) any later
8             # version.
9             #
10             # PodLinkCheck is distributed in the hope that it will be useful, but
11             # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12             # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13             # for more details.
14             #
15             # You should have received a copy of the GNU General Public License along
16             # with PodLinkCheck. If not, see .
17              
18             package App::PodLinkCheck::ParseSections;
19 1     1   678 use 5.006;
  1         4  
  1         38  
20 1     1   4 use strict;
  1         2  
  1         26  
21 1     1   4 use warnings;
  1         5  
  1         26  
22 1     1   4 use base 'Pod::Simple';
  1         2  
  1         1016  
23              
24 1     1   34560 use vars '$VERSION';
  1         2  
  1         461  
25             $VERSION = 12;
26              
27             # uncomment this to run the ### lines
28             #use Smart::Comments;
29              
30             sub new {
31 7     7 1 9804 my ($class, $plc) = @_;
32 7         36 my $self = $class->SUPER::new;
33 7         173 $self->{(__PACKAGE__)}->{'sections'} = {};
34 7         26 $self->nix_X_codes(1);
35 7         59 $self->no_errata_section(1);
36 7         58 $self->preserve_whitespace(1);
37 7 50       54 if (! $plc->{'verbose'}) {
38 7         21 $self->no_whining(1);
39             }
40 7         58 return $self;
41             }
42              
43             # return hashref where keys are the section names
44             sub sections_hashref {
45 6     6 0 82 my ($self) = @_;
46 6         17 return $self->{(__PACKAGE__)}->{'sections'};
47             }
48              
49             sub _handle_element_start {
50 18     18   2683 my ($self, $ename, $attr) = @_;
51             ### _handle_element_start(): $ename, $attr
52              
53             # head1, head2, etc, and item-text, item-bullet, item-number
54 18 100       76 if ($ename =~ /^(head|item)/) {
55 6         17 $self->{(__PACKAGE__)}->{'item_text'} = '';
56             }
57             }
58             sub _handle_text {
59 9     9   72 my ($self, $text) = @_;
60             ### _handle_text(): $text
61 9 100       26 if (exists $self->{(__PACKAGE__)}->{'item_text'}) {
62 6         22 $self->{(__PACKAGE__)}->{'item_text'} .= $text;
63             }
64             }
65             sub _handle_element_end {
66 18     18   529 my ($self, $ename) = @_;
67             ### _handle_element_end(): $ename
68              
69 18 100       79 if ($ename =~ /^(head|item)/) {
70 6         16 my $section = delete $self->{(__PACKAGE__)}->{'item_text'};
71             ### section: $section
72              
73 6         14 $section = _collapse_whitespace ($section);
74 6         15 $self->{(__PACKAGE__)}->{'sections'}->{$section} = 1;
75              
76             # Like Pod::Checker take the first word, meaning up to the first
77             # whitespace, as a section name too, which is much used for
78             # cross-references to perlfunc.
79             #
80             # THINK-ABOUT-ME: CHI.pm is better treated by taking the first \w word
81             # so as to exclude parens etc.
82             #
83 6 100       34 if ($section =~ s/\s.*//) {
84             ### section one word: $section
85 3         13 $self->{(__PACKAGE__)}->{'sections'}->{$section} = 1;
86             }
87             }
88             }
89              
90             sub _collapse_whitespace {
91 6     6   9 my ($str) = @_;
92 6         19 $str =~ s/\s+/ /g;
93 6         13 $str =~ s/^\s+//;
94 6         13 $str =~ s/\s+$//;
95 6         14 return $str;
96             }
97              
98             1;
99             __END__