| 1 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package HTML::FormFu::OutputProcessor::Indent; | ||||||||||||||||||||||||||||
| 4 | 3 | $HTML::FormFu::OutputProcessor::Indent::VERSION = '2.06'; | |||||||||||||||||||||||||||
| 5 | # ABSTRACT: Nicely Indent HTML Output | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use MooseX::Attribute::Chained; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | extends 'HTML::FormFu::OutputProcessor'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use HTML::FormFu::Constants qw( $EMPTY_STR $SPACE ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use HTML::TokeParser::Simple; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use List::Util 1.33 qw( any ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | has indent => ( | |||||||||||||||||||||||||||
| 16 | is => 'rw', | ||||||||||||||||||||||||||||
| 17 | default => "\t", | ||||||||||||||||||||||||||||
| 18 | lazy => 1, | ||||||||||||||||||||||||||||
| 19 | traits => ['Chained'], | ||||||||||||||||||||||||||||
| 20 | ); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | has preserve_tags => ( | ||||||||||||||||||||||||||||
| 23 | is => 'rw', | ||||||||||||||||||||||||||||
| 24 | 2 | ▶ | default => sub { [qw( pre textarea )] }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 3 | lazy => 1, | |||||||||||||||||||||||||||
| 26 | traits => ['Chained'], | ||||||||||||||||||||||||||||
| 27 | ); | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | sub process { | ||||||||||||||||||||||||||||
| 30 | 5 | ▶ | my ( $self, $input ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
process: called CC=17 SCAR=28.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 5 | my $indent = $self->indent; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 5 | my $parser = HTML::TokeParser::Simple->new( \$input ); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 5 | my @preserve_tags = @{ $self->preserve_tags }; | |||||||||||||||||||||||||||
| 37 | 5 | my $count = 0; | |||||||||||||||||||||||||||
| 38 | 5 | my $in_pre = 0; | |||||||||||||||||||||||||||
| 39 | 5 | my $output = $EMPTY_STR; | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 5 | ▶ | while ( my $token = $parser->get_token ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 116 | ▶ | if ( $token->is_start_tag ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 44 | 33 | my $tag = $token->get_tag; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 33 | ▶ | if ( any { $tag eq $_ } @preserve_tags ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 7 | $in_pre = 1; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 33 | $output .= $indent x $count; | |||||||||||||||||||||||||||
| 51 | 33 | $output .= $token->as_is; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 33 | ▶ | if ( !defined $token->get_attrseq->[-1] | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not defined $token->get_attrseq->[-1] or $token->get_attrseq->[-1] ne "/"
MC/DC100%2/2
not defined $token->get_attrseq->[-1] or $token->get_attrseq->[-1] ne "/"
not defined $token->get_attrseq->[-1]
$token->get_attrseq->[-1] ne "/"
Truth table100%3/3
not(A) or B
A= not defined $token->get_attrseq->[-1]B= $token->get_attrseq->[-1] ne "/"
| |||||||||||||||||||||||||||||
| 54 | || $token->get_attrseq->[-1] ne "/" ) | ||||||||||||||||||||||||||||
| 55 | { | ||||||||||||||||||||||||||||
| 56 | 28 | $count++; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | elsif ( $token->is_end_tag ) { | ||||||||||||||||||||||||||||
| 60 | 28 | my $tag = $token->get_tag; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 28 | $count--; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 28 | ▶ | if ( $output =~ m/ > \s* \z /x && !$in_pre ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$output =~ / > \s* \z /x and not $in_pre
MC/DC100%2/2
$output =~ / > \s* \z /x and not $in_pre
$output =~ / > \s* \z /x
not $in_pre
Truth table100%3/3
A and not(B)
A= $output =~ / > \s* \z /xB= not $in_pre
| |||||||||||||||||||||||||||||
| 65 | 15 | $output .= "\n" . $indent x $count; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 28 | ▶ | if ( any { $tag eq $_ } @preserve_tags ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | 7 | $in_pre = 0; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 28 | $output .= $token->as_is; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | elsif ( $token->is_text ) { | ||||||||||||||||||||||||||||
| 75 | 55 | my $text = $token->as_is; | |||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 55 | ▶ | if ( length $parser->peek && !$in_pre ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
length $parser->peek and not $in_pre
MC/DC100%2/2
length $parser->peek and not $in_pre
length $parser->peek
not $in_pre
Truth table100%3/3
A and not(B)
A= length $parser->peekB= not $in_pre
| |||||||||||||||||||||||||||||
| 78 | 49 | $text =~ s/\A\s+/ /; | |||||||||||||||||||||||||||
| 79 | 49 | $text =~ s/\s+\z/ /; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 55 | ▶ | if ( $text eq $SPACE && $parser->peek =~ m/ < /x ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$text eq $HTML::FormFu::OutputProcessor::Indent::SPACE and $parser->peek =~ / < /x
MC/DC50%1/2
$text eq $HTML::FormFu::OutputProcessor::Indent::SPACE and $parser->peek =~ / < /x
$text eq $HTML::FormFu::OutputProcessor::Indent::SPACE
$parser->peek =~ / < /x
Truth table66%2/3
A and B
A= $text eq $HTML::FormFu::OutputProcessor::Indent::SPACEB= $parser->peek =~ / < /x
| |||||||||||||||||||||||||||||
| 83 | 43 | $text = $EMPTY_STR; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 55 | $output .= $text; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | else { | ||||||||||||||||||||||||||||
| 89 | 0 | $output .= $token->as_is; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 116 | ▶ | if ( $parser->peek =~ m{ < (?!/) }x && !$in_pre ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$parser->peek =~ m[ < (?!/) ]x and not $in_pre
MC/DC50%1/2
$parser->peek =~ m[ < (?!/) ]x and not $in_pre
$parser->peek =~ m[ < (?!/) ]x
not $in_pre
Truth table66%2/3
A and not(B)
A= $parser->peek =~ m[ < (?!/) ]xB= not $in_pre
| |||||||||||||||||||||||||||||
| 93 | 28 | $output .= "\n"; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 5 | return $output; | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 3 | 1; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | __END__ | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =pod | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head1 NAME | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | HTML::FormFu::OutputProcessor::Indent - Nicely Indent HTML Output | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head1 VERSION | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | version 2.06 | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | --- | ||||||||||||||||||||||||||||
| 121 | output_processors: | ||||||||||||||||||||||||||||
| 122 | - Indent | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head1 METHODS | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head2 indent | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | Arguments: $string | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | Default Value: "\t" | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | The string to be used to indent the HTML. | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =head2 preserve_tags | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | Arguments: \@tags | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | Default Value: ['pre', 'textarea'] | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | An arrayref of tag names who's contents should not be processed. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | Is a sub-class of, and inherits methods from L<HTML::FormFu::OutputProcessor> | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | L<HTML::FormFu> | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | Carl Franks C<cfranks@cpan.org> | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | This library is free software, you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 155 | the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | Carl Franks <cpan@fireartist.com> | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | This software is copyright (c) 2018 by Carl Franks. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 166 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | =cut | ||||||||||||||||||||||||||||