Algorithm-Diff-HTMLTable

 view release on metacpan or  search on metacpan

lib/Algorithm/Diff/HTMLTable.pm  view on Meta::CPAN

                    line_a    => $items_1[ $index - 1 ] // '',
                    line_b    => $items_2[ $index - 1 ] // '',
                    color_a   => 'red',
                    color_b   => 'green',
                );
            }
        }
    }

    return $rows;
}

sub _add_tablerow {
    my $self = shift;

    my %params = @_;

    my ($line_nr_a, $line_a, $color_a) = @params{qw/line_nr_a line_a color_a/};
    my ($line_nr_b, $line_b, $color_b) = @params{qw/line_nr_b line_b color_b/};

    $color_a = $color_a ? qq~style="color: $color_a;"~ : '';
    $color_b = $color_b ? qq~style="color: $color_b;"~ : '';

    $line_a = encode_entities( $line_a // '' );
    $line_b = encode_entities( $line_b // '' );

    $line_a =~ s{ }{ }g;
    $line_b =~ s{ }{ }g;

    my $row = qq~
        <tr style="border: 1px solid">
            <td style="background-color: gray">$line_nr_a</td>
            <td $color_a>$line_a</td>
            <td style="background-color: gray">$line_nr_b</td>
            <td $color_b>$line_b</td>
        </tr>
    ~;
}

sub _end_table {
    my $self = shift;

    return qq~
            </tbody>
        </table>
    ~;
}

sub _file_info {
    my ($self, $file, $index) = @_;

    if ( $self->{"title_$index"} ) {
        return $self->{"title_$index"};
    }

    return '' if !-f $file;

    my $mtime = (stat $file)[9];
    my $date  = _format_date( $mtime );

    return "$file<br />$date";
}

sub _format_date {
    my ($time) = @_;

    my $date = localtime $time;
    return $date->cdate;
}

sub _read_file {
    my ($self, $file) = @_;
    
    return if !$file;

    if ( ref $file && ref $file eq 'ARRAY' ) {
        return @{ $file };
    }

    return if !-r $file;
    
    my @lines;
    open my $fh, '<', $file;
    if ( $self->{encoding} ) {
        binmode $fh, ':encoding(' . $self->{encoding} . ')';
    }
    
    local $/ = $self->{eol} // "\n";
    
    @lines = <$fh>;
    close $fh;
    
    return @lines;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

Algorithm::Diff::HTMLTable - Show differences of a file as a HTML table

=head1 VERSION

version 0.05

=head1 SYNOPSIS

    my $builder = Algorithm::Diff::HTMLTable->new(
       id       => 'diff_table',
       encoding => 'utf8',
    );
    
    $diff = $builder->diff( $sourcefile, $targetfile );

=head1 DESCRIPTION



( run in 0.712 second using v1.01-cache-2.11-cpan-df04353d9ac )