File Coverage

blib/lib/Stenciller/Plugin/ToHtmlPreBlock.pm
Criterion Covered Total %
statement 50 50 100.0
branch 12 12 100.0
condition 3 3 100.0
subroutine 12 12 100.0
pod 1 3 33.3
total 78 80 97.5


line stmt bran cond sub pod time code
1 3     3   1698 use 5.10.1;
  3         9  
2 3     3   15 use strict;
  3         5  
  3         88  
3 3     3   17 use warnings;
  3         6  
  3         197  
4              
5             package Stenciller::Plugin::ToHtmlPreBlock;
6              
7             our $VERSION = '0.1400'; # VERSION:
8             # ABSTRACT: A plugin that transforms to html
9              
10 3     3   18 use Moose;
  3         6  
  3         22  
11             with 'Stenciller::Transformer';
12 3     3   19381 use namespace::autoclean;
  3         6  
  3         31  
13              
14 3     3   2073 use List::MoreUtils qw/first_index/;
  3         24911  
  3         25  
15 3     3   3000 use Types::Standard qw/Bool Str Maybe/;
  3         6  
  3         39  
16 3     3   4030 use MooseX::AttributeShortcuts;
  3         1106344  
  3         13  
17 3     3   266979 use HTML::Entities 'encode_entities';
  3         15432  
  3         1456  
18              
19             has output_also_as_html => (
20             is => 'ro',
21             isa => Bool,
22             default => 0,
23             );
24             has separator => (
25             is => 'ro',
26             isa => Maybe[Str],
27             predicate => 1,
28             );
29              
30              
31             sub transform {
32 3     3 1 7 my $self = shift;
33 3         5 my $transform_args = shift;
34              
35 3         92 my @out = $self->init_out($self->stenciller, $transform_args);
36              
37             STENCIL:
38 3         82 for my $i (0 .. $self->stenciller->max_stencil_index) {
39 7 100       26 next STENCIL if $self->should_skip_stencil_by_index($i, $transform_args);
40              
41 6         150 my $stencil = $self->stenciller->get_stencil($i);
42 6 100       20 next STENCIL if $self->should_skip_stencil($stencil, $transform_args);
43              
44 5 100 100     221 push @out => $self->normal($stencil->all_before_input),
    100          
45             $self->pre($stencil->all_input),
46             $self->normal($stencil->all_between),
47             $self->pre($stencil->all_output),
48             $self->output_also_as_html ? $self->normal([$stencil->all_output]) : (),
49             $self->normal($stencil->all_after_output),
50             $self->has_separator && $i < $self->stenciller->max_stencil_index ? $self->separator : ();
51              
52             }
53 3         59 return join "\n" => @out;
54             }
55              
56             sub normal {
57 17     17 0 35 my $self = shift;
58 17         32 my @lines = @_;
59              
60 17 100       180 return () if !scalar @lines;
61 12         18 my $tag = 'p';
62 12 100       29 if(ref $lines[0] eq 'ARRAY') {
63 2         4 $tag = 'div';
64 2         3 @lines = @{ $lines[0] };
  2         5  
65             }
66 12         441 return join '' => ("<$tag>", join ('' => @lines), "</$tag>");
67             }
68             sub pre {
69 10     10 0 20 my $self = shift;
70 10         29 my @lines = @_;
71              
72 10         21 my @encoded_lines = map { $_ =~ s{^ {4}}{}; encode_entities($_) } @lines;
  20         337  
  20         49  
73 10         450 return join '' => ('<pre>', join ("\n" => @encoded_lines), '</pre>');
74             }
75              
76             __PACKAGE__->meta->make_immutable;
77              
78             1;
79              
80             __END__
81              
82             =pod
83              
84             =encoding UTF-8
85              
86             =head1 NAME
87              
88             Stenciller::Plugin::ToHtmlPreBlock - A plugin that transforms to html
89              
90             =head1 VERSION
91              
92             Version 0.1400, released 2016-02-03.
93              
94              
95              
96             =head1 SYNOPSIS
97              
98             use Stenciller;
99             my $stenciller = Stenciller->new(filepath => 't/corpus/test-1.stencil');
100             my $content = $stenciller->transform('ToHtmlPreBlock');
101              
102             =head1 DESCRIPTION
103              
104             This plugin to L<Stenciller> places the C<before_input>, C<between> and C<after_output> regions in C<E<lt>pE<gt>> tags and the C<input> and C<output> regions inside C<E<lt>preE<gt>> tags.
105              
106             Content that will be placed in C<pre> tags (input and output sections in stencils) also have four leading spaces removed.
107              
108             =head1 ATTRIBUTES
109              
110             =head2 output_also_as_html
111              
112             Default: C<0>
113              
114             If set to a true value, the contents of C<output> in stencils is rendered as html (directly following the pre-block).
115              
116             =head2 separator
117              
118             Default: C<undef>
119              
120             When set, this text is used to separate two stencils.
121              
122             =head1 METHODS
123              
124             =head2 transform
125              
126             See L<transform|Stenciller::Transformer/"transform"> in L<Stenciller::Transformer>.
127              
128             =head1 SOURCE
129              
130             L<https://github.com/Csson/p5-Stenciller>
131              
132             =head1 HOMEPAGE
133              
134             L<https://metacpan.org/release/Stenciller>
135              
136             =head1 AUTHOR
137              
138             Erik Carlsson <info@code301.com>
139              
140             =head1 COPYRIGHT AND LICENSE
141              
142             This software is copyright (c) 2016 by Erik Carlsson.
143              
144             This is free software; you can redistribute it and/or modify it under
145             the same terms as the Perl 5 programming language system itself.
146              
147             =cut