sub
register {
my
(
$self
,
$context
) =
@_
;
$context
->register_hook(
$self
,
'publish.feed'
=> \
&feed
,
'publish.finalize'
=> \
&mknmz
,
);
}
sub
feed {
my
(
$self
,
$context
,
$args
) =
@_
;
my
$dir
=
$self
->conf->{dir};
unless
(-e
$dir
&& -d _) {
mkdir
$dir
, 0755 or
$context
->error(
"mkdir $dir: $!"
);
}
for
my
$entry
(
$args
->{feed}->entries) {
my
$file
=
$entry
->id_safe .
'.html'
;
my
$path
= File::Spec->catfile(
$dir
,
$file
);
$context
->
log
(
info
=>
"writing output to $path"
);
my
$body
=
$self
->templatize(
'namazu.tt'
, {
entry
=>
$entry
,
feed
=>
$args
->{feed} });
open
my
$out
,
">:encoding(euc-jp)"
,
$path
or
$context
->error(
"$path: $!"
);
print
$out
$body
;
close
$out
;
my
$now
=
time
;
my
$time
=
eval
{
$entry
->date->epoch };
$time
=
$now
if
!
$time
or
$time
>=
$now
;
utime
$time
,
$time
,
$path
;
}
}
sub
mknmz {
my
(
$self
,
$context
) =
@_
;
my
$opt
=
$self
->conf->{mknmz_opt} ||
''
;
my
$dir
=
$self
->conf->{dir};
my
$idx
=
$self
->conf->{
index
}
or
$context
->error(
"config: index is missing"
);
unless
(-e
$idx
&& -d _) {
mkdir
$idx
, 0755 or
$context
->error(
"mkdir $idx: $!"
);
}
my
$code
=
$self
->replace_code;
system
(
"mknmz --replace='$code' --output-dir=$idx --indexing-lang=ja --media-type='text/html' $opt $dir"
);
}
sub
replace_code {
my
$foo
=
<<'CODE'; $foo =~ s/\n\s+/ /g; return $foo;
open my $fh, $_
or return util::vprint("$_: $!");
while (defined(my $foo = <$fh>)) {
$foo =~ m!<link rel="self" type="text/html" href="(.*?)" />!
and $_ = $1;
}
CODE
}
1;