| 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
Condition33%1/3
$self->document and $self->document->_srclabel
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 40 | 2 | ▶ | return $self->_str . | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$self->parent and $c = $self->parent->children
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
| |||||||||||||||||||||||||||||
| 51 | 44 | my $addr = refaddr($self); | |||||||||||||||||||||||||||
| 52 | 44 | ▶ | for (my $i=0; $i < @$c; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 75 | ▶ | return $i if refaddr($c->[$i]) == $addr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $sen and $sen > 0
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 72 | 7 | my $c = $self->parent->children; | |||||||||||||||||||||||||||
| 73 | 7 | ▶ | return undef unless $sen < @$c-1; ## no critic: Subroutines::ProhibitExplicitReturnUndef | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$_level //= 0
$_level
Truth table100%2/2
A
A= $_level
| |||||||||||||||||||||||||||||
| 82 | 1053 | $code->($self, $_level); | |||||||||||||||||||||||||||
| 83 | 1051 | ▶ | if ($self->children) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 103 | 12 | ▶ | push @res, $el if $criteria->($el); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | } elsif ($criteria =~ /^\w+$/) { | ||||||||||||||||||||||||||||
| 105 | 46 | ▶ | push @res, $el if $el->isa("Org::Element::$criteria"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | } else { | ||||||||||||||||||||||||||||
| 107 | 0 | ▶ | push @res, $el if $el->isa($criteria); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 117 | 17 | ▶ | return $parent unless $code->($self, $parent); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 158 | 0 | ▶ | if (ref $criteria eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
$prev and $prev->isa("Org::Element::Text")
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")
| |||||||||||||||||||||||||||||
| 172 | 5 | my $text = $prev->as_string; | |||||||||||||||||||||||||||
| 173 | 5 | ▶ | if ($text =~ /(?:\A|\R)\s*(.+?)\s*:\s*\z/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 3 | return $1; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | 2 | my $parent = $self->parent; | |||||||||||||||||||||||||||
| 178 | 2 | ▶ | if ($parent && $parent->isa('Org::Element::ListItem')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$parent and $parent->isa("Org::Element::ListItem")
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")
| |||||||||||||||||||||||||||||
| 179 | 1 | my $list = $parent->parent; | |||||||||||||||||||||||||||
| 180 | 1 | ▶ | if ($list->type eq 'D') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 195 | 1 | splice @{$parent->children}, $self->seniority, 1; | |||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 46 | 1; | |||||||||||||||||||||||||||
| 199 | # ABSTRACT: Base class for Org document elements | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | __END__ | ||||||||||||||||||||||||||||