| 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
Condition33%1/3
-e $global or -e $local
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
| |||||||||||||||||||||||||||||
| 28 | 40 | ▶ | return $global if !-e $local; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | return $local if !-e $global; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 37 | 0 | ▶ | my $y_stat = stat($y) or die "Can't stat $y: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | return $x if $x_stat->mtime > $y_stat->mtime; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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/&/&/g; | |||||||||||||||||||||||||||
| 47 | 0 | $text =~ s/</</g; | |||||||||||||||||||||||||||
| 48 | 0 | $text =~ s/>/>/g; | |||||||||||||||||||||||||||
| 49 | 0 | $text =~ s/"/"/g; | |||||||||||||||||||||||||||
| 50 | 0 | $text =~ s/ / /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
| |||||||||||||||||||||||||||||
| 57 | 0 | ▶ | mkdir $res_dir or die "Can't create $res_dir: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 99 | 0 | ▶ | open my $handle, '<:encoding(utf8)', $tablet_file or die "Can't open $tablet_file: $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 106 | 2 | ▶ | return $tablet{$topic} if defined $tablet{$topic}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 4 | my $entry; | |||||||||||||||||||||||||||
| 126 | 4 | ▶ | while (my $line = <$tablet_handle>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 16636 | ▶ | $entry = $1 if $line =~ /^=head2 C<<< (.+) >>>$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 16636 | ▶ | $tablet{$entry} .= $line if defined $entry; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | 4 | ▶ | while (my ($key, $value) = each %tablet) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||