Document.pm

filter: Statementstmt 98.5% 394 / 400 Branchbran 93.3% 183 / 196 Conditioncond 83.2% 119 / 143 MC/DCmcdc 68.1% 60 / 88 Subroutinesub 100% 25 / 25 Podpod 66.6% 2 / 3 totaltotal 91.5% 783 / 855 SCAR 50.5
CC
152
Cov
94%
_parse
55.2
_add_text
44.6
_apply_markup
17.0
CRAP 156.6
↑ Summary Element/Headline.pm »
1 package Org::Document;
2  
3 26 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 26 ▶use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 26 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 26 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 26 ▶no if $] >= 5.021_006, warnings => "locale";
Subroutine BEGIN: called CC=1 SCAR=0.0
8 26 extends 'Org::Element';
9  
10 26 ▶use List::MoreUtils qw(firstidx);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 26 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 26 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
14 26 our $DATE = '2023-08-05'; # DATE
15 26 our $DIST = 'Org-Parser'; # DIST
16 26 our $VERSION = '0.560'; # VERSION
17  
18 26 has _srclabel => (is => 'rw');
19 26 has tags => (is => 'rw');
20 26 has todo_states => (is => 'rw');
21 26 has done_states => (is => 'rw');
22 26 has priorities => (is => 'rw');
23 26 has drawer_names => (is => 'rw');
24 26 has properties => (is => 'rw');
25 26 has radio_targets => (is => 'rw');
26  
27 26 has time_zone => (is => 'rw');
28  
29 26 has ignore_unknown_settings => (is => 'rw');
30  
31 26 our $tags_re = qr/:(?:[\w@]+:)+/u;
32 26 my $ls_re = qr/(?:(?<=[\015\012])|\A)/; # line start
33 26 my $le_re = qr/(?:\R|\z)/; # line end
34 26 our $arg_re = qr/(?: '(?<squote> [^']*)' |
35 "(?<dquote> [^"]*)" |
36 (?<bare> \S+) )
37 /x;
38 26 our $args_re = qr/(?: $arg_re (?:[ \t]+ $arg_re)*)/x;
39 26 my $tstamp_re = qr/(?:\[\d{4}-\d{2}-\d{2} [^\n\]]*\])/x;
40 26 my $act_tstamp_re = qr/(?: <\d{4}-\d{2}-\d{2} [^\n>]* >)/x;
41 26 my $fn_name_re = qr/(?:[^ \t\n:\]]+)/x;
42 26 my $text_re =
43 qr{
44 (?<link> \[\[(?<link_link> [^\]\n]+)\]
45 (?:\[(?<link_desc> (?:[^\]]|\R)+)\])?\]) |
46 (?<radio_target> <<<(?<rt_target> [^>\n]+)>>>) |
47 (?<target> <<(?<t_target> [^>\n]+)>>) |
48  
49 # timestamp & time range
50 (?<trange> (?<trange_ts1> $tstamp_re)--
51 (?<trange_ts2> $tstamp_re)) |
52 (?<tstamp> $tstamp_re) |
53 (?<act_trange> (?<act_trange_ts1> $act_tstamp_re)--
54 (?<act_trange_ts2> $act_tstamp_re)) |
55 (?<act_tstamp> $act_tstamp_re) |
56  
57 # footnote (num, name + def, name + inline definition)
58 (?<fn_num> \[(?<fn_num_num>\d+)\]) |
59 (?<fn_namedef> $ls_re \[fn:(?<fn_namedef_name> $fn_name_re)\]
60 [ \t]* (?<fn_namedef_def> [^ \t\n]+)) |
61 (?<fn_nameidef> \[fn:(?<fn_nameidef_name> $fn_name_re?):?
62 (?<fn_nameidef_def> ([^\n\]]+)?)\]) |
63  
64 (?<markup_start> (?:(?<=\s|\(|\{)|\A) # whitespace, open paren, open curly paren
65 [*/+=~_]
66 (?=\S)) |
67 (?<markup_end> (?<=\S)
68 [*/+=~_]
69 # actually emacs doesn't allow ! after markup
70 (?:(?=[ \t\n:;"',.!?\)*-])|\z)) |
71  
72 (?<plain_text> (?:[^\[<*/+=~_\n]+|.+?))
73 #(?<plain_text> .+?) # too dispersy
74 }sxi;
75  
76 # XXX parser must be fixed: block elements have different precedence instead of
77 # flat like this. a headline has the highest precedence and a block or a drawer
78 # cannot contain a headline (e.g. "#+BEGIN_SRC foo\n* header\n#+END_SRC" should
79 # not contain a literal "* header" text but that is a headline. currently, a
80 # block or a drawer swallows a headline.
81  
82 26 my $block_elems_re = # top level elements
83 qr/(?<block> $ls_re (?<block_begin_indent>[ \t]*)
84 \#\+BEGIN_(?<block_name>\w+)
85 (?:[ \t]+(?<block_raw_arg>[^\n]*))?\R
86 (?<block_content>(?:.|\R)*?)
87 \R(?<block_end_indent>[ \t]*)
88 \#\+END_\k<block_name> $le_re) |
89 (?<setting> $ls_re (?<setting_indent>[ \t]*) \#\+
90 (?<setting_name> \w+): (?: [ \t]+
91 (?<setting_raw_arg> [^\n]*))? $le_re) |
92 (?<fixedw> (?: $ls_re [ \t]* (?::[ ][^\n]* | :$) $le_re )+ ) |
93 (?<comment> $ls_re [ \t]*\#[^\n]*(?:\R\#[^\n]*)* (?:\R|\z)) |
94 (?<headline> $ls_re (?<h_bullet>\*+) [ \t]
95 (?<h_title>[^\n]*?)
96 (?:[ \t]+(?<h_tags> $tags_re))?[ \t]* $le_re) |
97 (?<li_header> $ls_re (?<li_indent>[ \t]*)
98 (?<li_bullet>[+*-]|\d+\.) [ \t]+
99 (?<li_checkbox> \[(?<li_cbstate> [ X-])\])?
100 (?: (?<li_dt> [^\n]+?) [ ]::)?) |
101 (?<table> (?: $ls_re [ \t]* \| [ \t]* \S[^\n]* $le_re)+) |
102 (?<drawer> $ls_re [ \t]* :(?<drawer_name> \w+): [ \t]*\R
103 (?<drawer_content>(?:.|\R)*?)
104 $ls_re [ \t]* :END:) |
105 (?<text> (?:[^#|:+*0-9\n-]+|\n+|.)+?)
106 #(?<text> .+?) # too dispersy
107 /msxi;
108  
109 sub _init_pass1 {
110 95 ▶ my ($self) = @_;
Subroutine _init_pass1: called CC=1 SCAR=0.0
111 95 $self->tags([]);
112 95 $self->todo_states([]);
113 95 $self->done_states([]);
114 95 $self->priorities([]);
115 95 $self->properties({});
116 95 $self->drawer_names([qw/CLOCK LOGBOOK PROPERTIES/]);
117 # FEEDSTATUS
118 95 $self->radio_targets([]);
119 }
120  
121 sub _init_pass2 {
122 93 ▶ my ($self) = @_;
Subroutine _init_pass2: called CC=4 SCAR=13.9
123 93 ▶ if (!@{ $self->todo_states } && !@{ $self->done_states }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->todo_states;} or @{$self->done_states;}) 81 12 93
Condition66%2/3
@{$self->todo_states;} or @{$self->done_states;}
l!l&&r!l&&!r
12081
MC/DC50%1/2
@{$self->todo_states;} or @{$self->done_states;}
@{$self->todo_states;} @{$self->done_states;}
Truth table66%2/3
A or B
A= @{$self->todo_states;}B= @{$self->done_states;}
ABresult
1X1
011
000
124 81 $self->todo_states(['TODO']);
125 81 $self->done_states(['DONE']);
126 }
127 93 ▶ if (!@{ $self->priorities }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->priorities;}) 89 4 93
128 89 $self->priorities([qw/A B C/]);
129 }
130 93 $self->children([]);
131 }
132  
133 sub __parse_args {
134 57 ▶ my $args = shift;
Subroutine __parse_args: called CC=6 SCAR=18.8
135 57 ▶ return [] unless defined($args) && length($args);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args and length $args 2 55 57
Condition66%2/3
defined $args and length $args
!ll&&!rl&&r
2055
MC/DC50%1/2
defined $args and length $args
defined $args length $args
Truth table66%2/3
A and B
A= defined $argsB= length $args
ABresult
0X0
100
111
136 #$log->tracef("args = %s", $args);
137 55 my @args;
138 55 ▶ while ($args =~ /$arg_re (?:\s+|\z)/xg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args =~ /$Org::Document::arg_re (?:\s+|\z)/glx) 165 55 220
139 165 ▶ if (defined $+{squote}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $+{'squote'}) { } 0 165 165
elsif (defined $+{'dquote'}) { } 0 165 165
140 0 push @args, $+{squote};
141 } elsif (defined $+{dquote}) {
142 0 push @args, $+{dquote};
143 } else {
144 165 push @args, $+{bare};
145 }
146 }
147 #$log->tracef("\\\@args = %s", \@args);
148 55 \@args;
149 }
150  
151 sub __format_args {
152 2 ▶ my ($args) = @_;
Subroutine __format_args: called CC=4 SCAR=16.4
153 2 my @s;
154 2 for (@$args) {
155 4 ▶ if (/\A(?:[A-Za-z0-9_:-]+|\|)\z/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/\A(?:[A-Za-z0-9_:-]+|\|)\z/l) { } 4 0 4
elsif (/"/l) { } 0 0 0
156 4 push @s, $_;
157 } elsif (/"/) {
158 0 push @s, qq('$_');
159 } else {
160 0 push @s, qq("$_");
161 }
162 }
163 2 join " ", @s;
164 }
165  
166 sub BUILD {
167 96 ▶ my ($self, $args) = @_;
Subroutine BUILD: called CC=3 SCAR=11.0
Pod undocumented
168 96 ▶ $self->document($self) unless $self->document;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->document 96 0 96
169  
170 96 ▶ if (defined $args->{from_string}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"from_string"}) 95 1 96
171  
172 # NOTE: parsing is done twice. first pass will set settings (e.g. custom
173 # todo keywords set by #+TODO), scan for radio targets. after that we
174 # scan again to build the elements tree.
175  
176 95 $self->_init_pass1();
177 95 $self->_parse($args->{from_string}, 1);
178 93 $self->_init_pass2();
179 93 $self->_parse($args->{from_string}, 2);
180 }
181 }
182  
183 # parse blocky elements: setting, blocks, headline, drawer
184 sub _parse {
185 188 ▶ my ($self, $str, $pass) = @_;
Subroutine _parse: called CC=55 SCAR=40.1
186 188 log_trace('-> _parse(%s, pass=%d)', $str, $pass);
187 188 my $t0 = [gettimeofday];
188  
189 188 my $last_el;
190  
191 my $last_headline;
192 188 my $last_headlines = [$self]; # [$doc, $last_hl_level1, $last_hl_lvl2, ...]
193 188 my $last_listitem;
194 188 my $last_lists = []; # [last_List_obj_for_indent_level0, ...]
195 188 my $parent;
196  
197 my @text;
198 188 ▶ while ($str =~ /$block_elems_re/og) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /$block_elems_re/glo) 4729 184 4913
199 4729 ▶ $parent = $last_listitem // $last_headline // $self;
Condition83%5/6
($last_listitem // $last_headline) // $self
l!l&&r!l&&!r
161531140
$last_listitem // $last_headline
l!l&&r!l&&!r
8787373114
MC/DC0%0/3
($last_listitem // $last_headline) // $self
$last_listitem $last_headline $self
Truth table75%3/4
A // B // C
A= $last_listitemB= $last_headlineC= $self
ABCresult
1XX1
01X1
0011
0000
200 #$log->tracef("TMP: parent=%s (%s)", ref($parent), $parent->_str);
201 4729 my %m = %+;
202 4729 ▶ next unless keys %m; # perlre bug?
Branch: 1 branch
BranchTrueFalseTotal
unless keys %m 0 4729 4729
203 #if ($log->is_trace) {
204 # # profiler shows that this is very heavy, so commenting this out
205 # $log->tracef("TMP: match block element: %s", \%+) if $pass==2;
206 #}
207  
208 4729 ▶ if (defined $m{text}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $m{'text'}) { } 4049 680 4729
209 4049 push @text, $m{text};
210 4049 next;
211 } else {
212 680 ▶ if (@text) {
Branch: 1 branch
BranchTrueFalseTotal
if (@text) 270 410 680
213 270 my $text = join("", @text);
214 270 ▶ if ($last_el && $last_el->isa('Org::Element::ListItem')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_el and $last_el->isa("Org::Element::ListItem")) 85 185 270
Condition100%3/3
$last_el and $last_el->isa("Org::Element::ListItem")
!ll&&!rl&&r
1414485
MC/DC100%2/2
$last_el and $last_el->isa("Org::Element::ListItem")
$last_el $last_el->isa("Org::Element::ListItem")
Truth table100%3/3
A and B
A= $last_elB= $last_el->isa("Org::Element::ListItem")
ABresult
0X0
100
111
215 # a list is broken by either: a) another list (where the
216 # bullet type or indent is different; handled in the
217 # handling of $m{li_header}) or b) by two blank lines, or c)
218 # by non-blank text that is indented less than or equal to
219 # the last list item's indent.
220  
221 # a single blank line does not break a list. a text that is
222 # more indented than the last list item's indent will become
223 # the child of that list item.
224  
225 85 my ($firstline, $restlines) = $text =~ /(.*?\r?\n)(.+)/s;
226 85 ▶ if ($restlines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($restlines) 17 68 85
227 17 $restlines =~ /\A([ \t]*)/;
228 17 my $restlineslevel = length($1);
229 17 my $listlevel = length($last_el->parent->indent);
230 17 ▶ if ($restlineslevel <= $listlevel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($restlineslevel <= $listlevel) 11 6 17
231 11 my $origparent = $parent;
232 # find lesser-indented list
233 11 ▶ $parent = $last_headline // $self;
Condition33%1/3
$last_headline // $self
l!l&&r!l&&!r
1100
MC/DC0%0/2
$last_headline // $self
$last_headline $self
Truth table33%1/3
A // B
A= $last_headlineB= $self
ABresult
1X1
011
000
234 11 ▶ for (my $i=$restlineslevel-1; $i>=0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 2 9 11
235 2 ▶ if ($last_lists->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_lists->[$i]) 2 0 2
236 2 $parent = $last_lists->[$i];
237 2 last;
238 }
239 }
240 11 splice @$last_lists, $restlineslevel;
241 11 $self->_add_text($firstline, $origparent, $pass);
242 11 $self->_add_text($restlines, $parent, $pass);
243 11 goto SKIP1;
244 }
245 }
246 }
247 259 $self->_add_text($text, $parent, $pass);
248 270 SKIP1:
249 @text = ();
250 270 $last_el = undef;
251 }
252 }
253  
254 680 my $el;
255 680 ▶ if ($m{block} && $pass == 2) {
Branch: 8 branches
BranchTrueFalseTotal
if ($m{'block'} and $pass == 2) { } 3 677 680
elsif ($m{'setting'}) { } 60 617 677
elsif ($m{'fixedw'} and $pass == 2) { } 7 610 617
elsif ($m{'comment'} and $pass == 2) { } 18 592 610
elsif ($m{'table'} and $pass == 2) { } 6 586 592
elsif ($m{'drawer'} and $pass == 2) { } 15 571 586
elsif ($m{'li_header'} and $pass == 2) { } 90 481 571
elsif ($m{'headline'} and $pass == 2) { } 171 449 620
Condition100%3/3
$m{"block"} and $pass == 2
!ll&&!rl&&r
67433
MC/DC100%2/2
$m{"block"} and $pass == 2
$m{"block"} $pass == 2
Truth table100%3/3
A and B
A= $m{"block"}B= $pass == 2
ABresult
0X0
100
111
256  
257 3 require Org::Element::Block;
258 $el = Org::Element::Block->new(
259 _str=>$m{block},
260 document=>$self, parent=>$parent,
261 begin_indent=>$m{block_begin_indent},
262 end_indent=>$m{block_end_indent},
263 name=>$m{block_name}, args=>__parse_args($m{block_raw_arg}),
264 raw_content=>$m{block_content},
265 3 );
266  
267 } elsif ($m{setting}) {
268  
269 60 require Org::Element::Setting;
270 60 my $uc_setting_name = uc($m{setting_name});
271 60 ▶ if ($m{setting_indent} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($m{'setting_indent'} and not grep({$_ eq $uc_setting_name;} @{'Org::Element::Setting'->indentable_settings;})) { } 6 54 60
Condition100%3/3
$m{"setting_indent"} and not grep({$_ eq $uc_setting_name;} @{"Org::Element::Setting"->indentable_settings;})
!ll&&!rl&&r
5226
MC/DC100%2/2
$m{"setting_indent"} and not grep({$_ eq $uc_setting_name;} @{"Org::Element::Setting"->indentable_settings;})
$m{"setting_indent"} not grep({$_ eq $uc_setting_name;} @{"Org::Element::Setting"->indentable_settings;})
Truth table100%3/3
A and not(B)
A= $m{"setting_indent"}B= not grep({$_ eq $uc_setting_name;} @{"Org::Element::Setting"->indentable_settings;})
ABresult
0X0
100
111
272 8 !(grep { $_ eq $uc_setting_name }
273 8 @{Org::Element::Setting->indentable_settings})) {
274 6 push @text, $m{setting};
275 6 next;
276 } else {
277 $el = Org::Element::Setting->new(
278 pass => $pass,
279 _str=>$m{setting},
280 document=>$self, parent=>$parent,
281 indent => $m{setting_indent},
282 name=>$m{setting_name},
283 raw_arg => $m{setting_raw_arg},
284 54 args=>__parse_args($m{setting_raw_arg}),
285 );
286 }
287  
288 ▶ } elsif ($m{fixedw} && $pass == 2) {
Condition100%3/3
$m{"fixedw"} and $pass == 2
!ll&&!rl&&r
60377
MC/DC100%2/2
$m{"fixedw"} and $pass == 2
$m{"fixedw"} $pass == 2
Truth table100%3/3
A and B
A= $m{"fixedw"}B= $pass == 2
ABresult
0X0
100
111
289  
290 7 require Org::Element::FixedWidthSection;
291 $el = Org::Element::FixedWidthSection->new(
292 pass => $pass,
293 _str=>$m{fixedw},
294 7 document=>$self, parent=>$parent,
295 );
296  
297 ▶ } elsif ($m{comment} && $pass == 2) {
Condition100%3/3
$m{"comment"} and $pass == 2
!ll&&!rl&&r
5741818
MC/DC100%2/2
$m{"comment"} and $pass == 2
$m{"comment"} $pass == 2
Truth table100%3/3
A and B
A= $m{"comment"}B= $pass == 2
ABresult
0X0
100
111
298  
299 18 require Org::Element::Comment;
300 $el = Org::Element::Comment->new(
301 _str=>$m{comment},
302 18 document=>$self, parent=>$parent,
303 );
304  
305 ▶ } elsif ($m{table} && $pass == 2) {
Condition100%3/3
$m{"table"} and $pass == 2
!ll&&!rl&&r
58066
MC/DC100%2/2
$m{"table"} and $pass == 2
$m{"table"} $pass == 2
Truth table100%3/3
A and B
A= $m{"table"}B= $pass == 2
ABresult
0X0
100
111
306  
307 6 require Org::Element::Table;
308 $el = Org::Element::Table->new(
309 pass=>$pass,
310 _str=>$m{table},
311 6 document=>$self, parent=>$parent,
312 );
313  
314 ▶ } elsif ($m{drawer} && $pass == 2) {
Condition100%3/3
$m{"drawer"} and $pass == 2
!ll&&!rl&&r
5561515
MC/DC100%2/2
$m{"drawer"} and $pass == 2
$m{"drawer"} $pass == 2
Truth table100%3/3
A and B
A= $m{"drawer"}B= $pass == 2
ABresult
0X0
100
111
315  
316 15 require Org::Element::Drawer;
317 15 my $raw_content = $m{drawer_content};
318 $el = Org::Element::Drawer->new(
319 document=>$self, parent=>$parent,
320 15 name => uc($m{drawer_name}), pass => $pass,
321 );
322 14 $self->_add_text($raw_content, $el, $pass);
323  
324 # for properties, we also parse property lines from raw drawer
325 # content. this is currently separate from normal Org text parsing,
326 # i'm not clear yet on how to do this canonically.
327 14 $el->_parse_properties($raw_content);
328  
329 ▶ } elsif ($m{li_header} && $pass == 2) {
Condition100%3/3
$m{"li_header"} and $pass == 2
!ll&&!rl&&r
3919090
MC/DC100%2/2
$m{"li_header"} and $pass == 2
$m{"li_header"} $pass == 2
Truth table100%3/3
A and B
A= $m{"li_header"}B= $pass == 2
ABresult
0X0
100
111
330  
331 90 require Org::Element::List;
332 90 require Org::Element::ListItem;
333  
334 90 my $level = length($m{li_indent});
335 90 my $bullet = $m{li_bullet};
336 90 my $indent = $m{li_indent};
337 90 my $dt = $m{li_dt};
338 90 my $cbstate = $m{li_cbstate};
339 90 ▶ my $type = defined($dt) ? 'D' :
Branch: 2 branches
BranchTrueFalseTotal
defined $dt ? : 8 82 90
$bullet =~ /^\d+\./l ? : 6 76 82
340 $bullet =~ /^\d+\./ ? 'O' : 'U';
341 90 ▶ my $bstyle = $type eq 'O' ? '<N>.' : $bullet;
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'O' ? : 6 84 90
342  
343 # parent for list is the last listitem of a lesser-indented list (or
344 # last headline, or document)
345 90 ▶ $parent = $last_headline // $self;
Condition66%2/3
$last_headline // $self
l!l&&r!l&&!r
8370
MC/DC0%0/2
$last_headline // $self
$last_headline $self
Truth table66%2/3
A // B
A= $last_headlineB= $self
ABresult
1X1
011
000
346 90 ▶ for (my $i=$level-1; $i>=0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 107 63 170
347 107 ▶ if ($last_lists->[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_lists->[$i]) 27 80 107
348 27 $parent = $last_lists->[$i]->children->[-1];
349 27 last;
350 }
351 }
352  
353 90 my $list = $last_lists->[$level];
354 90 ▶ if (!$list || $list->type ne $type ||
Branch: 1 branch
BranchTrueFalseTotal
if (not $list or $list->type ne $type or $list->bullet_style ne $bstyle) 6 39 45
Condition100%6/6
not $list or $list->type ne $type or $list->bullet_style ne $bstyle
l!l&&r!l&&!r
49239
not $list or $list->type ne $type
l!l&&r!l&&!r
45441
MC/DC100%3/3
not $list or $list->type ne $type or $list->bullet_style ne $bstyle
not $list $list->type ne $type $list->bullet_style ne $bstyle
Truth table100%4/4
not(A) or B or C
A= not $listB= $list->type ne $typeC= $list->bullet_style ne $bstyle
ABCresult
1XX1
01X1
0011
0000
355 $list->bullet_style ne $bstyle) {
356 51 $list = Org::Element::List->new(
357 document => $self, parent => $parent,
358 indent=>$indent, type=>$type, bullet_style=>$bstyle,
359 );
360 51 $last_lists->[$level] = $list;
361 51 ▶ $parent->children([]) if !$parent->children;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent->children 11 40 51
362 51 push @{ $parent->children }, $list;
363 }
364 90 $last_lists->[$level] = $list;
365  
366 # parent for list item is list
367 90 $parent = $list;
368  
369 90 $el = Org::Element::ListItem->new(
370 document=>$self, parent=>$list,
371 indent=>$indent, bullet=>$bullet);
372 90 ▶ $el->check_state($cbstate) if $cbstate;
Branch: 1 branch
BranchTrueFalseTotal
if $cbstate 6 84 90
373 90 ▶ $el->desc_term($self->_add_text_container($dt, $list, $pass))
Branch: 1 branch
BranchTrueFalseTotal
if defined $dt 8 82 90
374 if defined($dt);
375  
376 90 splice @$last_lists, $level+1;
377 90 $last_listitem = $el;
378  
379 ▶ } elsif ($m{headline} && $pass == 2) {
Condition100%3/3
$m{"headline"} and $pass == 2
!ll&&!rl&&r
139171171
MC/DC100%2/2
$m{"headline"} and $pass == 2
$m{"headline"} $pass == 2
Truth table100%3/3
A and B
A= $m{"headline"}B= $pass == 2
ABresult
0X0
100
111
380  
381 171 require Org::Element::Headline;
382 171 my $level = length $m{h_bullet};
383  
384 # parent is upper-level headline
385 171 $parent = undef;
386 171 ▶ for (my $i=$level-1; $i>=0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 181 0 181
387 181 ▶ $parent = $last_headlines->[$i] and last;
Branch: 1 branch
BranchTrueFalseTotal
$parent = $last_headlines->[$i] and last 171 10 181
388 }
389 171 ▶ $parent //= $self;
Condition33%1/3
$parent //= $self
l!l&&r!l&&!r
17100
MC/DC0%0/2
$parent //= $self
$parent $self
Truth table33%1/3
A //= B
A= $parentB= $self
ABresult
1X1
011
000
390  
391 $el = Org::Element::Headline->new(
392 _str=>$m{headline},
393 171 document=>$self, parent=>$parent,
394 level=>$level,
395 );
396 171 ▶ $el->tags(__split_tags($m{h_tags})) if ($m{h_tags});
Branch: 1 branch
BranchTrueFalseTotal
if $m{"h_tags"} 7 164 171
397 171 my $title = $m{h_title};
398  
399 # recognize todo keyword
400 my $todo_kw_re = "(?:".
401 595 join("|", map {quotemeta}
402 "COMMENT",
403 171 @{$self->todo_states}, @{$self->done_states}) . ")";
404 171 ▶ if ($title =~ s/^($todo_kw_re)(\s+|\W)/$2/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title =~ s/^($todo_kw_re)(\s+|\W)/$2/l) 38 133 171
405 38 my $state = $1;
406 38 $title =~ s/^\s+//;
407 38 $el->is_todo(1);
408 38 $el->todo_state($state);
409 38 ▶ $el->is_done((grep { $_ eq $state } @{ $self->done_states }) ? 1:0);
Branch: 1 branch
BranchTrueFalseTotal
grep({$_ eq $state;} @{$self->done_states;}) ? : 6 32 38
410 }
411  
412 # recognize priority cookie
413 my $prio_re = "(?:".
414 171 join("|", map {quotemeta} @{$self->priorities}) . ")";
415 171 ▶ if ($title =~ s/\[#($prio_re)\]\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title =~ s/\[#($prio_re)\]\s*//l) 4 167 171
416 4 $el->priority($1);
417 }
418  
419 # recognize statistics cookie
420 171 ▶ if ($title =~ s!\[(\d+%|\d+/\d+)\]\s*!!o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title =~ s(\[(\d+%|\d+/\d+)\]\s*)[]lo) 6 165 171
421 6 $el->statistics_cookie($1);
422 }
423  
424 171 $el->title($self->_add_text_container($title, $parent, $pass));
425  
426 171 $last_headlines->[$el->level] = $el;
427 171 splice @$last_headlines, $el->level+1;
428 171 $last_headline = $el;
429 171 $last_listitem = undef;
430 171 $last_lists = [];
431 }
432  
433 # we haven't caught other matches to become element
434 670 ▶ die "BUG1: no element" unless $el || $pass != 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $el or $pass != 2 0 670 670
Condition66%2/3
$el or $pass != 2
l!l&&r!l&&!r
3603100
MC/DC0%0/2
$el or $pass != 2
$el $pass != 2
Truth table66%2/3
A or B
A= $elB= $pass != 2
ABresult
1X1
011
000
435  
436 670 ▶ $parent->children([]) if !$parent->children;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent->children 155 515 670
437 670 push @{ $parent->children }, $el;
438 670 $last_el = $el;
439 }
440  
441 # remaining text
442 184 ▶ if (@text) {
Branch: 1 branch
BranchTrueFalseTotal
if (@text) 79 105 184
443 79 $self->_add_text(join("", @text), $parent, $pass);
444 }
445 181 @text = ();
446  
447 181 log_trace('<- _parse(), elapsed time=%.3fs',
448 tv_interval($t0, [gettimeofday]));
449 }
450  
451 sub _add_text_container {
452 185 ▶ require Org::Element::Text;
Subroutine _add_text_container: called CC=4 SCAR=13.9
453 185 my ($self, $str, $parent, $pass) = @_;
454 185 my $container = Org::Element::Text->new(
455 document=>$self, parent=>$parent,
456 text=>'', style=>'',
457 );
458 185 $self->_add_text($str, $container, $pass);
459 $container = $container->children->[0] if
460 185 ▶ $container->children && @{$container->children} == 1 &&
Branch: 1 branch
BranchTrueFalseTotal
if $container->children and @{$container->children;} == 1 and $container->children->[0]->isa("Org::Element::Text") 172 13 185
Condition83%5/6
$container->children and @{$container->children;} == 1 and $container->children->[0]->isa("Org::Element::Text")
!ll&&!rl&&r
130172
$container->children and @{$container->children;} == 1
!ll&&!rl&&r
211172
MC/DC66%2/3
$container->children and @{$container->children;} == 1 and $container->children->[0]->isa("Org::Element::Text")
$container->children @{$container->children;} == 1 $container->children->[0]->isa("Org::Element::Text")
Truth table75%3/4
A and B and C
A= $container->childrenB= @{$container->children;} == 1C= $container->children->[0]->isa("Org::Element::Text")
ABCresult
0XX0
10X0
1100
1111
461 $container->children->[0]->isa('Org::Element::Text');
462 185 $container;
463 }
464  
465 sub _add_text {
466 591 ▶ require Org::Element::Text;
Subroutine _add_text: called CC=44 SCAR=38.0
467 591 my ($self, $str, $parent, $pass) = @_;
468 591 ▶ $parent //= $self;
Condition33%1/3
$parent //= $self
l!l&&r!l&&!r
59100
MC/DC0%0/2
$parent //= $self
$parent $self
Truth table33%1/3
A //= B
A= $parentB= $self
ABresult
1X1
011
000
469 #$log->tracef("-> _add_text(%s, pass=%d)", $str, $pass);
470  
471 591 my @plain_text;
472 591 ▶ while ($str =~ /$text_re/og) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /$text_re/glo) 1976 588 2564
473 1976 my %m = %+;
474 #if ($log->is_trace) {
475 # # profiler shows that this is very heavy, so commenting this out
476 # $log->tracef("TMP: match text: %s", \%+);
477 #}
478 1976 my $el;
479  
480 1976 ▶ if (defined $m{plain_text} && $pass == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $m{'plain_text'} and $pass == 2) { } 1012 964 1976
Condition100%3/3
defined $m{"plain_text"} and $pass == 2
!ll&&!rl&&r
2666981012
MC/DC100%2/2
defined $m{"plain_text"} and $pass == 2
defined $m{"plain_text"} $pass == 2
Truth table100%3/3
A and B
A= defined $m{"plain_text"}B= $pass == 2
ABresult
0X0
100
111
481 1012 push @plain_text, $m{plain_text};
482 1012 next;
483 } else {
484 964 ▶ if (@plain_text) {
Branch: 1 branch
BranchTrueFalseTotal
if (@plain_text) 117 847 964
485 117 $self->_add_plain_text(join("", @plain_text), $parent, $pass);
486 117 @plain_text = ();
487 }
488 }
489  
490 964 ▶ if ($m{link} && $pass == 2) {
Branch: 12 branches
BranchTrueFalseTotal
if ($m{'link'} and $pass == 2) { } 6 958 964
elsif ($m{'radio_target'}) { } 4 954 958
elsif ($m{'target'} and $pass == 2) { } 0 954 954
elsif ($m{'fn_num'} and $pass == 2) { } 1 953 954
elsif ($m{'fn_namedef'} and $pass == 2) { } 1 952 953
elsif ($m{'fn_nameidef'} and $pass == 2) { } 3 949 952
elsif ($m{'trange'} and $pass == 2) { } 3 946 949
elsif ($m{'tstamp'} and $pass == 2) { } 26 920 946
elsif ($m{'act_trange'} and $pass == 2) { } 5 915 920
elsif ($m{'act_tstamp'} and $pass == 2) { } 29 886 915
elsif ($m{'markup_start'} and $pass == 2) { } 24 862 886
elsif ($m{'markup_end'} and $pass == 2) { } 48 1594 1642
Condition100%3/3
$m{"link"} and $pass == 2
!ll&&!rl&&r
95266
MC/DC100%2/2
$m{"link"} and $pass == 2
$m{"link"} $pass == 2
Truth table100%3/3
A and B
A= $m{"link"}B= $pass == 2
ABresult
0X0
100
111
491 6 require Org::Element::Link;
492 $el = Org::Element::Link->new(
493 document => $self, parent => $parent,
494 link=>$m{link_link},
495 6 );
496 6 ▶ if (defined($m{link_desc}) && length($m{link_desc})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $m{"link_desc"} and length $m{"link_desc"}) 3 3 6
Condition66%2/3
defined $m{"link_desc"} and length $m{"link_desc"}
!ll&&!rl&&r
303
MC/DC50%1/2
defined $m{"link_desc"} and length $m{"link_desc"}
defined $m{"link_desc"} length $m{"link_desc"}
Truth table66%2/3
A and B
A= defined $m{"link_desc"}B= length $m{"link_desc"}
ABresult
0X0
100
111
497 $el->description(
498 $self->_add_text_container($m{link_desc},
499 3 $el, $pass));
500 }
501 } elsif ($m{radio_target}) {
502 4 require Org::Element::RadioTarget;
503 $el = Org::Element::RadioTarget->new(
504 pass => $pass,
505 document => $self, parent => $parent,
506 target=>$m{rt_target},
507 4 );
508 ▶ } elsif ($m{target} && $pass == 2) {
Condition33%1/3
$m{"target"} and $pass == 2
!ll&&!rl&&r
95400
MC/DC0%0/2
$m{"target"} and $pass == 2
$m{"target"} $pass == 2
Truth table33%1/3
A and B
A= $m{"target"}B= $pass == 2
ABresult
0X0
100
111
509 0 require Org::Element::Target;
510 $el = Org::Element::Target->new(
511 document => $self, parent => $parent,
512 target=>$m{t_target},
513 0 );
514 ▶ } elsif ($m{fn_num} && $pass == 2) {
Condition100%3/3
$m{"fn_num"} and $pass == 2
!ll&&!rl&&r
95211
MC/DC100%2/2
$m{"fn_num"} and $pass == 2
$m{"fn_num"} $pass == 2
Truth table100%3/3
A and B
A= $m{"fn_num"}B= $pass == 2
ABresult
0X0
100
111
515 1 require Org::Element::Footnote;
516 $el = Org::Element::Footnote->new(
517 document => $self, parent => $parent,
518 1 name=>$m{fn_num_num}, is_ref=>1,
519 );
520 ▶ } elsif ($m{fn_namedef} && $pass == 2) {
Condition100%3/3
$m{"fn_namedef"} and $pass == 2
!ll&&!rl&&r
95111
MC/DC100%2/2
$m{"fn_namedef"} and $pass == 2
$m{"fn_namedef"} $pass == 2
Truth table100%3/3
A and B
A= $m{"fn_namedef"}B= $pass == 2
ABresult
0X0
100
111
521 1 require Org::Element::Footnote;
522 $el = Org::Element::Footnote->new(
523 document => $self, parent => $parent,
524 name=>$m{fn_namedef_name},
525 1 ▶ is_ref=>$m{fn_namedef_def} ? 0:1,
Branch: 1 branch
BranchTrueFalseTotal
$m{'fn_namedef_def'} ? : 1 0 1
526 );
527 $el->def($self->_add_text_container($m{fn_namedef_def},
528 1 $parent, $pass));
529 ▶ } elsif ($m{fn_nameidef} && $pass == 2) {
Condition100%3/3
$m{"fn_nameidef"} and $pass == 2
!ll&&!rl&&r
94633
MC/DC100%2/2
$m{"fn_nameidef"} and $pass == 2
$m{"fn_nameidef"} $pass == 2
Truth table100%3/3
A and B
A= $m{"fn_nameidef"}B= $pass == 2
ABresult
0X0
100
111
530 3 require Org::Element::Footnote;
531 $el = Org::Element::Footnote->new(
532 document => $self, parent => $parent,
533 name=>$m{fn_nameidef_name},
534 is_ref=>($m{fn_nameidef_def} ? 0:1) ||
535 3 ▶ !length($m{fn_nameidef_name}),
Branch: 1 branch
BranchTrueFalseTotal
$m{'fn_nameidef_def'} ? : 2 1 3
Condition100%3/3
($m{'fn_nameidef_def'} ? 0 : 1) || !length($m{'fn_nameidef_name'})
l!l&&r!l&&!r
111
MC/DC100%2/2
($m{'fn_nameidef_def'} ? 0 : 1) || !length($m{'fn_nameidef_name'})
($m{'fn_nameidef_def'} ? 0 : 1) !length($m{'fn_nameidef_name'})
Truth table100%3/3
A || not(B)
A= ($m{'fn_nameidef_def'} ? 0 : 1)B= !length($m{'fn_nameidef_name'})
ABresult
1X1
011
000
536 );
537 $el->def(length($m{fn_nameidef_def}) ?
538 $self->_add_text_container($m{fn_nameidef_def},
539 3 ▶ $parent, $pass) : undef);
Branch: 1 branch
BranchTrueFalseTotal
length $m{'fn_nameidef_def'} ? : 2 1 3
540 ▶ } elsif ($m{trange} && $pass == 2) {
Condition100%3/3
$m{"trange"} and $pass == 2
!ll&&!rl&&r
94333
MC/DC100%2/2
$m{"trange"} and $pass == 2
$m{"trange"} $pass == 2
Truth table100%3/3
A and B
A= $m{"trange"}B= $pass == 2
ABresult
0X0
100
111
541 3 require Org::Element::TimeRange;
542 3 require Org::Element::Timestamp;
543 3 $el = Org::Element::TimeRange->new(
544 document => $self, parent => $parent,
545 );
546 3 my $opts = {allow_event_duration=>0, allow_repeater=>0};
547 $el->ts1(Org::Element::Timestamp->new(
548 3 _str=>$m{trange_ts1}, document=>$self, parent=>$parent));
549 3 $el->ts1->_parse_timestamp($m{trange_ts1}, $opts);
550 $el->ts2(Org::Element::Timestamp->new(
551 3 _str=>$m{trange_ts2}, document=>$self, parent=>$parent));
552 3 $el->ts2->_parse_timestamp($m{trange_ts2}, $opts);
553 3 $el->children([$el->ts1, $el->ts2]);
554 ▶ } elsif ($m{tstamp} && $pass == 2) {
Condition100%3/3
$m{"tstamp"} and $pass == 2
!ll&&!rl&&r
8972326
MC/DC100%2/2
$m{"tstamp"} and $pass == 2
$m{"tstamp"} $pass == 2
Truth table100%3/3
A and B
A= $m{"tstamp"}B= $pass == 2
ABresult
0X0
100
111
555 26 require Org::Element::Timestamp;
556 $el = Org::Element::Timestamp->new(
557 26 _str => $m{tstamp}, document => $self, parent => $parent,
558 );
559 26 $el->_parse_timestamp($m{tstamp});
560 ▶ } elsif ($m{act_trange} && $pass == 2) {
Condition100%3/3
$m{"act_trange"} and $pass == 2
!ll&&!rl&&r
91055
MC/DC100%2/2
$m{"act_trange"} and $pass == 2
$m{"act_trange"} $pass == 2
Truth table100%3/3
A and B
A= $m{"act_trange"}B= $pass == 2
ABresult
0X0
100
111
561 5 require Org::Element::TimeRange;
562 5 require Org::Element::Timestamp;
563 5 $el = Org::Element::TimeRange->new(
564 document => $self, parent => $parent,
565 );
566 5 my $opts = {allow_event_duration=>0, allow_repeater=>0};
567 $el->ts1(Org::Element::Timestamp->new(
568 5 _str=>$m{act_trange_ts1}, document=>$self, parent=>$parent));
569 5 $el->ts1->_parse_timestamp($m{act_trange_ts1}, $opts);
570 $el->ts2(Org::Element::Timestamp->new(
571 3 _str=>$m{act_trange_ts2}, document=>$self, parent=>$parent));
572 3 $el->ts2->_parse_timestamp($m{act_trange_ts2}, $opts);
573 3 $el->children([$el->ts1, $el->ts2]);
574 ▶ } elsif ($m{act_tstamp} && $pass == 2) {
Condition100%3/3
$m{"act_tstamp"} and $pass == 2
!ll&&!rl&&r
8652129
MC/DC100%2/2
$m{"act_tstamp"} and $pass == 2
$m{"act_tstamp"} $pass == 2
Truth table100%3/3
A and B
A= $m{"act_tstamp"}B= $pass == 2
ABresult
0X0
100
111
575 29 require Org::Element::Timestamp;
576 $el = Org::Element::Timestamp->new(
577 29 _str => $m{act_tstamp}, document => $self, parent => $parent,
578 );
579 29 $el->_parse_timestamp($m{act_tstamp});
580 ▶ } elsif ($m{markup_start} && $pass == 2) {
Condition100%3/3
$m{"markup_start"} and $pass == 2
!ll&&!rl&&r
8431924
MC/DC100%2/2
$m{"markup_start"} and $pass == 2
$m{"markup_start"} $pass == 2
Truth table100%3/3
A and B
A= $m{"markup_start"}B= $pass == 2
ABresult
0X0
100
111
581 24 require Org::Element::Text;
582 $el = Org::Element::Text->new(
583 document => $self, parent => $parent,
584 style=>'', text=>$m{markup_start},
585 24 );
586 # temporary mark, we need to apply markup later
587 24 $el->{_mu_start}++;
588 ▶ } elsif ($m{markup_end} && $pass == 2) {
Condition100%3/3
$m{"markup_end"} and $pass == 2
!ll&&!rl&&r
7803448
MC/DC100%2/2
$m{"markup_end"} and $pass == 2
$m{"markup_end"} $pass == 2
Truth table100%3/3
A and B
A= $m{"markup_end"}B= $pass == 2
ABresult
0X0
100
111
589 48 require Org::Element::Text;
590 $el = Org::Element::Text->new(
591 document => $self, parent => $parent,
592 style=>'', text=>$m{markup_end},
593 48 );
594 # temporary mark, we need to apply markup later
595 48 $el->{_mu_end}++;
596 }
597 961 ▶ die "BUG2: no element" unless $el || $pass != 2;
Branch: 1 branch
BranchTrueFalseTotal
unless $el or $pass != 2 0 961 961
Condition66%2/3
$el or $pass != 2
l!l&&r!l&&!r
1478140
MC/DC0%0/2
$el or $pass != 2
$el $pass != 2
Truth table66%2/3
A or B
A= $elB= $pass != 2
ABresult
1X1
011
000
598 961 ▶ $parent->children([]) if !$parent->children;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent->children 40 921 961
599 961 push @{ $parent->children }, $el;
600 }
601  
602 # remaining text
603 588 ▶ if (@plain_text && $pass == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@plain_text and $pass == 2) 394 194 588
Condition66%2/3
@plain_text and $pass == 2
!ll&&!rl&&r
1940394
MC/DC50%1/2
@plain_text and $pass == 2
@plain_text $pass == 2
Truth table66%2/3
A and B
A= @plain_textB= $pass == 2
ABresult
0X0
100
111
604 394 ▶ $parent->children([]) if !$parent->children;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent->children 268 126 394
605 394 push @{$parent->children}, Org::Element::Text->new(
606 text => join("", @plain_text), style=>'',
607 document=>$self, parent=>$parent);
608 394 @plain_text = ();
609 }
610  
611 588 ▶ if ($pass == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pass == 2) 413 175 588
612 413 $self->_apply_markup($parent);
613 413 ▶ if (@{$self->radio_targets}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->radio_targets;}) 1 412 413
614 1 my $re = join "|", map {quotemeta} @{$self->radio_targets};
615 1 $re = qr/(?:$re)/i;
616 1 $self->_linkify_rt_recursive($re, $parent);
617 }
618 413 ▶ my $c = $parent->children // [];
Condition100%2/2
$parent->children // []
l!l
4076
MC/DC100%1/1
$parent->children // []
$parent->children
Truth table100%2/2
A
A= $parent->children
Aresult
11
00
619 }
620  
621 #$log->tracef('<- _add_text()');
622 }
623  
624 # to keep parser's regexes simple and fast, we detect markup in regex rather
625 # simplistically (as text element) and then apply some more filtering & applying
626 # logic here
627  
628 sub _apply_markup {
629 #$log->trace("-> _apply_markup()");
630 413 ▶ my ($self, $parent) = @_;
Subroutine _apply_markup: called CC=17 SCAR=28.4
631 413 my $last_index = 0;
632 413 ▶ my $c = $parent->children or return;
Branch: 1 branch
BranchTrueFalseTotal
my $c = $parent->children or return 6 407 413
633  
634 407 while (1) {
635 #$log->tracef("text cluster = %s", [map {$_->as_string} @$c]);
636 # find a new mu_start
637 429 my $mu_start_index = -1;
638 429 my $mu;
639 429 ▶ for (my $i = $last_index; $i < @$c; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$c) 831 407 1238
640 831 ▶ next unless $c->[$i]->{_mu_start};
Branch: 1 branch
BranchTrueFalseTotal
unless $c->[$i]{"_mu_start"} 809 22 831
641 22 $mu_start_index = $i; $mu = $c->[$i]->text;
642 #$log->tracef("found mu_start at %d (%s)", $i, $mu);
643 22 last;
644 }
645 429 ▶ unless ($mu_start_index >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mu_start_index >= 0) 407 22 429
646 #$log->trace("no more mu_start found");
647 407 last;
648 }
649  
650 # check whether this is a valid markup (has text, has markup end, not
651 # interspersed with non-text, no more > 1 newlines)
652 22 my $mu_end_index = 0;
653 22 my $newlines = 0;
654 22 my $has_text;
655 my $has_unmarkable;
656 22 ▶ for (my $i=$mu_start_index+1; $i < @$c; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$c) 60 7 67
657 60 ▶ if ($c->[$i]->isa('Org::Element::Text')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->[$i]->isa('Org::Element::Text')) { } 59 1 60
658 59 $has_text++;
659 } elsif (1) {
660 } else {
661 $has_unmarkable++; last;
662 }
663 60 ▶ if ($c->[$i]->{_mu_end} && $c->[$i]->text eq $mu) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->[$i]{"_mu_end"} and $c->[$i]->text eq $mu) 13 47 60
Condition100%3/3
$c->[$i]{"_mu_end"} and $c->[$i]->text eq $mu
!ll&&!rl&&r
40713
MC/DC100%2/2
$c->[$i]{"_mu_end"} and $c->[$i]->text eq $mu
$c->[$i]{"_mu_end"} $c->[$i]->text eq $mu
Truth table100%3/3
A and B
A= $c->[$i]{"_mu_end"}B= $c->[$i]->text eq $mu
ABresult
0X0
100
111
664 #$log->tracef("found mu_end at %d", $i);
665 13 $mu_end_index = $i; last;
666 }
667 47 my $text = $c->[$i]->as_string;
668 47 ▶ $newlines++ while $text =~ /\R/g;
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /\R/gl and do { ++$newlines } 10 47 57
669 47 ▶ last if $newlines > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $newlines > 1 2 45 47
670 }
671 22 ▶ my $valid = $has_text && !$has_unmarkable
Condition55%5/9
$has_text && !$has_unmarkable && $mu_end_index && $newlines <= 1
!ll&&!rl&&r
9013
$has_text and not $has_unmarkable and $mu_end_index
!ll&&!rl&&r
0913
$has_text and not $has_unmarkable
!ll&&!rl&&r
0022
MC/DC25%1/4
$has_text && !$has_unmarkable && $mu_end_index && $newlines <= 1
$has_text not $has_unmarkable $mu_end_index $newlines <= 1
Truth table40%2/5
A and not(B) and C && D
A= $has_textB= not $has_unmarkableC= $mu_end_indexD= $newlines <= 1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
672 && $mu_end_index && $newlines <= 1;
673 #$log->tracef("mu candidate: start=%d, end=%s, ".
674 # "has_text=%s, has_unmarkable=%s, newlines=%d, valid=%s",
675 # $mu_start_index, $mu_end_index,
676 # $has_text, $has_unmarkable, $newlines, $valid
677 # );
678 22 ▶ if ($valid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($valid) { } 13 9 22
679 26 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
680 my $mu_el = Org::Element::Text->new(
681 document => $self, parent => $parent,
682 13 style=>$Org::Element::Text::mu2style{$mu}, text=>'',
683 );
684 13 my @c2 = splice @$c, $mu_start_index,
685 $mu_end_index-$mu_start_index+1, $mu_el;
686 #$log->tracef("grouping %s", [map {$_->text} @c2]);
687 13 $mu_el->children(\@c2);
688 13 shift @c2;
689 13 pop @c2;
690 13 for (@c2) {
691 22 $_->{parent} = $mu_el;
692 }
693 13 $self->_merge_text_elements(\@c2);
694 # squish if only one child
695 13 ▶ if (@c2 == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@c2 == 1) 12 1 13
696 12 $mu_el->text($c2[0]->text);
697 12 $mu_el->children(undef);
698 }
699 } else {
700 9 undef $c->[$mu_start_index]->{_mu_start};
701 9 $last_index++;
702 }
703 }
704 407 $self->_merge_text_elements($c);
705 #$log->trace("<- _apply_markup()");
706 }
707  
708 sub _merge_text_elements {
709 420 ▶ my ($self, $els) = @_;
Subroutine _merge_text_elements: called CC=12 SCAR=24.9
710 #$log->tracef("-> _merge_text_elements(%s)", [map {$_->as_string} @$els]);
711 420 ▶ return unless @$els >= 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @$els >= 2 310 110 420
712 110 my $i=-1;
713 110 while (1) {
714 462 $i++;
715 462 ▶ last if $i >= @$els;
Branch: 1 branch
BranchTrueFalseTotal
if $i >= @$els 110 352 462
716 352 ▶ next if $els->[$i]->children || !$els->[$i]->isa('Org::Element::Text');
Branch: 1 branch
BranchTrueFalseTotal
if $els->[$i]->children or not $els->[$i]->isa("Org::Element::Text") 137 215 352
Condition100%3/3
$els->[$i]->children or not $els->[$i]->isa("Org::Element::Text")
l!l&&r!l&&!r
33104215
MC/DC100%2/2
$els->[$i]->children or not $els->[$i]->isa("Org::Element::Text")
$els->[$i]->children not $els->[$i]->isa("Org::Element::Text")
Truth table100%3/3
A or not(B)
A= $els->[$i]->childrenB= not $els->[$i]->isa("Org::Element::Text")
ABresult
1X1
011
000
717 215 ▶ my $istyle = $els->[$i]->style // "";
Condition50%1/2
$els->[$i]->style // ""
l!l
2150
MC/DC0%0/1
$els->[$i]->style // ""
$els->[$i]->style
Truth table50%1/2
A
A= $els->[$i]->style
Aresult
11
00
718 215 while (1) {
719 298 ▶ last if $i+1 >= @$els || $els->[$i+1]->children ||
Branch: 1 branch
BranchTrueFalseTotal
if $i + 1 >= @$els or $$els[$i + 1]->children or not $$els[$i + 1]->isa("Org::Element::Text") 91 104 195
Condition100%6/6
$i + 1 >= @$els or $$els[$i + 1]->children or not $$els[$i + 1]->isa("Org::Element::Text")
l!l&&r!l&&!r
12371104
$i + 1 >= @$els or $$els[$i + 1]->children
l!l&&r!l&&!r
10320175
MC/DC100%3/3
$i + 1 >= @$els or $$els[$i + 1]->children or not $$els[$i + 1]->isa("Org::Element::Text")
$i + 1 >= @$els $$els[$i + 1]->children not $$els[$i + 1]->isa("Org::Element::Text")
Truth table100%4/4
A or B or not(C)
A= $i + 1 >= @$elsB= $$els[$i + 1]->childrenC= not $$els[$i + 1]->isa("Org::Element::Text")
ABCresult
1XX1
01X1
0011
0000
720 !$els->[$i+1]->isa('Org::Element::Text');
721 104 ▶ last if ($els->[$i+1]->style // "") ne $istyle;
Branch: 1 branch
BranchTrueFalseTotal
if ($$els[$i + 1]->style // "") ne $istyle 21 83 104
Condition50%1/2
$$els[$i + 1]->style // ""
l!l
1040
MC/DC0%0/1
$$els[$i + 1]->style // ""
$$els[$i + 1]->style
Truth table50%1/2
A
A= $$els[$i + 1]->style
Aresult
11
00
722 #$log->tracef("merging text[%d] '%s' with '%s'",
723 # $i, $els->[$i]->text, $els->[$i+1]->text);
724 83 ▶ $els->[$i]->{text} .= $els->[$i+1]->{text} // "";
Condition50%1/2
$$els[$i + 1]{"text"} // ""
l!l
830
MC/DC0%0/1
$$els[$i + 1]{"text"} // ""
$$els[$i + 1]{"text"}
Truth table50%1/2
A
A= $$els[$i + 1]{"text"}
Aresult
11
00
725 83 splice @$els, $i+1, 1;
726 }
727 }
728 #$log->tracef("merge result = %s", [map {$_->as_string} @$els]);
729 #$log->trace("<- _merge_text_elements()");
730 }
731  
732 sub _linkify_rt_recursive {
733 8 ▶ require Org::Element::Text;
Subroutine _linkify_rt_recursive: called CC=8 SCAR=20.8
734 8 require Org::Element::Link;
735 8 my ($self, $re, $parent) = @_;
736 8 my $c = $parent->children;
737 8 ▶ return unless $c;
Branch: 1 branch
BranchTrueFalseTotal
unless $c 7 1 8
738 1 ▶ for (my $i=0; $i<@$c; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$c) 12 1 13
739 12 my $el = $c->[$i];
740 12 ▶ if ($el->isa('Org::Element::Text')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($el->isa("Org::Element::Text")) 7 5 12
741 7 my @split0 = split /\b($re)\b/, $el->text;
742 7 ▶ next unless @split0 > 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @split0 > 1 5 2 7
743 2 my @split;
744 2 for my $s (@split0) {
745 8 ▶ if ($s =~ /^$re$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($s =~ /^$re$/l) { } 3 5 8
elsif (length $s) { } 4 1 5
746 3 push @split, Org::Element::Link->new(
747 document=>$self, parent=>$parent,
748 link=>$s, description=>undef,
749 from_radio_target=>1,
750 );
751 } elsif (length $s) {
752 4 push @split, Org::Element::Text->new(
753 document=>$self, parent=>$parent,
754 text=>$s, style=>$el->style,
755 );
756 }
757 }
758 2 splice @$c, $i, 1, @split;
759 }
760 7 $self->_linkify_rt_recursive($re, $el);
761 }
762 }
763  
764 sub _add_plain_text {
765 117 ▶ require Org::Element::Text;
Subroutine _add_plain_text: called CC=2 SCAR=6.9
766 117 my ($self, $str, $parent, $pass) = @_;
767 117 my $el = Org::Element::Text->new(
768 document=>$self, parent=>$parent, style=>'', text=>$str);
769 117 ▶ $parent->children([]) if !$parent->children;
Branch: 1 branch
BranchTrueFalseTotal
unless $parent->children 30 87 117
770 117 push @{ $parent->children }, $el;
771 }
772  
773 sub __split_tags {
774 7 ▶ [$_[0] =~ /:([^:]+)/g];
Subroutine __split_tags: called CC=1 SCAR=0.0
775 }
776  
777 sub load_element_modules {
778 2 ▶ require Module::List;
Subroutine load_element_modules: called CC=2 SCAR=6.9
Pod documented
779 2 require Module::Load;
780  
781 2 my $mm = Module::List::list_modules("Org::Element::", {list_modules=>1});
782 2 for (keys %$mm) {
783 38 Module::Load::load($_);
784 }
785 }
786  
787 sub cmp_priorities {
788 11 ▶ my ($self, $p1, $p2) = @_;
Subroutine cmp_priorities: called CC=3 SCAR=11.0
Pod documented
789  
790 11 my $pp = $self->priorities;
791 11 ▶ my $pos1 = firstidx {$_ eq $p1} @$pp;
Subroutine __ANON__: called CC=1 SCAR=0.0
792 11 ▶ return undef unless $pos1 >= 0; ## no critic: Subroutines::ProhibitExplicitReturnUndef
Branch: 1 branch
BranchTrueFalseTotal
unless $pos1 >= 0 2 9 11
793 9 ▶ my $pos2 = firstidx {$_ eq $p2} @$pp;
Subroutine __ANON__: called CC=1 SCAR=0.0
794 9 ▶ return undef unless $pos2 >= 0; ## no critic: Subroutines::ProhibitExplicitReturnUndef
Branch: 1 branch
BranchTrueFalseTotal
unless $pos2 >= 0 2 7 9
795 7 $pos1 <=> $pos2;
796 }
797  
798 26 1;
799 # ABSTRACT: Represent an Org document
800  
801 __END__
802  
803 =pod
804  
805 =encoding UTF-8
806  
807 =head1 NAME
808  
809 Org::Document - Represent an Org document
810  
811 =head1 VERSION
812  
813 This document describes version 0.560 of Org::Document (from Perl distribution Org-Parser), released on 2023-08-05.
814  
815 =head1 SYNOPSIS
816  
817 use Org::Document;
818  
819 # create a new Org document tree from string
820 my $org = Org::Document->new(from_string => <<EOF);
821 * heading 1a
822 some text
823 ** heading 2
824 * heading 1b
825 EOF
826  
827 =head1 DESCRIPTION
828  
829 Derived from L<Org::Element>.
830  
831 =head1 ATTRIBUTES
832  
833 =head2 tags => ARRAY
834  
835 List of tags for this file, usually set via #+FILETAGS.
836  
837 =head2 todo_states => ARRAY
838  
839 List of known (action-requiring) todo states. Default is ['TODO'].
840  
841 =head2 done_states => ARRAY
842  
843 List of known done (non-action-requiring) states. Default is ['DONE'].
844  
845 =head2 priorities => ARRAY
846  
847 List of known priorities. Default is ['A', 'B', 'C'].
848  
849 =head2 drawer_names => ARRAY
850  
851 List of known drawer names. Default is [qw/CLOCK LOGBOOK PROPERTIES/].
852  
853 =head2 properties => ARRAY
854  
855 File-wide properties.
856  
857 =head2 radio_targets => ARRAY
858  
859 List of radio target text.
860  
861 =head2 time_zone => ARRAY
862  
863 If set, will be passed to DateTime->new() (e.g. by L<Org::Element::Timestamp>).
864  
865 =head2 ignore_unknown_settings => bool
866  
867 If set to true, unknown settings will not cause a parse failure.
868  
869 =head1 METHODS
870  
871 =for Pod::Coverage BUILD
872  
873 =head2 new
874  
875 Usage:
876  
877 $doc = Org::Document->new(%args);
878  
879 Create document object. If C<from_string> argument is specified, will parse
880 the string. Otherwise, will create an empty document object. Arguments:
881  
882 =over
883  
884 =item * from_string
885  
886 String. String to parse into document object tree content.
887  
888 =back
889  
890 =head2 load_element_modules()
891  
892 Load all Org::Element::* modules. This is useful when wanting to work with
893 element objects retrieved from serialization, where the element modules have not
894 been loaded.
895  
896 =head2 cmp_priorities($p1, $p2) => -1|0|-1
897  
898 Compare two priorities C<$p1> and C<$p2>. Return result like Perl's C<cmp>: 0 if
899 the two are the same, -1 if C<$p1> is of I<higher> priority (since it's more to
900 the left position in priority list, which is sorted highest-first) than C<$p2>,
901 and 1 if C<$p2> is of I<lower> priority than C<$p1>.
902  
903 If either C<$p1> or C<$p2> has unknown priority, will return undef.
904  
905 Examples:
906  
907 $doc->cmp_priorities('A', 'A') # -> 0
908 $doc->cmp_priorities('A', 'B') # -> -1 (A is higher than B)
909 $doc->cmp_priorities('C', 'B') # -> 1 (C is lower than B)
910 $doc->cmp_priorities('X', 'A') # -> undef (X is unknown)
911  
912 Note that X could be known if there is a C<#+PRIORITIES> setting which defines
913 it.
914  
915 =head1 HOMEPAGE
916  
917 Please visit the project's homepage at L<https://metacpan.org/release/Org-Parser>.
918  
919 =head1 SOURCE
920  
921 Source repository is at L<https://github.com/perlancar/perl-Org-Parser>.
922  
923 =head1 AUTHOR
924  
925 perlancar <perlancar@cpan.org>
926  
927 =head1 CONTRIBUTING
928  
929  
930 To contribute, you can send patches by email/via RT, or send pull requests on
931 GitHub.
932  
933 Most of the time, you don't need to build the distribution yourself. You can
934 simply modify the code, then test via:
935  
936 % prove -l
937  
938 If you want to build the distribution (e.g. to try to install it locally on your
939 system), you can install L<Dist::Zilla>,
940 L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
941 L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
942 Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
943 that are considered a bug and can be reported to me.
944  
945 =head1 COPYRIGHT AND LICENSE
946  
947 This software is copyright (c) 2023, 2022, 2021, 2020, 2019, 2017, 2016, 2015, 2014, 2013, 2012, 2011 by perlancar <perlancar@cpan.org>.
948  
949 This is free software; you can redistribute it and/or modify it under
950 the same terms as the Perl 5 programming language system itself.
951  
952 =head1 BUGS
953  
954 Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Org-Parser>
955  
956 When submitting a bug or request, please include a test-file or a
957 patch to an existing test-file that illustrates the bug or desired
958 feature.
959  
960 =cut
↑ Summary Element/Headline.pm »