1 package Org::Element::Table;
7 extends 'Org::Element';
9 our $VERSION = '0.23'; # VERSION
11 has _dummy => (is => 'rw'); # workaround Moo bug
14 require Org::Element::TableRow;
15 require Org::Element::TableVLine;
16 require Org::Element::TableCell;
17 my ($self, $args) = @_;
18 my $pass = $args->{pass} // 1;
20 # parse _str into rows & cells
21 my $_str = $args->{_str};
22 if (defined $_str && !defined($self->children)) {
24 if (!defined($self->_str_include_children)) {
25 $self->_str_include_children(1);
28 my $doc = $self->document;
29 my @rows0 = split /\R/, $_str;
31 for my $row0 (@rows0) {
32 $log->tracef("table line: %s", $row0);
33 next unless $row0 =~ /\S/;
35 if ($row0 =~ /^\s*\|--+(?:\+--+)*\|?\s*$/) {
36 $row = Org::Element::TableVLine->new(parent => $self);
37 } elsif ($row0 =~ /^\s*\|\s*(.+?)\s*\|?\s*$/) {
39 $row = Org::Element::TableRow->new(
40 parent => $self, children=>[]);
41 for my $cell0 (split /\s*\|\s*/, $s) {
42 my $cell = Org::Element::TableCell->new(
43 parent => $row, children=>[]);
44 $doc->_add_text($cell0, $cell, $pass);
45 push @{ $row->children }, $cell;
48 die "Invalid line in table: $row0";
50 push @{$self->children}, $row;
57 return [] unless $self->children;
59 for my $el (@{$self->children}) {
60 push @$rows, $el if $el->isa('Org::Element::TableRow');
67 return 0 unless $self->children;
69 for my $el (@{$self->children}) {
70 $n++ if $el->isa('Org::Element::TableRow');
77 return 0 unless $self->children;
81 for my $el (@{$self->children}) {
82 if ($el->isa('Org::Element::TableRow')) {
87 return 0 unless $row; # table doesn't have any row
90 for my $el (@{$row->children}) {
91 $n++ if $el->isa('Org::Element::TableCell');
98 return [] unless $self->children;
101 for my $row (@{$self->children}) {
102 next unless $row->isa('Org::Element::TableRow');
103 push @rows, $row->as_array;
109 # ABSTRACT: Represent Org table
116 Org::Element::Table - Represent Org table
124 Derived from L<Org::Element>. Must have L<Org::Element::TableRow> or
125 L<Org::Element::TableVLine> instances as its children.
131 =for Pod::Coverage BUILD
133 =head2 $table->rows() => ELEMENTS
135 Return the rows of the table.
137 =head2 $table->as_aoa() => ARRAYREF
139 Return the rows of the table, each row already an array of cells produced using
140 as_array() method. Vertical lines will be skipped/ignored.
142 =head2 $table->row_count() => INT
144 Return the number of rows that the table has.
146 =head2 $table->column_count() => INT
148 Return the number of columns that the table has. It is counted from the first
153 Steven Haryanto <stevenharyanto@gmail.com>
155 =head1 COPYRIGHT AND LICENSE
157 This software is copyright (c) 2012 by Steven Haryanto.
159 This is free software; you can redistribute it and/or modify it under
160 the same terms as the Perl 5 programming language system itself.