File Coverage

blib/lib/Dist/Zilla/Plugin/Stenciller/HtmlExamples.pm
Criterion Covered Total %
statement 36 36 100.0
branch n/a
condition n/a
subroutine 9 9 100.0
pod 0 1 0.0
total 45 46 97.8


line stmt bran cond sub pod time code
1 1     1   2576712 use 5.14.0;
  1         4  
2 1     1   6 use strict;
  1         3  
  1         28  
3 1     1   8 use warnings;
  1         3  
  1         81  
4              
5             package Dist::Zilla::Plugin::Stenciller::HtmlExamples {
6              
7             our $VERSION = '0.0101'; # VERSION
8             # ABSTRACT: Create Html example files from text files parsed with Stenciller
9              
10 1     1   7 use Moose;
  1         2  
  1         9  
11             with 'Dist::Zilla::Role::FileGatherer';
12 1     1   7405 use Stenciller;
  1         1402300  
  1         47  
13 1     1   10 use Types::Stenciller -types;
  1         2  
  1         16  
14 1     1   6128 use Path::Tiny;
  1         2  
  1         69  
15 1     1   944 use Dist::Zilla::File::InMemory;
  1         89730  
  1         707  
16              
17             has source_directory => (
18             is => 'ro',
19             isa => Dir,
20             coerce => 1,
21             default => 'examples/source',
22             );
23             has file_pattern => (
24             is => 'ro',
25             isa => Str,
26             default => '.+\.stencil',
27             );
28             has output_directory => (
29             is => 'ro',
30             isa => Path,
31             coerce => 1,
32             default => 'examples',
33             );
34             has template_file => (
35             is => 'ro',
36             isa => AbsFile,
37             lazy => 1,
38             coerce => 1,
39             default => sub { shift->source_directory->child('template.html')->absolute },
40             );
41             has output_also_as_html => (
42             is => 'ro',
43             isa => Bool,
44             default => 0,
45             );
46             has separator => (
47             is => 'ro',
48             isa => Maybe[Str],
49             );
50              
51             sub gather_files {
52 1     1 0 666805 my $self = shift;
53              
54 1         53 my $template = $self->template_file->slurp_utf8;
55 1         294 my @source_files = $self->source_directory->children(qr{^@{[ $self->file_pattern ]}$});
  1         65  
56              
57 1         133 $self->log('Generating from stencils');
58              
59 1         337 foreach my $file (@source_files) {
60 1         29 my $contents = Stenciller->new(filepath => $file->stringify)->transform(
61             plugin_name => 'ToHtmlPreBlock',
62             constructor_args => {
63             output_also_as_html => $self->output_also_as_html,
64             separator => $self->separator,
65             },
66             transform_args => {
67             require_in_extra => {
68             key => 'is_html_example',
69             value => 1,
70             default => 1,
71             },
72             },
73             );
74 1         378325 my $all_contents = $template;
75 1         206 $all_contents =~ s{\[STENCILS\]}{$contents};
76 1         11 my $new_filename = $file->basename(qr/\.[^.]+$/) . '.html';
77 1         102 $self->log("Generated $new_filename");
78              
79 1         516 my $generated_file = Dist::Zilla::File::InMemory->new(
80             name => path($self->output_directory, $new_filename)->stringify,
81             content => $all_contents,
82             );
83 1         461 $self->add_file($generated_file);
84              
85             }
86             }
87              
88             }
89              
90             1;
91              
92             __END__
93              
94             =pod
95              
96             =encoding UTF-8
97              
98             =head1 NAME
99              
100             Dist::Zilla::Plugin::Stenciller::HtmlExamples - Create Html example files from text files parsed with Stenciller
101              
102              
103              
104             =begin HTML
105              
106             <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>
107              
108             =end HTML
109              
110              
111             =begin markdown
112              
113             ![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)
114              
115             =end markdown
116              
117             =head1 VERSION
118              
119             Version 0.0101, released 2015-11-23.
120              
121             =head1 SYNOPSIS
122              
123             ; in dist.ini
124             ; these are the defaults
125             [Stenciller::HtmlExamples]
126             source_directory = examples/source
127             output_directory = examples
128             template_file = examples/source/template.html
129             file_pattern = .+\.stencil
130              
131             =head1 DESCRIPTION
132              
133             Dist::Zilla::Plugin::Stenciller::HtmlExamples uses L<Stenciller> and L<Stenciller::Plugin::ToHtmlPreBlock> to turn
134             stencil files in C<source_directory> (that matches the C<file_pattern>) into
135             html example files in C<output_directory> by applying the C<template_file>.
136              
137             This L<Dist::Zilla> plugin does the C<FileGatherer> role.
138              
139             =head1 ATTRIBUTES
140              
141             =head2 source_directory
142              
143             Path to where the stencil files are.
144              
145             =head2 output_directory
146              
147             Path to where the generated files are saved.
148              
149             =head2 file_pattern
150              
151             This is put inside a regular expression (with start and end anchors) to find stencil files in the C<source_directory>. The output files
152             will have the same basename, but the suffix is replaced by C<html>.
153              
154             =head2 template_file
155              
156             The template file is an ordinary html file, with one exception: The first occurence of C<[STENCILS]> will be replaced with the
157             string returned from L<Stenciller::Plugin::ToHtmlPreBlock>. The template file is applied to each stencil file, so the number of generated example files is equal
158             to the number of stencil files.
159              
160             =head1 SEE ALSO
161              
162             =over 4
163              
164             =item *
165              
166             L<Stenciller>
167              
168             =item *
169              
170             L<Stenciller::Plugin::ToHtmlPreBlock>
171              
172             =back
173              
174             =head1 SOURCE
175              
176             L<https://github.com/Csson/p5-Dist-Zilla-Plugin-Stenciller-HtmlExamples>
177              
178             =head1 HOMEPAGE
179              
180             L<https://metacpan.org/release/Dist-Zilla-Plugin-Stenciller-HtmlExamples>
181              
182             =head1 AUTHOR
183              
184             Erik Carlsson <info@code301.com>
185              
186             =head1 COPYRIGHT AND LICENSE
187              
188             This software is copyright (c) 2015 by Erik Carlsson.
189              
190             This is free software; you can redistribute it and/or modify it under
191             the same terms as the Perl 5 programming language system itself.
192              
193             =cut