File Coverage

blib/lib/Perl/Critic/Policy/Documentation/ProhibitVerbatimMarkup.pm
Criterion Covered Total %
statement 61 61 100.0
branch 10 12 83.3
condition 4 6 66.6
subroutine 16 16 100.0
pod 1 1 100.0
total 92 96 95.8


line stmt bran cond sub pod time code
1             # Copyright 2010, 2011, 2012, 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             # self:
20             # perlcritic -s ProhibitVerbatimMarkup ProhibitVerbatimMarkup.pm
21              
22             package Perl::Critic::Policy::Documentation::ProhibitVerbatimMarkup;
23 40     40   25682 use 5.006;
  40         217  
24 40     40   189 use strict;
  40         75  
  40         903  
25 40     40   179 use warnings;
  40         74  
  40         931  
26 40     40   219 use base 'Perl::Critic::Policy';
  40         154  
  40         3859  
27 40     40   147625 use Perl::Critic::Utils;
  40         94  
  40         599  
28              
29             # uncomment this to run the ### lines
30             #use Smart::Comments;
31              
32             our $VERSION = 97;
33              
34 40     40   28994 use constant supported_parameters => ();
  40         82  
  40         2162  
35 40     40   395 use constant default_severity => $Perl::Critic::Utils::SEVERITY_LOW;
  40         90  
  40         1985  
36 40     40   217 use constant default_themes => qw(pulp cosmetic);
  40         78  
  40         2082  
37 40     40   219 use constant applies_to => 'PPI::Document';
  40         92  
  40         4298  
38              
39             sub violates {
40 13     13 1 1139704 my ($self, $elem, $document) = @_;
41             ### ProhibitVerbatimMarkup on: $elem->content
42              
43 13         203 my $parser = Perl::Critic::PodParser::ProhibitVerbatimMarkup->new
44             (policy => $self);
45 13         58 $parser->parse_from_elem ($elem);
46 13         165 return $parser->violations;
47             }
48              
49             package Perl::Critic::PodParser::ProhibitVerbatimMarkup;
50 40     40   249 use strict;
  40         94  
  40         861  
51 40     40   200 use warnings;
  40         248  
  40         1179  
52 40     40   444 use base 'Perl::Critic::Pulp::PodParser';
  40         289  
  40         17160  
53              
54             sub command {
55 17     17   1440 my $self = shift;
56 17         51 my ($command, $text, $linenum, $paraobj) = @_;
57             ### command: $command
58             ### $text
59 17         63 $self->SUPER::command(@_); # maintain 'in_begin'
60              
61 17 100 66     71 if ($command eq 'for'
62             && $text =~ /^ProhibitVerbatimMarkup\b\s*(.*)/) {
63 4         10 my $directive = $1;
64             ### $directive
65 4 50       16 if ($directive =~ /^allow next( (\d+))?/) {
66             # numbered "allow next 5" means up to that many following verbatims
67             # unnumbered "allow next" means one following verbatim
68 4 100       15 $self->{'allow_next'} = (defined $2 ? $2 : 1);
69             }
70             }
71 17         150 return '';
72             }
73              
74             sub verbatim {
75 18     18   1001 my ($self, $text, $linenum, $paraobj) = @_;
76             ### verbatim: $text
77              
78 18 100       47 if ($self->{'allow_next'}) {
79             ### allow next: $self->{'allow_next'}
80 4         10 $self->{'allow_next'}--;
81 4         36 return '';
82             }
83              
84             # process outside =begin, and inside =begin which is ":" markup
85 14 100 66     58 unless ($self->{'in_begin'} eq '' || $self->{'in_begin'} =~ /^:/) {
86 1         50 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         71 while ($text =~ /\bDB<\d+>|([IBCLEFSXZJ]<)/g) {
103 10 50       39 next unless $1;
104 10         30 my $markup = "$1>";
105              
106 10         52 $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         200 return '';
111             }
112              
113             sub textblock {
114 3     3   163 my ($self) = @_;
115 3         8 $self->{'allow_next'} = 0;
116 3         27 return '';
117             }
118              
119             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 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