Text-MarkdownTable

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

0.3.1  2015-07-08 12:12:59 CEST
    - fix minimal dependency on Moo
    - additional option 'edges' to disable first and last border
    - public method 'streaming'

0.3.0  2015-07-01 12:02:42 CEST
    - additional option 'header'

0.2.4  2014-10-23 00:17:08 CEST
    - fixed undefined $_ in simple.t test (Patrick Hochstenbach)

0.2.3  2014-07-16 09:45:11 CEST
    - improved performance by using printf
    - support undefined column names

README  view on Meta::CPAN

          a|table
          is|nice

        Note that single-column tables are don't look like tables on
        condense format.

METHODS
    add( $row )
        Add a row as hash reference. Returns the table instance.

    streaming
        Returns whether rows are directly written or buffered until "done"
        is called.

    done
        Finish and write the table unless it has already been written in
        "streaming" mode.

SEE ALSO
    See Catmandu::Exporter::Table for an application of this module that can
    be used to easily convert data to Markdown tables.

    Similar table-generating modules include:

    Text::Table::Tiny
    Text::TabularDisplay
    Text::SimpleTable

lib/Text/MarkdownTable.pm  view on Meta::CPAN


has edges => (
    is => 'rw',
    default => sub { 1 },
);

has condense => (
    is => 'rw',
);

has streaming => (is => 'rwp');

has _fixed_width => (is => 'rw', default => sub { 1 });

# TODO: duplicated in Catmandu::Exporter::CSV fields-coerce
sub _coerce_list {
    if (ref $_[0]) {
        return $_[0] if ref $_[0] eq 'ARRAY';
        return [sort keys %{$_[0]}] if ref $_[0] eq 'HASH';
    }    
    return [split ',', $_[0]];

lib/Text/MarkdownTable.pm  view on Meta::CPAN


sub add {
    my ($self, $data) = @_;
    unless ($self->fields) {
        $self->{fields} = [ sort keys %$data ]
    }
    my $fields = $self->fields;
    my $widths = $self->widths; # may set 
    my $row = [ ];

    if (!$self->streaming and ($self->condense or $self->_fixed_width)) {
        $self->_set_streaming(1);
        $self->_print_header if $self->header;
    }

    foreach my $col (0..(@$fields-1)) {
        my $field = $fields->[$col];
        my $width = $widths->[$col];

        my $value = $data->{$field} // "";
        $value =~ s/[\n|]/ /g;

lib/Text/MarkdownTable.pm  view on Meta::CPAN

        push @$row, $value;
    }

    $self->_add_row($row);
    $self;
}

sub _add_row {
    my ($self, $row) = @_;

    if ($self->streaming) {
        $self->_print_row($row);
    } else {
        push @{$self->{_rows}}, $row;
    }
}

sub done {
    my ($self) = @_;

    if ($self->{_rows}) {

lib/Text/MarkdownTable.pm  view on Meta::CPAN

=back

=head1 METHODS

=over

=item add( $row )

Add a row as hash reference. Returns the table instance.

=item streaming

Returns whether rows are directly written or buffered until C<done> is called.

=item done

Finish and write the table unless it has already been written in C<streaming>
mode.

=back

=head1 SEE ALSO

See L<Catmandu::Exporter::Table> for an application of this module that can be
used to easily convert data to Markdown tables.

Similar table-generating modules include:

t/simple.t  view on Meta::CPAN

TABLE

is_table [{ aa => 'Hi', b => 'World', c => 'long value' }],
          widths => '5,3,6', condense => 1,
<<TABLE, "condense with truncation";
aa|b|c
--|-|-
Hi|Wor|lon...
TABLE

ok !Text::MarkdownTable->new->streaming, "no streaming by default";

my $out = "";
my $table = Text::MarkdownTable->new( condense => 1, file => \$out );
$table->add({ foo => 1, bar => 1024 });
ok $table->streaming, "streaming mode";
is $out, <<TABLE, "streaming mode";
bar|foo
---|---
1024|1
TABLE

$table->add({ doz => 7, bar => 0, doz => 'ignored' });
is $out, <<TABLE, "streaming mode";
bar|foo
---|---
1024|1
0|
TABLE

is_table [{a => 7},{a => 8}], 
    header => 0,
    "| 7 |\n| 8 |\n", "disable header";



( run in 0.329 second using v1.01-cache-2.11-cpan-4d50c553e7e )