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 = '2026-07-21'; # DATE
11 46 our $DIST = 'Org-Parser'; # DIST
12 46 our $VERSION = '0.563'; # 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 6 ▶sub die {
Subroutine die: called CC=3 SCAR=13.1
Pod documented
24 6 my ($self, $msg) = @_;
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 258 ▶sub children_as_string {
Subroutine children_as_string: called CC=2 SCAR=6.9
Pod documented
31 258 my ($self) = @_;
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 20 ▶sub as_string {
Subroutine as_string: called CC=3 SCAR=11.0
Pod documented
37 20 my ($self) = @_;
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 44 ▶sub seniority {
Subroutine seniority: called CC=5 SCAR=17.5
Pod documented
48 44 my ($self) = @_;
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 21 ▶sub prev_sibling {
Subroutine prev_sibling: called CC=3 SCAR=11.0
Pod documented
59 21 my ($self) = @_;
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 7 ▶sub next_sibling {
Subroutine next_sibling: called CC=3 SCAR=11.0
Pod documented
68 7 my ($self) = @_;
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 1053 ▶sub walk {
Subroutine walk: called CC=5 SCAR=16.1
Pod documented
80 1053 my ($self, $code, $_level) = @_;
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 7 ▶sub find {
Subroutine find: called CC=2 SCAR=7.2
Pod documented
96 7 my ($self, $criteria) = @_;
97 7 ▶ return unless $self->children;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->children 0 7 7
98 7 my @res;
99 $self->walk(
100 58 ▶ sub {
Subroutine __ANON__: called CC=6 SCAR=19.0
101 58 my $el = shift;
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 10 ▶sub walk_parents {
Subroutine walk_parents: called CC=3 SCAR=11.0
Pod documented
114 10 my ($self, $code) = @_;
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 2 ▶sub headline {
Subroutine headline: called CC=1 SCAR=0.1
Pod documented
124 2 my ($self) = @_;
125 2 my $h;
126 $self->walk_parents(
127 2 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.4
128 2 my ($el, $p) = @_;
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 1 ▶sub headlines {
Subroutine headlines: called CC=1 SCAR=0.0
Pod documented
139 1 my ($self) = @_;
140 1 my @res;
141 $self->walk_parents(
142 4 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
143 4 my ($el, $p) = @_;
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 0 ▶sub settings {
Subroutine settings: not called CC=3 SCAR=24.8
Pod documented
153 0 my ($self, $criteria) = @_;
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 5 ▶sub field_name {
Subroutine field_name: called CC=7 SCAR=19.9
Pod documented
168 5 my ($self) = @_;
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 1 ▶sub remove {
Subroutine remove: called CC=2 SCAR=7.0
Pod documented
192 1 my ($self) = @_;
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__
« Element/Footnote.pm ↑ Summary Element/Text.pm »