App-termpub

 view release on metacpan or  search on metacpan

lib/App/termpub/Pager/HTML.pm  view on Meta::CPAN

package App::termpub::Pager::HTML;
use Mojo::Base 'App::termpub::Pager';

use Mojo::DOM;
use Curses;

has pad_columns => sub {
    my $default = 80;
    my ( $rows, $columns );
    getmaxyx( $rows, $columns );
    return $columns < $default ? $columns : $default;
    return $default;
};

has row     => 0;
has hrefs   => sub { [] };
has id_line => sub {
    {}
};

has 'hyphenator';

my %noshow =
  map { $_ => 1 } qw[base basefont bgsound meta param script style];

my %empty = map { $_ => 1 } qw[br canvas col command embed frame
  img is index keygen link];

my %inline = map { $_ => 1 }
  qw[a abbr area b bdi bdo big button cite code dfn em font i
  input kbd label mark meter nobr progress q rp rt ruby s
  samp small span strike strong sub sup time tt u var wbr];

my %block = map { $_ => 1 }
  qw[address applet article aside audio blockquote body caption
  center colgroup datalist del dir div dd details dl dt
  fieldset figcaption figure footer form frameset h1 h2 h3
  h4 h5 h6 head header hgroup hr html iframe ins legend li
  listing map marquee menu nav noembed noframes noscript
  object ol optgroup option p pre select section source summary
  table tbody td tfoot th thead title tr track ul video];

my %attrs       = ( h1 => A_STANDOUT );
my %vspace      = ( li => 1 );
my %left_margin = ( li => 2, pre => 2, code => 2 );

sub render {
    my ( $self, $content ) = @_;

    $self->row(0);
    $self->pad->clear;
    $self->pad->resize( $self->pad_rows, $self->pad_columns );
    $self->hrefs( [] );
    $self->id_line( {} );

    my $node = Mojo::DOM->new($content)->at('body');
    return if !$node;
    my $nodes = [];
    $self->process_node( $node, $nodes );

    $self->render_nodes($nodes);
    $self->pad_rows( $self->row + 1 );
    $self->pad->resize( $self->pad_rows, $self->pad_columns );
    return;
}

sub process_node {
    my ( $self, $node, $nodes ) = @_;

    foreach my $node ( $node->child_nodes->each ) {
        if ( $node->type eq 'text' ) {
            push @$nodes, [ text => $node->content ];
            next;
        }
        my $tag = lc $node->tag;

        if ( $tag eq 'hr' ) {
            push @$nodes, [ text => '--------' ];
            next;
        }
        elsif ( $tag eq 'br' ) {
            push @$nodes, [ newline => 1 ];
            next;
        }
        elsif ( $tag eq 'img' ) {
            if ( my $alt = $node->attr('alt') ) {
                my $src = $node->attr('src');
                my $num = push @{ $self->hrefs }, [ img => $src ];
                if ( $alt =~ /^\s*$/ ) {
                    $alt = $src;



( run in 0.463 second using v1.01-cache-2.11-cpan-5735350b133 )