Parser.pm

filter: Statementstmt 77.2% 61 / 79 Branchbran 69.4% 25 / 36 Conditioncond 66.6% 20 / 30 MC/DCmcdc 43.7% 7 / 16 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 71.6% 121 / 169 SCAR 39.9
CC
33
Cov
73%
parse_file
47.0
parse
14.4
BEGIN
1.0
CRAP 54.2
« Element/Timestamp.pm ↑ Summary Element.pm »
1 package Org::Parser;
2  
3 24 ▶use 5.014; # compilation failure in older perls, RT#141560
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 24 ▶use Org::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 24 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 24 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10 24 our $DATE = '2023-08-05'; # DATE
11 24 our $DIST = 'Org-Parser'; # DIST
12 24 our $VERSION = '0.560'; # VERSION
13  
14 sub parse {
15 96 ▶ my ($self, $arg, $opts) = @_;
Subroutine parse: called CC=14 SCAR=26.7
Pod documented
16 96 ▶ die "Please specify a defined argument to parse()\n" unless defined($arg);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg 2 94 96
17  
18 94 ▶ $opts //= {};
Condition100%2/2
$opts //= {}
l!l
1381
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
19  
20 94 my $str;
21 94 my $srclabel = $opts->{_srclabel};
22 94 my $r = ref($arg);
23 94 ▶ if (!$r) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $r) { } 90 4 94
elsif ($r eq 'ARRAY') { } 1 3 4
elsif ($r eq 'GLOB' or builtin::blessed($arg) and $arg->isa('IO::Handle')) { } 1 2 3
elsif ($r eq 'CODE') { } 1 1 2
24 90 $str = $arg;
25 90 ▶ $srclabel //= "string";
Condition100%2/2
$srclabel //= "string"
l!l
1278
MC/DC100%1/1
$srclabel //= "string"
$srclabel
Truth table100%2/2
A
A= $srclabel
Aresult
11
00
26 } elsif ($r eq 'ARRAY') {
27 1 $str = join "", @$arg;
28 1 ▶ $srclabel //= "arrayref";
Condition50%1/2
$srclabel //= "arrayref"
l!l
01
MC/DC0%0/1
$srclabel //= "arrayref"
$srclabel
Truth table50%1/2
A
A= $srclabel
Aresult
11
00
29 ▶ } elsif ($r eq 'GLOB' || blessed($arg) && $arg->isa('IO::Handle')) {
Condition50%3/6
$r eq "GLOB" or builtin::blessed($arg) and $arg->isa("IO::Handle")
l!l&&r!l&&!r
102
builtin::blessed($arg) and $arg->isa("IO::Handle")
!ll&&!rl&&r
200
MC/DC33%1/3
$r eq "GLOB" or builtin::blessed($arg) and $arg->isa("IO::Handle")
$r eq "GLOB" builtin::blessed($arg) $arg->isa("IO::Handle")
Truth table50%2/4
A or B and C
A= $r eq "GLOB"B= builtin::blessed($arg)C= $arg->isa("IO::Handle")
ABCresult
1XX1
0111
00X0
0100
30 1 $str = join "", <$arg>;
31 1 ▶ $srclabel //= "filehandle";
Condition50%1/2
$srclabel //= "filehandle"
l!l
01
MC/DC0%0/1
$srclabel //= "filehandle"
$srclabel
Truth table50%1/2
A
A= $srclabel
Aresult
11
00
32 } elsif ($r eq 'CODE') {
33 1 my @chunks;
34 1 ▶ while (defined(my $chunk = $arg->())) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $chunk = &$arg())) 4 1 5
35 4 push @chunks, $chunk;
36 }
37 1 $str = join "", @chunks;
38 1 ▶ $srclabel //= "code";
Condition50%1/2
$srclabel //= "code"
l!l
01
MC/DC0%0/1
$srclabel //= "code"
$srclabel
Truth table50%1/2
A
A= $srclabel
Aresult
11
00
39 } else {
40 1 die "Invalid argument, please supply a ".
41 "string|arrayref|coderef|filehandle\n";
42 }
43 Org::Document->new(
44 _srclabel=>$srclabel,
45 from_string=>$str,
46 time_zone=>$opts->{time_zone},
47 ignore_unknown_settings=>$opts->{ignore_unknown_settings},
48 93 );
49 }
50  
51 sub parse_file {
52 13 ▶ require File::Slurper;
Subroutine parse_file: called CC=20 SCAR=38.5
Pod documented
53 13 my ($self, $filename, $opts) = @_;
54 13 ▶ $opts = {%$opts} if $opts; # shallow copy
Branch: 1 branch
BranchTrueFalseTotal
if $opts 3 10 13
55 13 ▶ $opts //= {};
Condition100%2/2
$opts //= {}
l!l
310
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
56  
57 13 state $loaded;
58  
59 13 my $content = File::Slurper::read_text($filename);
60 13 $opts->{_srclabel} = "file:$filename";
61  
62 13 my $cf = $opts->{cache_file}; # old option, new option is 'cache' (automatic setting of cache file)
63 13 my $doc;
64 my $cache; # undef = no caching; 0 = not cached, should cache; 1 = cached
65 13 ▶ if (!$cf && ($opts->{cache} // $ENV{PERL_ORG_PARSER_CACHE})) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $cf and $opts->{"cache"} // $ENV{"PERL_ORG_PARSER_CACHE"}) 0 13 13
Condition50%3/6
not $cf and $opts->{"cache"} // $ENV{"PERL_ORG_PARSER_CACHE"}
!ll&&!rl&&r
3100
$opts->{"cache"} // $ENV{"PERL_ORG_PARSER_CACHE"}
l!l&&r!l&&!r
0010
MC/DC0%0/3
not $cf and $opts->{"cache"} // $ENV{"PERL_ORG_PARSER_CACHE"}
not $cf $opts->{"cache"} $ENV{"PERL_ORG_PARSER_CACHE"}
Truth table50%2/4
not(A) and B // C
A= not $cfB= $opts->{"cache"}C= $ENV{"PERL_ORG_PARSER_CACHE"}
ABCresult
0XX0
1000
11X1
1011
66 0 require Cwd;
67 0 require Digest::MD5;
68 0 my @dirs = ("$ENV{HOME}/.cache/perl-org-parser", $ENV{HOME});
69 0 my $dir;
70 0 for (@dirs) {
71 0 ▶ if (-d $_) { $dir = $_; last }
Branch: 2 branches
BranchTrueFalseTotal
if (-d $_) { } 0 0 0
elsif (mkdir $_) { } 0 0 0
72 0 elsif (mkdir $_) { $dir = $_; last }
73 }
74 0 ▶ die "Can't find a suitable cache directory" unless $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless $dir 0 0 0
75 0 ▶ my $abs = Cwd::abs_path($filename) or die "Can't find $filename";
Branch: 1 branch
BranchTrueFalseTotal
my $abs = Cwd::abs_path($filename) or die "Can't find $filename" 0 0 0
76 0 my $base = $abs; $base =~ s!.+/!!;
77 0 $cf = "$dir/$base.".Digest::MD5::md5_hex($abs).".storable";
78 }
79 13 ▶ if ($cf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cf) 3 10 13
80 3 require Storable;
81 3 ▶ $cache = !!((-e $cf) && (-M $cf) <= (-M $filename));
Condition66%2/3
-e $cf && -M $cf <= -M $filename
!ll&&!rl&&r
021
MC/DC50%1/2
-e $cf && -M $cf <= -M $filename
-e $cf -M $cf <= -M $filename
Truth table66%2/3
A && B
A= -e $cfB= -M $cf <= -M $filename
ABresult
0X0
100
111
82 3 ▶ if ($cache) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache) 1 2 3
83 1 eval {
84 1 $doc = Storable::retrieve($cf);
85 1 ▶ $doc->load_element_modules unless $loaded++;
Branch: 1 branch
BranchTrueFalseTotal
unless $loaded++ 1 0 1
86 1 $doc->{_srclabel} = " (from cached file:$cf)";
87 };
88 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
89 0 warn "Failed retrieving document from cache: $@, reparsing ...";
90 0 $cache = 0;
91 }
92 }
93 }
94  
95 13 ▶ $doc = $self->parse($content, $opts) unless $cache;
Branch: 1 branch
BranchTrueFalseTotal
unless $cache 12 1 13
96 12 ▶ if (defined($cache) && !$cache) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cache and not $cache) 1 11 12
Condition100%3/3
defined $cache and not $cache
!ll&&!rl&&r
1011
MC/DC100%2/2
defined $cache and not $cache
defined $cache not $cache
Truth table100%3/3
A and not(B)
A= defined $cacheB= not $cache
ABresult
0X0
100
111
97 1 require Storable;
98 1 for ($doc->find('Timestamp')) {
99 0 $_->clear_parse_result;
100 }
101 1 Storable::store($doc, $cf);
102 }
103  
104 12 $doc;
105 }
106  
107 24 1;
108 # ABSTRACT: Parse Org documents
109  
110 __END__
111  
112 =pod
113  
114 =encoding UTF-8
115  
116 =head1 NAME
117  
118 Org::Parser - Parse Org documents
119  
120 =head1 VERSION
121  
122 This document describes version 0.560 of Org::Parser (from Perl distribution Org-Parser), released on 2023-08-05.
123  
124 =head1 SYNOPSIS
125  
126 use 5.010;
127 use Org::Parser;
128 my $orgp = Org::Parser->new();
129  
130 # parse a file
131 my $doc = $orgp->parse_file("$ENV{HOME}/todo.org");
132  
133 # parse a string
134 $doc = $orgp->parse(<<EOF);
135 #+TODO: TODO | DONE CANCELLED
136 <<<radio target>>>
137 * heading1a
138 ** TODO heading2a
139 SCHEDULED: <2011-03-31 Thu>
140 [[some][link]]
141 ** DONE heading2b
142 [2011-03-18 ]
143 this will become a link: radio target
144 * TODO heading1b *bold*
145 - some
146 - plain
147 - list
148 - [ ] with /checkbox/
149 * and
150 * sublist
151 * CANCELLED heading1c
152 + definition :: list
153 + another :: def
154 EOF
155  
156 # walk the document tree
157 $doc->walk(sub {
158 my ($el) = @_;
159 return unless $el->isa('Org::Element::Headline');
160 say "heading level ", $el->level, ": ", $el->title->as_string;
161 });
162  
163 will print something like:
164  
165 heading level 1: heading1a
166 heading level 2: heading2a
167 heading level 2: heading2b *bold*
168 heading level 1: heading1b
169 heading level 1: heading1c
170  
171 A command-line utility (in a separate distribution: L<App::OrgUtils>) is
172 available for debugging:
173  
174 % dump-org-structure ~/todo.org
175 Document:
176 Setting: "#+TODO: TODO | DONE CANCELLED\n"
177 RadioTarget: "<<<radio target>>>"
178 Text: "\n"
179 Headline: l=1
180 (title)
181 Text: "heading1a"
182 (children)
183 Headline: l=2 todo=TODO
184 (title)
185 Text: "heading2a"
186 (children)
187 Text: "SCHEDULED: "
188 ...
189  
190 =head1 DESCRIPTION
191  
192 This module parses Org documents. See http://orgmode.org/ for more details on
193 Org documents.
194  
195 See C<todo.org> in the distribution for the list of already- and not yet
196 implemented stuffs.
197  
198 =head1 ATTRIBUTES
199  
200 =head1 METHODS
201  
202 =head2 new()
203  
204 Create a new parser instance.
205  
206 =head2 $orgp->parse($str | $arrayref | $coderef | $filehandle, \%opts) => $doc
207  
208 Parse document (which can be contained in a scalar $str, an arrayref of lines
209 $arrayref, a subroutine which will be called for chunks until it returns undef,
210 or a filehandle).
211  
212 Returns L<Org::Document> object.
213  
214 If 'handler' attribute is specified, will call handler repeatedly during
215 parsing. See the 'handler' attribute for more details.
216  
217 Will die if there are syntax errors in documents.
218  
219 Known options:
220  
221 =over 4
222  
223 =item * time_zone => STR
224  
225 Will be passed to Org::Document's constructor.
226  
227 =back
228  
229 =head2 $orgp->parse_file($filename, \%opts) => $doc
230  
231 Just like parse(), but will load document from file instead.
232  
233 Known options (aside from those known by parse()):
234  
235 =over 4
236  
237 =item * cache => bool (default: from PERL_ORG_PARSER_CACHE, or 0)
238  
239 Since Org::Parser can spend some time to parse largish Org files, this is an
240 option to store the parse result (using L<Storable>). If caching is turned on,
241 then after the first parse, the result will be stored in:
242  
243 ~/.cache/perl-org-parser/<filename>.<md5-digest-of-file-absolute-path>.storable
244  
245 and subsequent calls to this function can directly use this cache, as long as
246 the cache is not stale.
247  
248 =back
249  
250 =head1 FAQ
251  
252 =head2 Why? Just as only perl can parse Perl, only org-mode can parse Org anyway!
253  
254 True. I'm only targetting good enough. As long as I can parse/process all my Org
255 notes and todo files, I have no complaints.
256  
257 =head2 It's too slow!
258  
259 Parser is completely regex-based at the moment (I plan to use L<Marpa> someday).
260 Performance is quite lousy but I'm not annoyed enough at the moment to overhaul
261 it.
262  
263 =head1 ENVIRONMENT
264  
265 =head2 PERL_ORG_PARSER_CACHE => bool
266  
267 Set default for C<cache> option in C<parse_file()>.
268  
269 =head1 HOMEPAGE
270  
271 Please visit the project's homepage at L<https://metacpan.org/release/Org-Parser>.
272  
273 =head1 SOURCE
274  
275 Source repository is at L<https://github.com/perlancar/perl-Org-Parser>.
276  
277 =head1 SEE ALSO
278  
279 L<Org::Document>
280  
281 =head1 AUTHOR
282  
283 perlancar <perlancar@cpan.org>
284  
285 =head1 CONTRIBUTORS
286  
287 =for stopwords Alex White Karl Williamson Steven Haryanto Tekki Trent Fisher Wong Meng Weng
288  
289 =over 4
290  
291 =item *
292  
293 Alex White <VVu@geekfarm.org>
294  
295 =item *
296  
297 Karl Williamson <khw@cpan.org>
298  
299 =item *
300  
301 Steven Haryanto <stevenharyanto@gmail.com>
302  
303 =item *
304  
305 Tekki <tekki@tekki.ch>
306  
307 =item *
308  
309 Trent Fisher <trent@cs.pdx.edu>
310  
311 =item *
312  
313 Wong Meng Weng <mengwong@pobox.com>
314  
315 =back
316  
317 =head1 CONTRIBUTING
318  
319  
320 To contribute, you can send patches by email/via RT, or send pull requests on
321 GitHub.
322  
323 Most of the time, you don't need to build the distribution yourself. You can
324 simply modify the code, then test via:
325  
326 % prove -l
327  
328 If you want to build the distribution (e.g. to try to install it locally on your
329 system), you can install L<Dist::Zilla>,
330 L<Dist::Zilla::PluginBundle::Author::PERLANCAR>,
331 L<Pod::Weaver::PluginBundle::Author::PERLANCAR>, and sometimes one or two other
332 Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond
333 that are considered a bug and can be reported to me.
334  
335 =head1 COPYRIGHT AND LICENSE
336  
337 This software is copyright (c) 2023, 2022, 2021, 2020, 2019, 2017, 2016, 2015, 2014, 2013, 2012, 2011 by perlancar <perlancar@cpan.org>.
338  
339 This is free software; you can redistribute it and/or modify it under
340 the same terms as the Perl 5 programming language system itself.
341  
342 =head1 BUGS
343  
344 Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Org-Parser>
345  
346 When submitting a bug or request, please include a test-file or a
347 patch to an existing test-file that illustrates the bug or desired
348 feature.
349  
350 =cut
« Element/Timestamp.pm ↑ Summary Element.pm »