| HTML-FormFu documentation | Contained in the HTML-FormFu distribution. |
HTML::FormFu::OutputProcessor::Indent - Nicely Indent HTML Output
---
output_processors:
- Indent
Arguments: $string
Default Value: "\t"
The string to be used to indent the HTML.
Is a sub-class of, and inherits methods from HTML::FormFu::OutputProcessor
Carl Franks cfranks@cpan.org
This library is free software, you can redistribute it and/or modify it under the same terms as Perl itself.
| HTML-FormFu documentation | Contained in the HTML-FormFu distribution. |
package HTML::FormFu::OutputProcessor::Indent; use Moose; extends 'HTML::FormFu::OutputProcessor'; use HTML::FormFu::Constants qw( $EMPTY_STR $SPACE ); use HTML::TokeParser::Simple; use List::MoreUtils qw( any ); has indent => ( is => 'rw', default => "\t", lazy => 1, traits => ['Chained'], ); has preserve_tags => ( is => 'rw', default => sub { [qw( pre textarea )] }, lazy => 1, traits => ['Chained'], ); sub process { my ( $self, $input ) = @_; my $indent = $self->indent; my $parser = HTML::TokeParser::Simple->new( \$input ); my @preserve_tags = @{ $self->preserve_tags }; my $count = 0; my $in_pre = 0; my $output = $EMPTY_STR; while ( my $token = $parser->get_token ) { if ( $token->is_start_tag ) { my $tag = $token->get_tag; if ( any { $tag eq $_ } @preserve_tags ) { $in_pre = 1; } $output .= $indent x $count; $output .= $token->as_is; if ( !defined $token->get_attrseq->[-1] || $token->get_attrseq->[-1] ne "/" ) { $count++; } } elsif ( $token->is_end_tag ) { my $tag = $token->get_tag; $count--; if ( $output =~ m/ > \s* \z /x && !$in_pre ) { $output .= "\n" . $indent x $count; } if ( any { $tag eq $_ } @preserve_tags ) { $in_pre = 0; } $output .= $token->as_is; } elsif ( $token->is_text ) { my $text = $token->as_is; if ( length $parser->peek && !$in_pre ) { $text =~ s/\A\s+/ /; $text =~ s/\s+\z/ /; } if ( $text eq $SPACE && $parser->peek =~ m/ < /x ) { $text = $EMPTY_STR; } $output .= $text; } else { $output .= $token->as_is; } if ( $parser->peek =~ m{ < (?!/) }x && !$in_pre ) { $output .= "\n"; } } return $output; } __PACKAGE__->meta->make_immutable; 1; __END__