File Coverage

blib/lib/Stenciller/Plugin/ToHtmlPreBlock.pm
Criterion Covered Total %
statement 47 47 100.0
branch 12 12 100.0
condition 3 3 100.0
subroutine 11 11 100.0
pod 1 3 33.3
total 74 76 97.3


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