package Statocles::Page::Document;
# ABSTRACT: Render documents into HTML
$Statocles::Page::Document::VERSION = '0.040';
use Statocles::Base 'Class';
with 'Statocles::Page';
use Text::Markdown;
use Statocles::Template;


has document => (
    is => 'ro',
    isa => InstanceOf['Statocles::Document'],
    required => 1,
);


has title => (
    is => 'ro',
    isa => Str,
    lazy => 1,
    default => sub { $_[0]->document->title },
);


has author => (
    is => 'ro',
    isa => Str,
    lazy => 1,
    default => sub { $_[0]->document->author || '' },
);


has '+last_modified' => (
    lazy => 1,
    default => sub {
        my ( $self ) = @_;
        $self->document->last_modified || Time::Piece->new;
    },
);


has _tags => (
    is => 'ro',
    isa => LinkArray,
    default => sub { [] },
    coerce => LinkArray->coercion,
    init_arg => 'tags',
);

has '+_links' => (
    default => sub { $_[0]->document->links },
);


sub content {
    my ( $self, %vars ) = @_;
    my $content = $self->document->content;
    my $tmpl = $self->site->theme->build_template( $self->path, $content );
    my $rendered = $tmpl->render( %vars, $self->vars );
    return $self->markdown->markdown( $rendered );
}


sub vars {
    my ( $self ) = @_;
    return (
        doc => $self->document,
    );
}


sub sections {
    my ( $self ) = @_;
    my @sections = split /\n---\n/, $self->document->content;
    return map { $self->markdown->markdown( $_ ) } @sections;
}


sub tags {
    my ( $self ) = @_;
    return @{ $self->_tags };
}

1;

__END__

=pod

=head1 NAME

Statocles::Page::Document - Render documents into HTML

=head1 VERSION

version 0.040

=head1 DESCRIPTION

This page class takes a single L<document|Statocles::Document> and renders it as HTML.

=head1 ATTRIBUTES

=head2 document

The L<document|Statocles::Document> this page will render.

=head2 title

The title of the page.

=head2 author

The author of the page.

=head2 last_modified

Get the last modified date of this page by checking the document.

=head2 tags

The tag links for this document. An array of L<link objects|Statocles::Link>. The
most important attributes are:

    text    - The text of the link
    href    - The page of the link

=head1 METHODS

=head2 content( vars )

Generate the document HTML by processing template directives and converting
Markdown. C<vars> is a set of name-value pairs to give to the template.

=head2 vars

Get the template variables for this page.

=head2 sections

Get a list of content divided into sections. The Markdown "---" marker divides
sections.

=head2 tags()

Get the list of tags for this page.

=head1 AUTHOR

Doug Bell <preaction@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2015 by Doug Bell.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
