File Coverage

blib/lib/Stenciller/Plugin/ToUnparsedText.pm
Criterion Covered Total %
statement 38 38 100.0
branch 7 8 87.5
condition n/a
subroutine 9 9 100.0
pod 1 2 50.0
total 55 57 96.4


line stmt bran cond sub pod time code
1 3     3   1478 use 5.10.1;
  3         8  
2 3     3   19 use strict;
  3         4  
  3         64  
3 3     3   13 use warnings;
  3         4  
  3         170  
4              
5             package Stenciller::Plugin::ToUnparsedText;
6              
7             our $VERSION = '0.1400'; # VERSION:
8             # ABSTRACT: A plugin that doesn't transform the text
9              
10 3     3   22 use Moose;
  3         5  
  3         23  
11             with 'Stenciller::Transformer';
12 3     3   18740 use namespace::autoclean;
  3         6  
  3         28  
13              
14 3     3   1865 use List::MoreUtils qw/first_index/;
  3         24098  
  3         25  
15 3     3   2929 use Types::Standard qw/Bool/;
  3         8  
  3         38  
16              
17             has text_as_html_pod => (
18             is => 'ro',
19             isa => Bool,
20             default => 0,
21             );
22              
23             sub transform {
24 4     4 1 8 my $self = shift;
25 4         8 my $transform_args = shift;
26              
27 4         106 my @out = $self->init_out($self->stenciller, $transform_args);
28              
29             STENCIL:
30 4         95 for my $i (0 .. $self->stenciller->max_stencil_index) {
31 10 100       27 next STENCIL if $self->should_skip_stencil_by_index($i, $transform_args);
32              
33 4         102 my $stencil = $self->stenciller->get_stencil($i);
34 4 50       14 next STENCIL if $self->should_skip_stencil($stencil, $transform_args);
35              
36 4         135 push @out => '',
37             $self->maybe_as_html_pod($stencil->all_before_input), '',
38             $stencil->all_input, '',
39             $self->maybe_as_html_pod($stencil->all_between), '',
40             $stencil->all_output, '',
41             $self->maybe_as_html_pod($stencil->all_after_output), '';
42             }
43 4         23 my $content = join "\n" => '', @out, '';
44 4         41 $content =~ s{\v{2,}}{\n\n}g;
45 4         35 return $content;
46             }
47              
48             sub maybe_as_html_pod {
49 12     12 0 20 my $self = shift;
50 12         29 my @text = @_;
51              
52 12 100       319 return @text if !$self->text_as_html_pod;
53 3 100       29 return @text if !scalar @text;
54              
55 2         7 unshift @text => '', '=begin html', '';
56 2         5 push @text => '', '=end html', '';
57 2         64 return @text;
58              
59             }
60              
61             __PACKAGE__->meta->make_immutable;
62              
63             1;
64              
65             __END__
66              
67             =pod
68              
69             =encoding UTF-8
70              
71             =head1 NAME
72              
73             Stenciller::Plugin::ToUnparsedText - A plugin that doesn't transform the text
74              
75             =head1 VERSION
76              
77             Version 0.1400, released 2016-02-03.
78              
79              
80              
81             =head1 SYNOPSIS
82              
83             use Stenciller;
84             my $stenciller = Stenciller->new(filepath => 't/corpus/test-1.stencil');
85             my $content = $stenciller->transform('ToUnparsedText');
86              
87             =head1 DESCRIPTION
88              
89             This plugin to L<Stenciller> basically returns all text content of the stencils.
90              
91             If this plugin is used via L<Pod::Elemental::Transformer::Stenciller> it could be used like this in pod:
92              
93             =pod
94              
95             # includes header_lines and all stencils
96             :stenciller ToUnparsedText atestfile-1.stencil
97              
98             # includes header_lines and all stencils
99             :stenciller ToUnparsedText atestfile-1.stencil { }
100              
101             # includes only the first stencil in the file
102             :stenciller ToUnparsedText atestfile-1.stencil { stencils => [0], skip_header_lines => 1 }
103              
104             # includes only the header_lines
105             :stenciller ToUnparsedText atestfile-1.stencil { stencils => [] }
106              
107             =head1 ATTRIBUTES
108              
109             =head2 text_as_html_pod
110              
111             Default: 0
112              
113             If set to a true value, the parts that are neither C<input> or C<output> will be rendered between C<=begin html> and C<=end html>. This is useful
114             when rendering the same stencils to html examples files as well as including in pod.
115              
116             =head1 METHODS
117              
118             =head2 transform
119              
120             See L<transform|Stenciller::Transformer/"transform"> in L<Stenciller::Transformer>.
121              
122             =head1 SOURCE
123              
124             L<https://github.com/Csson/p5-Stenciller>
125              
126             =head1 HOMEPAGE
127              
128             L<https://metacpan.org/release/Stenciller>
129              
130             =head1 AUTHOR
131              
132             Erik Carlsson <info@code301.com>
133              
134             =head1 COPYRIGHT AND LICENSE
135              
136             This software is copyright (c) 2016 by Erik Carlsson.
137              
138             This is free software; you can redistribute it and/or modify it under
139             the same terms as the Perl 5 programming language system itself.
140              
141             =cut