Weaver/Section/GenerateSection.pm

filter: Statementstmt 93.7% 60 / 64 Branchbran 62.5% 10 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.2% 15 / 17 Podpod 0.0% 0 / 3 totaltotal 85.0% 85 / 100 CC 9 SCAR 10.6
CC
9
Cov
88%
weave_section
6.2
fill_in_string
4.6
__ANON__
2.0
CRAP 2.9
« Weaver/Section/Collect.pm ↑ Summary Weaver/Section/Name.pm »
1 package Pod::Weaver::Section::GenerateSection 4.022;
2 # ABSTRACT: add pod section from an interpolated piece of text
3  
4 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 with 'Pod::Weaver::Role::Section';
6  
7 # BEGIN BOILERPLATE
8 1 ▶use v5.22.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶no feature 'switch';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use experimental qw(postderef postderef_qq); # This experiment gets mainlined.
Subroutine BEGIN: called CC=1 SCAR=0.0
13 # END BOILERPLATE
14  
15 1 ▶use Pod::Elemental::Element::Nested;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use Pod::Elemental::Element::Pod5::Ordinary;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Text::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 #pod =head1 SYNOPSIS
22 #pod
23 #pod In your F<weaver.ini>
24 #pod
25 #pod [GenerateSection]
26 #pod title = HOMEPAGE
27 #pod text = This is the POD for distribution {{$name}}. Check out what we have
28 #pod text = been up to at {{$homepage}}
29 #pod
30 #pod The title value can be omited if passed as the plugin name:
31 #pod
32 #pod [GenerateSection / HOMEPAGE]
33 #pod
34 #pod =head1 DESCRIPTION
35 #pod
36 #pod This plugin allows the creation of simple text sections, with or without the
37 #pod use of Text::Template for templated text.
38 #pod
39 #pod The C<text> parameters become the lines of the template.
40 #pod
41 #pod The values of text are concatenated and variable names with matching values on
42 #pod the distribution are interpolated. Specifying the heading level allows one to
43 #pod write down a rather long section of POD text without need for extra files. For
44 #pod example:
45 #pod
46 #pod [GenerateSection / FEEDBACK]
47 #pod head = 1
48 #pod [GenerateSection / Reporting bugs]
49 #pod head = 2
50 #pod text = Please report bugs when you find them. While we do have a mailing
51 #pod text = list, please use the bug tracker at {{$bugtracker_web}}
52 #pod text = to report bugs
53 #pod [GenerateSection / Homegape]
54 #pod head = 2
55 #pod text = Also, come check out our other projects at
56 #pod text = {{$homepage}}
57 #pod
58 #pod =head1 TEMPLATE RENDERING
59 #pod
60 #pod When rendering as a template, the variables C<$plugin>, C<$dist>, and
61 #pod C<$distmeta> will be provided, set to the GenerateSection plugin,
62 #pod C<Dist::Zilla> object, and the distribution metadata hash respectively. For
63 #pod convenience, the following variables are also set:
64 #pod
65 #pod =for :list
66 #pod * C<< $name >>
67 #pod * C<< $version >>
68 #pod * C<< $homepage >>
69 #pod * C<< $repository_web >>
70 #pod * C<< $repository_url >>
71 #pod * C<< $bugtracker_web >>
72 #pod * C<< $bugtracker_email >>
73 #pod
74 #pod =attr text
75 #pod
76 #pod The text to be added to the section. Multiple values are allowed and will be
77 #pod concatenated. Certain sequences on the text will be replaced (see below).
78 #pod
79 #pod =cut
80  
81 7 ▶sub mvp_multivalue_args { return qw(text) }
Subroutine mvp_multivalue_args: called CC=1 SCAR=0.0
Pod undocumented
82 has text => (
83 is => 'ro',
84 isa => 'ArrayRef',
85 lazy => 1,
86 1 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
87 1 );
88  
89 #pod =attr head
90 #pod
91 #pod This is the I<X> to use in the C<=headX> that's created. If it's C<0> then no
92 #pod heading is added. It defaults to C<1>.
93 #pod
94 #pod =cut
95  
96 1 has head => (
97 is => 'ro',
98 isa => 'Int',
99 lazy => 1,
100 default => 1,
101 );
102  
103 #pod =attr title
104 #pod
105 #pod The title for this section. If none is given, the plugin's name is used.
106 #pod
107 #pod =cut
108  
109 has title => (
110 is => 'ro',
111 isa => 'Str',
112 lazy => 1,
113 0 ▶ default => sub { $_[0]->plugin_name },
Subroutine __ANON__: not called CC=1 SCAR=6.9
114 1 );
115  
116 #pod =attr main_module_only
117 #pod
118 #pod If true, this attribute indicates that only the main module's Pod should be
119 #pod altered. By default, it is false.
120 #pod
121 #pod =cut
122  
123 1 has main_module_only => (
124 is => 'ro',
125 isa => 'Bool',
126 lazy => 1,
127 default => 0,
128 );
129  
130 #pod =attr
131 #pod
132 #pod If true, the text is treated as a L<Text::Template> template and rendered.
133 #pod This attribute B<is true by default>.
134 #pod
135 #pod =cut
136  
137 1 has is_template => (
138 is => 'ro',
139 isa => 'Bool',
140 lazy => 1,
141 default => 1,
142 );
143  
144 7 ▶sub weave_section {
Subroutine weave_section: called CC=6 SCAR=18.2
Pod undocumented
145 7 my ($self, $document, $input) = @_;
146  
147 7 ▶ if ($self->main_module_only) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->main_module_only) 0 7 7
148 0 ▶ return if $input->{zilla}->main_module->name ne $input->{filename};
Branch: 1 branch
BranchTrueFalseTotal
if $input->{"zilla"}->main_module->name ne $input->{"filename"} 0 0 0
149 }
150  
151 7 my $text = join ("\n", $self->text->@*);
152  
153 7 ▶ if ($self->is_template) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_template) 6 1 7
154 6 my %stash;
155  
156 6 ▶ if ($input->{zilla}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($input->{"zilla"}) 2 4 6
157 %stash = (
158 dist => \($input->{zilla}),
159 distmeta => \($input->{distmeta}),
160 plugin => \($self),
161  
162 name => $input->{distmeta}{name},
163 version => $input->{distmeta}{version},
164 homepage => $input->{distmeta}{resources}{homepage},
165 repository_web => $input->{distmeta}{resources}{repository}{web},
166 repository_url => $input->{distmeta}{resources}{repository}{url},
167 bugtracker_web => $input->{distmeta}{resources}{bugtracker}{web},
168 bugtracker_email => $input->{distmeta}{resources}{bugtracker}{mailto},
169 2 );
170 }
171  
172 6 $text = $self->fill_in_string($text, \%stash);
173 }
174  
175 7 my $element = Pod::Elemental::Element::Pod5::Ordinary->new({ content => $text });
176  
177 7 ▶ if ($self->head) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->head) 6 1 7
178 6 $element = Pod::Elemental::Element::Nested->new({
179 command => "head" . $self->head,
180 content => $self->title,
181 children => [ $element ],
182 });
183 }
184  
185 7 push $document->children->@*, $element;
186 }
187  
188 # BEGIN CODE IMPORTED FROM Dist::Zilla::Role::TextTemplate
189 #pod =attr delim
190 #pod
191 #pod If given, this must be an arrayref with two elements. These will be the
192 #pod opening and closing delimiters of template variable sections. By default they
193 #pod are C<{{> and C<}}>.
194 #pod
195 #pod =cut
196  
197 has delim => (
198 is => 'ro',
199 isa => 'ArrayRef',
200 lazy => 1,
201 init_arg => undef,
202 6 ▶ default => sub { [ qw( {{ }} ) ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
203 1 );
204  
205 6 ▶sub fill_in_string {
Subroutine fill_in_string: called CC=4 SCAR=15.2
Pod undocumented
206 6 my ($self, $string, $stash, $arg) = @_;
207  
208 6 ▶ $self->log_fatal("Cannot use undef as a template string")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 6 6
209 unless defined $string;
210  
211 my $tmpl = Text::Template->new(
212 TYPE => 'STRING',
213 SOURCE => $string,
214 DELIMITERS => $self->delim,
215 0 ▶ BROKEN => sub { my %hash = @_; die $hash{error}; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
216 6 %$arg,
217 );
218  
219 6 ▶ $self->log_fatal("Could not create a Text::Template object from:\n$string")
Branch: 1 branch
BranchTrueFalseTotal
unless $tmpl 0 6 6
220 unless $tmpl;
221  
222 6 my $content = $tmpl->fill_in(%$arg, HASH => $stash);
223  
224 6 ▶ $self->log_fatal("Filling in the template returned undef for:\n$string")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $content 0 6 6
225 unless defined $content;
226  
227 6 return $content;
228 }
229 # END CODE IMPORTED FROM Dist::Zilla::Role::TextTemplate
230  
231 1 __PACKAGE__->meta->make_immutable;
232 1 1;
233  
234 __END__
« Weaver/Section/Collect.pm ↑ Summary Weaver/Section/Name.pm »