File Coverage

blib/lib/Pod/Weaver/Section/GenerateSection.pm
Criterion Covered Total %
statement 46 49 93.8
branch 10 16 62.5
condition n/a
subroutine 11 12 91.6
pod 0 3 0.0
total 67 80 83.7


line stmt bran cond sub pod time code
1             package Pod::Weaver::Section::GenerateSection;
2             # ABSTRACT: add pod section from an interpolated piece of text
3             $Pod::Weaver::Section::GenerateSection::VERSION = '4.017';
4 1     1   3763 use strict;
  1         5  
  1         41  
5 1     1   6 use warnings;
  1         2  
  1         36  
6 1     1   5 use utf8;
  1         2  
  1         10  
7              
8 1     1   25 use Moose;
  1         4  
  1         9  
9              
10             with 'Pod::Weaver::Role::Section';
11              
12 1     1   8830 use Pod::Elemental::Element::Nested;
  1         66362  
  1         51  
13 1     1   12 use Pod::Elemental::Element::Pod5::Ordinary;
  1         2  
  1         36  
14 1     1   973 use Text::Template;
  1         4192  
  1         79  
15              
16 1     1   14 use namespace::autoclean;
  1         4  
  1         16  
17              
18             #pod =head1 SYNOPSIS
19             #pod
20             #pod In your F<weaver.ini>
21             #pod
22             #pod [GenerateSection]
23             #pod title = HOMEPAGE
24             #pod text = This is the POD for distribution {{$name}}. Check out what we have
25             #pod text = been up to at {{$homepage}}
26             #pod
27             #pod The title value can be omited if passed as the plugin name:
28             #pod
29             #pod [GenerateSection / HOMEPAGE]
30             #pod
31             #pod =head1 DESCRIPTION
32             #pod
33             #pod This plugin allows the creation of simple text sections, with or without the
34             #pod use of Text::Template for templated text.
35             #pod
36             #pod The C<text> parameters become the lines of the template.
37             #pod
38             #pod The values of text are concatenated and variable names with matching values on
39             #pod the distribution are interpolated. Specifying the heading level allows one to
40             #pod write down a rather long section of POD text without need for extra files. For
41             #pod example:
42             #pod
43             #pod [GenerateSection / FEEDBACK]
44             #pod head = 1
45             #pod [GenerateSection / Reporting bugs]
46             #pod head = 2
47             #pod text = Please report bugs when you find them. While we do have a mailing
48             #pod text = list, please use the bug tracker at {{$bugtracker_web}}
49             #pod text = to report bugs
50             #pod [GenerateSection / Homegape]
51             #pod head = 2
52             #pod text = Also, come check out our other projects at
53             #pod text = {{$homepage}}
54             #pod
55             #pod =head1 TEMPLATE RENDERING
56             #pod
57             #pod When rendering as a template, the variables C<$plugin>, C<$dist>, and
58             #pod C<$distmeta> will be provided, set to the GenerateSection plugin,
59             #pod C<Dist::Zilla> object, and the distribution metadata hash respectively. For
60             #pod convenience, the following variables are also set:
61             #pod
62             #pod =for :list
63             #pod * C<< $name >>
64             #pod * C<< $version >>
65             #pod * C<< $homepage >>
66             #pod * C<< $repository_web >>
67             #pod * C<< $repository_url >>
68             #pod * C<< $bugtracker_web >>
69             #pod * C<< $bugtracker_email >>
70             #pod
71             #pod =attr text
72             #pod
73             #pod The text to be added to the section. Multiple values are allowed and will be
74             #pod concatenated. Certain sequences on the text will be replaced (see below).
75             #pod
76             #pod =cut
77              
78 7     7 0 17441 sub mvp_multivalue_args { return qw(text) }
79             has text => (
80             is => 'ro',
81             isa => 'ArrayRef',
82             lazy => 1,
83             default => sub { [] },
84             );
85              
86             #pod =attr head
87             #pod
88             #pod This is the I<X> to use in the C<=headX> that's created. If it's C<0> then no
89             #pod heading is added. It defaults to C<1>.
90             #pod
91             #pod =cut
92              
93             has head => (
94             is => 'ro',
95             isa => 'Int',
96             lazy => 1,
97             default => 1,
98             );
99              
100             #pod =attr title
101             #pod
102             #pod The title for this section. If none is given, the plugin's name is used.
103             #pod
104             #pod =cut
105              
106             has title => (
107             is => 'ro',
108             isa => 'Str',
109             lazy => 1,
110             default => sub { $_[0]->plugin_name },
111             );
112              
113             #pod =attr main_module_only
114             #pod
115             #pod If true, this attribute indicates that only the main module's Pod should be
116             #pod altered. By default, it is false.
117             #pod
118             #pod =cut
119              
120             has main_module_only => (
121             is => 'ro',
122             isa => 'Bool',
123             lazy => 1,
124             default => 0,
125             );
126              
127             #pod =attr
128             #pod
129             #pod If true, the text is treated as a L<Text::Template> template and rendered.
130             #pod This attribute B<is true by default>.
131             #pod
132             #pod =cut
133              
134             has is_template => (
135             is => 'ro',
136             isa => 'Bool',
137             lazy => 1,
138             default => 1,
139             );
140              
141             sub weave_section {
142 7     7 0 28 my ($self, $document, $input) = @_;
143              
144 7 50       272 if ($self->main_module_only) {
145 0 0       0 return if $input->{zilla}->main_module->name ne $input->{filename};
146             }
147              
148 7         20 my $text = join ("\n", @{ $self->text });
  7         241  
149              
150 7 100       258 if ($self->is_template) {
151 6         12 my %stash;
152              
153 6 100       23 if ($input->{zilla}) {
154             %stash = (
155             dist => \($input->{zilla}),
156             distmeta => \($input->{distmeta}),
157             plugin => \($self),
158              
159             name => $input->{distmeta}{name},
160             version => $input->{distmeta}{version},
161             homepage => $input->{distmeta}{resources}{homepage},
162             repository_web => $input->{distmeta}{resources}{repository}{web},
163             repository_url => $input->{distmeta}{resources}{repository}{url},
164             bugtracker_web => $input->{distmeta}{resources}{bugtracker}{web},
165             bugtracker_email => $input->{distmeta}{resources}{bugtracker}{mailto},
166 2         38 );
167             }
168              
169 6         31 $text = $self->fill_in_string($text, \%stash);
170             }
171              
172 7         420 my $element = Pod::Elemental::Element::Pod5::Ordinary->new({ content => $text });
173              
174 7 100       1567 if ($self->head) {
175 6         195 $element = Pod::Elemental::Element::Nested->new({
176             command => "head" . $self->head,
177             content => $self->title,
178             children => [ $element ],
179             });
180             }
181              
182 7         2925 push @{ $document->children }, $element;
  7         248  
183             }
184              
185             # BEGIN CODE IMPORTED FROM Dist::Zilla::Role::TextTemplate
186             #pod =attr delim
187             #pod
188             #pod If given, this must be an arrayref with two elements. These will be the
189             #pod opening and closing delimiters of template variable sections. By default they
190             #pod are C<{{> and C<}}>.
191             #pod
192             #pod =cut
193              
194             has delim => (
195             is => 'ro',
196             isa => 'ArrayRef',
197             lazy => 1,
198             init_arg => undef,
199             default => sub { [ qw( {{ }} ) ] },
200             );
201              
202             sub fill_in_string {
203 6     6 0 20 my ($self, $string, $stash, $arg) = @_;
204              
205 6 50       18 $self->log_fatal("Cannot use undef as a template string")
206             unless defined $string;
207              
208             my $tmpl = Text::Template->new(
209             TYPE => 'STRING',
210             SOURCE => $string,
211             DELIMITERS => $self->delim,
212 0     0   0 BROKEN => sub { my %hash = @_; die $hash{error}; },
  0         0  
213 6         208 %$arg,
214             );
215              
216 6 50       1123 $self->log_fatal("Could not create a Text::Template object from:\n$string")
217             unless $tmpl;
218              
219 6         65 my $content = $tmpl->fill_in(%$arg, HASH => $stash);
220              
221 6 50       4205 $self->log_fatal("Filling in the template returned undef for:\n$string")
222             unless defined $content;
223              
224 6         73 return $content;
225             }
226             # END CODE IMPORTED FROM Dist::Zilla::Role::TextTemplate
227              
228             __PACKAGE__->meta->make_immutable;
229             1;
230              
231             __END__
232              
233             =pod
234              
235             =encoding UTF-8
236              
237             =head1 NAME
238              
239             Pod::Weaver::Section::GenerateSection - add pod section from an interpolated piece of text
240              
241             =head1 VERSION
242              
243             version 4.017
244              
245             =head1 SYNOPSIS
246              
247             In your F<weaver.ini>
248              
249             [GenerateSection]
250             title = HOMEPAGE
251             text = This is the POD for distribution {{$name}}. Check out what we have
252             text = been up to at {{$homepage}}
253              
254             The title value can be omited if passed as the plugin name:
255              
256             [GenerateSection / HOMEPAGE]
257              
258             =head1 DESCRIPTION
259              
260             This plugin allows the creation of simple text sections, with or without the
261             use of Text::Template for templated text.
262              
263             The C<text> parameters become the lines of the template.
264              
265             The values of text are concatenated and variable names with matching values on
266             the distribution are interpolated. Specifying the heading level allows one to
267             write down a rather long section of POD text without need for extra files. For
268             example:
269              
270             [GenerateSection / FEEDBACK]
271             head = 1
272             [GenerateSection / Reporting bugs]
273             head = 2
274             text = Please report bugs when you find them. While we do have a mailing
275             text = list, please use the bug tracker at {{$bugtracker_web}}
276             text = to report bugs
277             [GenerateSection / Homegape]
278             head = 2
279             text = Also, come check out our other projects at
280             text = {{$homepage}}
281              
282             =head1 ATTRIBUTES
283              
284             =head2 text
285              
286             The text to be added to the section. Multiple values are allowed and will be
287             concatenated. Certain sequences on the text will be replaced (see below).
288              
289             =head2 head
290              
291             This is the I<X> to use in the C<=headX> that's created. If it's C<0> then no
292             heading is added. It defaults to C<1>.
293              
294             =head2 title
295              
296             The title for this section. If none is given, the plugin's name is used.
297              
298             =head2 main_module_only
299              
300             If true, this attribute indicates that only the main module's Pod should be
301             altered. By default, it is false.
302              
303             =head2
304              
305             If true, the text is treated as a L<Text::Template> template and rendered.
306             This attribute B<is true by default>.
307              
308             =head2 delim
309              
310             If given, this must be an arrayref with two elements. These will be the
311             opening and closing delimiters of template variable sections. By default they
312             are C<{{> and C<}}>.
313              
314             =head1 TEMPLATE RENDERING
315              
316             When rendering as a template, the variables C<$plugin>, C<$dist>, and
317             C<$distmeta> will be provided, set to the GenerateSection plugin,
318             C<Dist::Zilla> object, and the distribution metadata hash respectively. For
319             convenience, the following variables are also set:
320              
321             =over 4
322              
323             =item *
324              
325             C<< $name >>
326              
327             =item *
328              
329             C<< $version >>
330              
331             =item *
332              
333             C<< $homepage >>
334              
335             =item *
336              
337             C<< $repository_web >>
338              
339             =item *
340              
341             C<< $repository_url >>
342              
343             =item *
344              
345             C<< $bugtracker_web >>
346              
347             =item *
348              
349             C<< $bugtracker_email >>
350              
351             =back
352              
353             =head1 AUTHOR
354              
355             Ricardo SIGNES <rjbs@cpan.org>
356              
357             =head1 COPYRIGHT AND LICENSE
358              
359             This software is copyright (c) 2021 by Ricardo SIGNES.
360              
361             This is free software; you can redistribute it and/or modify it under
362             the same terms as the Perl 5 programming language system itself.
363              
364             =cut