File Coverage

blib/lib/Org/Element/Table.pm
Criterion Covered Total %
statement 74 75 98.6
branch 19 30 63.3
condition 2 5 40.0
subroutine 9 9 100.0
pod 4 5 80.0
total 108 124 87.1


line stmt bran cond sub pod time code
1             package Org::Element::Table;
2              
3 5     5   1130 use 5.010;
  5         28  
4 5     5   36 use locale;
  5         9  
  5         28  
5 5     5   187 use Log::ger;
  5         19  
  5         31  
6 5     5   1132 use Moo;
  5         12  
  5         48  
7             extends 'Org::Element';
8             with 'Org::ElementRole';
9             with 'Org::ElementRole::Block';
10              
11             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
12             our $DATE = '2023-08-05'; # DATE
13             our $DIST = 'Org-Parser'; # DIST
14             our $VERSION = '0.560'; # VERSION
15              
16             has _dummy => (is => 'rw'); # workaround Moo bug
17              
18             sub BUILD {
19 6     6 0 6301 require Org::Element::TableRow;
20 6         1372 require Org::Element::TableHLine;
21 6         1264 require Org::Element::TableCell;
22 6         16 my ($self, $args) = @_;
23 6   50     27 my $pass = $args->{pass} // 1;
24              
25             # parse _str into rows & cells
26 6         10 my $_str = $args->{_str};
27 6 50 33     56 if (defined $_str && !defined($self->children)) {
28              
29 6 50       31 if (!defined($self->_str_include_children)) {
30 6         16 $self->_str_include_children(1);
31             }
32              
33 6         18 my $doc = $self->document;
34 6         43 my @rows0 = split /\R/, $_str;
35 6         22 $self->children([]);
36 6         13 for my $row0 (@rows0) {
37 20         67 log_trace("table line: %s", $row0);
38 20 50       109 next unless $row0 =~ /\S/;
39 20         31 my $row;
40 20 100       229 if ($row0 =~ /^\s*\|--+(?:\+--+)*\|?\s*$/) {
    50          
41 2         44 $row = Org::Element::TableHLine->new(parent => $self);
42             } elsif ($row0 =~ /^\s*\|\s*(.+?)\s*\|?\s*$/) {
43 18         47 my $s = $1;
44 18         324 $row = Org::Element::TableRow->new(
45             parent => $self, children=>[]);
46 18         10282 for my $cell0 (split /\s*\|\s*/, $s) {
47 32         614 my $cell = Org::Element::TableCell->new(
48             parent => $row, children=>[]);
49 32         5817 $doc->_add_text($cell0, $cell, $pass);
50 32         50 push @{ $row->children }, $cell;
  32         111  
51             }
52             } else {
53 0         0 $self->die("Invalid line in table: $row0");
54             }
55 20         3629 push @{$self->children}, $row;
  20         106  
56             }
57             }
58             }
59              
60             sub rows {
61 2     2 1 9 my ($self) = @_;
62 2 50       12 return [] unless $self->children;
63 2         6 my $rows = [];
64 2         3 for my $el (@{$self->children}) {
  2         6  
65 10 100       31 push @$rows, $el if $el->isa('Org::Element::TableRow');
66             }
67 2         27 $rows;
68             }
69              
70             sub row_count {
71 1     1 1 5 my ($self) = @_;
72 1 50       7 return 0 unless $self->children;
73 1         3 my $n = 0;
74 1         3 for my $el (@{$self->children}) {
  1         4  
75 5 100       24 $n++ if $el->isa('Org::Element::TableRow');
76             }
77 1         11 $n;
78             }
79              
80             sub column_count {
81 1     1 1 6 my ($self) = @_;
82 1 50       6 return 0 unless $self->children;
83              
84             # get first row
85 1         4 my $row;
86 1         3 for my $el (@{$self->children}) {
  1         4  
87 1 50       7 if ($el->isa('Org::Element::TableRow')) {
88 1         2 $row = $el;
89 1         4 last;
90             }
91             }
92 1 50       4 return 0 unless $row; # table doesn't have any row
93              
94 1         2 my $n = 0;
95 1         3 for my $el (@{$row->children}) {
  1         3  
96 3 50       11 $n++ if $el->isa('Org::Element::TableCell');
97             }
98 1         5 $n;
99             }
100              
101             sub as_aoa {
102 1     1 1 7 my ($self) = @_;
103 1 50       6 return [] unless $self->children;
104              
105 1         3 my @rows;
106 1         4 for my $row (@{$self->children}) {
  1         4  
107 5 100       16 next unless $row->isa('Org::Element::TableRow');
108 4         10 push @rows, $row->as_array;
109             }
110 1         36 \@rows;
111             }
112              
113             1;
114             # ABSTRACT: Represent Org table
115              
116             __END__
117              
118             =pod
119              
120             =encoding UTF-8
121              
122             =head1 NAME
123              
124             Org::Element::Table - Represent Org table
125              
126             =head1 VERSION
127              
128             This document describes version 0.560 of Org::Element::Table (from Perl distribution Org-Parser), released on 2023-08-05.
129              
130             =head1 DESCRIPTION
131              
132             Derived from L<Org::Element>. Must have L<Org::Element::TableRow> or
133             L<Org::Element::TableHLine> instances as its children.
134              
135             =for Pod::Coverage BUILD
136              
137             =head1 ATTRIBUTES
138              
139             =head1 METHODS
140              
141             =head2 $table->rows() => ELEMENTS
142              
143             Return the rows of the table.
144              
145             =head2 $table->as_aoa() => ARRAY
146              
147             Return the rows of the table, each row already an arrayref of cells produced
148             using as_array() method. Horizontal lines will be skipped/ignored.
149              
150             =head2 $table->row_count() => INT
151              
152             Return the number of rows that the table has.
153              
154             =head2 $table->column_count() => INT
155              
156             Return the number of columns that the table has. It is counted from the first
157             row.
158              
159             =head1 HOMEPAGE
160              
161             Please visit the project's homepage at L<https://metacpan.org/release/Org-Parser>.
162              
163             =head1 SOURCE
164              
165             Source repository is at L<https://github.com/perlancar/perl-Org-Parser>.
166              
167             =head1 AUTHOR
168              
169             perlancar <perlancar@cpan.org>
170              
171             =head1 CONTRIBUTING
172              
173              
174             To contribute, you can send patches by email/via RT, or send pull requests on
175             GitHub.
176              
177             Most of the time, you don't need to build the distribution yourself. You can
178             simply modify the code, then test via:
179              
180             % prove -l
181              
182             If you want to build the distribution (e.g. to try to install it locally on your
183             system), you can install L<Dist::Zilla>,
184             L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
185             L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
186             Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
187             that are considered a bug and can be reported to me.
188              
189             =head1 COPYRIGHT AND LICENSE
190              
191             This software is copyright (c) 2023, 2022, 2021, 2020, 2019, 2017, 2016, 2015, 2014, 2013, 2012, 2011 by perlancar <perlancar@cpan.org>.
192              
193             This is free software; you can redistribute it and/or modify it under
194             the same terms as the Perl 5 programming language system itself.
195              
196             =head1 BUGS
197              
198             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Org-Parser>
199              
200             When submitting a bug or request, please include a test-file or a
201             patch to an existing test-file that illustrates the bug or desired
202             feature.
203              
204             =cut