Element/Headline.pm

filter: Statementstmt 98.3% 182 / 185 Branchbran 71.4% 70 / 98 Conditioncond 77.3% 41 / 53 MC/DCmcdc 58.6% 17 / 29 Subroutinesub 95.2% 20 / 21 Podpod 85.7% 12 / 14 totaltotal 85.5% 342 / 400 CC 86 SCAR 22.3
CC
86
Cov
87%
update_statistics_cookie
18.6
promote_node
12.0
header_as_string
11.3
CRAP 9.3
« Element/Block.pm ↑ Summary Element/List.pm »
1 package Org::Element::Headline;
2  
3 17 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 17 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 17 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 extends 'Org::Element';
9 17 with 'Org::ElementRole';
10 17 with 'Org::ElementRole::Block';
11  
12 17 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
13 17 our $DATE = '2026-07-21'; # DATE
14 17 our $DIST = 'Org-Parser'; # DIST
15 17 our $VERSION = '0.563'; # VERSION
16  
17 17 has level => (is => 'rw');
18 17 has title => (is => 'rw');
19 17 has priority => (is => 'rw');
20 17 has tags => (is => 'rw');
21 17 has is_todo => (is => 'rw');
22 17 has is_done => (is => 'rw');
23 17 has todo_state => (is => 'rw');
24 17 has statistics_cookie => (is => 'rw');
25  
26 # old name, deprecated since 2014-07-17, will be removed in the future
27 0 ▶sub todo_priority { shift->priority(@_) }
Subroutine todo_priority: not called CC=1 SCAR=6.9
Pod undocumented
28  
29 161 ▶sub extra_walkables {
Subroutine extra_walkables: called CC=1 SCAR=0.0
Pod documented
30 161 my $self = shift;
31 161 grep {defined} ($self->title);
32 }
33  
34 24 ▶sub header_as_string {
Subroutine header_as_string: called CC=7 SCAR=24.2
Pod undocumented
35 24 my ($self) = @_;
36 24 ▶ return $self->_str if defined $self->_str;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->_str 23 1 24
37 join("",
38 "*" x $self->level,
39 " ",
40 $self->is_todo ? $self->todo_state." " : "",
41 $self->priority ? "[#".$self->priority."] " : "",
42 $self->statistics_cookie ? "[".$self->statistics_cookie."] " : "",
43 $self->title->as_string,
44 0 $self->tags && @{$self->tags} ?
45 0 ▶ " :".join(":", @{$self->tags}).":" : "",
Branch: 4 branches
BranchTrueFalseTotal
$self->is_todo ? : 0 1 1
$self->priority ? : 0 1 1
$self->statistics_cookie ? : 0 1 1
$self->tags && @{$self->tags;} ? : 0 1 1
Condition33%1/3
$self->tags and @{$self->tags;}
!ll&&!rl&&r
100
MC/DC0%0/2
$self->tags and @{$self->tags;}
$self->tags @{$self->tags;}
Truth table33%1/3
A and B
A= $self->tagsB= @{$self->tags;}
ABresult
0X0
100
111
46 "\n");
47 }
48  
49 24 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
50 24 my ($self) = @_;
51 24 $self->header_as_string . $self->children_as_string;
52 }
53  
54 6 ▶sub get_tags {
Subroutine get_tags: called CC=4 SCAR=13.9
Pod documented
55 6 my ($self, $name) = @_;
56 6 ▶ my @res = @{ $self->tags // [] };
Condition100%2/2
$self->tags // []
l!l
51
MC/DC100%1/1
$self->tags // []
$self->tags
Truth table100%2/2
A
A= $self->tags
Aresult
11
00
57 $self->walk_parents(
58 7 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
59 7 my ($el, $parent) = @_;
60 7 ▶ return 1 unless $parent->isa('Org::Element::Headline');
Branch: 1 branch
BranchTrueFalseTotal
unless $parent->isa("Org::Element::Headline") 6 1 7
61 1 ▶ if ($parent->tags) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->tags) 1 0 1
62 1 for my $tag (@{ $parent->tags }) {
63 1 ▶ push @res, $tag unless grep { $_ eq $tag } @res;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $tag;} @res 1 0 1
64 }
65 }
66 1 1;
67 6 });
68 6 for my $tag (@{ $self->document->tags }) {
69 6 ▶ push @res, $tag unless grep { $_ eq $tag } @res;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $tag;} @res 6 0 6
70 }
71 6 @res;
72 }
73  
74 5 ▶sub get_active_timestamp {
Subroutine get_active_timestamp: called CC=3 SCAR=11.0
Pod documented
75 5 my ($self) = @_;
76  
77 5 for my $s ($self->title, $self) {
78 8 my $ats;
79 $s->walk(
80 24 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
81 24 my ($el) = @_;
82 24 ▶ return if $ats;
Branch: 1 branch
BranchTrueFalseTotal
if $ats 5 19 24
83 19 ▶ $ats = $el if $el->isa('Org::Element::Timestamp') &&
Branch: 1 branch
BranchTrueFalseTotal
if $el->isa("Org::Element::Timestamp") and $el->is_active 3 16 19
Condition100%3/3
$el->isa("Org::Element::Timestamp") and $el->is_active
!ll&&!rl&&r
1423
MC/DC100%2/2
$el->isa("Org::Element::Timestamp") and $el->is_active
$el->isa("Org::Element::Timestamp") $el->is_active
Truth table100%3/3
A and B
A= $el->isa("Org::Element::Timestamp")B= $el->is_active
ABresult
0X0
100
111
84 $el->is_active;
85 }
86 8 );
87 8 ▶ return $ats if $ats;
Branch: 1 branch
BranchTrueFalseTotal
if $ats 3 5 8
88 }
89 2 return;
90 }
91  
92 4 ▶sub is_leaf {
Subroutine is_leaf: called CC=4 SCAR=14.0
Pod documented
93 4 my ($self) = @_;
94  
95 4 ▶ return 1 unless $self->children;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->children 2 2 4
96  
97 2 my $res;
98 2 for my $child (@{ $self->children }) {
99 $child->walk(
100 2 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
101 2 ▶ return if defined($res);
Branch: 1 branch
BranchTrueFalseTotal
if defined $res 0 2 2
102 2 my ($el) = @_;
103 2 ▶ if ($el->isa('Org::Element::Headline')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($el->isa("Org::Element::Headline")) 2 0 2
104 2 $res = 0;
105 2 goto EXIT_WALK;
106 }
107 }
108 2 );
109 }
110 EXIT_WALK:
111 2 ▶ $res //= 1;
Condition50%1/2
$res //= 1
l!l
20
MC/DC0%0/1
$res //= 1
$res
Truth table50%1/2
A
A= $res
Aresult
11
00
112 2 $res;
113 }
114  
115 8 ▶sub promote_node {
Subroutine promote_node: called CC=12 SCAR=24.9
Pod documented
116 8 my ($self, $num_levels) = @_;
117 8 ▶ $num_levels //= 1;
Condition100%2/2
$num_levels //= 1
l!l
17
MC/DC100%1/1
$num_levels //= 1
$num_levels
Truth table100%2/2
A
A= $num_levels
Aresult
11
00
118 8 ▶ return if $num_levels == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num_levels == 0 0 8 8
119 8 ▶ $self->die("Please specify a positive number of levels") if $num_levels < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num_levels < 0 0 8 8
120  
121 8 for my $i (1..$num_levels) {
122  
123 9 my $l = $self->level;
124 9 ▶ last if $l <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $l <= 1 1 8 9
125 8 $l--;
126 8 $self->level($l);
127  
128 8 $self->_str(undef);
129  
130 8 my $parent = $self->parent;
131 8 my $siblings = $parent->children;
132 8 my $pos = $self->seniority;
133  
134 # our children stay as children
135  
136 # our right sibling headline(s) become children
137 8 while (1) {
138 11 my $s = $siblings->[$pos+1];
139 11 ▶ last unless $s && $s->isa('Org::Element::Headline')
Branch: 1 branch
BranchTrueFalseTotal
unless $s and $s->isa("Org::Element::Headline") and $s->level > $l 8 3 11
Condition83%5/6
$s and $s->isa("Org::Element::Headline") and $s->level > $l
!ll&&!rl&&r
623
$s and $s->isa("Org::Element::Headline")
!ll&&!rl&&r
605
MC/DC66%2/3
$s and $s->isa("Org::Element::Headline") and $s->level > $l
$s $s->isa("Org::Element::Headline") $s->level > $l
Truth table75%3/4
A and B and C
A= $sB= $s->isa("Org::Element::Headline")C= $s->level > $l
ABCresult
0XX0
10X0
1100
1111
140 && $s->level > $l;
141 3 ▶ $self->children([]) unless defined $self->children;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->children 1 2 3
142 3 push @{$self->children}, $s;
143 3 splice @$siblings, $pos+1, 1;
144 3 $s->parent($self);
145 }
146  
147 # our parent headline can become sibling if level is the same
148 8 ▶ if ($parent->isa('Org::Element::Headline') && $parent->level == $l) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->isa("Org::Element::Headline") and $parent->level == $l) 2 6 8
Condition100%3/3
$parent->isa("Org::Element::Headline") and $parent->level == $l
!ll&&!rl&&r
422
MC/DC100%2/2
$parent->isa("Org::Element::Headline") and $parent->level == $l
$parent->isa("Org::Element::Headline") $parent->level == $l
Truth table100%3/3
A and B
A= $parent->isa("Org::Element::Headline")B= $parent->level == $l
ABresult
0X0
100
111
149 2 splice @$siblings, $pos, 1;
150 2 my $gparent = $parent->parent;
151 2 splice @{$gparent->children}, $parent->seniority+1, 0, $self;
152 2 $self->parent($gparent);
153 }
154  
155 }
156 }
157  
158 6 ▶sub demote_node {
Subroutine demote_node: called CC=9 SCAR=22.1
Pod documented
159 6 my ($self, $num_levels) = @_;
160 6 ▶ $num_levels //= 1;
Condition100%2/2
$num_levels //= 1
l!l
15
MC/DC100%1/1
$num_levels //= 1
$num_levels
Truth table100%2/2
A
A= $num_levels
Aresult
11
00
161 6 ▶ return if $num_levels == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num_levels == 0 0 6 6
162 6 ▶ $self->die("Please specify a positive number of levels") if $num_levels < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num_levels < 0 0 6 6
163  
164 6 for my $i (1..$num_levels) {
165  
166 8 my $l = $self->level;
167 8 $l++;
168 8 $self->level($l);
169  
170 8 $self->_str(undef);
171  
172 # prev sibling can become parent
173 8 my $ps = $self->prev_sibling;
174 8 ▶ if ($ps && $ps->isa('Org::Element::Headline') && $ps->level < $l) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ps and $ps->isa("Org::Element::Headline") and $ps->level < $l) 1 7 8
Condition83%5/6
$ps and $ps->isa("Org::Element::Headline") and $ps->level < $l
!ll&&!rl&&r
611
$ps and $ps->isa("Org::Element::Headline")
!ll&&!rl&&r
602
MC/DC66%2/3
$ps and $ps->isa("Org::Element::Headline") and $ps->level < $l
$ps $ps->isa("Org::Element::Headline") $ps->level < $l
Truth table75%3/4
A and B and C
A= $psB= $ps->isa("Org::Element::Headline")C= $ps->level < $l
ABCresult
0XX0
10X0
1100
1111
175 1 splice @{$self->parent->children}, $self->seniority, 1;
176 1 ▶ $ps->children([]) if !defined($ps->children);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ps->children 1 0 1
177 1 push @{$ps->children}, $self;
178 1 $self->parent($ps);
179 }
180  
181 }
182 }
183  
184 1 ▶sub promote_branch {
Subroutine promote_branch: called CC=7 SCAR=20.7
Pod documented
185 1 my ($self, $num_levels) = @_;
186 1 ▶ $num_levels //= 1;
Condition50%1/2
$num_levels //= 1
l!l
01
MC/DC0%0/1
$num_levels //= 1
$num_levels
Truth table50%1/2
A
A= $num_levels
Aresult
11
00
187 1 ▶ return if $num_levels == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num_levels == 0 0 1 1
188 1 ▶ $self->die("Please specify a positive number of levels") if $num_levels < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num_levels < 0 0 1 1
189  
190 1 for my $i (1..$num_levels) {
191 1 ▶ last if $self->level <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->level <= 1 0 1 1
192 1 $_->promote_node() for $self->find('Headline');
193 }
194 }
195  
196 1 ▶sub demote_branch {
Subroutine demote_branch: called CC=6 SCAR=18.8
Pod documented
197 1 my ($self, $num_levels) = @_;
198 1 ▶ $num_levels //= 1;
Condition50%1/2
$num_levels //= 1
l!l
01
MC/DC0%0/1
$num_levels //= 1
$num_levels
Truth table50%1/2
A
A= $num_levels
Aresult
11
00
199 1 ▶ return if $num_levels == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num_levels == 0 0 1 1
200 1 ▶ $self->die("Please specify a positive number of levels") if $num_levels < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $num_levels < 0 0 1 1
201  
202 1 for my $i (1..$num_levels) {
203 1 $_->demote_node() for $self->find('Headline');
204 }
205 }
206  
207 13 ▶sub get_drawer {
Subroutine get_drawer: called CC=7 SCAR=19.7
Pod documented
208 13 my $self = shift;
209 13 ▶ my $wanted_drawer_name = shift || "PROPERTIES";
Condition50%1/2
shift() || 'PROPERTIES'
l!l
130
MC/DC0%0/1
shift() || 'PROPERTIES'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
210  
211 13 ▶ for my $d (@{$self->children||[]}) {
Condition50%1/2
$self->children || []
l!l
130
MC/DC0%0/1
$self->children || []
$self->children
Truth table50%1/2
A
A= $self->children
Aresult
11
00
212 28 log_trace("seeking $wanted_drawer_name drawer in child: %s (%s)", $d->as_string, ref($d));
213 28 ▶ next unless ($d->isa('Org::Element::Drawer')
Branch: 1 branch
BranchTrueFalseTotal
unless $d->isa("Org::Element::Drawer") and $d->name eq $wanted_drawer_name and $d->properties 15 13 28
Condition83%5/6
$d->isa("Org::Element::Drawer") and $d->name eq $wanted_drawer_name and $d->properties
!ll&&!rl&&r
15013
$d->isa("Org::Element::Drawer") and $d->name eq $wanted_drawer_name
!ll&&!rl&&r
14113
MC/DC66%2/3
$d->isa("Org::Element::Drawer") and $d->name eq $wanted_drawer_name and $d->properties
$d->isa("Org::Element::Drawer") $d->name eq $wanted_drawer_name $d->properties
Truth table75%3/4
A and B and C
A= $d->isa("Org::Element::Drawer")B= $d->name eq $wanted_drawer_nameC= $d->properties
ABCresult
0XX0
10X0
1100
1111
214 && $d->name eq $wanted_drawer_name
215 && $d->properties);
216 13 return $d;
217 }
218 }
219  
220 12 ▶sub get_property {
Subroutine get_property: called CC=10 SCAR=23.2
Pod documented
221 12 my ($self, $name, $search_parent, $search_docprop) = @_;
222 #$log->tracef("-> get_property(%s, search_par=%s)", $name, $search_parent);
223 12 my $parent = $self->parent;
224  
225 12 my $propd = $self->get_drawer("PROPERTIES");
226 return $propd->properties->{$name} if
227 12 ▶ $propd && defined $propd->properties->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $propd and defined $propd->properties->{$name} 5 7 12
Condition66%2/3
$propd and defined $propd->properties->{$name}
!ll&&!rl&&r
075
MC/DC50%1/2
$propd and defined $propd->properties->{$name}
$propd defined $propd->properties->{$name}
Truth table66%2/3
A and B
A= $propdB= defined $propd->properties->{$name}
ABresult
0X0
100
111
228  
229 7 ▶ if ($parent && $search_parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent and $search_parent) 2 5 7
Condition66%2/3
$parent and $search_parent
!ll&&!rl&&r
052
MC/DC50%1/2
$parent and $search_parent
$parent $search_parent
Truth table66%2/3
A and B
A= $parentB= $search_parent
ABresult
0X0
100
111
230 2 ▶ while ($parent) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent) 3 0 3
231 3 ▶ if ($parent->isa('Org::Element::Headline')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent->isa("Org::Element::Headline")) 3 0 3
232 3 my $res = $parent->get_property($name, 0, 0);
233 3 ▶ return $res if defined $res;
Branch: 1 branch
BranchTrueFalseTotal
if defined $res 2 1 3
234 }
235 1 $parent = $parent->parent;
236 }
237 }
238  
239 5 ▶ if ($search_docprop // 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($search_docprop // 1) 4 1 5
Condition100%2/2
$search_docprop // 1
l!l
14
MC/DC100%1/1
$search_docprop // 1
$search_docprop
Truth table100%2/2
A
A= $search_docprop
Aresult
11
00
240 4 log_trace("Getting property from document's .properties");
241 4 return $self->document->properties->{$name};
242 }
243 1 undef;
244 }
245  
246 8 ▶sub update_statistics_cookie {
Subroutine update_statistics_cookie: called CC=18 SCAR=29.2
Pod documented
247 8 my $self = shift;
248  
249 8 my $statc = $self->statistics_cookie;
250 8 ▶ return unless $statc;
Branch: 1 branch
BranchTrueFalseTotal
unless $statc 4 4 8
251  
252 4 my $num_done = 0;
253 4 my $num_total = 0;
254  
255 # count using checks on first-level list's children, or from first-level
256 # subheadlines
257 4 ▶ for my $chld (@{ $self->children // [] }) {
Condition100%2/2
$self->children // []
l!l
22
MC/DC100%1/1
$self->children // []
$self->children
Truth table100%2/2
A
A= $self->children
Aresult
11
00
258 2 ▶ if ($chld->isa("Org::Element::Headline")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($chld->isa('Org::Element::Headline')) { } 1 1 2
elsif ($chld->isa('Org::Element::List')) { } 1 0 1
259 1 for my $el (@{ $self->children }) {
260 3 ▶ next unless $el->isa("Org::Element::Headline");
Branch: 1 branch
BranchTrueFalseTotal
unless $el->isa("Org::Element::Headline") 0 3 3
261 3 ▶ if ($el->is_todo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($el->is_todo) 3 0 3
262 3 $num_total++;
263 3 ▶ $num_done++ if $el->is_done;
Branch: 1 branch
BranchTrueFalseTotal
if $el->is_done 1 2 3
264 }
265 }
266 1 last;
267 } elsif ($chld->isa("Org::Element::List")) {
268 1 for my $el (@{ $self->children }) {
269 1 ▶ next unless $el->isa("Org::Element::List");
Branch: 1 branch
BranchTrueFalseTotal
unless $el->isa("Org::Element::List") 0 1 1
270 1 for my $el2 (@{ $el->children }) {
271 3 ▶ next unless $el2->isa("Org::Element::ListItem");
Branch: 1 branch
BranchTrueFalseTotal
unless $el2->isa("Org::Element::ListItem") 0 3 3
272 3 my $state = $el2->check_state;
273 3 ▶ if (defined $state) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $state) 3 0 3
274 3 $num_total++;
275 3 ▶ $num_done++ if $state eq 'X';
Branch: 1 branch
BranchTrueFalseTotal
if $state eq "X" 2 1 3
276 }
277 }
278 }
279 1 last;
280 }
281 }
282  
283 4 undef $self->{_str}; # we modify content
284 4 ▶ if ($statc =~ /%/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($statc =~ /%/l) { } 1 3 4
285 1 ▶ $self->statistics_cookie(
Branch: 1 branch
BranchTrueFalseTotal
$num_total == 0 ? : 1 0 1
286 sprintf("%d%%", $num_total == 0 ? 0 : $num_done/$num_total * 100));
287 } else {
288 3 $self->statistics_cookie(sprintf("%d/%d", $num_done, $num_total));
289 }
290 }
291  
292 17 1;
293 # ABSTRACT: Represent Org headline
294  
295 __END__
« Element/Block.pm ↑ Summary Element/List.pm »