| 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 = '2026-07-21'; # DATE | |||||||||||||||||||||||||||||||||||||||||
| 11 | 24 | our $DIST = 'Org-Parser'; # DIST | |||||||||||||||||||||||||||||||||||||||||
| 12 | 24 | our $VERSION = '0.563'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 96 | ▶ | sub parse { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse: called CC=14 SCAR=26.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 96 | my ($self, $arg, $opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 16 | 96 | ▶ | die "Please specify a defined argument to parse()\n" unless defined($arg); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 94 | ▶ | $opts //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts //= {}
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 94 | my $str; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 94 | my $srclabel = $opts->{_srclabel}; | |||||||||||||||||||||||||||||||||||||||||
| 22 | 94 | my $r = ref($arg); | |||||||||||||||||||||||||||||||||||||||||
| 23 | 94 | ▶ | if (!$r) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | 90 | $str = $arg; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 90 | ▶ | $srclabel //= "string"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$srclabel //= "string"
MC/DC100%1/1
$srclabel //= "string"
$srclabel
Truth table100%2/2
A
A= $srclabel
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | } elsif ($r eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | $str = join "", @$arg; | |||||||||||||||||||||||||||||||||||||||||
| 28 | 1 | ▶ | $srclabel //= "arrayref"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$srclabel //= "arrayref"
MC/DC0%0/1
$srclabel //= "arrayref"
$srclabel
Truth table50%1/2
A
A= $srclabel
| |||||||||||||||||||||||||||||||||||||||||||
| 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")
builtin::blessed($arg) and $arg->isa("IO::Handle")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | $str = join "", <$arg>; | |||||||||||||||||||||||||||||||||||||||||
| 31 | 1 | ▶ | $srclabel //= "filehandle"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$srclabel //= "filehandle"
MC/DC0%0/1
$srclabel //= "filehandle"
$srclabel
Truth table50%1/2
A
A= $srclabel
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | } elsif ($r eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 1 | my @chunks; | |||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | ▶ | while (defined(my $chunk = $arg->())) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 4 | push @chunks, $chunk; | |||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 1 | $str = join "", @chunks; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 1 | ▶ | $srclabel //= "code"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$srclabel //= "code"
MC/DC0%0/1
$srclabel //= "code"
$srclabel
Truth table50%1/2
A
A= $srclabel
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | ignore_unknown_drawers =>$opts->{ignore_unknown_drawers}, | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 93 | ); | |||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 13 | ▶ | sub parse_file { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_file: called CC=20 SCAR=38.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 53 | 13 | require File::Slurper; | |||||||||||||||||||||||||||||||||||||||||
| 54 | 13 | my ($self, $filename, $opts) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 55 | 13 | ▶ | $opts = {%$opts} if $opts; # shallow copy | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 13 | ▶ | $opts //= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$opts //= {}
MC/DC100%1/1
$opts //= {}
$opts
Truth table100%2/2
A
A= $opts
| |||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 13 | state $loaded; | |||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | 13 | my $content = File::Slurper::read_text($filename); | |||||||||||||||||||||||||||||||||||||||||
| 61 | 13 | $opts->{_srclabel} = "file:$filename"; | |||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 13 | my $cf = $opts->{cache_file}; # old option, new option is 'cache' (automatic setting of cache file) | |||||||||||||||||||||||||||||||||||||||||
| 64 | 13 | my $doc; | |||||||||||||||||||||||||||||||||||||||||
| 65 | my $cache; # undef = no caching; 0 = not cached, should cache; 1 = cached | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 13 | ▶ | if (!$cf && ($opts->{cache} // $ENV{PERL_ORG_PARSER_CACHE})) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
not $cf and $opts->{"cache"} // $ENV{"PERL_ORG_PARSER_CACHE"}
$opts->{"cache"} // $ENV{"PERL_ORG_PARSER_CACHE"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | require Cwd; | |||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | require Digest::MD5; | |||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | my @dirs = ("$ENV{HOME}/.cache/perl-org-parser", $ENV{HOME}); | |||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | my $dir; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | for (@dirs) { | |||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if (-d $_) { $dir = $_; last } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | elsif (mkdir $_) { $dir = $_; last } | |||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | ▶ | die "Can't find a suitable cache directory" unless $dir; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | my $abs = Cwd::abs_path($filename) or die "Can't find $filename"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | my $base = $abs; $base =~ s!.+/!!; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | $cf = "$dir/$base.".Digest::MD5::md5_hex($abs).".storable"; | |||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||
| 80 | 13 | ▶ | if ($cf) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 3 | require Storable; | |||||||||||||||||||||||||||||||||||||||||
| 82 | 3 | ▶ | $cache = !!((-e $cf) && (-M $cf) <= (-M $filename)); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
-e $cf && -M $cf <= -M $filename
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
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 3 | ▶ | if ($cache) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | eval { | |||||||||||||||||||||||||||||||||||||||||
| 85 | 1 | $doc = Storable::retrieve($cf); | |||||||||||||||||||||||||||||||||||||||||
| 86 | 1 | ▶ | $doc->load_element_modules unless $loaded++; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 1 | $doc->{_srclabel} = " (from cached file:$cf)"; | |||||||||||||||||||||||||||||||||||||||||
| 88 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 90 | 0 | warn "Failed retrieving document from cache: $@, reparsing ..."; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 0 | $cache = 0; | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 13 | ▶ | $doc = $self->parse($content, $opts) unless $cache; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 12 | ▶ | if (defined($cache) && !$cache) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $cache and not $cache
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
| |||||||||||||||||||||||||||||||||||||||||||
| 98 | 1 | require Storable; | |||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | for ($doc->find('Timestamp')) { | |||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | $_->clear_parse_result; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | Storable::store($doc, $cf); | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | 12 | $doc; | |||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | 24 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 109 | # ABSTRACT: Parse Org documents | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | __END__ | ||||||||||||||||||||||||||||||||||||||||||