HTML::Split::Pager - Pager that contains splitted HTMLs.


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

Index


Code Index:

NAME

Top

HTML::Split::Pager - Pager that contains splitted HTMLs.

SYNOPSIS

Top

  use HTML::Split::Pager;

  my $html = <<HTML;
  <div class="pkg">
  <h1>HTML::Split</h1>
  <p>Splitting HTML by number of characters.</p>
  </div>
  HTML;

  my $pager = HTML::Split::Pager->new(html => $html, lenght => 50);
  print $pager->text;

DESCRIPTION

Top

CLASS METHODS

Top

new

Create an instance of HTML::Split. Accept same arguments as split method.

INSTANCE METHODS

Top

current_page

Set/Get current page.

total_pages

Return the number of total pages.

next_page

Return the next page number. If the next page doesn't exists, return undef.

prev_page

Return the previous page number. If the previous page doesn't exists, return undef.

text

Return the text of current page.

AUTHOR

Top

Hiroshi Sakai <ziguzagu@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-Split documentation Contained in the HTML-Split distribution.

package HTML::Split::Pager;

use strict;
use warnings;

use base qw( Class::Accessor::Fast );
__PACKAGE__->mk_ro_accessors(qw( total_pages prev_page next_page ));

use HTML::Split;

sub new {
    my $class = shift;
    my %param = @_;

    return warn q{'html' is required}       unless $param{html};
    return warn q{'length' is required}     unless $param{length};
    return warn q{'length' is not numeric.} unless $param{length} =~ /^\d+$/;

    my @pages = HTML::Split->split(
        html        => $param{html},
        length      => $param{length},
        extend_tags => $param{extend_tags} || [],
    );

    my $self = bless {
        pages       => \@pages,
        total_pages => scalar @pages,
    }, $class;

    $self->current_page(1);

    return $self;
}

sub current_page {
    my ($self, $page) = @_;
    if (defined $page && $page > 0) {
        $self->{current_page} = $page;
        $self->{prev_page} = ($page - 1 > 0) ? $page - 1 : undef;
        $self->{next_page} = ($page + 1 <= $self->total_pages) ? $page + 1 : undef;
        return $self;
    }
    return $self->{current_page};
}

sub text {
    my $self = shift;
    return wantarray ? @{ $self->{pages} }
                     : $self->{pages}[$self->current_page - 1];
}

1;
__END__