]> git.donarmstrong.com Git - liborg-parser-perl.git/blob - lib/Org/Element/Table.pm
Import original source of Org-Parser 0.23
[liborg-parser-perl.git] / lib / Org / Element / Table.pm
1 package Org::Element::Table;
2
3 use 5.010;
4 use locale;
5 use Log::Any '$log';
6 use Moo;
7 extends 'Org::Element';
8
9 our $VERSION = '0.23'; # VERSION
10
11 has _dummy => (is => 'rw'); # workaround Moo bug
12
13 sub BUILD {
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;
19
20     # parse _str into rows & cells
21     my $_str = $args->{_str};
22     if (defined $_str && !defined($self->children)) {
23
24         if (!defined($self->_str_include_children)) {
25             $self->_str_include_children(1);
26         }
27
28         my $doc = $self->document;
29         my @rows0 = split /\R/, $_str;
30         $self->children([]);
31         for my $row0 (@rows0) {
32             $log->tracef("table line: %s", $row0);
33             next unless $row0 =~ /\S/;
34             my $row;
35             if ($row0 =~ /^\s*\|--+(?:\+--+)*\|?\s*$/) {
36                 $row = Org::Element::TableVLine->new(parent => $self);
37             } elsif ($row0 =~ /^\s*\|\s*(.+?)\s*\|?\s*$/) {
38                 my $s = $1;
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;
46                 }
47             } else {
48                 die "Invalid line in table: $row0";
49             }
50             push @{$self->children}, $row;
51         }
52     }
53 }
54
55 sub rows {
56     my ($self) = @_;
57     return [] unless $self->children;
58     my $rows = [];
59     for my $el (@{$self->children}) {
60         push @$rows, $el if $el->isa('Org::Element::TableRow');
61     }
62     $rows;
63 }
64
65 sub row_count {
66     my ($self) = @_;
67     return 0 unless $self->children;
68     my $n = 0;
69     for my $el (@{$self->children}) {
70         $n++ if $el->isa('Org::Element::TableRow');
71     }
72     $n;
73 }
74
75 sub column_count {
76     my ($self) = @_;
77     return 0 unless $self->children;
78
79     # get first row
80     my $row;
81     for my $el (@{$self->children}) {
82         if ($el->isa('Org::Element::TableRow')) {
83             $row = $el;
84             last;
85         }
86     }
87     return 0 unless $row; # table doesn't have any row
88
89     my $n = 0;
90     for my $el (@{$row->children}) {
91         $n++ if $el->isa('Org::Element::TableCell');
92     }
93     $n;
94 }
95
96 sub as_aoa {
97     my ($self) = @_;
98     return [] unless $self->children;
99
100     my @rows;
101     for my $row (@{$self->children}) {
102         next unless $row->isa('Org::Element::TableRow');
103         push @rows, $row->as_array;
104     }
105     \@rows;
106 }
107
108 1;
109 # ABSTRACT: Represent Org table
110
111
112 =pod
113
114 =head1 NAME
115
116 Org::Element::Table - Represent Org table
117
118 =head1 VERSION
119
120 version 0.23
121
122 =head1 DESCRIPTION
123
124 Derived from L<Org::Element>. Must have L<Org::Element::TableRow> or
125 L<Org::Element::TableVLine> instances as its children.
126
127 =head1 ATTRIBUTES
128
129 =head1 METHODS
130
131 =for Pod::Coverage BUILD
132
133 =head2 $table->rows() => ELEMENTS
134
135 Return the rows of the table.
136
137 =head2 $table->as_aoa() => ARRAYREF
138
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.
141
142 =head2 $table->row_count() => INT
143
144 Return the number of rows that the table has.
145
146 =head2 $table->column_count() => INT
147
148 Return the number of columns that the table has. It is counted from the first
149 row.
150
151 =head1 AUTHOR
152
153 Steven Haryanto <stevenharyanto@gmail.com>
154
155 =head1 COPYRIGHT AND LICENSE
156
157 This software is copyright (c) 2012 by Steven Haryanto.
158
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.
161
162 =cut
163
164
165 __END__
166