blib/lib/App/Grok/Resource/Tablet.pm

filter: Statementstmt 52.1% 49 / 94 Branchbran 35.7% 15 / 42 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 75.0% 12 / 16 Podpod 100% 4 / 4 totaltotal 50.3% 81 / 161 CC 25 SCAR 30.0
CC
25
Cov
47%
tablet_update
72.0
_newer
20.0
tablet_fetch
9.9
CRAP 20.0
« blib/lib/App/Grok/Resource/Spec.pm ↑ Summary blib/lib/App/Grok/Resource/Functions.pm »
1 package App::Grok::Resource::Tablet;
2 40 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 40 $App::Grok::Resource::Tablet::AUTHORITY = 'cpan:HINRIK';
4 }
5 {
6 40 $App::Grok::Resource::Tablet::VERSION = '0.26';
7 }
8  
9 40 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 40 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 40 ▶use App::Grok::Common qw<data_dir download>;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 40 ▶use File::ShareDir qw<dist_dir>;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 40 ▶use File::Spec::Functions qw<catdir catfile>;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 40 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 40 ▶use base qw<Exporter>;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 40 our @EXPORT_OK = qw<tablet_index tablet_fetch tablet_locate tablet_update>;
18 40 our %EXPORT_TAGS = ( ALL => [@EXPORT_OK] );
19  
20 40 my %tablet;
21 40 my $tablet_file = _find_tablet_file();
22  
23 40 ▶sub _find_tablet_file {
Subroutine _find_tablet_file: called CC=5 SCAR=21.7
24 40 my $global = catfile(dist_dir('Perl6-Doc'), 'table_index.pod');
25 40 my $local = catfile(data_dir(), 'resources', 'tablet', 'perl_6_index_tablet.pod');
26  
27 40 ▶ die "Tablet file not found\n" if !-e $global && !-e $local;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $global or -e $local 0 40 40
Condition33%1/3
-e $global or -e $local
l!l&&r!l&&!r
4000
MC/DC0%0/2
-e $global or -e $local
-e $global -e $local
Truth table33%1/3
A or B
A= -e $globalB= -e $local
ABresult
1X1
011
000
28 40 ▶ return $global if !-e $local;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $local 40 0 40
29 0 ▶ return $local if !-e $global;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $global 0 0 0
30 0 return _newer($local, $global);
31 }
32  
33 0 ▶sub _newer {
Subroutine _newer: not called CC=4 SCAR=30.0
34 0 my ($x, $y) = @_;
35  
36 0 ▶ my $x_stat = stat($x) or die "Can't stat $x: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $x_stat = &App::Grok::Resource::Tablet::stat($x) or die "Can't stat ${x}: $!" 0 0 0
37 0 ▶ my $y_stat = stat($y) or die "Can't stat $y: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $y_stat = &App::Grok::Resource::Tablet::stat($y) or die "Can't stat ${y}: $!" 0 0 0
38 0 ▶ return $x if $x_stat->mtime > $y_stat->mtime;
Branch: 1 branch
BranchTrueFalseTotal
if $x_stat->mtime > $y_stat->mtime 0 0 0
39 0 return $y;
40 }
41  
42 0 ▶sub _to_text {
Subroutine _to_text: not called CC=1 SCAR=6.9
43 0 my $text = shift;
44 0 $text =~ s/<em>(.+?)<\/em>/$1/g;
45 0 $text =~ s/<.+?>//g;
46 0 $text =~ s/&amp;/&/g;
47 0 $text =~ s/&lt;/</g;
48 0 $text =~ s/&gt;/>/g;
49 0 $text =~ s/&quot;/"/g;
50 0 $text =~ s/&nbsp;/ /g;
51 0 return $text;
52 }
53  
54 0 ▶sub tablet_update {
Subroutine tablet_update: not called CC=8 SCAR=42.8
Pod documented
55 0 my $res_dir = catdir(data_dir(), 'resources', 'tablet');
56 0 ▶ if (!-d $res_dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $res_dir) 0 0 0
57 0 ▶ mkdir $res_dir or die "Can't create $res_dir: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
mkdir $res_dir or die "Can't create ${res_dir}: $!\n" 0 0 0
58  
59 }
60  
61 0 print "Downloading Perl 6 Index Tablet...\n";
62 0 my $content = download(
63 '(1/1) perl_6_index_tablet',
64 'http://www.perlfoundation.org/perl6/index.cgi?perl_6_index_tablet',
65 );
66  
67 0 my %help;
68 0 for my $line (split /\n/, $content) {
69 0 chomp $line;
70  
71 0 ▶ if ($line =~ /<li><strong>(.+?)<\/strong>(.+?)<\/li>/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ m[<li><strong>(.+?)</strong>(.+?)</li>]) 0 0 0
72 0 my ($item, $item_description)= (_to_text($1), _to_text($2) );
73 0 $item_description =~ s/^\s+//;
74 0 ▶ if ($help{$item}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($help{$item}) { } 0 0 0
75 0 $help{$item} .= "\n=item $item_description\n";
76 }
77 else {
78 0 $help{$item} = "=item $item_description\n";
79 }
80 }
81 }
82  
83 0 my $pod;
84 0 for my $item (sort keys %help) {
85 0 $pod .= "=head2 C<<< $item >>>\n\n=over\n\n" . $help{$item} . "\n=back\n\n";
86 }
87  
88 0 my $file = catfile($res_dir, 'perl_6_index_tablet.pod');
89 0 ▶ open my $fh, '>:encoding(utf8)', $file or die "Can't create $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">:encoding(utf8)", $file or die "Can't create ${file}: $!" 0 0 0
90 0 print $fh $pod;
91 0 close $fh;
92 0 return;
93 }
94  
95 2 ▶sub tablet_fetch {
Subroutine tablet_fetch: called CC=5 SCAR=22.9
Pod documented
96 2 my ($topic) = @_;
97
98 2 ▶ if ($topic eq 'tablet_index') {
Branch: 1 branch
BranchTrueFalseTotal
if ($topic eq "tablet_index") 0 2 2
99 0 ▶ open my $handle, '<:encoding(utf8)', $tablet_file or die "Can't open $tablet_file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $handle, "<:encoding(utf8)", $tablet_file or die "Can't open ${tablet_file}: $!" 0 0 0
100 0 my $pod = do { local $/ = undef; scalar <$handle> };
101 0 close $handle;
102 0 return $pod;
103 }
104  
105 2 ▶ _build_tablet() if !%tablet;
Branch: 1 branch
BranchTrueFalseTotal
unless %tablet 2 0 2
106 2 ▶ return $tablet{$topic} if defined $tablet{$topic};
Branch: 1 branch
BranchTrueFalseTotal
if defined $tablet{$topic} 0 2 2
107 2 return;
108 }
109  
110 2 ▶sub tablet_index {
Subroutine tablet_index: called CC=2 SCAR=7.2
Pod documented
111 2 ▶ _build_tablet() if !%tablet;
Branch: 1 branch
BranchTrueFalseTotal
unless %tablet 2 0 2
112 2 return keys %tablet;
113 }
114  
115 0 ▶sub tablet_locate {
Subroutine tablet_locate: not called CC=1 SCAR=6.9
Pod documented
116 0 return $tablet_file;
117 }
118  
119 4 ▶sub _build_tablet {
Subroutine _build_tablet: called CC=6 SCAR=17.9
120 4 my ($self) = @_;
121  
122 ## no critic (InputOutput::RequireBriefOpen)
123 4 ▶ open my $tablet_handle, '<', $tablet_file or die "Can't open '$tablet_file': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $tablet_handle, "<", $tablet_file or die "Can't open '${tablet_file}': $!" 4 0 4
124  
125 4 my $entry;
126 4 ▶ while (my $line = <$tablet_handle>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $tablet_handle)) 16636 4 16640
127 16636 ▶ $entry = $1 if $line =~ /^=head2 C<<< (.+) >>>$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^=head2 C<<< (.+) >>>$/ 2020 14616 16636
128 16636 ▶ $tablet{$entry} .= $line if defined $entry;
Branch: 1 branch
BranchTrueFalseTotal
if defined $entry 16560 76 16636
129 }
130 4 ▶ while (my ($key, $value) = each %tablet) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %tablet) 2020 4 2024
131 2020 $tablet{$key} = "=encoding utf8\n\n$value";
132 }
133  
134 4 return;
135 }
136  
137 40 1;
138  
139 =encoding utf8
140  
141 =head1 NAME
142  
143 App::Grok::Resource::Tablet - Perl 6 Tablet Index resource for grok
144  
145 =head1 SYNOPSIS
146  
147 use strict;
148 use warnings;
149 use App::Grok::Resource::Tablet qw<:ALL>;
150  
151 # a list of all entries in the tablet
152 my @index = tablet_index();
153  
154 # documentation for a tablet entry
155 my $pod = tablet_fetch('+');
156  
157 # filename where the tablet entry was found
158 my $file = tablet_locate('+');
159  
160 =head1 DESCRIPTION
161  
162 This resource looks up entries in the Perl 6 Tablet Index
163 (L<http://www.perlfoundation.org/perl6/index.cgi?perl_6_index_tablet>).
164  
165 =head1 FUNCTIONS
166  
167 =head2 C<tablet_update>
168  
169 Takes no arguments. Downloads the latest tablet into grok's data dir.
170  
171 =head2 C<tablet_index>
172  
173 Takes no arguments. Lists all entry names in the tablet.
174  
175 =head2 C<tablet_fetch>
176  
177 Takes an entry name as an argument. Returns the documentation for it.
178  
179 =head2 C<tablet_locate>
180  
181 Takes an entry name as an argument. Returns the name of the file where it
182 was found.
183  
184 =cut
185 __DATA__
« blib/lib/App/Grok/Resource/Spec.pm ↑ Summary blib/lib/App/Grok/Resource/Functions.pm »