| 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($context, { entry => $entry, feed => $args->{feed} }); |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
open my $out, ">:encoding(euc-jp)", $path or $context->error("$path: $!"); |
|---|
| 33 |
print $out $body; |
|---|
| 34 |
close $out; |
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 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 templatize { |
|---|
| 45 |
my($self, $context, $vars) = @_; |
|---|
| 46 |
|
|---|
| 47 |
my $tt = $context->template(); |
|---|
| 48 |
$tt->process('namazu.tt', $vars, \my $out) |
|---|
| 49 |
or $context->error($tt->error); |
|---|
| 50 |
|
|---|
| 51 |
$out; |
|---|
| 52 |
} |
|---|
| 53 |
|
|---|
| 54 |
sub mknmz { |
|---|
| 55 |
my($self, $context) = @_; |
|---|
| 56 |
|
|---|
| 57 |
my $opt = $self->conf->{mknmz_opt} || ''; |
|---|
| 58 |
my $dir = $self->conf->{dir}; |
|---|
| 59 |
my $idx = $self->conf->{index} |
|---|
| 60 |
or $context->error("config: index is missing"); |
|---|
| 61 |
|
|---|
| 62 |
unless (-e $idx && -d _) { |
|---|
| 63 |
mkdir $idx, 0755 or $context->error("mkdir $idx: $!"); |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
my $code = $self->replace_code; |
|---|
| 67 |
system("mknmz --replace='$code' --output-dir=$idx --indexing-lang=ja --media-type='text/html' $opt $dir"); |
|---|
| 68 |
} |
|---|
| 69 |
|
|---|
| 70 |
sub replace_code { |
|---|
| 71 |
my $foo = <<'CODE'; $foo =~ s/\n\s+/ /g; return $foo; |
|---|
| 72 |
open my $fh, $_ |
|---|
| 73 |
or return util::vprint("$_: $!"); |
|---|
| 74 |
while (defined(my $foo = <$fh>)) { |
|---|
| 75 |
$foo =~ m!<link rel="self" type="text/html" href="(.*?)" />! |
|---|
| 76 |
and $_ = $1; |
|---|
| 77 |
} |
|---|
| 78 |
CODE |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
1; |
|---|
| 82 |
|
|---|
| 83 |
__END__ |
|---|
| 84 |
|
|---|
| 85 |
=head1 NAME |
|---|
| 86 |
|
|---|
| 87 |
Plagger::Plugin::Search::Namazu - Search Feed updates by Namazu |
|---|
| 88 |
|
|---|
| 89 |
=head1 SYNOPSIS |
|---|
| 90 |
|
|---|
| 91 |
- module: Search::Namazu |
|---|
| 92 |
config: |
|---|
| 93 |
dir: /home/miyagawa/plagger-namazu |
|---|
| 94 |
index: /var/namazu/index |
|---|
| 95 |
|
|---|
| 96 |
=head1 DESCRIPTION |
|---|
| 97 |
|
|---|
| 98 |
This plugin creates HTML files which can be indexed via Namazu. |
|---|
| 99 |
|
|---|
| 100 |
=head1 AUTHOR |
|---|
| 101 |
|
|---|
| 102 |
Tatsuhiko Miyagawa |
|---|
| 103 |
|
|---|
| 104 |
=head1 SEE ALSO |
|---|
| 105 |
|
|---|
| 106 |
L<Plagger>, L<http://www.namazu.org/> |
|---|
| 107 |
|
|---|
| 108 |
=cut |
|---|