File Coverage

blib/lib/App/Grok/Resource/Tablet.pm
Criterion Covered Total %
statement 26 87 29.8
branch 1 38 2.6
condition 1 3 33.3
subroutine 9 16 56.2
pod 4 4 100.0
total 41 148 27.7


line stmt bran cond sub pod time code
1             package App::Grok::Resource::Tablet;
2             BEGIN {
3 1     1   33 $App::Grok::Resource::Tablet::AUTHORITY = 'cpan:HINRIK';
4             }
5             {
6             $App::Grok::Resource::Tablet::VERSION = '0.26';
7             }
8              
9 1     1   5 use strict;
  1         2  
  1         32  
10 1     1   28 use warnings FATAL => 'all';
  1         3  
  1         46  
11 1     1   6 use App::Grok::Common qw<data_dir download>;
  1         2  
  1         57  
12 1     1   4 use File::ShareDir qw<dist_dir>;
  1         2  
  1         43  
13 1     1   4 use File::Spec::Functions qw<catdir catfile>;
  1         2  
  1         43  
14 1     1   1419 use File::stat;
  1         20682  
  1         8  
15              
16 1     1   83 use base qw<Exporter>;
  1         3  
  1         1243  
17             our @EXPORT_OK = qw<tablet_index tablet_fetch tablet_locate tablet_update>;
18             our %EXPORT_TAGS = ( ALL => [@EXPORT_OK] );
19              
20             my %tablet;
21             my $tablet_file = _find_tablet_file();
22              
23             sub _find_tablet_file {
24 1     1   8 my $global = catfile(dist_dir('Perl6-Doc'), 'table_index.pod');
25 1         367 my $local = catfile(data_dir(), 'resources', 'tablet', 'perl_6_index_tablet.pod');
26              
27 1 0 33     39 die "Tablet file not found\n" if !-e $global && !-e $local;
28 1 50       27 return $global if !-e $local;
29 0 0         return $local if !-e $global;
30 0           return _newer($local, $global);
31             }
32              
33             sub _newer {
34 0     0     my ($x, $y) = @_;
35              
36 0 0         my $x_stat = stat($x) or die "Can't stat $x: $!";
37 0 0         my $y_stat = stat($y) or die "Can't stat $y: $!";
38 0 0         return $x if $x_stat->mtime > $y_stat->mtime;
39 0           return $y;
40             }
41              
42             sub _to_text {
43 0     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             sub tablet_update {
55 0     0 1   my $res_dir = catdir(data_dir(), 'resources', 'tablet');
56 0 0         if (!-d $res_dir) {
57 0 0         mkdir $res_dir or die "Can't create $res_dir: $!\n";
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 0         if ($line =~ /<li><strong>(.+?)<\/strong>(.+?)<\/li>/) {
72 0           my ($item, $item_description)= (_to_text($1), _to_text($2) );
73 0           $item_description =~ s/^\s+//;
74 0 0         if ($help{$item}) {
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 0         open my $fh, '>:encoding(utf8)', $file or die "Can't create $file: $!";
90 0           print $fh $pod;
91 0           close $fh;
92 0           return;
93             }
94              
95             sub tablet_fetch {
96 0     0 1   my ($topic) = @_;
97            
98 0 0         if ($topic eq 'tablet_index') {
99 0 0         open my $handle, '<:encoding(utf8)', $tablet_file or die "Can't open $tablet_file: $!";
100 0           my $pod = do { local $/ = undef; scalar <$handle> };
  0            
  0            
101 0           close $handle;
102 0           return $pod;
103             }
104              
105 0 0         _build_tablet() if !%tablet;
106 0 0         return $tablet{$topic} if defined $tablet{$topic};
107 0           return;
108             }
109              
110             sub tablet_index {
111 0 0   0 1   _build_tablet() if !%tablet;
112 0           return keys %tablet;
113             }
114              
115             sub tablet_locate {
116 0     0 1   return $tablet_file;
117             }
118              
119             sub _build_tablet {
120 0     0     my ($self) = @_;
121              
122             ## no critic (InputOutput::RequireBriefOpen)
123 0 0         open my $tablet_handle, '<', $tablet_file or die "Can't open '$tablet_file': $!";
124              
125 0           my $entry;
126 0           while (my $line = <$tablet_handle>) {
127 0 0         $entry = $1 if $line =~ /^=head2 C<<< (.+) >>>$/;
128 0 0         $tablet{$entry} .= $line if defined $entry;
129             }
130 0           while (my ($key, $value) = each %tablet) {
131 0           $tablet{$key} = "=encoding utf8\n\n$value";
132             }
133              
134 0           return;
135             }
136              
137             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__
186             =head1 Perl 6 table index
187              
188             This is the POD version of http://www.perlfoundation.org/perl6/index.cgi?perl_6_index_tablet
189              
190             =head1 AUTHORS
191              
192             For authors of the original wiki place, see:
193             http://www.perlfoundation.org/perl6/index.cgi?action=revision_list;page_name=perl_table_index
194              
195             =head1 LICENSE
196              
197             Copyright (c) 2006-2010 under the same (always latest) license(s) used by the Perl 6 /src
198             branch of the Pugs trunk.
199              
200             =head1 Table index