blib/lib/Dist/Zilla/Plugin/Stenciller/HtmlExamples.pm

filter: Statementstmt 98.4% 65 / 66 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 1 totaltotal 96.3% 79 / 82 CC 2 SCAR 1.7
CC
2
Cov
98%
__ANON__
2.0
gather_files
2.0
BEGIN
1.0
CRAP 1.2
↑ Summary
1 2 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Dist::Zilla::Plugin::Stenciller::HtmlExamples;
6  
7 # ABSTRACT: Create Html example files from text files parsed with Stenciller
8 2 our $AUTHORITY = 'cpan:CSSON'; # AUTHORITY
9 2 our $VERSION = '0.0201';
10  
11 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 with 'Dist::Zilla::Role::FileGatherer';
13 2 ▶use Stenciller;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Types::Standard qw/Bool Maybe Str/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Types::Path::Tiny qw/AbsFile Dir Path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Types::Stenciller -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Dist::Zilla::File::InMemory;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use String::Stomp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use syntax 'qs';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 has '+zilla' => (
24 traits => ['Documented'],
25 documentation_order => 0,
26 );
27 2 has '+plugin_name' => (
28 traits => ['Documented'],
29 documentation_order => 0,
30 );
31 2 has '+logger' => (
32 traits => ['Documented'],
33 documentation_order => 0,
34 );
35  
36 2 has source_directory => (
37 is => 'ro',
38 isa => Dir,
39 coerce => 1,
40 default => 'examples/source',
41 traits => ['Documented'],
42 documentation => 'Path to where the stencil files are.',
43 documentation_order => 1,
44 );
45 2 has file_pattern => (
46 is => 'ro',
47 isa => Str,
48 2 default => '.+\.stencil',
49 traits => ['Documented'],
50 documentation => stomp qs{
51 This is used as a part of a regular expression (so do not use start and end anchors) to find stencil files in the C<source_directory>.
52 },
53 documentation_order => 3,
54 );
55 has output_directory => (
56 0 ▶ is => 'ro',
Subroutine __ANON__: not called CC=1 SCAR=6.9
57 2 isa => Path,
58 coerce => 1,
59 default => 'examples',
60 traits => ['Documented'],
61 2 documentation => stomp qs{
62 Path to where the generated files are saved. The output files
63 will have the same basename as the stencil they are based on, but with the suffix replaced by C<html>.
64 },
65 documentation_order => 2,
66 );
67 has template_file => (
68 2 is => 'ro',
69 isa => AbsFile,
70 lazy => 1,
71 coerce => 1,
72 default => sub { shift->source_directory->child('template.html')->absolute },
73 traits => ['Documented'],
74 documentation => stomp qs{
75 The template file should be an html file. The first occurence of C<[STENCILS]> will be replaced with the output from L<Stenciller::Plugin::ToHtmlPreBlock>
76 for each stencil.
77 1 ▶ },
Subroutine gather_files: called CC=2 SCAR=6.9
Pod undocumented
78 1 documentation_default => q{'template.html' in L</source_directory>},
79 documentation_order => 4,
80 1 );
81 1 has separator => (
82 is => 'ro',
83 1 isa => Maybe[Str],
84 traits => ['Documented'],
85 1 documentation => q{Passed on to the L<Stenciller::Plugin::ToHtmlPreBlock> constructor.},
86 1 documentation_order => 5,
87 );
88 has output_also_as_html => (
89 is => 'ro',
90 isa => Bool,
91 default => 0,
92 traits => ['Documented'],
93 documentation => q{Passed on to the L<Stenciller::Plugin::ToHtmlPreBlock> constructor.},
94 documentation_order => 6,
95 );
96  
97 sub gather_files {
98 my $self = shift;
99  
100 1 my $template = $self->template_file->slurp_utf8;
101 1 my @source_files = $self->source_directory->children(qr{^@{[ $self->file_pattern ]}$});
102 1  
103 1 $self->log('Generating from stencils');
104  
105 1 foreach my $file (@source_files) {
106 my $contents = Stenciller->new(filepath => $file->stringify)->transform(
107 plugin_name => 'ToHtmlPreBlock',
108 constructor_args => {
109 1 output_also_as_html => $self->output_also_as_html,
110 separator => $self->separator,
111 },
112 transform_args => {
113 require_in_extra => {
114 2 key => 'is_html_example',
115 value => 1,
116 2 default => 1,
117 },
118 },
119 );
120 my $all_contents = $template;
121 $all_contents =~ s{\[STENCILS\]}{$contents};
122 my $new_filename = $file->basename(qr/\.[^.]+$/) . '.html';
123 $self->log("Generated $new_filename");
124  
125 my $generated_file = Dist::Zilla::File::InMemory->new(
126 name => path($self->output_directory, $new_filename)->stringify,
127 content => $all_contents,
128 );
129 $self->add_file($generated_file);
130  
131 }
132 }
133  
134 __PACKAGE__->meta->make_immutable;
135  
136 1;
137  
138 __END__
↑ Summary