File Coverage

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


line stmt bran cond sub pod time code
1             package Pod::Weaver::Role::StringFromComment;
2             # ABSTRACT: Extract a string from a specially formatted comment
3             $Pod::Weaver::Role::StringFromComment::VERSION = '4.017';
4 9     9   6418 use Moose::Role;
  9         26  
  9         75  
5 9     9   48632 use namespace::autoclean;
  9         24  
  9         82  
6              
7             #pod =head1 OVERVIEW
8             #pod
9             #pod This role assists L<Pod::Weaver sections|Pod::Weaver::Role::Section> by
10             #pod allowing them to pull strings from the source comments formatted like:
11             #pod
12             #pod # KEYNAME: Some string...
13             #pod
14             #pod This is probably the most familiar to people using lines like the following to
15             #pod allow the L<Name section|Pod::Weaver::Section::Name> to determine a module's
16             #pod abstract:
17             #pod
18             #pod # ABSTRACT: Provides the HypnoToad with mind-control powers
19             #pod
20             #pod It will extract these strings by inspecting the C<ppi_document> which
21             #pod must be given.
22             #pod
23             #pod =head1 PRIVATE METHODS
24             #pod
25             #pod This role supplies only methods meant to be used internally by its consumer.
26             #pod
27             #pod =head2 _extract_comment_content($ppi_doc, $key)
28             #pod
29             #pod Given a key, try to find a comment matching C<# $key:> in the C<$ppi_document>
30             #pod and return everything but the prefix.
31             #pod
32             #pod e.g., given a document with a comment in it of the form:
33             #pod
34             #pod # ABSTRACT: Yada yada...
35             #pod
36             #pod ...and this is called...
37             #pod
38             #pod $self->_extract_comment_content($ppi, 'ABSTRACT')
39             #pod
40             #pod ...it returns to us:
41             #pod
42             #pod Yada yada...
43             #pod
44             #pod =cut
45              
46             sub _extract_comment_content {
47 60     60   184 my ($self, $ppi_document, $key) = @_;
48              
49 60         1916 my $regex = qr/^\s*#+\s*$key:\s*(.+)$/m;
50              
51 60         188 my $content;
52             my $finder = sub {
53 745     745   7841 my $node = $_[1];
54 745 100       2852 return 0 unless $node->isa('PPI::Token::Comment');
55 64 100       241 if ( $node->content =~ $regex ) {
56 33         426 $content = $1;
57 33         100 return 1;
58             }
59 31         422 return 0;
60 60         329 };
61              
62 60         444 $ppi_document->find_first($finder);
63              
64 60         1088 return $content;
65             }
66              
67             1;
68              
69             __END__
70              
71             =pod
72              
73             =encoding UTF-8
74              
75             =head1 NAME
76              
77             Pod::Weaver::Role::StringFromComment - Extract a string from a specially formatted comment
78              
79             =head1 VERSION
80              
81             version 4.017
82              
83             =head1 OVERVIEW
84              
85             This role assists L<Pod::Weaver sections|Pod::Weaver::Role::Section> by
86             allowing them to pull strings from the source comments formatted like:
87              
88             # KEYNAME: Some string...
89              
90             This is probably the most familiar to people using lines like the following to
91             allow the L<Name section|Pod::Weaver::Section::Name> to determine a module's
92             abstract:
93              
94             # ABSTRACT: Provides the HypnoToad with mind-control powers
95              
96             It will extract these strings by inspecting the C<ppi_document> which
97             must be given.
98              
99             =head1 PRIVATE METHODS
100              
101             This role supplies only methods meant to be used internally by its consumer.
102              
103             =head2 _extract_comment_content($ppi_doc, $key)
104              
105             Given a key, try to find a comment matching C<# $key:> in the C<$ppi_document>
106             and return everything but the prefix.
107              
108             e.g., given a document with a comment in it of the form:
109              
110             # ABSTRACT: Yada yada...
111              
112             ...and this is called...
113              
114             $self->_extract_comment_content($ppi, 'ABSTRACT')
115              
116             ...it returns to us:
117              
118             Yada yada...
119              
120             =head1 AUTHOR
121              
122             Ricardo SIGNES <rjbs@cpan.org>
123              
124             =head1 COPYRIGHT AND LICENSE
125              
126             This software is copyright (c) 2021 by Ricardo SIGNES.
127              
128             This is free software; you can redistribute it and/or modify it under
129             the same terms as the Perl 5 programming language system itself.
130              
131             =cut