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.019;
2             # ABSTRACT: Extract a string from a specially formatted comment
3              
4 9     9   5074 use Moose::Role;
  9         30  
  9         73  
5              
6             # BEGIN BOILERPLATE
7 9     9   47220 use v5.20.0;
  9         42  
8 9     9   53 use warnings;
  9         23  
  9         486  
9 9     9   63 use utf8;
  9         30  
  9         76  
10 9     9   307 no feature 'switch';
  9         27  
  9         1211  
11 9     9   195 use experimental qw(postderef postderef_qq); # This experiment gets mainlined.
  9         29  
  9         73  
12             # END BOILERPLATE
13              
14 9     9   943 use namespace::autoclean;
  9         36  
  9         71  
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   198 my ($self, $ppi_document, $key) = @_;
57              
58 60         1744 my $regex = qr/^\s*#+\s*$key:\s*(.+)$/m;
59              
60 60         188 my $content;
61             my $finder = sub {
62 745     745   7832 my $node = $_[1];
63 745 100       2792 return 0 unless $node->isa('PPI::Token::Comment');
64 64 100       290 if ( $node->content =~ $regex ) {
65 33         417 $content = $1;
66 33         140 return 1;
67             }
68 31         371 return 0;
69 60         350 };
70              
71 60         464 $ppi_document->find_first($finder);
72              
73 60         1181 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.019
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
109              
110             This module should work on any version of perl still receiving updates from
111             the Perl 5 Porters. This means it should work on any version of perl released
112             in the last two to three years. (That is, if the most recently released
113             version is v5.40, then this module should work on both v5.40 and v5.38.)
114              
115             Although it may work on older versions of perl, no guarantee is made that the
116             minimum required version will not be increased. The version may be increased
117             for any reason, and there is no promise that patches will be accepted to lower
118             the minimum required perl.
119              
120             =head1 PRIVATE METHODS
121              
122             This role supplies only methods meant to be used internally by its consumer.
123              
124             =head2 _extract_comment_content($ppi_doc, $key)
125              
126             Given a key, try to find a comment matching C<# $key:> in the C<$ppi_document>
127             and return everything but the prefix.
128              
129             e.g., given a document with a comment in it of the form:
130              
131             # ABSTRACT: Yada yada...
132              
133             ...and this is called...
134              
135             $self->_extract_comment_content($ppi, 'ABSTRACT')
136              
137             ...it returns to us:
138              
139             Yada yada...
140              
141             =head1 AUTHOR
142              
143             Ricardo SIGNES <cpan@semiotic.systems>
144              
145             =head1 COPYRIGHT AND LICENSE
146              
147             This software is copyright (c) 2023 by Ricardo SIGNES.
148              
149             This is free software; you can redistribute it and/or modify it under
150             the same terms as the Perl 5 programming language system itself.
151              
152             =cut