Templates/File.pm

filter: Statementstmt 91.1% 72 / 79 Branchbran 83.3% 10 / 12 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 93.3% 14 / 15 Podpod 100% 6 / 6 totaltotal 91.4% 107 / 117 CC 10 SCAR 11.2
CC
10
Cov
90%
pod2text
6.7
chomp
4.0
indent
2.0
CRAP 3.1
« Templates.pm ↑ Summary
1 # ---------------------------------------------------------------------- copyright and license ---
2 #
3 # file: lib/Dist/Zilla/Plugin/Templates/File.pm
4 #
5 # Copyright © 2015, 2016, 2018 Van de Bugger.
6 #
7 # This file is part of perl-Dist-Zilla-Plugin-Templates.
8 #
9 # perl-Dist-Zilla-Plugin-Templates is free software: you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by the Free Software Foundation,
11 # either version 3 of the License, or (at your option) any later version.
12 #
13 # perl-Dist-Zilla-Plugin-Templates is distributed in the hope that it will be useful, but WITHOUT
14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 # PURPOSE. See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # perl-Dist-Zilla-Plugin-Templates. If not, see <http://www.gnu.org/licenses/>.
19 #
20 # SPDX-License-Identifier: GPL-3.0-or-later
21 #
22 # ---------------------------------------------------------------------- copyright and license ---
23  
24 #pod =for test_synopsis BEGIN { die "SKIP: Not Perl code.\n" };
25 #pod
26 #pod =head1 SYNOPSIS
27 #pod
28 #pod In a template:
29 #pod
30 #pod {{ include( 'as-is.txt' ); }}
31 #pod {{ include( 'verbatim.txt' )->indent; }}
32 #pod {{ include( 'template.txt' )->fill_in; }}
33 #pod {{ include( 'readme.pod' )->pod2text; }}
34 #pod {{ include( 'assa.txt' )->chomp->trim->indent; }}
35 #pod …
36 #pod
37 #pod =head1 DESCRIPTION
38 #pod
39 #pod This is a helper module for C<Dist::Zilla::Templates>. It provides few frequently used operations
40 #pod on include files.
41 #pod
42 #pod Usually objects are created by "include" function (see
43 #pod L<Dist::Zilla::Plugin::Templates/"include">).
44 #pod
45 #pod The object evaluated in string context returns the file content, so
46 #pod
47 #pod $file->content
48 #pod
49 #pod and
50 #pod
51 #pod "$file"
52 #pod
53 #pod produces the same result.
54 #pod
55 #pod =head1 SEE ALSO
56 #pod
57 #pod =for :list
58 #pod = L<Dist::Zilla>
59 #pod = L<Dist::Zilla::Role::FileFinderUser>
60 #pod = L<Dist::Zilla::Role::TextTemplater>
61 #pod = L<Text::Template>
62 #pod = L<Pod::Text>
63 #pod = L<Dist::Zilla::Plugin::Templates::Manual>
64 #pod
65 #pod =cut
66  
67 package Dist::Zilla::Plugin::Templates::File;
68  
69 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 3 ▶use namespace::autoclean 0.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 # ^ `namespace::autoclean` pre-0.16 wipes out overloads,
72 # see <https://rt.cpan.org/Ticket/Display.html?id=50938>.
73 3 ▶use version 0.77;
Subroutine BEGIN: called CC=1 SCAR=0.0
74  
75 # ABSTRACT: Frequently used operations on include files
76 3 our $VERSION = 'v0.6.5'; # VERSION
77  
78 3 extends 'Dist::Zilla::File::InMemory';
79  
80 3 ▶use overload '""' => sub {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
81 21 my ( $self ) = @_;
82 21 return $self->content;
83 3 };
84  
85 3 ▶use Pod::Text qw{}; # `Pod::Text` exports `pod2text` by default. Let us avoid it.
Subroutine BEGIN: called CC=1 SCAR=0.0
86 3 ▶use Carp qw{ croak };
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 # --------------------------------------------------------------------------------------------------
89  
90 #pod =attr _plugin
91 #pod
92 #pod Reference to the plugin created this object.
93 #pod
94 #pod C<Object>, read-only.
95 #pod
96 #pod =cut
97  
98 3 has _plugin => (
99 isa => 'Object',
100 is => 'ro',
101 weak_ref => 1,
102 );
103  
104 # --------------------------------------------------------------------------------------------------
105  
106 #pod =method chomp
107 #pod
108 #pod $file->chomp();
109 #pod $file->chomp( $count );
110 #pod
111 #pod Chomps the file content C<$count> times. If C<$count> is not specified, the method chomps all the
112 #pod trailing newlines (more exactly: all the trailing input record separators; see core function
113 #pod C<chomp> for details).
114 #pod
115 #pod Note: The methods chomps newlines from the end of file, I<not> from each line.
116 #pod
117 #pod The method returns C<$self> for chaining.
118 #pod
119 #pod =cut
120  
121 3 ▶sub chomp { ## no critic ( ProhibitBuiltinHomonyms )
Subroutine chomp: called CC=4 SCAR=13.9
Pod documented
122 3 my ( $self, $count ) = @_;
123 3 my $content = $self->content;
124 3 ▶ if ( not defined( $count ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $count) 2 1 3
125 2 $count = -1;
126 };
127 3 ▶ while ( $count and CORE::chomp( $content ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count and CORE::chomp $content) 7 3 10
Condition100%3/3
$count and CORE::chomp $content
!ll&&!rl&&r
127
MC/DC100%2/2
$count and CORE::chomp $content
$count CORE::chomp $content
Truth table100%3/3
A and B
A= $countB= CORE::chomp $content
ABresult
0X0
100
111
128 7 -- $count;
129 };
130 3 $self->content( $content );
131 3 return $self;
132 };
133  
134 # --------------------------------------------------------------------------------------------------
135  
136 #pod =method fill_in
137 #pod
138 #pod $file->fill_in();
139 #pod $file->fill_in( \%variables, \%extra_args );
140 #pod
141 #pod Calls plugin's C<fill_in_file> method (which is defined in C<TextTemplater> role), passing C<$self>
142 #pod as the first argument. Returns C<$self>.
143 #pod
144 #pod Primary purpose of the method is including a file which is template itself:
145 #pod
146 #pod {{ include( 'doc/chapter1.pod' )->fill_in; }}
147 #pod
148 #pod Without C<fill_in>, F<doc/chapter1.pod> is included as-is, Perl code fragments, if any, are not
149 #pod evaluated.
150 #pod
151 #pod See L<Dist::Zilla::Role::TextTemplater/"fill_in_file">.
152 #pod
153 #pod =cut
154  
155 # TODO: Pass arguments recursively?
156  
157 6 ▶sub fill_in {
Subroutine fill_in: called CC=1 SCAR=0.0
Pod documented
158 6 my ( $self, @args ) = @_;
159 6 $self->_plugin->fill_in_file( $self, @args );
160 6 return $self;
161 };
162  
163 # --------------------------------------------------------------------------------------------------
164  
165 #pod =method indent
166 #pod
167 #pod $file->indent();
168 #pod $file->indent( $size );
169 #pod
170 #pod Indents file content by inserting specified number of spaces to the beginning of every non-empty
171 #pod line. By default, file is indented with 4 spaces. The method returns C<$self> for chaining.
172 #pod
173 #pod Primary purpose of the method is including a file into POD as verbatim paragraph(s):
174 #pod
175 #pod =head2 example.pl
176 #pod
177 #pod {{ include( 'ex/example.pl' )->indent; }}
178 #pod
179 #pod =cut
180 #pod
181 #pod =cut
182  
183 3 ▶sub indent {
Subroutine indent: called CC=2 SCAR=6.9
Pod documented
184 3 my ( $self, $size ) = @_;
185 3 ▶ my $indent = ' ' x ( defined( $size ) ? $size : 4 );
Branch: 1 branch
BranchTrueFalseTotal
defined $size ? : 1 2 3
186 3 my $content = $self->content;
187 3 $content =~ s{^(?!$)}{$indent}gmx;
188 3 $self->content( $content );
189 3 return $self;
190 };
191  
192 # --------------------------------------------------------------------------------------------------
193  
194 #pod =method munge
195 #pod
196 #pod $file->munge( \&func );
197 #pod $file->munge( sub { …modify $_ here… } );
198 #pod
199 #pod Calls the specified function. File content is passed to the function in C<$_> variable. Return
200 #pod value of the function is ignored. Value of C<$_> variable becomes new file content. The method
201 #pod returns C<$self> for chaining.
202 #pod
203 #pod =cut
204  
205 1 ▶sub munge {
Subroutine munge: called CC=1 SCAR=0.0
Pod documented
206 1 my ( $self, $sub ) = @_;
207 1 local $_ = $self->content;
208 1 $sub->();
209 1 $self->content( $_ );
210 1 return $self;
211 };
212  
213 # --------------------------------------------------------------------------------------------------
214  
215 #pod =method pod2text
216 #pod
217 #pod $file->pod2text( %options );
218 #pod
219 #pod Converts POD to plain text. It is a simple wrapper over C<Pod::Text>. See documentation on
220 #pod C<Pod::Text::new> for description of available options. The method returns C<$self> for chaining.
221 #pod
222 #pod Example:
223 #pod
224 #pod {{ include( 'readme.pod' )->pod2text( width => 80, quotes => 'none', loose => 1 ); }}
225 #pod
226 #pod See L<Pod::Text/"DESCRIPTION">.
227 #pod
228 #pod =cut
229  
230 3 ▶sub pod2text {
Subroutine pod2text: called CC=6 SCAR=19.1
Pod documented
231 3 my ( $self, %opts ) = @_;
232 3 my $name = $self->name;
233 3 my $parser = Pod::Text->new( %opts );
234 3 my ( $text, $errata );
235 3 $parser->output_string( \$text );
236 3 ▶ if ( $parser->can( 'errata_seen' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parser->can('errata_seen')) { } 3 0 3
237 3 $parser->complain_stderr( 0 );
238 3 $parser->parse_string_document( $self->content );
239 3 $errata = $parser->errata_seen;
240 } else {
241 # Now do a dirty hack. `Pod::Simple` parser prior to 3.32 either print messages to `STDERR`
242 # or creates a section at the end of the document. There is no simple way to get error
243 # messages. Let us redefine its `_complain_warn` function to catch error messages.
244 0 $parser->complain_stderr( 1 );
245 3 ▶ no warnings 'redefine'; ## no critic ( ProhibitNoWarnings )
Subroutine BEGIN: called CC=1 SCAR=0.0
246 0 ▶ local *Pod::Simple::_complain_warn = sub { ## no critic ( ProtectPrivateVars )
Subroutine __ANON__: not called CC=1 SCAR=6.9
247 0 my ( undef, $line, $msg ) = @_;
248 0 push( @{ $errata->{ $line } }, $msg );
249 0 };
250 # Ok, now parse POD and reports errors, if any:
251 0 $parser->parse_string_document( $self->content );
252 };
253 ## no critic ( RequireCarping )
254 3 ▶ if ( $parser->any_errata_seen ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parser->any_errata_seen) 1 2 3
255 1 for my $line ( sort( keys( %$errata ) ) ) {
256 2 warn "$_ at $name line $line.\n" for @{ $errata->{ $line } };
257 };
258 # TODO: Use `log_errors_in_file` to repot POD errors?
259 1 die "POD errata found at $name.\n";
260 };
261 2 ▶ if ( not $parser->content_seen ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parser->content_seen) 0 2 2
262 0 die "No POD content found at $name.\n";
263 };
264 ## critic ( RequireCarping )
265 2 $self->content( $text );
266 2 return $self;
267 };
268  
269 # --------------------------------------------------------------------------------------------------
270  
271 #pod =method trim
272 #pod
273 #pod $file->trim();
274 #pod
275 #pod Trims trailing whitespaces from every line. The method returns C<$self> for chaining.
276 #pod
277 #pod =cut
278  
279 1 ▶sub trim {
Subroutine trim: called CC=1 SCAR=0.0
Pod documented
280 1 my ( $self ) = @_;
281 1 my $content = $self->content;
282 1 $content =~ s{(?:(?!\n)\s)+$}{}gmx;
283 1 $self->content( $content );
284 1 return $self;
285 };
286  
287 # --------------------------------------------------------------------------------------------------
288  
289 3 __PACKAGE__->meta->make_immutable();
290  
291 3 1;
292  
293 # --------------------------------------------------------------------------------------------------
294  
295 #pod =head1 COPYRIGHT AND LICENSE
296 #pod
297 #pod Copyright (C) 2015, 2016, 2018 Van de Bugger
298 #pod
299 #pod License GPLv3+: The GNU General Public License version 3 or later
300 #pod <http://www.gnu.org/licenses/gpl-3.0.txt>.
301 #pod
302 #pod This is free software: you are free to change and redistribute it. There is
303 #pod NO WARRANTY, to the extent permitted by law.
304 #pod
305 #pod
306 #pod =cut
307  
308 # end of file #
309  
310 __END__
« Templates.pm ↑ Summary