Readme/Plugin.pm

filter: Statementstmt 92.8% 78 / 84 Branchbran 95.8% 23 / 24 Conditioncond 66.6% 10 / 15 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 93.7% 15 / 16 Podpod 100% 3 / 3 totaltotal 88.0% 133 / 151 CC 19 SCAR 16.1
CC
19
Cov
90%
_parse_arguments
9.0
parse_cmd_args
8.1
__ANON__
2.3
CRAP 5.0
« Readme/Plugin/requires.pm ↑ Summary Readme/Plugin/version.pm »
1 package Pod::Readme::Plugin;
2  
3 5 ▶use v5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 our $VERSION = 'v1.2.3';
8  
9 5 ▶use Class::Method::Modifiers qw/ fresh /;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 5 ▶use Hash::Util qw/ lock_keys /;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 5 ▶use Pod::Readme::Types qw/ Indentation /;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 Pod::Readme::Plugin - Plugin role for Pod::Readme
18  
19 =head1 DESCRIPTION
20  
21 L<Pod::Readme> v1.0 and later supports plugins that extend the
22 capabilities of the module.
23  
24 =head1 WRITING PLUGINS
25  
26 Writing plugins is straightforward. Plugins are L<Moo::Role> modules
27 in the C<Pod::Readme::Plugin> namespace. For example,
28  
29 package Pod::Readme::Plugin::myplugin;
30  
31 use Moo::Role;
32  
33 sub cmd_myplugin {
34 my ($self, @args) = @_;
35 my $res = $self->parse_cmd_args( [qw/ arg1 arg2 /], @args );
36  
37 ...
38 }
39  
40 When L<Pod::Readme> encounters POD with
41  
42 =for readme plugin myplugin arg1 arg2
43  
44 the plugin role will be loaded, and the C<cmd_myplugin> method will be
45 run.
46  
47 Note that you do not need to specify a C<cmd_myplugin> method.
48  
49 Any method prefixed with "cmd_" will be a command that can be called
50 using the C<=for readme command> syntax.
51  
52 A plugin parses arguments using the L</parse_cmd_arguments> method and
53 writes output using the write methods noted above.
54  
55 See some of the included plugins, such as
56 L<Pod::Readme::Plugin::version> for examples.
57  
58 Any attributes in the plugin should be prefixed with the name of the
59 plugin, to avoid any conflicts with attribute and method names from
60 other plugins, e.g.
61  
62 use Types::Standard qw/ Int /;
63  
64 has 'myplugin_heading_level' => (
65 is => 'rw',
66 isa => Int,
67 default => 1,
68 lazy => 1,
69 );
70  
71 Attributes should be lazy to ensure that their defaults are properly
72 set.
73  
74 Be aware that changing default values of an attribute based on
75 arguments means that the next time a plugin method is run, the
76 defaults will be changed.
77  
78 Custom types in L<Pod::Readme::Types> may be useful for attributes
79 when writing plugins, e.g.
80  
81 use Pod::Readme::Types qw/ File HeadingLevel /;
82  
83 has 'myplugin_file' => (
84 is => 'rw',
85 isa => File,
86 coerce => sub { File->coerce(@_) },
87 default => 'Changes',
88 lazy => 1,
89 );
90  
91 # We add this file to the list of dependencies
92  
93 around 'depends_on' => sub {
94 my ($orig, $self) = @_;
95 return ($self->myplugin_file, $self->$orig);
96 };
97  
98 =head1 ATTRIBUTES
99  
100 =head2 C<verbatim_indent>
101  
102 The number of columns to indent a verbatim paragraph.
103  
104 =cut
105  
106 5 has verbatim_indent => (
107 is => 'ro',
108 isa => Indentation,
109 default => 2,
110 );
111  
112 =head1 METHODS
113  
114 =cut
115  
116 26 ▶sub _parse_arguments {
Subroutine _parse_arguments: called CC=9 SCAR=22.0
117 26 my ( $self, $line ) = @_;
118 26 my @args = ();
119  
120 26 my $i = 0;
121 26 my $prev;
122 26 my $in_quote = '';
123 26 my $arg_buff = '';
124 26 ▶ while ( $i < length($line) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < length $line) 643 26 669
125  
126 643 my $curr = substr( $line, $i, 1 );
127 643 ▶ if ( $curr !~ m/\s/ || $in_quote ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $curr =~ /\s/ or $in_quote) { } 547 96 643
elsif ($arg_buff ne '') { } 70 26 96
Condition100%3/3
not $curr =~ /\s/ or $in_quote
l!l&&r!l&&!r
543496
MC/DC100%2/2
not $curr =~ /\s/ or $in_quote
not $curr =~ /\s/ $in_quote
Truth table100%3/3
not(A) or B
A= not $curr =~ /\s/B= $in_quote
ABresult
1X1
011
000
128 547 $arg_buff .= $curr;
129 547 ▶ if ( $curr =~ /["']/ && $prev ne "\\" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curr =~ /["']/ and $prev ne "\\") 13 534 547
Condition66%2/3
$curr =~ /["']/ and $prev ne "\\"
!ll&&!rl&&r
534013
MC/DC50%1/2
$curr =~ /["']/ and $prev ne "\\"
$curr =~ /["']/ $prev ne "\\"
Truth table66%2/3
A and B
A= $curr =~ /["']/B= $prev ne "\\"
ABresult
0X0
100
111
130 13 ▶ $in_quote = ( $curr eq $in_quote ) ? '' : $curr;
Branch: 1 branch
BranchTrueFalseTotal
$curr eq $in_quote ? : 5 8 13
131 }
132 }
133 elsif ( $arg_buff ne '' ) {
134 70 push @args, $arg_buff;
135 70 $arg_buff = '';
136 }
137 643 $prev = $curr;
138 643 $i++;
139 }
140  
141 26 ▶ if ( $arg_buff ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg_buff ne "") 1 25 26
142 1 push @args, $arg_buff;
143 }
144  
145 26 return @args;
146 }
147  
148 =head2 C<parse_cmd_args>
149  
150 my $hash_ref = $self->parse_cmd_args( \@allowed_keys, @args);
151  
152 This command parses arguments for a plugin and returns a hash
153 reference containing the argument values.
154  
155 The C<@args> parameter is a list of arguments passed to the command
156 method by L<Pod::Readme::Filter>.
157  
158 If an argument contains an equals sign, then it is assumed to take a
159 string. (Strings containing whitespace should be surrounded by
160 quotes.)
161  
162 Otherwise, an argument is assumed to be boolean, which defaults to
163 true. If the argument is prefixed by "no-" or "no_" then it is given a
164 false value.
165  
166 If the C<@allowed_keys> parameter is given, then it will reject
167 argument keys that are not in that list.
168  
169 For example,
170  
171 my $res = $self->parse_cmd_args(
172 undef,
173 'arg1',
174 'no-arg2',
175 'arg3="This is a string"',
176 'arg4=value',
177 );
178  
179 will return a hash reference containing
180  
181 {
182 arg1 => 1,
183 arg2 => 0,
184 arg3 => 'This is a string',
185 arg4 => 'value',
186 }
187  
188 =cut
189  
190 14 ▶sub parse_cmd_args {
Subroutine parse_cmd_args: called CC=8 SCAR=20.9
Pod documented
191 14 my ( $self, $allowed, @args ) = @_;
192  
193 14 my ( $key, $val, %res );
194 14 ▶ while ( my $arg = shift @args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $arg = shift @args) 20 14 34
195  
196 20 state $eq = qr/=/;
197  
198 20 ▶ if ( $arg =~ $eq ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /$eq/) { } 13 7 20
199 13 ( $key, $val ) = split $eq, $arg;
200  
201 # TODO - better way to remove surrounding quotes
202 13 ▶ if ( ( $val =~ /^(['"])(.*)(['"])$/ ) && ( $1 eq $3 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^(['"])(.*)(['"])$/ and $1 eq $3) 8 5 13
Condition66%2/3
$val =~ /^(['"])(.*)(['"])$/ and $1 eq $3
!ll&&!rl&&r
508
MC/DC50%1/2
$val =~ /^(['"])(.*)(['"])$/ and $1 eq $3
$val =~ /^(['"])(.*)(['"])$/ $1 eq $3
Truth table66%2/3
A and B
A= $val =~ /^(['"])(.*)(['"])$/B= $1 eq $3
ABresult
0X0
100
111
203 8 ▶ $val = $2 // '';
Condition50%1/2
$2 // ""
l!l
80
MC/DC0%0/1
$2 // ""
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
204 }
205  
206 }
207 else {
208 7 $val = 1;
209 7 ▶ if ( ($key) = ( $arg =~ /^no[_-](\w+(?:[-_]\w+)*)$/ ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (($key) = $arg =~ /^no[_-](\w+(?:[-_]\w+)*)$/) { } 4 3 7
210 4 $val = 0;
211 }
212 else {
213 3 $key = $arg;
214 }
215 }
216  
217 20 $res{$key} = $val;
218 }
219  
220 14 ▶ if ($allowed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allowed) 13 1 14
221 13 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
222 13 lock_keys( %res, @{$allowed} );
223 }
224 3 ▶ catch {
Subroutine __ANON__: called CC=2 SCAR=8.1
225 3 ▶ if (/Hash has key '(.+)' which is not in the new key set/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/Hash has key '(.+)' which is not in the new key set/) { } 3 0 3
226 3 die sprintf( "Invalid argument key '\%s'\n", $1 );
227 }
228 else {
229 0 die "Unknown error checking argument keys\n";
230 }
231 13 };
232 }
233  
234 11 return \%res;
235 }
236  
237 =head2 C<write_verbatim>
238  
239 $self->write_verbatim($text);
240  
241 A utility method to write verbatim text, indented by
242 L</verbatim_indent>.
243  
244 =cut
245  
246 0 ▶sub write_verbatim {
Subroutine write_verbatim: not called CC=1 SCAR=6.9
Pod documented
247 0 my ( $self, $text ) = @_;
248  
249 0 my $indent = ' ' x ( $self->verbatim_indent );
250 0 $text =~ s/^/${indent}/mg;
251 0 $text =~ s/([^\n])\n?$/$1\n\n/;
252  
253 0 $self->write($text);
254 }
255  
256 =begin :internal
257  
258 =head2 C<_write_cmd>
259  
260 $self->_write_cmd('=head1 SECTION');
261  
262 An internal utility method to write a command line.
263  
264 =end :internal
265  
266 =cut
267  
268 59 ▶sub _write_cmd {
Subroutine _write_cmd: called CC=1 SCAR=0.0
269 59 my ( $self, $text ) = @_;
270 59 $text =~ s/([^\n])\n?$/$1\n\n/;
271  
272 59 $self->write($text);
273 }
274  
275 =head2 C<write_para>
276  
277 $self->write_para('This is a paragraph');
278  
279 Utility method to write a POD paragraph.
280  
281 =cut
282  
283 10 ▶sub write_para {
Subroutine write_para: called CC=2 SCAR=7.1
Pod documented
284 10 my ( $self, $text ) = @_;
285 10 ▶ $text //= '';
Condition50%1/2
$text //= ""
l!l
100
MC/DC0%0/1
$text //= ""
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
286 10 $self->write( $text . "\n\n" );
287 }
288  
289 =head2 C<write_head1>
290  
291 =head2 C<write_head2>
292  
293 =head2 C<write_head3>
294  
295 =head2 C<write_head4>
296  
297 =head2 C<write_over>
298  
299 =head2 C<write_item>
300  
301 =head2 C<write_back>
302  
303 =head2 C<write_begin>
304  
305 =head2 C<write_end>
306  
307 =head2 C<write_for>
308  
309 =head2 C<write_encoding>
310  
311 =head2 C<write_cut>
312  
313 =head2 C<write_pod>
314  
315 $self->write_head1($text);
316  
317 Utility methods to write POD specific commands to the C<output_file>.
318  
319 These methods ensure the POD commands have extra newlines for
320 compatibility with older POD parsers.
321  
322 =cut
323  
324 {
325 5 foreach my $cmd (
326 qw/ head1 head2 head3 head4
327 over item begin end for encoding /
328 )
329 {
330 fresh(
331 55 ▶ "write_${cmd}" => sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
332 55 my ( $self, $text ) = @_;
333 55 ▶ $text //= '';
Condition50%1/2
$text //= ""
l!l
550
MC/DC0%0/1
$text //= ""
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
334 55 $self->_write_cmd( '=' . $cmd . ' ' . $text );
335 }
336 50 );
337 }
338  
339 5 foreach my $cmd (qw/ pod back cut /) {
340 fresh(
341 4 ▶ "write_${cmd}" => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
342 4 my ($self) = @_;
343 4 $self->_write_cmd( '=' . $cmd );
344 }
345 15 );
346 }
347  
348 }
349  
350 5 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
351  
352 5 1;
« Readme/Plugin/requires.pm ↑ Summary Readme/Plugin/version.pm »