File Coverage

blib/lib/Pod/Weaver/Role/StringFromComment.pm
Criterion Covered Total %
statement 32 32 100.0
branch 4 4 100.0
condition n/a
subroutine 9 9 100.0
pod n/a
total 45 45 100.0


line stmt bran cond sub pod time code
1             package Pod::Weaver::Role::StringFromComment 4.018;
2             # ABSTRACT: Extract a string from a specially formatted comment
3              
4 9     9   5155 use Moose::Role;
  9         21  
  9         60  
5              
6             # BEGIN BOILERPLATE
7 9     9   45018 use v5.20.0;
  9         34  
8 9     9   55 use warnings;
  9         33  
  9         453  
9 9     9   56 use utf8;
  9         23  
  9         57  
10 9     9   287 no feature 'switch';
  9         22  
  9         1167  
11 9     9   143 use experimental qw(postderef postderef_qq); # This experiment gets mainlined.
  9         24  
  9         69  
12             # END BOILERPLATE
13              
14 9     9   862 use namespace::autoclean;
  9         21  
  9         68  
15              
16             #pod =head1 OVERVIEW
17             #pod
18             #pod This role assists L<Pod::Weaver sections|Pod::Weaver::Role::Section> by
19             #pod allowing them to pull strings from the source comments formatted like:
20             #pod
21             #pod # KEYNAME: Some string...
22             #pod
23             #pod This is probably the most familiar to people using lines like the following to
24             #pod allow the L<Name section|Pod::Weaver::Section::Name> to determine a module's
25             #pod abstract:
26             #pod
27             #pod # ABSTRACT: Provides the HypnoToad with mind-control powers
28             #pod
29             #pod It will extract these strings by inspecting the C<ppi_document> which
30             #pod must be given.
31             #pod
32             #pod =head1 PRIVATE METHODS
33             #pod
34             #pod This role supplies only methods meant to be used internally by its consumer.
35             #pod
36             #pod =head2 _extract_comment_content($ppi_doc, $key)
37             #pod
38             #pod Given a key, try to find a comment matching C<# $key:> in the C<$ppi_document>
39             #pod and return everything but the prefix.
40             #pod
41             #pod e.g., given a document with a comment in it of the form:
42             #pod
43             #pod # ABSTRACT: Yada yada...
44             #pod
45             #pod ...and this is called...
46             #pod
47             #pod $self->_extract_comment_content($ppi, 'ABSTRACT')
48             #pod
49             #pod ...it returns to us:
50             #pod
51             #pod Yada yada...
52             #pod
53             #pod =cut
54              
55             sub _extract_comment_content {
56 60     60   168 my ($self, $ppi_document, $key) = @_;
57              
58 60         1935 my $regex = qr/^\s*#+\s*$key:\s*(.+)$/m;
59              
60 60         204 my $content;
61             my $finder = sub {
62 745     745   7521 my $node = $_[1];
63 745 100       2663 return 0 unless $node->isa('PPI::Token::Comment');
64 64 100       232 if ( $node->content =~ $regex ) {
65 33         411 $content = $1;
66 33         104 return 1;
67             }
68 31         358 return 0;
69 60         306 };
70              
71 60         409 $ppi_document->find_first($finder);
72              
73 60         1057 return $content;
74             }
75              
76             1;
77              
78             __END__
79              
80             =pod
81              
82             =encoding UTF-8
83              
84             =head1 NAME
85              
86             Pod::Weaver::Role::StringFromComment - Extract a string from a specially formatted comment
87              
88             =head1 VERSION
89              
90             version 4.018
91              
92             =head1 OVERVIEW
93              
94             This role assists L<Pod::Weaver sections|Pod::Weaver::Role::Section> by
95             allowing them to pull strings from the source comments formatted like:
96              
97             # KEYNAME: Some string...
98              
99             This is probably the most familiar to people using lines like the following to
100             allow the L<Name section|Pod::Weaver::Section::Name> to determine a module's
101             abstract:
102              
103             # ABSTRACT: Provides the HypnoToad with mind-control powers
104              
105             It will extract these strings by inspecting the C<ppi_document> which
106             must be given.
107              
108             =head1 PERL VERSION SUPPORT
109              
110             This module has the same support period as perl itself: it supports the two
111             most recent versions of perl. (That is, if the most recently released version
112             is v5.40, then this module should work on both v5.40 and v5.38.)
113              
114             Although it may work on older versions of perl, no guarantee is made that the
115             minimum required version will not be increased. The version may be increased
116             for any reason, and there is no promise that patches will be accepted to lower
117             the minimum required perl.
118              
119             =head1 PRIVATE METHODS
120              
121             This role supplies only methods meant to be used internally by its consumer.
122              
123             =head2 _extract_comment_content($ppi_doc, $key)
124              
125             Given a key, try to find a comment matching C<# $key:> in the C<$ppi_document>
126             and return everything but the prefix.
127              
128             e.g., given a document with a comment in it of the form:
129              
130             # ABSTRACT: Yada yada...
131              
132             ...and this is called...
133              
134             $self->_extract_comment_content($ppi, 'ABSTRACT')
135              
136             ...it returns to us:
137              
138             Yada yada...
139              
140             =head1 AUTHOR
141              
142             Ricardo SIGNES <rjbs@semiotic.systems>
143              
144             =head1 COPYRIGHT AND LICENSE
145              
146             This software is copyright (c) 2021 by Ricardo SIGNES.
147              
148             This is free software; you can redistribute it and/or modify it under
149             the same terms as the Perl 5 programming language system itself.
150              
151             =cut