HTML::FormFu::OutputProcessor::Indent - Nicely Indent HTML Output


HTML-FormFu documentation Contained in the HTML-FormFu distribution.

Index


Code Index:

NAME

Top

HTML::FormFu::OutputProcessor::Indent - Nicely Indent HTML Output

SYNOPSIS

Top

    ---
    output_processors:
      - Indent

METHODS

Top

indent

Arguments: $string

Default Value: "\t"

The string to be used to indent the HTML.

preserve_tags

Arguments: \@tags

Default Value: ['pre', 'textarea']

An arrayref of tag names who's contents should not be processed.

SEE ALSO

Top

Is a sub-class of, and inherits methods from HTML::FormFu::OutputProcessor

HTML::FormFu

AUTHOR

Top

Carl Franks cfranks@cpan.org

LICENSE

Top

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__