Policy/Documentation/ProhibitVerbatimMarkup.pm

filter: Statementstmt 100% 63 / 63 Branchbran 85.7% 12 / 14 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 16 / 16 Podpod 100% 1 / 1 totaltotal 94.2% 98 / 104 SCAR 23.0
CC
10
Cov
95%
verbatim
6.0
command
5.0
BEGIN
1.0
CRAP 10.0
« Policy/ValuesAndExpressions/ProhibitBarewordDoubleColon.pm ↑ Summary Policy/Documentation/RequireFilenameMarkup.pm »
1 # Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 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 # self:
20 # perlcritic -s ProhibitVerbatimMarkup ProhibitVerbatimMarkup.pm
21  
22 package Perl::Critic::Policy::Documentation::ProhibitVerbatimMarkup;
23 40 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 40 ▶use base 'Perl::Critic::Policy';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 40 ▶use Perl::Critic::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # uncomment this to run the ### lines
30 #use Smart::Comments;
31  
32 40 our $VERSION = 99;
33  
34 40 ▶use constant supported_parameters => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
35 40 ▶use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 40 ▶use constant default_themes => qw(pulp cosmetic);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 40 ▶use constant applies_to => 'PPI::Document';
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 sub violates {
40 13 ▶ my ($self, $elem, $document) = @_;
Subroutine violates: called CC=1 SCAR=0.0
Pod documented
41 ### ProhibitVerbatimMarkup on: $elem->content
42  
43 13 my $parser = Perl::Critic::PodParser::ProhibitVerbatimMarkup->new
44 (policy => $self);
45 13 $parser->parse_from_elem ($elem);
46 13 return $parser->violations;
47 }
48  
49 package Perl::Critic::PodParser::ProhibitVerbatimMarkup;
50 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 40 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 40 ▶use base 'Perl::Critic::Pulp::PodParser';
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 sub command {
55 17 ▶ my $self = shift;
Subroutine command: called CC=5 SCAR=16.2
56 17 my ($command, $text, $linenum, $paraobj) = @_;
57 ### command: $command
58 ### $text
59 17 $self->SUPER::command(@_); # maintain 'in_begin'
60  
61 17 ▶ if ($command eq 'for'
Branch: 1 branch
BranchTrueFalseTotal
if ($command eq "for" and $text =~ /^ProhibitVerbatimMarkup\b\s*(.*)/) 4 13 17
Condition66%2/3
$command eq "for" and $text =~ /^ProhibitVerbatimMarkup\b\s*(.*)/
!ll&&!rl&&r
1304
MC/DC50%1/2
$command eq "for" and $text =~ /^ProhibitVerbatimMarkup\b\s*(.*)/
$command eq "for" $text =~ /^ProhibitVerbatimMarkup\b\s*(.*)/
Truth table66%2/3
A and B
A= $command eq "for"B= $text =~ /^ProhibitVerbatimMarkup\b\s*(.*)/
ABresult
0X0
100
111
62 && $text =~ /^ProhibitVerbatimMarkup\b\s*(.*)/) {
63 4 my $directive = $1;
64 ### $directive
65 4 ▶ if ($directive =~ /^allow next( (\d+))?/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($directive =~ /^allow next( (\d+))?/) 4 0 4
66 # numbered "allow next 5" means up to that many following verbatims
67 # unnumbered "allow next" means one following verbatim
68 4 ▶ $self->{'allow_next'} = (defined $2 ? $2 : 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $2 ? : 2 2 4
69 }
70 }
71 17 return '';
72 }
73  
74 sub verbatim {
75 18 ▶ my ($self, $text, $linenum, $paraobj) = @_;
Subroutine verbatim: called CC=6 SCAR=18.0
76 ### verbatim: $text
77  
78 18 ▶ if ($self->{'allow_next'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"allow_next"}) 4 14 18
79 ### allow next: $self->{'allow_next'}
80 4 $self->{'allow_next'}--;
81 4 return '';
82 }
83  
84 # process outside =begin, and inside =begin which is ":" markup
85 14 ▶ unless ($self->{'in_begin'} eq '' || $self->{'in_begin'} =~ /^:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"in_begin"} eq "" or $self->{"in_begin"} =~ /^:/) 1 13 14
Condition66%2/3
$self->{"in_begin"} eq "" or $self->{"in_begin"} =~ /^:/
l!l&&r!l&&!r
1301
MC/DC50%1/2
$self->{"in_begin"} eq "" or $self->{"in_begin"} =~ /^:/
$self->{"in_begin"} eq "" $self->{"in_begin"} =~ /^:/
Truth table66%2/3
A or B
A= $self->{"in_begin"} eq ""B= $self->{"in_begin"} =~ /^:/
ABresult
1X1
011
000
86 1 return '';
87 }
88  
89 # I<> italic
90 # B<> bold
91 # C<> code
92 # L<> link
93 # E<> escape
94 # F<> filename
95 # S<> no break
96 # X<> index
97 # Z<> empty
98 # J<> Pod::MultiLang
99 #
100 # DB<123> sample debugger output exempted
101 #
102 13 ▶ while ($text =~ /\bDB<\d+>|([IBCLEFSXZJ]<)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\bDB<\d+>|([IBCLEFSXZJ]<)/g) 10 13 23
103 10 ▶ next unless $1;
Branch: 1 branch
BranchTrueFalseTotal
unless $1 0 10 10
104 10 my $markup = "$1>";
105  
106 10 $self->violation_at_linenum_and_textpos
107 ("$markup markup in verbatim paragraph, is it meant to be so?",
108 $linenum, $text, pos($text));
109 }
110 13 return '';
111 }
112  
113 sub textblock {
114 3 ▶ my ($self) = @_;
Subroutine textblock: called CC=1 SCAR=0.0
115 3 $self->{'allow_next'} = 0;
116 3 return '';
117 }
118  
119 40 1;
120 __END__
121  
122 =for stopwords Ryde fontification ascii unindented verbatimness ok unexpanded
123  
124 =head1 NAME
125  
126 Perl::Critic::Policy::Documentation::ProhibitVerbatimMarkup - unexpanded CE<lt>E<gt> etc markup in POD verbatim paras
127  
128 =head1 DESCRIPTION
129  
130 This policy is part of the L<C<Perl::Critic::Pulp>|Perl::Critic::Pulp>
131 add-on. It reports POD verbatim paragraphs which contain markup like
132 BE<lt>E<gt> or CE<lt>E<gt>. That markup will appear literally in the
133 formatted output where you may have meant fontification.
134  
135 =for ProhibitVerbatimMarkup allow next 3
136  
137 =head1 SOME THING
138  
139 Paragraph of text introducing an example,
140  
141 # call the C<foo> function # bad
142 &foo();
143  
144 This is purely cosmetic so this policy is low severity and under the
145 "cosmetic" theme (see L<Perl::Critic/POLICY THEMES>). Normally it means one
146 of two things,
147  
148 =over
149  
150 =item *
151  
152 You want markup -- it should be a plain paragraph not a verbatim indented
153 one. An C<=over> can be used for indentation if desired.
154  
155 =item *
156  
157 You want verbatim -- replace the markup with an ascii approximation like
158 C<func()> or perhaps C<*bold*> or C<_underline_>.
159  
160 =back
161  
162 Don't forget that a verbatim paragraph extends to the next blank line and
163 includes unindented lines until then too (see L<perlpodspec/Pod
164 Definitions>). If you forget the blank line then the verbatimness continues
165  
166 =for ProhibitVerbatimMarkup allow next 2
167  
168 =pod
169  
170 $some->sample;
171 code();
172 And this was I<meant> to be plain text. # bad
173  
174 =head2 Markup Forms
175  
176 The check for markup is unsophisticated. Any of the POD specified "IE<lt>"
177 "CE<lt>" etc is taken to be markup, plus "JE<lt>" of C<Pod::MultiLang>.
178  
179 =for ProhibitVerbatimMarkup allow next
180  
181 I< # bad
182 B< # bad
183 C< # bad
184 L< # bad
185 E< # bad
186 F< # bad
187 S< # bad
188 X< # bad
189 Z< # bad
190 J< # bad, for Pod::MultiLang
191  
192 It's possible a C<E<lt>> might be something mathematical like "XE<lt>Y", but
193 in practice spaces S<"X E<lt> Y"> or lower case letters are more common (and
194 are ok).
195  
196 C<DBE<lt>1E<gt>> style sample Perl debugger output is exempted (see
197 L<perldebug>). It's uncommon, but not likely to have intended
198 C<BE<lt>E<gt>> bold.
199  
200 DB<123> dump b # ok
201  
202 =head2 Disabling
203  
204 If a verbatim paragraph is showing how to write POD markup then you can add
205 an C<=for> to tell C<ProhibitVerbatimMarkup> to allow it. This happens most
206 often in documentation for modules which themselves operate on POD markup.
207  
208 =for ProhibitVerbatimMarkup allow next 5
209  
210 =for ProhibitVerbatimMarkup allow next
211  
212 blah blah E<gt> etc
213  
214 =for ProhibitVerbatimMarkup allow next 2
215  
216 Two verbatims of C<code>
217  
218 or B<bold> etc
219  
220 The usual no critic works too,
221  
222 ## no critic (ProhibitVerbatimMarkup)
223  
224 But the annotation must be before any C<__END__> token, and if the POD is
225 after an C<__END__> token then C<Perl::Critic> 1.112 up is required. An
226 C<=for> has the advantage of being together with the exception.
227  
228 As always if you don't care at all about this at all then disable
229 C<ProhibitVerbatimMarkup> from your F<.perlcriticrc> in the usual way (see
230 L<Perl::Critic/CONFIGURATION>),
231  
232 [-Documentation::ProhibitVerbatimMarkup]
233  
234 =head1 SEE ALSO
235  
236 L<Perl::Critic::Pulp>,
237 L<Perl::Critic>,
238 L<Perl::Critic::Policy::Documentation::ProhibitBadAproposMarkup>,
239 L<Perl::Critic::Policy::Documentation::RequireEndBeforeLastPod>
240  
241 =head1 HOME PAGE
242  
243 http://user42.tuxfamily.org/perl-critic-pulp/index.html
244  
245 =head1 COPYRIGHT
246  
247 Copyright 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2019, 2021 Kevin Ryde
248  
249 Perl-Critic-Pulp is free software; you can redistribute it and/or modify it
250 under the terms of the GNU General Public License as published by the Free
251 Software Foundation; either version 3, or (at your option) any later
252 version.
253  
254 Perl-Critic-Pulp is distributed in the hope that it will be useful, but
255 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
256 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
257 more details.
258  
259 You should have received a copy of the GNU General Public License along with
260 Perl-Critic-Pulp. If not, see <http://www.gnu.org/licenses/>.
261  
262 =cut
« Policy/ValuesAndExpressions/ProhibitBarewordDoubleColon.pm ↑ Summary Policy/Documentation/RequireFilenameMarkup.pm »