File Coverage

blib/lib/Pod/Spell.pm
Criterion Covered Total %
statement 67 68 98.5
branch 33 38 86.8
condition 15 21 71.4
subroutine 13 14 92.8
pod 4 4 100.0
total 132 145 91.0


line stmt bran cond sub pod time code
1             package Pod::Spell;
2 5     5   348972 use 5.008;
  5         49  
3 5     5   22 use strict;
  5         9  
  5         111  
4 5     5   21 use warnings;
  5         7  
  5         1318  
5              
6             our $VERSION = '1.25';
7              
8             sub new {
9 6     6 1 9169 my ( $class, %args ) = @_;
10              
11 6         15 my $no_wide_chars = delete $args{no_wide_chars};
12 6 50       22 my $debug = exists $args{debug} ? delete $args{debug} : $ENV{PERL_POD_SPELL_DEBUG};
13              
14 6   33     33 my $stopwords = $args{stopwords} || do {
15             require Pod::Wordlist;
16             Pod::Wordlist->new(
17             _is_debug => $debug,
18             no_wide_chars => $no_wide_chars
19             )
20             };
21              
22 6         581 my $parser = Pod::Spell::_Processor->new;
23 6         26 $parser->stopwords($stopwords);
24 6         64 $parser->_is_debug($debug);
25 6         54 $parser->output_fh(\*STDOUT);
26              
27 6         43 my %self = (
28             processor => $parser,
29             stopwords => $stopwords,
30             debug => $debug,
31             );
32              
33 6         26 bless \%self, $class
34             }
35              
36 2 100   2   346 sub _is_debug { (shift)->{debug} ? 1 : 0; }
37              
38 1     1 1 35 sub stopwords { (shift)->{stopwords} }
39              
40             sub parse_from_file {
41 0     0 1 0 shift->{processor}->parse_from_file(@_)
42             }
43              
44             sub parse_from_filehandle {
45 4     4 1 1790 shift->{processor}->parse_from_file(@_)
46             }
47              
48             package # Hide from indexing
49             Pod::Spell::_Processor;
50 5     5   809 use parent 'Pod::Simple';
  5         529  
  5         26  
51              
52 5     5   134805 use Text::Wrap ();
  5         11541  
  5         2672  
53              
54             __PACKAGE__->_accessorize(qw(
55             stopwords
56             _is_debug
57             ));
58              
59             sub new {
60 6     6   11 my $class = shift;
61 6         47 my $self = $class->SUPER::new(@_);
62 6         175 $self->accept_targets('stopwords');
63 6         128 return $self;
64             }
65              
66             my %track_elements = (
67             for => 1,
68             Verbatim => 1,
69             L => 1,
70             C => 1,
71             F => 1,
72             );
73              
74             sub _handle_element_start { ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
75 38     38   12964 my ($self, $element_name, $attr) = @_;
76             $self->{buffer} = ''
77 38 100       106 if !defined $self->{buffer};
78              
79 38 100       92 if ($track_elements{$element_name}) {
80 13         15 push @{ $self->{in_element} }, [ $element_name, $attr ];
  13         39  
81             }
82             }
83              
84             sub _handle_text { ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
85 36     36   292 my ($self, $text) = @_;
86              
87 36         48 my $in = $self->{in_element};
88 36 100 100     108 if ($in && @$in) {
89 14         17 my ($element_name, $attr) = @{$in->[-1]};
  14         28  
90             ## no critic (ControlStructures::ProhibitCascadingIfElse)
91 14 100 66     66 if ($element_name eq 'for' && $attr->{target_matching} eq 'stopwords') {
    100 33        
    100          
    50          
92             # this will match both for/begin and stopwords/:stopwords
93              
94 6 50       15 print "Stopword para: <$text>\n"
95             if $self->_is_debug;
96 6         282 $self->stopwords->learn_stopwords($text);
97 6         27 return;
98             }
99             # totally ignore verbatim sections
100             elsif ($element_name eq 'Verbatim') {
101 1         2 return;
102             }
103             elsif ($element_name eq 'L') {
104             return
105 1 50       5 if $attr->{'content-implicit'};
106             }
107             elsif ($element_name eq 'C' || $element_name eq 'F') {
108             # maintain word boundaries
109 6 100       20 my $pre = $text =~ s{\A\s+}{} ? ' ' : '';
110 6 100       18 my $post = $text =~ s{\s+\z}{} ? ' ' : '';
111             # if _ is joined with text before or after, it will be treated as
112             # a Perl token and the entire word ignored
113 6 50       13 $text = $pre . (length $text ? '_' : '') . $post;
114             }
115             }
116              
117 28         62 $self->{buffer} .= $text;
118             }
119              
120             sub _handle_element_end { ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
121 38     38   870 my ($self, $element_name) = @_;
122              
123 38         51 my $in = $self->{in_element};
124 38 100 100     144 if ($in && @$in && $in->[-1][0] eq $element_name) {
      100        
125 13         18 pop @$in;
126             }
127              
128             return
129 38 100       129 if $element_name !~ m{\A(?:Para|head\d|item-.*|over-block)\z};
130              
131 18         36 my $buffer = delete $self->{buffer};
132 18 100 66     87 if (!defined $buffer || !length $buffer) {
133 3         22 return;
134             }
135              
136 15         38 my $fh = $self->output_fh;
137              
138 15         98 my $out = $self->stopwords->strip_stopwords($buffer);
139              
140             # maintain exact output of older Pod::Parser based implementation
141 15 100       39 print { $fh } "\n"
  4         43  
142             if $element_name ne 'Para';
143              
144             return
145 15 100       38 if !length $out;
146              
147 12         17 local $Text::Wrap::huge = 'overflow'; ## no critic ( Variables::ProhibitPackageVars )
148 12         14 print { $fh } Text::Wrap::wrap( '', '', $out ) . "\n\n";
  12         46  
149             }
150              
151             1;
152              
153             __END__