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