File Coverage

blib/lib/Dist/Zilla/Plugin/Stenciller/HtmlExamples.pm
Criterion Covered Total %
statement 42 42 100.0
branch n/a
condition n/a
subroutine 11 11 100.0
pod 0 1 0.0
total 53 54 98.1


line stmt bran cond sub pod time code
1 1     1   2954868 use 5.14.0;
  1         4  
2 1     1   6 use strict;
  1         2  
  1         21  
3 1     1   6 use warnings;
  1         1  
  1         65  
4              
5             package Dist::Zilla::Plugin::Stenciller::HtmlExamples {
6              
7             our $VERSION = '0.0102'; # VERSION
8             # ABSTRACT: Create Html example files from text files parsed with Stenciller
9              
10 1     1   5 use Moose;
  1         3  
  1         10  
11             with 'Dist::Zilla::Role::FileGatherer';
12 1     1   7765 use Stenciller;
  1         1541623  
  1         48  
13 1     1   10 use Types::Stenciller -types;
  1         2  
  1         16  
14 1     1   5848 use Path::Tiny;
  1         3  
  1         68  
15 1     1   964 use Dist::Zilla::File::InMemory;
  1         84839  
  1         54  
16 1     1   895 use String::Stomp;
  1         412  
  1         71  
17 1     1   16 use syntax 'qs';
  1         2  
  1         9  
18              
19             has '+zilla' => (
20             traits => ['Documented'],
21             documentation_order => 0,
22             );
23             has '+plugin_name' => (
24             traits => ['Documented'],
25             documentation_order => 0,
26             );
27             has '+logger' => (
28             traits => ['Documented'],
29             documentation_order => 0,
30             );
31              
32             has source_directory => (
33             is => 'ro',
34             isa => Dir,
35             coerce => 1,
36             default => 'examples/source',
37             traits => ['Documented'],
38             documentation => 'Path to where the stencil files are.',
39             documentation_order => 1,
40             );
41             has file_pattern => (
42             is => 'ro',
43             isa => Str,
44             default => '.+\.stencil',
45             traits => ['Documented'],
46             documentation => stomp qs{
47             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>.
48             },
49             documentation_order => 3,
50             );
51             has output_directory => (
52             is => 'ro',
53             isa => Path,
54             coerce => 1,
55             default => 'examples',
56             traits => ['Documented'],
57             documentation => stomp qs{
58             Path to where the generated files are saved. The output files
59             will have the same basename as the stencil they are based on, but with the suffix replaced by C<html>.
60             },
61             documentation_order => 2,
62             );
63             has template_file => (
64             is => 'ro',
65             isa => AbsFile,
66             lazy => 1,
67             coerce => 1,
68             default => sub { shift->source_directory->child('template.html')->absolute },
69             traits => ['Documented'],
70             documentation => stomp qs{
71             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>
72             for each stencil.
73             },
74 1     1 0 4164 documentation_default => q{'template.html' in L</source_directory>},
75             documentation_order => 4,
76 1         57 );
77 1         271 has separator => (
  1         50  
78             is => 'ro',
79 1         142 isa => Maybe[Str],
80             traits => ['Documented'],
81 1         266 documentation => q{Passed on to the L<Stenciller::Plugin::ToHtmlPreBlock> constructor.},
82 1         7 documentation_order => 5,
83             );
84             has output_also_as_html => (
85             is => 'ro',
86             isa => Bool,
87             default => 0,
88             traits => ['Documented'],
89             documentation => q{Passed on to the L<Stenciller::Plugin::ToHtmlPreBlock> constructor.},
90             documentation_order => 6,
91             );
92              
93             sub gather_files {
94             my $self = shift;
95              
96 1         358932 my $template = $self->template_file->slurp_utf8;
97 1         209 my @source_files = $self->source_directory->children(qr{^@{[ $self->file_pattern ]}$});
98 1         12  
99 1         90 $self->log('Generating from stencils');
100              
101 1         416 foreach my $file (@source_files) {
102             my $contents = Stenciller->new(filepath => $file->stringify)->transform(
103             plugin_name => 'ToHtmlPreBlock',
104             constructor_args => {
105 1         416 output_also_as_html => $self->output_also_as_html,
106             separator => $self->separator,
107             },
108             transform_args => {
109             require_in_extra => {
110             key => 'is_html_example',
111             value => 1,
112             default => 1,
113             },
114             },
115             );
116             my $all_contents = $template;
117             $all_contents =~ s{\[STENCILS\]}{$contents};
118             my $new_filename = $file->basename(qr/\.[^.]+$/) . '.html';
119             $self->log("Generated $new_filename");
120              
121             my $generated_file = Dist::Zilla::File::InMemory->new(
122             name => path($self->output_directory, $new_filename)->stringify,
123             content => $all_contents,
124             );
125             $self->add_file($generated_file);
126              
127             }
128             }
129              
130             }
131              
132             1;
133              
134             __END__
135              
136             =pod
137              
138             =encoding UTF-8
139              
140             =head1 NAME
141              
142             Dist::Zilla::Plugin::Stenciller::HtmlExamples - Create Html example files from text files parsed with Stenciller
143              
144              
145              
146             =begin HTML
147              
148             <p><img src="https://img.shields.io/badge/perl-5.14+-brightgreen.svg" alt="Requires Perl 5.14+" /> <a href="https://travis-ci.org/Csson/p5-Dist-Zilla-Plugin-Stenciller-HtmlExamples"><img src="https://api.travis-ci.org/Csson/p5-Dist-Zilla-Plugin-Stenciller-HtmlExamples.svg?branch=master" alt="Travis status" /></a></p>
149              
150             =end HTML
151              
152              
153             =begin markdown
154              
155             ![Requires Perl 5.14+](https://img.shields.io/badge/perl-5.14+-brightgreen.svg) [![Travis status](https://api.travis-ci.org/Csson/p5-Dist-Zilla-Plugin-Stenciller-HtmlExamples.svg?branch=master)](https://travis-ci.org/Csson/p5-Dist-Zilla-Plugin-Stenciller-HtmlExamples)
156              
157             =end markdown
158              
159             =head1 VERSION
160              
161             Version 0.0102, released 2016-01-10.
162              
163              
164              
165             =head1 SYNOPSIS
166              
167             ; in dist.ini
168             ; these are the defaults
169             [Stenciller::HtmlExamples]
170             source_directory = examples/source
171             output_directory = examples
172             template_file = examples/source/template.html
173             file_pattern = .+\.stencil
174             output_also_as_html = 0
175              
176             =head1 DESCRIPTION
177              
178             Dist::Zilla::Plugin::Stenciller::HtmlExamples uses L<Stenciller> and L<Stenciller::Plugin::ToHtmlPreBlock> to turn
179             stencil files in C<source_directory> (that matches the C<file_pattern>) into
180             html example files in C<output_directory> by applying the C<template_file>.
181              
182             This L<Dist::Zilla> plugin does the C<FileGatherer> role.
183              
184             =head1 ATTRIBUTES
185              
186              
187             =head2 source_directory
188              
189             =begin HTML
190              
191             <table cellpadding="0" cellspacing="0">
192             <tr><td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Path::Tiny#Dir">Dir</a>
193              
194             </td>
195             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default: <code>examples/source</code>
196              
197             </td>
198             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
199             </table>
200              
201             <p>Path to where the stencil files are.</p>
202              
203             =end HTML
204              
205             =head2 output_directory
206              
207             =begin HTML
208              
209             <table cellpadding="0" cellspacing="0">
210             <tr><td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Path::Tiny#Path">Path</a>
211              
212             </td>
213             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default: <code>examples</code>
214              
215             </td>
216             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
217             </table>
218              
219             <p>Path to where the generated files are saved. The output files
220             will have the same basename as the stencil they are based on, but with the suffix replaced by C<html>.</p>
221              
222             =end HTML
223              
224             =head2 file_pattern
225              
226             =begin HTML
227              
228             <table cellpadding="0" cellspacing="0">
229             <tr><td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#Str">Str</a>
230              
231             </td>
232             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default: <code>.+\.stencil</code>
233              
234             </td>
235             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
236             </table>
237              
238             <p>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>.</p>
239              
240             =end HTML
241              
242             =head2 template_file
243              
244             =begin HTML
245              
246             <table cellpadding="0" cellspacing="0">
247             <tr><td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Path::Tiny#AbsFile">AbsFile</a>
248              
249             </td>
250             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default: <code>&#39;template.html&#39; in <a href="#source_directory">&quot;source_directory&quot;</a></code>
251              
252             </td>
253             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
254             </table>
255              
256             <p>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>
257             for each stencil.</p>
258              
259             =end HTML
260              
261             =head2 separator
262              
263             =begin HTML
264              
265             <table cellpadding="0" cellspacing="0">
266             <tr><td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#Maybe">Maybe</a> [ <a href="https://metacpan.org/pod/Types::Standard#Str">Str</a> ]</td>
267             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional</td>
268             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
269             </table>
270              
271             <p>Passed on to the L<Stenciller::Plugin::ToHtmlPreBlock> constructor.</p>
272              
273             =end HTML
274              
275             =head2 output_also_as_html
276              
277             =begin HTML
278              
279             <table cellpadding="0" cellspacing="0">
280             <tr><td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;"><a href="https://metacpan.org/pod/Types::Standard#Bool">Bool</a>
281              
282             </td>
283             <td style="padding-right: 6px; padding-left: 6px; border-right: 1px solid #b8b8b8; white-space: nowrap;">optional, default: <code>0</code>
284              
285             </td>
286             <td style="padding-left: 6px; padding-right: 6px; white-space: nowrap;">read-only</td></tr>
287             </table>
288              
289             <p>Passed on to the L<Stenciller::Plugin::ToHtmlPreBlock> constructor.</p>
290              
291             =end HTML
292              
293             =head1 SEE ALSO
294              
295             =over 4
296              
297             =item *
298              
299             L<Stenciller>
300              
301             =item *
302              
303             L<Stenciller::Plugin::ToHtmlPreBlock>
304              
305             =back
306              
307             =head1 SOURCE
308              
309             L<https://github.com/Csson/p5-Dist-Zilla-Plugin-Stenciller-HtmlExamples>
310              
311             =head1 HOMEPAGE
312              
313             L<https://metacpan.org/release/Dist-Zilla-Plugin-Stenciller-HtmlExamples>
314              
315             =head1 AUTHOR
316              
317             Erik Carlsson <info@code301.com>
318              
319             =head1 COPYRIGHT AND LICENSE
320              
321             This software is copyright (c) 2015 by Erik Carlsson.
322              
323             This is free software; you can redistribute it and/or modify it under
324             the same terms as the Perl 5 programming language system itself.
325              
326             =cut