| 1 | package Org::Element::Table; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use locale; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | ▶ | use Log::ger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 6 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 6 | extends 'Org::Element'; | |||||||||||||||||||||||||||
| 8 | 6 | with 'Org::ElementRole'; | |||||||||||||||||||||||||||
| 9 | 6 | with 'Org::ElementRole::Block'; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 6 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 12 | 6 | our $DATE = '2023-11-06'; # DATE | |||||||||||||||||||||||||||
| 13 | 6 | our $DIST = 'Org-Parser'; # DIST | |||||||||||||||||||||||||||
| 14 | 6 | our $VERSION = '0.561'; # VERSION | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 6 | has _dummy => (is => 'rw'); # workaround Moo bug | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | sub BUILD { | ||||||||||||||||||||||||||||
| 19 | 6 | ▶ | require Org::Element::TableRow; | ||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=10 SCAR=23.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 20 | 6 | require Org::Element::TableHLine; | |||||||||||||||||||||||||||
| 21 | 6 | require Org::Element::TableCell; | |||||||||||||||||||||||||||
| 22 | 6 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 23 | 6 | ▶ | my $pass = $args->{pass} // 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args->{"pass"} // 1
MC/DC0%0/1
$args->{"pass"} // 1
$args->{"pass"}
Truth table50%1/2
A
A= $args->{"pass"}
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # parse _str into rows & cells | ||||||||||||||||||||||||||||
| 26 | 6 | my $_str = $args->{_str}; | |||||||||||||||||||||||||||
| 27 | 6 | ▶ | if (defined $_str && !defined($self->children)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $_str and not defined $self->children
MC/DC0%0/2
defined $_str and not defined $self->children
defined $_str
not defined $self->children
Truth table33%1/3
A and not(B)
A= defined $_strB= not defined $self->children
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 6 | ▶ | if (!defined($self->_str_include_children)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 6 | $self->_str_include_children(1); | |||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 6 | my $doc = $self->document; | |||||||||||||||||||||||||||
| 34 | 6 | my @rows0 = split /\R/, $_str; | |||||||||||||||||||||||||||
| 35 | 6 | $self->children([]); | |||||||||||||||||||||||||||
| 36 | 6 | for my $row0 (@rows0) { | |||||||||||||||||||||||||||
| 37 | 20 | log_trace("table line: %s", $row0); | |||||||||||||||||||||||||||
| 38 | 20 | ▶ | next unless $row0 =~ /\S/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 20 | my $row; | |||||||||||||||||||||||||||
| 40 | 20 | ▶ | if ($row0 =~ /^\s*\|--+(?:\+--+)*\|?\s*$/) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 41 | 2 | $row = Org::Element::TableHLine->new(parent => $self); | |||||||||||||||||||||||||||
| 42 | } elsif ($row0 =~ /^\s*\|\s*(.+?)\s*\|?\s*$/) { | ||||||||||||||||||||||||||||
| 43 | 18 | my $s = $1; | |||||||||||||||||||||||||||
| 44 | 18 | $row = Org::Element::TableRow->new( | |||||||||||||||||||||||||||
| 45 | parent => $self, children=>[]); | ||||||||||||||||||||||||||||
| 46 | 18 | for my $cell0 (split /\s*\|\s*/, $s) { | |||||||||||||||||||||||||||
| 47 | 32 | my $cell = Org::Element::TableCell->new( | |||||||||||||||||||||||||||
| 48 | parent => $row, children=>[]); | ||||||||||||||||||||||||||||
| 49 | 32 | $doc->_add_text($cell0, $cell, $pass); | |||||||||||||||||||||||||||
| 50 | 32 | push @{ $row->children }, $cell; | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | } else { | ||||||||||||||||||||||||||||
| 53 | 0 | $self->die("Invalid line in table: $row0"); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | 20 | push @{$self->children}, $row; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | sub rows { | ||||||||||||||||||||||||||||
| 61 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
rows: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | 2 | ▶ | return [] unless $self->children; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 2 | my $rows = []; | |||||||||||||||||||||||||||
| 64 | 2 | for my $el (@{$self->children}) { | |||||||||||||||||||||||||||
| 65 | 10 | ▶ | push @$rows, $el if $el->isa('Org::Element::TableRow'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 2 | $rows; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | sub row_count { | ||||||||||||||||||||||||||||
| 71 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
row_count: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 1 | ▶ | return 0 unless $self->children; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 1 | my $n = 0; | |||||||||||||||||||||||||||
| 74 | 1 | for my $el (@{$self->children}) { | |||||||||||||||||||||||||||
| 75 | 5 | ▶ | $n++ if $el->isa('Org::Element::TableRow'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | 1 | $n; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | sub column_count { | ||||||||||||||||||||||||||||
| 81 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
column_count: called CC=7 SCAR=19.9
Pod
documented
| |||||||||||||||||||||||||||||
| 82 | 1 | ▶ | return 0 unless $self->children; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | # get first row | ||||||||||||||||||||||||||||
| 85 | 1 | my $row; | |||||||||||||||||||||||||||
| 86 | 1 | for my $el (@{$self->children}) { | |||||||||||||||||||||||||||
| 87 | 1 | ▶ | if ($el->isa('Org::Element::TableRow')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 1 | $row = $el; | |||||||||||||||||||||||||||
| 89 | 1 | last; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 1 | ▶ | return 0 unless $row; # table doesn't have any row | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 1 | my $n = 0; | |||||||||||||||||||||||||||
| 95 | 1 | for my $el (@{$row->children}) { | |||||||||||||||||||||||||||
| 96 | 3 | ▶ | $n++ if $el->isa('Org::Element::TableCell'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 1 | $n; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | sub as_aoa { | ||||||||||||||||||||||||||||
| 102 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
as_aoa: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 1 | ▶ | return [] unless $self->children; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 1 | my @rows; | |||||||||||||||||||||||||||
| 106 | 1 | for my $row (@{$self->children}) { | |||||||||||||||||||||||||||
| 107 | 5 | ▶ | next unless $row->isa('Org::Element::TableRow'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 4 | push @rows, $row->as_array; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 1 | \@rows; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 6 | 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.561 of Org::Element::Table (from Perl distribution Org-Parser), released on 2023-11-06. | ||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||