/
Entry.pm
74 lines (57 loc) · 1.56 KB
/
Entry.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package Nim::Plugin::Render::Entry;
use Any::Moose;
use DateTime;
use URI::Template;
with 'Nim::Plugin';
has path => (
is => 'rw',
isa => 'Str',
default => '{path}',
);
has filename => (
is => 'rw',
isa => 'Str',
default => '{filename}.html',
);
use Carp;
no Any::Moose;
sub register {
my ($self, $context) = @_;
$context->register_hook(
$self,
'entry.render' => \&render,
);
}
sub render {
my ($self, $context, $entry) = @_;
my $template = URI::Template->new( $self->path );
my $dt = DateTime->from_epoch(
epoch => $entry->time,
time_zone => $context->conf->time_zone,
);
my $uri = $template->process(
path => $entry->path || '/',
filename => $entry->filename,
year => $entry->year,
month => $entry->month,
day => $entry->day,
);
my $dir = $context->conf->site_dir;
my @path = grep { $_ } split '/', $uri;
$dir = $dir->subdir(@path) if @path;
$template = URI::Template->new($self->filename);
$uri = $template->process(
path => $entry->path || '/',
filename => $entry->filename,
year => $entry->year,
month => $entry->month,
day => $entry->day,
);
my $file = $dir->file($uri);
$context->log->info('render: %s', $file);
$file->parent->mkpath unless -d $file->parent;
my $fh = $file->openw or croak "Can't write file: $!";
print $fh $entry->rendered_body;
$fh->close;
}
__PACKAGE__->meta->make_immutable;