diff --git a/Web/lib/MJB/Web.pm b/Web/lib/MJB/Web.pm index c6a2199..8c2d1d1 100644 --- a/Web/lib/MJB/Web.pm +++ b/Web/lib/MJB/Web.pm @@ -114,8 +114,9 @@ sub startup ($self) { $auth->post( '/password' )->to('UserSettings#do_change_password' )->name('do_change_password' ); # Dashboard - $auth->get ( '/dashboard' )->to('Dashboard#index' )->name('show_dashboard' ); - $auth->get ( '/dashboard/blog/:id' )->to('Dashboard#blog' )->name('show_dashboard_blog' ); + $auth->get ( '/dashboard' )->to('Dashboard#index' )->name('show_dashboard' ); + $auth->get ( '/dashboard/blog/:id' )->to('Dashboard#blog' )->name('show_dashboard_blog' ); + $auth->get ( '/dashboard/blog/:id/posts' )->to('Dashboard#blog_posts' )->name('show_dashboard_blog_posts' ); # Blog Management $auth->get ( '/blog/create' )->to('Blog#create' )->name('show_blog_create' ); diff --git a/Web/lib/MJB/Web/Controller/Dashboard.pm b/Web/lib/MJB/Web/Controller/Dashboard.pm index 2b93c00..13c96c6 100644 --- a/Web/lib/MJB/Web/Controller/Dashboard.pm +++ b/Web/lib/MJB/Web/Controller/Dashboard.pm @@ -21,4 +21,22 @@ sub blog ( $c ) { } } +# TODO: There is a lot of repetition here, check out making a chain +# like the auth ones, but for loading up the blog and ensuring +# the user has access to it? +sub blog_posts ( $c ) { + my $blog = $c->stash->{blog} = $c->db->blog( $c->param('id') ); + + if ( $blog->person->id ne $c->stash->{person}->id ) { + $c->render( + text => "Error: This blog isn't owned by you.", + status => 404, + format => 'txt', + ); + return; + } + + my $blog_posts = $c->stash->{blog_posts} = [ map { $_->read } @{$c->jekyll($blog->domain->name)->list_posts} ]; +} + 1; diff --git a/Web/templates/dashboard/blog_posts.html.ep b/Web/templates/dashboard/blog_posts.html.ep new file mode 100644 index 0000000..2504bba --- /dev/null +++ b/Web/templates/dashboard/blog_posts.html.ep @@ -0,0 +1,47 @@ +% layout 'standard', title => 'Dashboard', sb_active => 'dashboard'; + + + +% if ( $blog ) { +

My Websites

+ + + + + + + + + + + + + +
DomainStatus Link
<%= $blog->domain->name %>Manage Blog
+% } + +% if ( $blog_posts ) { +

My Posts

+ + + + + + + + + % for my $post ( @{$blog_posts} ) { + + + + + % } + +
TitleDate
<%= $post->headers->{title} %><%= $post->headers->{date} %>
+% } + diff --git a/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm b/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm index 86ea28e..a4c2489 100644 --- a/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm +++ b/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm @@ -104,7 +104,7 @@ sub get_title_of_post { open my $lf, "<", $file or die "Failed to open $file for reading: $!"; while ( defined( my $line = <$lf> ) ) { - if ( $line =~ /^title: "(.+)"$/ ) { + if ( $line =~ /^title: (.+)$/ ) { close $lf; return $1; } diff --git a/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll/MarkdownFile.pm b/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll/MarkdownFile.pm new file mode 100644 index 0000000..f0062c6 --- /dev/null +++ b/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll/MarkdownFile.pm @@ -0,0 +1,65 @@ +package MJB::Backend::Jekyll::MarkdownFile; +use Moo; +use YAML::XS qw( Load Dump ); + +# File path we are read/write from +has path => ( + is => 'ro', + required => 1, +); + +has headers => ( + is => 'rw', + default => sub { return +{} }, +); + +has markdown => ( + is => 'rw', +); + +sub read { + my ( $self ) = @_; + + # Ensure any content we alread have is discarded before reading. + $self->markdown( undef ); + $self->headers( { } ); + + open my $lf, "<", $self->path + or die "Failed to open " . $self->path . " for reading: $!"; + + my $sep_count = 0; + my ( $yaml, $markdown ) = ( undef, undef ); + + while ( defined( my $line = <$lf> ) ) { + $sep_count++ if $line =~ /^---$/; + + if ( $sep_count < 2 ) { + $yaml .= $line; + } else { + $markdown .= $line; + } + } + + $self->headers( Load($yaml) ); + $self->markdown( $markdown ); + + return $self; +} + +sub write { + my ( $self, $file ) = @_; + + $file ||= $self->path; + + open my $sf, ">", $file + or die "Failed to open $file for writing: $!"; + + print $sf Dump($self->headers); + print $sf $self->markdown; + + close $sf; + + return $self; +} + +1;