File Coverage

blib/lib/Perl/Critic/Policy/Documentation/RequireFilenameMarkup.pm
Criterion Covered Total %
statement 69 73 94.5
branch 7 10 70.0
condition 2 5 40.0
subroutine 18 18 100.0
pod 1 1 100.0
total 97 107 90.6


line stmt bran cond sub pod time code
1             # Copyright 2013, 2014, 2015, 2016, 2017 Kevin Ryde
2              
3             # This file is part of Perl-Critic-Pulp.
4              
5             # Perl-Critic-Pulp is free software; you can redistribute it and/or modify
6             # it under the terms of the GNU General Public License as published by the
7             # Free Software Foundation; either version 3, or (at your option) any later
8             # version.
9             #
10             # Perl-Critic-Pulp 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 Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
17              
18              
19             # perlcritic -s RequireFilenameMarkup RequireFilenameMarkup.pm
20              
21             # unmarked /usr/local
22             # perlcritic -s RequireFilenameMarkup /usr/share/perl5/XML/Twig.pm
23              
24              
25             package Perl::Critic::Policy::Documentation::RequireFilenameMarkup;
26 40     40   29009 use 5.006;
  40         126  
27 40     40   241 use strict;
  40         83  
  40         742  
28 40     40   166 use warnings;
  40         85  
  40         1129  
29 40     40   208 use base 'Perl::Critic::Policy';
  40         82  
  40         3828  
30 40     40   158137 use Perl::Critic::Utils;
  40         77  
  40         555  
31 40     40   47690 use Pod::Escapes;
  40         115420  
  40         2860  
32              
33             # uncomment this to run the ### lines
34             # use Smart::Comments;
35              
36             our $VERSION = 97;
37              
38 40     40   310 use constant supported_parameters => ();
  40         176  
  40         2166  
39 40     40   218 use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW;
  40         78  
  40         1880  
40 40     40   263 use constant default_themes => qw(pulp cosmetic);
  40         67  
  40         1772  
41 40     40   205 use constant applies_to => 'PPI::Document';
  40         74  
  40         4121  
42              
43             sub violates {
44 41     41 1 644215 my ($self, $elem, $document) = @_;
45             ### RequireFilenameMarkup on: $elem->content
46              
47 41         641 my $parser = Perl::Critic::Pulp::PodParser::RequireFilenameMarkup->new
48             (policy => $self);
49 41         171 $parser->parse_from_elem ($elem);
50 41         261 return $parser->violations;
51             }
52              
53             package Perl::Critic::Pulp::PodParser::RequireFilenameMarkup;
54 40     40   245 use strict;
  40         94  
  40         847  
55 40     40   175 use warnings;
  40         78  
  40         1599  
56 40     40   594 use Pod::ParseLink;
  40         816  
  40         1659  
57 40     40   204 use base 'Perl::Critic::Pulp::PodParser';
  40         97  
  40         20086  
58              
59             sub command {
60 41     41   3318 my $self = shift;
61 41         181 $self->SUPER::command(@_); # for $self->{'in_begin'}
62 41         145 $self->command_as_textblock(@_);
63 41         661 return '';
64             }
65              
66             sub textblock {
67 79     79   2292 my ($self, $text, $linenum, $pod_para) = @_;
68             ### textblock: "linenum=$linenum"
69              
70 79 50 50     357 if (($self->{'allow_next'}||0) > 0) {
71 0         0 $self->{'allow_next'}--;
72 0         0 return '';
73             }
74              
75             # process outside =begin, and inside =begin which is ":" markup
76 79 50 33     213 unless ($self->{'in_begin'} eq '' || $self->{'in_begin'} =~ /^:/) {
77 0         0 return '';
78             }
79              
80 79         3663 my $interpolated = $self->interpolate($text, $linenum);
81             ### $text
82             ### $interpolated
83              
84 79         708 while ($interpolated =~ m{(^ | (?<=[\([:space:]])) # BOL or preceding space
85             (
86             /(bin|etc|dev|opt|proc|tmp|usr|var)
87             ($ # EOL
88             |(?=[)[:space:]]) # or following space
89             |/\S*) # or /chars
90             |
91             [cC]:\\\S*
92             )
93             }mgx) {
94 36         103 my $before = $1;
95 36         76 my $match = $2;
96 36         99 $match =~ s/[.,;:]+$//;
97 36         70 my $pos = pos($interpolated) - length($match);
98              
99 36         191 $self->violation_at_linenum_and_textpos
100             ("Filename without F<> or other markup \"$match\"\n",
101             $linenum, $interpolated, $pos);
102             }
103             }
104              
105             sub interior_sequence {
106 12     12   46 my ($self, $cmd, $text, $pod_seq) = @_;
107             ### $cmd
108             ### $text
109              
110 12 100       36 if ($cmd eq 'E') {
111 3         12 my $char = Pod::Escapes::e2char($text);
112 3 50       37 if (! defined $char) {
113             ### oops, unrecognised E<> ...
114 0         0 return 'X';
115             }
116 3         117 return $char;
117             }
118 9 100       22 if ($cmd eq 'L') {
119 5         20 my ($display, $inferred, $name, $section, $type)
120             = Pod::ParseLink::parselink ($text);
121             ### $display
122             ### $inferred
123             ### $name
124 5         213 return $inferred; # the display part, or the name part if no display
125             }
126              
127             ### X,C keep only the newlines: $text
128 4         11 $text =~ tr/\n//cd;
129 4         122 return $text;
130             }
131              
132             1;
133             __END__
134              
135             =for stopwords Ryde filenames filename Filenames
136              
137             =head1 NAME
138              
139             Perl::Critic::Policy::Documentation::RequireFilenameMarkup - markup /foo filenames
140              
141             =head1 DESCRIPTION
142              
143             This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp>
144             add-on. It asks you to use C<FE<lt>E<gt>> or other markup on filenames.
145              
146             =for ProhibitVerbatimMarkup allow next 2
147              
148             /usr/bin # bad
149              
150             F</usr/bin> # ok
151             C</bin/sh> # ok
152              
153             C<FE<lt>E<gt>> lets the formatters show filenames in a usual way, such as
154             italics in man pages. This can help human readability but is a minor matter
155             and on that basis this policy is lowest severity and under the "cosmetic"
156             theme (see L<Perl::Critic/POLICY THEMES>).
157              
158             Filenames in text are identified by likely forms. Currently words starting
159             as follows are considered filenames. F</usr> and F</etc> are the most
160             common.
161              
162             /bin
163             /dev
164             /etc
165             /opt # some proprietary Unix
166             /proc
167             /tmp
168             /usr
169             /var
170             C:\ # MS-DOS
171              
172             Any markup on a filename satisfies this policy. C<FE<lt>E<gt>> is usual,
173             but C<CE<lt>E<gt>> might suit for instance C<CE<lt>/bin/shE<gt>> to show
174             it's a command with path rather than a file as such.
175              
176             C<=begin :foo> blocks with <:> POD type are checked since they can have
177             markup. "Verbatim" paragraphs are ignored since of course they cannot have
178             markup.
179              
180             =head2 Disabling
181              
182             If you don't care about filename markup you can disable
183             C<RequireFilenameMarkup> from your F<.perlcriticrc> in the usual way (see
184             L<Perl::Critic/CONFIGURATION>),
185              
186             [-Documentation::RequireFilenameMarkup]
187              
188             =head1 SEE ALSO
189              
190             L<Perl::Critic::Pulp>,
191             L<Perl::Critic>
192              
193             =head1 HOME PAGE
194              
195             L<http://user42.tuxfamily.org/perl-critic-pulp/index.html>
196              
197             =head1 COPYRIGHT
198              
199             Copyright 2013, 2014, 2015, 2016, 2017 Kevin Ryde
200              
201             Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
202             under the terms of the GNU General Public License as published by the Free
203             Software Foundation; either version 3, or (at your option) any later
204             version.
205              
206             Perl-Critic-Pulp is distributed in the hope that it will be useful, but
207             WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
208             or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
209             more details.
210              
211             You should have received a copy of the GNU General Public License along with
212             Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
213              
214             =cut
215              
216             # /usr/local
217             # /opt.
218             # /tmp
219             # /dev/null
220             # /dev/
221             # /dev.
222             # blah/option
223             #
224             # /option
225             #
226             # blah/blah/etc
227             #
228             # E<sol>dev