blib/lib/Org/Parser/Tiny.pm

filter: Statementstmt 58.9% 82 / 139 Branchbran 35.2% 24 / 68 Conditioncond 42.1% 8 / 19 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 50.0% 11 / 22 Podpod 100% 3 / 3 totaltotal 50.1% 131 / 261 CC 43 SCAR 27.4
CC
43
Cov
50%
parse
42.2
_parse
22.2
parse_file
12.0
CRAP 15.5
↑ Summary
1 ## no critic: Modules::RequireExplicitPackage
2  
3 2 ▶use 5.010001;
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 package Org::Parser::Tiny;
8  
9 2 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10 2 our $DATE = '2020-02-07'; # DATE
11 2 our $DIST = 'Org-Parser-Tiny'; # DIST
12 2 our $VERSION = '0.005'; # VERSION
13  
14 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
15 0 my $class = shift;
16 0 bless {}, $class;
17 }
18  
19 1 ▶sub _parse {
Subroutine _parse: called CC=17 SCAR=31.0
20 1 my ($self, $lines, $opts) = @_;
21  
22 # stage 1: get todo keywords
23  
24 1 my @undone_keywords;
25 my @done_keywords;
26 1 my $linenum = 0;
27 1 ▶ while ($linenum < @$lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($linenum < @$lines) 17 1 18
28 17 my $line = $lines->[$linenum];
29 17 $linenum++;
30 17 ▶ if ($line =~ s/^#\+TODO:\s*//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/^#\+TODO:\s*//) 0 17 17
31 0 ▶ my ($undone_keywords, $done_keywords) =
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ /^\s*(.*?)\s*\|\s*(.*?)\s*$/ or die "Line ${linenum}: Invalid #+TODO: please use ... | ..." 0 0 0
32 $line =~ /^\s*(.*?)\s*\|\s*(.*?)\s*$/
33 or die "Line $linenum: Invalid #+TODO: please use ... | ...";
34 0 ▶ while ($undone_keywords =~ /(\w+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($undone_keywords =~ /(\w+)/g) 0 0 0
35 0 push @undone_keywords, $1;
36 }
37 0 ▶ while ($done_keywords =~ /(\w+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($done_keywords =~ /(\w+)/g) 0 0 0
38 0 push @done_keywords, $1;
39 }
40 }
41 }
42 1 ▶ @undone_keywords = ("TODO") unless @undone_keywords;
Branch: 1 branch
BranchTrueFalseTotal
unless @undone_keywords 1 0 1
43 1 ▶ @done_keywords = ("DONE") unless @done_keywords;
Branch: 1 branch
BranchTrueFalseTotal
unless @done_keywords 1 0 1
44 1 my $undone_re = join("|", @undone_keywords); $undone_re= qr/(?:$undone_re)/;
45 1 my $done_re = join("|", @done_keywords ); $done_re = qr/(?:$done_re)/;
46  
47 # stage 2: build nodes
48  
49 # a linear list of nodes
50 1 my @nodes = (
51 Org::Parser::Tiny::Node::Document->new(),
52 );
53  
54 1 $linenum = 0;
55 1 ▶ while ($linenum < @$lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($linenum < @$lines) 17 1 18
56 17 my $line = $lines->[$linenum];
57 17 $linenum++;
58 17 ▶ if ($line =~ /^(\*+) (.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^(\*+) (.*)/) { } 11 6 17
59 #say "D: got headline $line";
60 11 my $level = length($1);
61 11 my $title = $2;
62 11 my $node = Org::Parser::Tiny::Node::Headline->new(
63 _str => $line,
64 level => $level,
65 );
66  
67 # extract todo state
68 11 ▶ if ($title =~ s/\s*($undone_re)\s+//) {
Branch: 2 branches
BranchTrueFalseTotal
if ($title =~ s/\s*($undone_re)\s+//) { } 0 11 11
elsif ($title =~ s/\s*($done_re)\s+//) { } 0 11 11
69 0 $node->{is_todo} = 1;
70 0 $node->{is_done} = 0;
71 0 $node->{todo_state} = $1;
72 } elsif ($title =~ s/\s*($done_re)\s+//) {
73 0 $node->{is_todo} = 1;
74 0 $node->{is_done} = 1;
75 0 $node->{todo_state} = $1;
76 } else {
77 11 $node->{is_todo} = 0;
78 11 $node->{is_done} = 0;
79 11 $node->{todo_state} = "";
80 }
81  
82 # extract tags
83 11 ▶ if ($title =~ s/\s+:((?:\w+:)+)$//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title =~ s/\s+:((?:\w+:)+)$//) 1 10 11
84 1 my $tags = $1;
85 1 my @tags;
86 1 ▶ while ($tags =~ /(\w+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tags =~ /(\w+)/g) 1 1 2
87 1 push @tags, $1;
88 }
89 1 $node->{tags} = \@tags;
90 }
91  
92 11 $node->{title} = $title;
93  
94 # find the first node which has the lower level (or the root node)
95 # as the parent node
96 11 my $i = $#nodes;
97 11 ▶ while ($i >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 44 0 44
98 44 ▶ if ($i == 0 || $nodes[$i]{level} < $level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0 or $nodes[$i]{"level"} < $level) 11 33 44
Condition100%3/3
$i == 0 or $nodes[$i]{"level"} < $level
l!l&&r!l&&!r
5633
MC/DC100%2/2
$i == 0 or $nodes[$i]{"level"} < $level
$i == 0 $nodes[$i]{"level"} < $level
Truth table100%3/3
A or B
A= $i == 0B= $nodes[$i]{"level"} < $level
ABresult
1X1
011
000
99 11 $node->{parent} = $nodes[$i];
100 11 push @{ $nodes[$i]{children} }, $node;
101 11 last;
102 }
103 33 $i--;
104 }
105 11 push @nodes, $node;
106 } else {
107 6 $nodes[-1]{preamble} .= $line;
108 }
109 }
110  
111 1 $nodes[0];
112 }
113  
114 1 ▶sub parse {
Subroutine parse: called CC=10 SCAR=37.4
Pod documented
115 1 my ($self, $arg, $opts) = @_;
116 1 ▶ die "Please specify a defined argument to parse()\n" unless defined($arg);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 0 1 1
117  
118 1 ▶ $opts ||= {};
Condition50%1/2
$opts ||= {}
l!l
01
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
119  
120 1 my $lines;
121 1 my $r = ref($arg);
122 1 ▶ if (!$r) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $r) { } 1 0 1
elsif ($r eq 'ARRAY') { } 0 0 0
elsif ($r eq 'GLOB' or blessed($arg) and $arg->isa('IO::Handle')) { } 0 0 0
elsif ($r eq 'CODE') { } 0 0 0
123 1 $lines = [split /^/, $arg];
124 } elsif ($r eq 'ARRAY') {
125 0 $lines = [@$arg];
126 ▶ } elsif ($r eq 'GLOB' || blessed($arg) && $arg->isa('IO::Handle')) {
Condition0%0/6
$r eq "GLOB" or blessed($arg) and $arg->isa("IO::Handle")
l!l&&r!l&&!r
000
blessed($arg) and $arg->isa("IO::Handle")
!ll&&!rl&&r
000
MC/DC0%0/3
$r eq "GLOB" or blessed($arg) and $arg->isa("IO::Handle")
$r eq "GLOB" blessed($arg) $arg->isa("IO::Handle")
Truth table0%0/4
A or B and C
A= $r eq "GLOB"B= blessed($arg)C= $arg->isa("IO::Handle")
ABCresult
1XX1
0111
00X0
0100
127 #$lines = split(/^/, join("", <$arg>));
128 0 $lines = [<$arg>];
129 } elsif ($r eq 'CODE') {
130 0 my @chunks;
131 0 ▶ while (defined(my $chunk = $arg->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $chunk = &$arg())) 0 0 0
132 0 push @chunks, $chunk;
133 }
134 0 $lines = [split /^/, (join "", @chunks)];
135 } else {
136 0 die "Invalid argument, please supply a ".
137 "string|arrayref|coderef|filehandle\n";
138 }
139 1 $self->_parse($lines, $opts);
140 }
141  
142 0 ▶sub parse_file {
Subroutine parse_file: not called CC=3 SCAR=24.8
Pod documented
143 0 my ($self, $filename, $opts) = @_;
144 0 ▶ $opts ||= {};
Condition0%0/2
$opts ||= {}
l!l
00
MC/DC0%0/1
$opts ||= {}
$opts
Truth table0%0/2
A
A= $opts
Aresult
11
00
145  
146 0 my $content = do {
147 0 ▶ open my($fh), "<", $filename or die "Can't open $filename: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $filename or die "Can't open ${filename}: $!\n" 0 0 0
148 0 local $/;
149 0 scalar(<$fh>);
150 };
151  
152 0 $self->parse($content, $opts);
153 }
154  
155  
156 # abstract class: Org::Parser::Tiny::Node
157 package Org::Parser::Tiny::Node;
158  
159 12 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
160 12 my ($class, %args) = @_;
161 12 ▶ $args{children} //= [];
Condition50%1/2
$args{"children"} //= []
l!l
012
MC/DC0%0/1
$args{"children"} //= []
$args{"children"}
Truth table50%1/2
A
A= $args{"children"}
Aresult
11
00
162 12 bless \%args, $class;
163 }
164  
165 0 ▶sub parent {
Subroutine parent: not called CC=2 SCAR=17.9
166 0 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
167 0 $_[0]{parent} = $_[1];
168 }
169 0 $_[0]{parent};
170 }
171  
172 11 ▶sub children {
Subroutine children: called CC=3 SCAR=13.1
173 11 ▶ if (@_ > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 11 11
174 0 $_[0]{children} = $_[1];
175 }
176 11 ▶ $_[0]{children} || [];
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"children"} or [] 11 0 11
177 }
178  
179 0 ▶sub as_string { $_[0]{_str} }
Subroutine as_string: not called CC=1 SCAR=6.9
180  
181 8 ▶sub children_as_string { join("", map { $_->as_string } @{ $_[0]->children }) }
Subroutine children_as_string: called CC=1 SCAR=0.0
182  
183 # abstract class: Org::Parser::Tiny::HasPreamble
184 package Org::Parser::Tiny::Node::HasPreamble;
185  
186 2 our @ISA = qw(Org::Parser::Tiny::Node);
187  
188 12 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
189 12 my ($class, %args) = @_;
190 12 ▶ $args{preamble} //= "";
Condition50%1/2
$args{"preamble"} //= ""
l!l
012
MC/DC0%0/1
$args{"preamble"} //= ""
$args{"preamble"}
Truth table50%1/2
A
A= $args{"preamble"}
Aresult
11
00
191 12 $class->SUPER::new(%args);
192 }
193  
194  
195 # class: Org::Parser::Tiny::Document: top level node
196 package Org::Parser::Tiny::Node::Document;
197  
198 2 our @ISA = qw(Org::Parser::Tiny::Node::HasPreamble);
199  
200 0 ▶sub as_string {
Subroutine as_string: not called CC=1 SCAR=6.9
201 0 $_[0]->{preamble} . $_[0]->children_as_string;
202 }
203  
204  
205 # class: Org::Parser::Tiny::Node::Headline: headline with its content
206 package Org::Parser::Tiny::Node::Headline;
207  
208 2 our @ISA = qw(Org::Parser::Tiny::Node::HasPreamble);
209  
210 0 ▶sub level {
Subroutine level: not called CC=2 SCAR=17.9
211 0 ▶ if (@_ > 1) { undef $_[0]{_str}; $_[0]{level} = $_[1] }
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
212 0 $_[0]{level};
213 }
214  
215 0 ▶sub title {
Subroutine title: not called CC=2 SCAR=17.9
216 0 ▶ if (@_ > 1) { undef $_[0]{_str}; $_[0]{title} = $_[1] }
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
217 0 $_[0]{title};
218 }
219  
220 0 ▶sub is_todo {
Subroutine is_todo: not called CC=2 SCAR=17.9
221 0 ▶ if (@_ > 1) { undef $_[0]{_str}; $_[0]{is_todo} = $_[1] }
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
222 0 $_[0]{is_todo};
223 }
224  
225 0 ▶sub is_done {
Subroutine is_done: not called CC=2 SCAR=17.9
226 0 ▶ if (@_ > 1) { undef $_[0]{_str}; $_[0]{is_done} = $_[1] }
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
227 0 $_[0]{is_done};
228 }
229  
230 0 ▶sub todo_state {
Subroutine todo_state: not called CC=2 SCAR=17.9
231 0 ▶ if (@_ > 1) { undef $_[0]{_str}; $_[0]{todo_state} = $_[1] }
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
232 0 $_[0]{todo_state};
233 }
234  
235 0 ▶sub tags {
Subroutine tags: not called CC=3 SCAR=24.8
236 0 ▶ if (@_ > 1) { undef $_[0]{_str}; $_[0]{tags} = $_[1] }
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
237 0 ▶ $_[0]{tags} || [];
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{"tags"} or [] 0 0 0
238 }
239  
240 8 ▶sub header_as_string {
Subroutine header_as_string: called CC=4 SCAR=14.5
241 ($_[0]->{_str} //
242 join('',
243 "*" x $_[0]{level},
244 " ",
245 (length $_[0]{todo_state} ? "$_[0]{todo_state} " : ""),
246 "$_[0]{title}",
247 8 ▶ (defined $_[0]{tags} ? " :".join(":", @{ $_[0]{tags} }).":" : ""),
Branch: 2 branches
BranchTrueFalseTotal
length $_[0]{'todo_state'} ? : 0 1 1
defined $_[0]{'tags'} ? : 1 0 1
Condition100%2/2
$_[0]{"_str"} // join("", "*" x $_[0]{"level"}, " ", length $_[0]{"todo_state"} ? qq[$_[0]{"todo_state"} ] : "", qq[$_[0]{"title"}], defined $_[0]{"tags"} ? ' :' . join(":", @{$_[0]{"tags"};}) . ':' : "", "\n")
l!l
71
MC/DC100%1/1
$_[0]{"_str"} // join("", "*" x $_[0]{"level"}, " ", length $_[0]{"todo_state"} ? qq[$_[0]{"todo_state"} ] : "", qq[$_[0]{"title"}], defined $_[0]{"tags"} ? ' :' . join(":", @{$_[0]{"tags"};}) . ':' : "", "\n")
$_[0]{"_str"}
Truth table100%2/2
A
A= $_[0]{"_str"}
Aresult
11
00
248 "\n",
249 ));
250 }
251  
252 8 ▶sub as_string {
Subroutine as_string: called CC=1 SCAR=0.0
253 $_[0]->header_as_string .
254 $_[0]->{preamble} .
255 8 $_[0]->children_as_string;
256 }
257  
258 2 1;
259 # ABSTRACT: Parse Org documents with as little code (and no non-core deps) as possible
260  
261 __END__
↑ Summary