FilterOrgByHeadlines.pm

filter: Statementstmt 12.2% 15 / 123 Branchbran 0.0% 0 / 92 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 60.0% 3 / 5 Podpod 100% 1 / 1 totaltotal 7.0% 19 / 270 CC 65 SCAR 81.6
CC
65
Cov
6%
filter_org_by_headlines
3906.0
_match
20.0
BEGIN
1.0
CRAP 3510.9
↑ Summary ListOrgHeadlines.pm »
1 package App::FilterOrgByHeadlines;
2  
3 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8 2 our $DATE = '2026-07-22'; # DATE
9 2 our $DIST = 'App-OrgUtils'; # DIST
10 2 our $VERSION = '0.489'; # VERSION
11  
12 2 our %SPEC;
13  
14 0 ▶sub _match {
Subroutine _match: not called CC=4 SCAR=30.0
15 0 my ($hl, $op) = @_;
16 0 ▶ if (ref($op) eq 'Regexp') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $op eq 'Regexp') { } 0 0 0
elsif ($op =~ m[\A/(.*)/(i?)\z]) { } 0 0 0
17 0 return $hl =~ $op;
18 } elsif ($op =~ m!\A/(.*)/(i?)\z!) {
19 0 my $re;
20 # XXX possible arbitrary code execution
21 0 eval "\$re = qr/$1/$2"; ## no critic: BuiltinFunctions::ProhibitStringyEval
22 0 ▶ die if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
23 0 return $hl =~ $re;
24 } else {
25 0 return index($hl, $op) >= 0;
26 }
27 }
28  
29 $SPEC{filter_org_by_headlines} = {
30 2 v => 1.1,
31 summary => 'Filter Org by headlines',
32 description => <<'MARKDOWN',
33  
34 This routine uses simple regex instead of Org::Parser, for faster performance.
35  
36 MARKDOWN
37 args => {
38 input => {
39 #schema => ['any*', of => ['str*', ['array*', of => 'str*']]],
40 schema => ['str*'],
41 description => <<'MARKDOWN',
42  
43 Value is either a string or an array of strings.
44  
45 MARKDOWN
46 req => 1,
47 pos => 0,
48 cmdline_src => 'stdin_or_files',
49 },
50 min_level => {
51 schema => 'int*',
52 tags => ['category:filtering'],
53 },
54 max_level => {
55 schema => 'int*',
56 tags => ['category:filtering'],
57 },
58 level => {
59 schema => 'int*',
60 tags => ['category:filtering'],
61 },
62 match => {
63 schema => ['any*', of=>['str*', 're*']],
64 summary => 'Only include headline which matches this',
65 description => <<'MARKDOWN',
66  
67 Value is either a string or a regex. If string is in the form of `/.../` or
68 `/.../i` it is assumed to be a regex.
69  
70 MARKDOWN
71 tags => ['category:filtering'],
72 },
73 parent_match => {
74 schema => ['any*', of=>['str*', 're*']],
75 summary => 'Only include headline whose parent matches this',
76 description => <<'MARKDOWN',
77  
78 Value is either a string or a regex. If string is in the form of `/.../` or
79 `/.../i` it is assumed to be a regex.
80  
81 MARKDOWN
82 tags => ['category:filtering'],
83 },
84 ascendant_match => {
85 schema => ['any*', of=>['str*', 're*']],
86 summary => 'Only include headline whose ascendant matches this',
87 description => <<'MARKDOWN',
88  
89 Value is either a string or a regex. If string is in the form of `/.../` or
90 `/.../i` it is assumed to be a regex.
91  
92 MARKDOWN
93 tags => ['category:filtering'],
94 },
95 is_todo => {
96 schema => 'bool',
97 summary => 'Only include headline which is a todo item',
98 'summary.alt.bool.not' =>
99 'Only include headline which is not a todo item',
100 tags => ['category:filtering'],
101 },
102 is_done => {
103 schema => 'bool',
104 summary => 'Only include headline which is a done todo item',
105 'summary.alt.bool.not' =>
106 'Only include headline which is a todo item but not done',
107 tags => ['category:filtering'],
108 },
109 has_tags => {
110 'x.name.is_plural' => 1,
111 schema => ['array*', of=>'str*'],
112 summary => 'Only include headline which have all these tags',
113 tags => ['category:filtering'],
114 },
115 lacks_tags => {
116 'x.name.is_plural' => 1,
117 schema => ['array*', of=>'str*'],
118 summary => 'Only include headline which lack all these tags',
119 tags => ['category:filtering'],
120 },
121  
122 # XXX todo_state => str*
123 with_content => {
124 schema => 'bool',
125 summary => 'Include headline content',
126 'summary.alt.bool.not' =>
127 "Don't include headline content, just print the headlines",
128 default => 1,
129 tags => ['category:result'],
130 },
131 with_preamble => {
132 schema => 'bool',
133 summary => 'Include text before any headline',
134 'summary.alt.bool.not' =>
135 "Don't include text before any headline",
136 default => 1,
137 tags => ['category:result'],
138 },
139 return_array => {
140 schema => ['bool*', is=>1],
141 summary => 'Return array of strings instead of strings',
142 tags => ['category:result'],
143 },
144 },
145 result_naked => 1,
146 result => {
147 schema => ['any*', of=>['str*', ['array*', of=>'str*']]],
148 },
149 };
150 0 ▶sub filter_org_by_headlines {
Subroutine filter_org_by_headlines: not called CC=62 SCAR=82.7
Pod documented
151 0 my %args = @_;
152  
153 0 my $input = $args{input};
154 0 ▶ my $with_ct = $args{with_content} // 1;
Condition0%0/2
$args{"with_content"} // 1
l!l
00
MC/DC0%0/1
$args{"with_content"} // 1
$args{"with_content"}
Truth table0%0/2
A
A= $args{"with_content"}
Aresult
11
00
155 0 ▶ my $with_pre = $args{with_preamble} // 1;
Condition0%0/2
$args{"with_preamble"} // 1
l!l
00
MC/DC0%0/1
$args{"with_preamble"} // 1
$args{"with_preamble"}
Truth table0%0/2
A
A= $args{"with_preamble"}
Aresult
11
00
156  
157 0 my $curhl;
158 my $curlevel;
159 0 my $curtodokw;
160 0 my $curhl_filtered;
161 0 my $curtags;
162 0 my @parenthls; # ([hl, level], ...)
163  
164 0 my %todo_keywords = map {$_=>1} qw(TODO);
165 0 my %done_keywords = map {$_=>1} qw(DONE);
166 0 my $seen_todo_def;
167  
168 my $out;
169 LINE:
170 0 ▶ for my $line (ref($input) ? @$input : split(/^/, $input)) {
Branch: 1 branch
BranchTrueFalseTotal
ref $input ? : 0 0 0
171 0 my $is_hl; # whether current line is a headline
172 0 ▶ if ($line =~ /^#\+TODO:\s*(.+)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^#\+TODO:\s*(.+)/) { } 0 0 0
elsif ($line =~ /^(\*+)\s+(.*)/) { } 0 0 0
173 0 my $arg = $1;
174 0 ▶ if (!$seen_todo_def++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen_todo_def++) 0 0 0
175 0 %todo_keywords = ();
176 0 %done_keywords = ();
177 }
178 0 ▶ my $done_s = $arg =~ s/\s*\|\s*(.*)// ? $1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$arg =~ s/\s*\|\s*(.*)// ? : 0 0 0
179 0 my $todo_s = $arg;
180 0 $todo_keywords{$_}++ for grep {length} split /\s+/, $todo_s;
181 0 $done_keywords{$_}++ for grep {length} split /\s+/, $done_s;
182 } elsif ($line =~ /^(\*+)\s+(.*)/) {
183 0 $is_hl++;
184  
185 0 my $level = length($1);
186 0 my $arg = $2;
187  
188 0 ▶ if ($line =~ /\s+:(\w+(?::\w+)*):(?:\s+|$)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\s+:(\w+(?::\w+)*):(?:\s+|$)/) { } 0 0 0
189 0 $curtags = [split /:/, $1];
190 } else {
191 0 undef $curtags;
192 }
193  
194 0 ▶ if ($curlevel && $curlevel < $level) {
Branch: 2 branches
BranchTrueFalseTotal
if ($curlevel and $curlevel < $level) { } 0 0 0
elsif (@parenthls) { } 0 0 0
Condition0%0/3
$curlevel and $curlevel < $level
!ll&&!rl&&r
000
MC/DC0%0/2
$curlevel and $curlevel < $level
$curlevel $curlevel < $level
Truth table0%0/3
A and B
A= $curlevelB= $curlevel < $level
ABresult
0X0
100
111
195 0 push @parenthls, [$curhl, $curlevel];
196 } elsif (@parenthls) {
197 0 @parenthls = grep { $_->[1] < $level } @parenthls;
198 }
199 0 $curhl = $line;
200 0 $curlevel = $level;
201  
202 0 $curtodokw = undef;
203 0 ▶ if ($arg =~ /(\w+)\s/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /(\w+)\s/) 0 0 0
204 0 ▶ if ($todo_keywords{$1} || $done_keywords{$1}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($todo_keywords{$1} or $done_keywords{$1}) 0 0 0
Condition0%0/3
$todo_keywords{$1} or $done_keywords{$1}
l!l&&r!l&&!r
000
MC/DC0%0/2
$todo_keywords{$1} or $done_keywords{$1}
$todo_keywords{$1} $done_keywords{$1}
Truth table0%0/3
A or B
A= $todo_keywords{$1}B= $done_keywords{$1}
ABresult
1X1
011
000
205 0 $curtodokw = $1;
206 }
207 }
208  
209 0 undef $curhl_filtered;
210 }
211  
212 # filtering
213 0 my $include;
214 FILTER: {
215 0 ▶ if (!$curhl) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($curhl) 0 0 0
216 0 ▶ $include++ if $with_pre;
Branch: 1 branch
BranchTrueFalseTotal
if $with_pre 0 0 0
217 0 last FILTER;
218 }
219 0 ▶ last if !$with_ct && !$is_hl;
Branch: 1 branch
BranchTrueFalseTotal
unless $with_ct or $is_hl 0 0 0
Condition0%0/3
$with_ct or $is_hl
l!l&&r!l&&!r
000
MC/DC0%0/2
$with_ct or $is_hl
$with_ct $is_hl
Truth table0%0/3
A or B
A= $with_ctB= $is_hl
ABresult
1X1
011
000
220 0 ▶ if (defined $curhl_filtered) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $curhl_filtered) 0 0 0
221 0 ▶ $include++ if !$curhl_filtered;
Branch: 1 branch
BranchTrueFalseTotal
unless $curhl_filtered 0 0 0
222 0 last FILTER;
223 }
224  
225 0 ▶ $curhl_filtered = 1 if $is_hl;
Branch: 1 branch
BranchTrueFalseTotal
if $is_hl 0 0 0
226 0 ▶ if (defined $args{min_level}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"min_level"}) 0 0 0
227 0 ▶ last if $curlevel < $args{min_level};
Branch: 1 branch
BranchTrueFalseTotal
if $curlevel < $args{"min_level"} 0 0 0
228 }
229 0 ▶ if (defined $args{max_level}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"max_level"}) 0 0 0
230 0 ▶ last if $curlevel > $args{max_level};
Branch: 1 branch
BranchTrueFalseTotal
if $curlevel > $args{"max_level"} 0 0 0
231 }
232 0 ▶ if (defined $args{level}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"level"}) 0 0 0
233 0 ▶ last if $curlevel != $args{level};
Branch: 1 branch
BranchTrueFalseTotal
if $curlevel != $args{"level"} 0 0 0
234 }
235 0 ▶ if (defined $args{is_todo}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"is_todo"}) 0 0 0
236 0 ▶ my $kw = $curtodokw // '';
Condition0%0/2
$curtodokw // ""
l!l
00
MC/DC0%0/1
$curtodokw // ""
$curtodokw
Truth table0%0/2
A
A= $curtodokw
Aresult
11
00
237 last if ( ($todo_keywords{$kw} || $done_keywords{$kw}) xor
238 0 ▶ $args{is_todo} );
Branch: 1 branch
BranchTrueFalseTotal
if $todo_keywords{$kw} or $done_keywords{$kw} xor $args{"is_todo"} 0 0 0
Condition0%0/7
$todo_keywords{$kw} or $done_keywords{$kw} xor $args{"is_todo"}
l&&rl&&!r!l&&r!l&&!r
0000
$todo_keywords{$kw} || $done_keywords{$kw}
l!l&&r!l&&!r
000
MC/DC0%0/3
$todo_keywords{$kw} or $done_keywords{$kw} xor $args{"is_todo"}
$todo_keywords{$kw} $done_keywords{$kw} $args{"is_todo"}
Truth table0%0/6
A || B xor C
A= $todo_keywords{$kw}B= $done_keywords{$kw}C= $args{"is_todo"}
ABCresult
1X10
0110
1X01
0101
0011
0000
239 }
240 0 ▶ if (defined $args{is_done}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"is_done"}) 0 0 0
241 0 ▶ my $kw = $curtodokw // '';
Condition0%0/2
$curtodokw // ""
l!l
00
MC/DC0%0/1
$curtodokw // ""
$curtodokw
Truth table0%0/2
A
A= $curtodokw
Aresult
11
00
242 0 ▶ last unless $kw;
Branch: 1 branch
BranchTrueFalseTotal
unless $kw 0 0 0
243 0 ▶ last if ( $done_keywords{$kw} xor $args{is_done} );
Branch: 1 branch
BranchTrueFalseTotal
if $done_keywords{$kw} xor $args{"is_done"} 0 0 0
Condition0%0/4
$done_keywords{$kw} xor $args{"is_done"}
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
$done_keywords{$kw} xor $args{"is_done"}
$done_keywords{$kw} $args{"is_done"}
Truth table0%0/4
A xor B
A= $done_keywords{$kw}B= $args{"is_done"}
ABresult
110
101
011
000
244 }
245 0 ▶ if (defined $args{match}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"match"}) 0 0 0
246 0 ▶ last unless _match($curhl, $args{match});
Branch: 1 branch
BranchTrueFalseTotal
unless _match($curhl, $args{"match"}) 0 0 0
247 }
248 0 ▶ if ($args{has_tags}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"has_tags"}) 0 0 0
249 0 ▶ last unless $curtags;
Branch: 1 branch
BranchTrueFalseTotal
unless $curtags 0 0 0
250 0 for my $t (@{ $args{has_tags} }) {
251 0 ▶ last FILTER unless grep {$t eq $_} @$curtags;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$t eq $_;} @$curtags 0 0 0
252 }
253 }
254 0 ▶ if ($args{lacks_tags}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"lacks_tags"}) 0 0 0
255 0 for my $t (@{ $args{lacks_tags} }) {
256 0 ▶ last FILTER if grep {$t eq $_} @{$curtags//[]};
Branch: 1 branch
BranchTrueFalseTotal
if grep {$t eq $_;} @{$curtags // [];} 0 0 0
Condition0%0/2
$curtags // []
l!l
00
MC/DC0%0/1
$curtags // []
$curtags
Truth table0%0/2
A
A= $curtags
Aresult
11
00
257 }
258 }
259 0 ▶ if (defined $args{parent_match}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"parent_match"}) 0 0 0
260 0 ▶ last unless @parenthls;
Branch: 1 branch
BranchTrueFalseTotal
unless @parenthls 0 0 0
261 0 ▶ last unless _match($parenthls[-1][0], $args{parent_match});
Branch: 1 branch
BranchTrueFalseTotal
unless _match($parenthls[-1][0], $args{"parent_match"}) 0 0 0
262 }
263 0 ▶ if (defined $args{ascendant_match}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"ascendant_match"}) 0 0 0
264 0 for (@parenthls) {
265 0 do { $include++; last FILTER }
266 0 ▶ if _match($_->[0], $args{ascendant_match});
Branch: 1 branch
BranchTrueFalseTotal
if (_match($_->[0], $args{"ascendant_match"})) 0 0 0
267 }
268 0 last;
269 }
270 0 ▶ $curhl_filtered = 0 if $is_hl;
Branch: 1 branch
BranchTrueFalseTotal
if $is_hl 0 0 0
271  
272 0 $include = 1;
273 }
274 0 ▶ if ($include) {
Branch: 1 branch
BranchTrueFalseTotal
if ($include) 0 0 0
275 0 ▶ if ($args{return_array}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'return_array'}) { } 0 0 0
276 0 ▶ $out //= [];
Condition0%0/2
$out //= []
l!l
00
MC/DC0%0/1
$out //= []
$out
Truth table0%0/2
A
A= $out
Aresult
11
00
277 0 ▶ if ($is_hl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_hl) { } 0 0 0
278 0 push @$out, $line;
279 } else {
280 0 $out->[-1] .= $line;
281 }
282 } else {
283 0 $out .= $line;
284 }
285 }
286 }
287  
288 0 $out;
289 }
290  
291 2 1;
292 # ABSTRACT: Filter Org by headlines
293  
294 __END__
↑ Summary ListOrgHeadlines.pm »