Element.pm

filter: Statementstmt 88.7% 110 / 124 Branchbran 69.6% 39 / 56 Conditioncond 52.9% 9 / 17 MC/DCmcdc 27.2% 3 / 11 Subroutinesub 95.6% 22 / 23 Podpod 100% 15 / 15 totaltotal 80.4% 198 / 246 CC 37 SCAR 14.7
CC
37
Cov
80%
settings
12.0
field_name
7.3
__ANON__
6.7
CRAP 4.4
« Element/Footnote.pm ↑ Summary Element/Text.pm »
1 package Org::Element;
2  
3 46 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 46 ▶use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 46 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 46 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 46 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 46 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10 46 our $DATE = '2023-11-06'; # DATE
11 46 our $DIST = 'Org-Parser'; # DIST
12 46 our $VERSION = '0.561'; # VERSION
13  
14 46 has document => (is => 'rw');
15 46 has parent => (is => 'rw');
16 46 has children => (is => 'rw');
17  
18 # store the raw string (to preserve original formatting), not all elements use
19 # this, usually only more complex elements
20 46 has _str => (is => 'rw');
21 46 has _str_include_children => (is => 'rw');
22  
23 sub die {
24 6 ▶ my ($self, $msg) = @_;
Subroutine die: called CC=3 SCAR=13.1
Pod documented
25 6 ▶ die $msg .
Branch: 1 branch
BranchTrueFalseTotal
$self->document && $self->document->_srclabel ? : 6 0 6
Condition33%1/3
$self->document and $self->document->_srclabel
!ll&&!rl&&r
006
MC/DC0%0/2
$self->document and $self->document->_srclabel
$self->document $self->document->_srclabel
Truth table33%1/3
A and B
A= $self->documentB= $self->document->_srclabel
ABresult
0X0
100
111
26 " (element: ".ref($self).
27 ", document: ".($self->document && $self->document->_srclabel ? $self->document->_srclabel : "-").")";
28 }
29  
30 sub children_as_string {
31 258 ▶ my ($self) = @_;
Subroutine children_as_string: called CC=2 SCAR=6.9
Pod documented
32 258 ▶ return "" unless $self->children;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->children 209 49 258
33 49 join "", map {$_->as_string} @{$self->children};
34 }
35  
36 sub as_string {
37 20 ▶ my ($self) = @_;
Subroutine as_string: called CC=3 SCAR=11.0
Pod documented
38  
39 20 ▶ if (defined $self->_str) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_str) { } 2 18 20
40 2 ▶ return $self->_str .
Branch: 1 branch
BranchTrueFalseTotal
$self->_str_include_children ? : 0 2 2
41 ($self->_str_include_children ? "" : $self->children_as_string);
42 } else {
43 18 return "" . $self->children_as_string;
44 }
45 }
46  
47 sub seniority {
48 44 ▶ my ($self) = @_;
Subroutine seniority: called CC=5 SCAR=17.5
Pod documented
49 44 my $c;
50 44 ▶ return -4 unless $self->parent && ($c = $self->parent->children);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->parent and $c = $self->parent->children 0 44 44
Condition33%1/3
$self->parent and $c = $self->parent->children
!ll&&!rl&&r
0044
MC/DC0%0/2
$self->parent and $c = $self->parent->children
$self->parent $c = $self->parent->children
Truth table33%1/3
A and B
A= $self->parentB= $c = $self->parent->children
ABresult
0X0
100
111
51 44 my $addr = refaddr($self);
52 44 ▶ for (my $i=0; $i < @$c; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$c) 75 0 75
53 75 ▶ return $i if refaddr($c->[$i]) == $addr;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::refaddr($c->[$i]) == $addr 44 31 75
54 }
55 0 return undef; ## no critic: Subroutines::ProhibitExplicitReturnUndef
56 }
57  
58 sub prev_sibling {
59 21 ▶ my ($self) = @_;
Subroutine prev_sibling: called CC=3 SCAR=11.0
Pod documented
60  
61 21 my $sen = $self->seniority;
62 21 ▶ return undef unless defined($sen) && $sen > 0; ## no critic: Subroutines::ProhibitExplicitReturnUndef
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sen and $sen > 0 9 12 21
Condition66%2/3
defined $sen and $sen > 0
!ll&&!rl&&r
0912
MC/DC50%1/2
defined $sen and $sen > 0
defined $sen $sen > 0
Truth table66%2/3
A and B
A= defined $senB= $sen > 0
ABresult
0X0
100
111
63 12 my $c = $self->parent->children;
64 12 $c->[$sen-1];
65 }
66  
67 sub next_sibling {
68 7 ▶ my ($self) = @_;
Subroutine next_sibling: called CC=3 SCAR=11.0
Pod documented
69  
70 7 my $sen = $self->seniority;
71 7 ▶ return undef unless defined($sen); ## no critic: Subroutines::ProhibitExplicitReturnUndef
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sen 0 7 7
72 7 my $c = $self->parent->children;
73 7 ▶ return undef unless $sen < @$c-1; ## no critic: Subroutines::ProhibitExplicitReturnUndef
Branch: 1 branch
BranchTrueFalseTotal
unless $sen < @$c - 1 2 5 7
74 5 $c->[$sen+1];
75 }
76  
77 890 ▶sub extra_walkables { return () }
Subroutine extra_walkables: called CC=1 SCAR=0.0
Pod documented
78  
79 sub walk {
80 1053 ▶ my ($self, $code, $_level) = @_;
Subroutine walk: called CC=5 SCAR=16.1
Pod documented
81 1053 ▶ $_level //= 0;
Condition100%2/2
$_level //= 0
l!l
96687
MC/DC100%1/1
$_level //= 0
$_level
Truth table100%2/2
A
A= $_level
Aresult
11
00
82 1053 $code->($self, $_level);
83 1051 ▶ if ($self->children) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->children) 376 675 1051
84 # we need to copy children first to a temporary array so that in the
85 # event when during walk a child is removed, all the children are still
86 # walked into.
87 376 my @children = @{ $self->children };
88 376 for (@children) {
89 805 $_->walk($code, $_level+1);
90 }
91 }
92 1051 $_->walk($code, $_level+1) for $self->extra_walkables;
93 }
94  
95 sub find {
96 7 ▶ my ($self, $criteria) = @_;
Subroutine find: called CC=2 SCAR=7.2
Pod documented
97 7 ▶ return unless $self->children;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->children 0 7 7
98 7 my @res;
99 $self->walk(
100 sub {
101 58 ▶ my $el = shift;
Subroutine __ANON__: called CC=6 SCAR=19.0
102 58 ▶ if (ref($criteria) eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $criteria eq 'CODE') { } 12 46 58
elsif ($criteria =~ /^\w+$/l) { } 46 0 46
103 12 ▶ push @res, $el if $criteria->($el);
Branch: 1 branch
BranchTrueFalseTotal
if &$criteria($el) 2 10 12
104 } elsif ($criteria =~ /^\w+$/) {
105 46 ▶ push @res, $el if $el->isa("Org::Element::$criteria");
Branch: 1 branch
BranchTrueFalseTotal
if $el->isa("Org::Element::$criteria") 15 31 46
106 } else {
107 0 ▶ push @res, $el if $el->isa($criteria);
Branch: 1 branch
BranchTrueFalseTotal
if $el->isa($criteria) 0 0 0
108 }
109 7 });
110 7 @res;
111 }
112  
113 sub walk_parents {
114 10 ▶ my ($self, $code) = @_;
Subroutine walk_parents: called CC=3 SCAR=11.0
Pod documented
115 10 my $parent = $self->parent;
116 10 ▶ while ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 17 7 24
117 17 ▶ return $parent unless $code->($self, $parent);
Branch: 1 branch
BranchTrueFalseTotal
unless &$code($self, $parent) 3 14 17
118 14 $parent = $parent->parent;
119 }
120 7 return;
121 }
122  
123 sub headline {
124 2 ▶ my ($self) = @_;
Subroutine headline: called CC=1 SCAR=0.1
Pod documented
125 2 my $h;
126 $self->walk_parents(
127 sub {
128 2 ▶ my ($el, $p) = @_;
Subroutine __ANON__: called CC=2 SCAR=7.4
129 2 ▶ if ($p->isa('Org::Element::Headline')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->isa("Org::Element::Headline")) 2 0 2
130 2 $h = $p;
131 2 return;
132 }
133 0 1;
134 2 });
135 2 $h;
136 }
137  
138 sub headlines {
139 1 ▶ my ($self) = @_;
Subroutine headlines: called CC=1 SCAR=0.0
Pod documented
140 1 my @res;
141 $self->walk_parents(
142 sub {
143 4 ▶ my ($el, $p) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
144 4 ▶ if ($p->isa('Org::Element::Headline')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->isa("Org::Element::Headline")) 3 1 4
145 3 push @res, $p;
146 }
147 4 1;
148 1 });
149 1 @res;
150 }
151  
152 sub settings {
153 0 ▶ my ($self, $criteria) = @_;
Subroutine settings: not called CC=3 SCAR=24.8
Pod documented
154  
155 0 my @settings = grep { $_->isa("Org::Element::Setting") }
156 0 @{ $self->children };
157 0 ▶ if ($criteria) {
Branch: 1 branch
BranchTrueFalseTotal
if ($criteria) 0 0 0
158 0 ▶ if (ref $criteria eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $criteria eq 'CODE') { } 0 0 0
159 0 @settings = grep { $criteria->($_) } @settings;
160 } else {
161 0 @settings = grep { $_->name eq $criteria } @settings;
162 }
163 }
164 0 @settings;
165 }
166  
167 sub field_name {
168 5 ▶ my ($self) = @_;
Subroutine field_name: called CC=7 SCAR=19.9
Pod documented
169  
170 5 my $prev = $self->prev_sibling;
171 5 ▶ if ($prev && $prev->isa('Org::Element::Text')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($prev and $prev->isa("Org::Element::Text")) 5 0 5
Condition33%1/3
$prev and $prev->isa("Org::Element::Text")
!ll&&!rl&&r
005
MC/DC0%0/2
$prev and $prev->isa("Org::Element::Text")
$prev $prev->isa("Org::Element::Text")
Truth table33%1/3
A and B
A= $prevB= $prev->isa("Org::Element::Text")
ABresult
0X0
100
111
172 5 my $text = $prev->as_string;
173 5 ▶ if ($text =~ /(?:\A|\R)\s*(.+?)\s*:\s*\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /(?:\A|\R)\s*(.+?)\s*:\s*\z/l) 3 2 5
174 3 return $1;
175 }
176 }
177 2 my $parent = $self->parent;
178 2 ▶ if ($parent && $parent->isa('Org::Element::ListItem')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent and $parent->isa("Org::Element::ListItem")) 1 1 2
Condition66%2/3
$parent and $parent->isa("Org::Element::ListItem")
!ll&&!rl&&r
011
MC/DC50%1/2
$parent and $parent->isa("Org::Element::ListItem")
$parent $parent->isa("Org::Element::ListItem")
Truth table66%2/3
A and B
A= $parentB= $parent->isa("Org::Element::ListItem")
ABresult
0X0
100
111
179 1 my $list = $parent->parent;
180 1 ▶ if ($list->type eq 'D') {
Branch: 1 branch
BranchTrueFalseTotal
if ($list->type eq "D") 1 0 1
181 1 return $parent->desc_term->as_string;
182 }
183 }
184 # TODO
185 #if ($parent && $parent->isa('Org::Element::Drawer') &&
186 # $parent->name eq 'PROPERTIES') {
187 #}
188 1 return;
189 }
190  
191 sub remove {
192 1 ▶ my ($self) = @_;
Subroutine remove: called CC=2 SCAR=7.0
Pod documented
193 1 my $parent = $self->parent;
194 1 ▶ return unless $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent 0 1 1
195 1 splice @{$parent->children}, $self->seniority, 1;
196 }
197  
198 46 1;
199 # ABSTRACT: Base class for Org document elements
200  
201 __END__
202  
203 =pod
204  
205 =encoding UTF-8
206  
207 =head1 NAME
208  
209 Org::Element - Base class for Org document elements
210  
211 =head1 VERSION
212  
213 This document describes version 0.561 of Org::Element (from Perl distribution Org-Parser), released on 2023-11-06.
214  
215 =head1 SYNOPSIS
216  
217 # Don't use directly, use the other Org::Element::* classes.
218  
219 =head1 DESCRIPTION
220  
221 This is the base class for all the other Org element classes.
222  
223 =head1 ATTRIBUTES
224  
225 =head2 document => DOCUMENT
226  
227 Link to document object. Elements need this to access file-wide settings,
228 properties, etc.
229  
230 =head2 parent => undef | ELEMENT
231  
232 Link to parent element. Undef if this element is the root element.
233  
234 =head2 children => undef | ARRAY_OF_ELEMENTS
235  
236 =head1 METHODS
237  
238 =head2 $el->children_as_string() => STR
239  
240 Return a concatenation of children's as_string(), or "" if there are no
241 children.
242  
243 =head2 $el->as_string() => STR
244  
245 Return the string representation of element. The default implementation will
246 just use _str (if defined) concatenated with children_as_string().
247  
248 =head2 $el->seniority => INT
249  
250 Find out the ranking of brothers/sisters of all sibling. If we are the first
251 child of parent, return 0. If we are the second child, return 1, and so on.
252  
253 =head2 $el->prev_sibling() => ELEMENT | undef
254  
255 =head2 $el->next_sibling() => ELEMENT | undef
256  
257 =head2 $el->extra_walkables => LIST
258  
259 Return extra walkable elements. The default is to return an empty list, but some
260 elements can have this, for L<Org::Element::Headline>'s title is also a walkable
261 element.
262  
263 =head2 $el->walk(CODEREF)
264  
265 Call CODEREF for node and all descendent nodes (and extra walkables),
266 depth-first. Code will be given the element object as argument.
267  
268 =head2 $el->find(CRITERIA) => ELEMENTS
269  
270 Find subelements. CRITERIA can be a word (e.g. 'Headline' meaning of class
271 'Org::Element::Headline') or a class name ('Org::Element::ListItem') or a
272 coderef (which will be given the element to test). Will return matched elements.
273  
274 =head2 $el->walk_parents(CODE)
275  
276 Run CODEREF for parent, and its parent, and so on until the root element (the
277 document), or until CODEREF returns a false value. CODEREF will be supplied
278 ($el, $parent). Will return the last parent walked.
279  
280 =head2 $el->headline() => ELEMENT
281  
282 Get current headline. Return undef if element is not under any headline.
283  
284 =head2 $el->headlines() => ELEMENTS
285  
286 Get current headline (in the first element of the result list), its parent, its
287 parent's parent, and so on until the topmost headline. Return empty list if
288 element is not under any headline.
289  
290 =head2 $el->settings(CRITERIA) => ELEMENTS
291  
292 Get L<Org::Element::Setting> nodes directly under the element. Equivalent to:
293  
294 my @settings = grep { $_->isa("Org::Element::Setting") } @{ $el->children };
295  
296 If CRITERIA is specified, will filter based on some criteria. CRITERIA can be a
297 coderef, or a string to filter by setting's name, example:
298  
299 my ($doc_title) = $doc->settings('TITLE');
300  
301 Take note of the list operator on the left because C<settings()> return a list.
302  
303 =head2 $el->field_name() => STR
304  
305 Try to extract "field name", being defined as either some text on the left side:
306  
307 DEADLINE: <2011-06-09 >
308  
309 or a description term in a description list:
310  
311 - wedding anniversary :: <2011-06-10 >
312  
313 =head2 $el->remove()
314  
315 Remove element from the tree. Basically just remove the element from its parent.
316  
317 =head2 $el->die(STR)
318  
319 Utility method to format C<die> message.
320  
321 =head1 HOMEPAGE
322  
323 Please visit the project's homepage at L<https://metacpan.org/release/Org-Parser>.
324  
325 =head1 SOURCE
326  
327 Source repository is at L<https://github.com/perlancar/perl-Org-Parser>.
328  
329 =head1 AUTHOR
330  
331 perlancar <perlancar@cpan.org>
332  
333 =head1 CONTRIBUTING
334  
335  
336 To contribute, you can send patches by email/via RT, or send pull requests on
337 GitHub.
338  
339 Most of the time, you don't need to build the distribution yourself. You can
340 simply modify the code, then test via:
341  
342 % prove -l
343  
344 If you want to build the distribution (e.g. to try to install it locally on your
345 system), you can install L<Dist::Zilla>,
346 L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
347 L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
348 Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
349 that are considered a bug and can be reported to me.
350  
351 =head1 COPYRIGHT AND LICENSE
352  
353 This software is copyright (c) 2023, 2022, 2021, 2020, 2019, 2017, 2016, 2015, 2014, 2013, 2012, 2011 by perlancar <perlancar@cpan.org>.
354  
355 This is free software; you can redistribute it and/or modify it under
356 the same terms as the Perl 5 programming language system itself.
357  
358 =head1 BUGS
359  
360 Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Org-Parser>
361  
362 When submitting a bug or request, please include a test-file or a
363 patch to an existing test-file that illustrates the bug or desired
364 feature.
365  
366 =cut
« Element/Footnote.pm ↑ Summary Element/Text.pm »