Plagger/Plugin/Search/Namazu.pm untested

filter: Statementstmt 0.0% 0 / 40 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 95 CC 4 SCAR 17.6
CC
4
Cov
0%
feed
12.0
mknmz
6.0
register
2.0
CRAP 5.8
1 package Plagger::Plugin::Search::Namazu;
2 use strict;
3 use base qw( Plagger::Plugin );
4  
5 use File::Spec;
6  
7 sub register {
8 my($self, $context) = @_;
9 $context->register_hook(
10 $self,
11 'publish.feed' => \&feed,
12 'publish.finalize' => \&mknmz,
13 );
14 }
15  
16 sub feed {
17 my($self, $context, $args) = @_;
18  
19 my $dir = $self->conf->{dir};
20 unless (-e $dir && -d _) {
21 mkdir $dir, 0755 or $context->error("mkdir $dir: $!");
22 }
23  
24 for my $entry ($args->{feed}->entries) {
25 my $file = $entry->id_safe . '.html';
26 my $path = File::Spec->catfile($dir, $file);
27 $context->log(info => "writing output to $path");
28  
29 my $body = $self->templatize('namazu.tt', { entry => $entry, feed => $args->{feed} });
30  
31 # save output as EUC-JP so Namazu can easily handle
32 open my $out, ">:encoding(euc-jp)", $path or $context->error("$path: $!");
33 print $out $body;
34 close $out;
35  
36 # "touch" with the entry date if set. otherwise use now
37 my $now = time;
38 my $time = eval { $entry->date->epoch };
39 $time = $now if !$time or $time >= $now;
40 utime $time, $time, $path;
41 }
42 }
43  
44 sub mknmz {
45 my($self, $context) = @_;
46  
47 my $opt = $self->conf->{mknmz_opt} || '';
48 my $dir = $self->conf->{dir};
49 my $idx = $self->conf->{index}
50 or $context->error("config: index is missing");
51  
52 unless (-e $idx && -d _) {
53 mkdir $idx, 0755 or $context->error("mkdir $idx: $!");
54 }
55  
56 my $code = $self->replace_code;
57 system("mknmz --replace='$code' --output-dir=$idx --indexing-lang=ja --media-type='text/html' $opt $dir");
58 }
59  
60 sub replace_code {
61 my $foo = <<'CODE'; $foo =~ s/\n\s+/ /g; return $foo;
62 open my $fh, $_
63 or return util::vprint("$_: $!");
64 while (defined(my $foo = <$fh>)) {
65 $foo =~ m!<link rel="self" type="text/html" href="(.*?)" />!
66 and $_ = $1;
67 }
68 CODE
69 }
70  
71 1;
72  
73 __END__