package Plagger::Plugin::Publish::HTML;
use strict;
use warnings;
use base qw( Plagger::Plugin );

our $VERSION = '0.01';

use File::Spec;
use IO::File;

sub register {
    my($self, $context) = @_;
    $context->register_hook(
        $self,
        'publish.feed' => \&feed,
    );
}

sub feed {
    my ($self, $context, $args) = @_;

    # open file
    my $dir = $self->conf->{dir};
    unless (-e $dir && -d _) {
        mkdir $dir, 0755 or $context->error("mkdir $dir: $!");
    }
    my $file = Plagger::Util::filename_for($args->{feed}, $self->conf->{filename} || "%i.html");
    my $path = File::Spec->catfile($dir, $file);
    my $io = IO::File->new("> $path");

    # output feeds
    my $feed = $args->{feed};
    my $encoding = $self->conf->{encoding} || 'utf-8';
    my $body = $self->templatize('html.tt', { feed => $feed, encoding => $encoding });

    $io->printf("%s\n", $body);

    # log
    $context->log(
        info => sprintf(
            "Write to %s: %d entries",
            $path,
            $args->{feed}->count
        )
    );
}

1;

__END__

=head1 NAME

Plagger::Plugin::Publish::HTML - Output to HTML

=head1 SYNOPSIS

  - module: Publish::HTML
    config:
      dir: /var/www/hoge
      encoding: euc-jp
      filename: my_%t.html

=head1 DESCRIPTION

This plugin creates HTML.

Template is loaded from assets/plugins/Publish-HTML/html.tt .

=head1 CONFIG

=head2 dir

Directory to save html files in.

=head2 filename

Filename to be used to create html files. It defaults to C<%i.html>. It
supports the following format like printf():

=over 4

=item * %u url

=item * %l link

=item * %t title

=item * %i id

=back

=head1 AUTHOR

yhara

=head1 SEE ALSO

L<Plagger>

=cut
