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