More blog post listing.

master
Blog Manager Robot 3 years ago
parent b4b7ee19b7
commit adaf1b6d4c
  1. 5
      Web/lib/MJB/Web.pm
  2. 18
      Web/lib/MJB/Web/Controller/Dashboard.pm
  3. 47
      Web/templates/dashboard/blog_posts.html.ep
  4. 2
      libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm
  5. 65
      libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll/MarkdownFile.pm

@ -114,8 +114,9 @@ sub startup ($self) {
$auth->post( '/password' )->to('UserSettings#do_change_password' )->name('do_change_password' ); $auth->post( '/password' )->to('UserSettings#do_change_password' )->name('do_change_password' );
# Dashboard # Dashboard
$auth->get ( '/dashboard' )->to('Dashboard#index' )->name('show_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/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 # Blog Management
$auth->get ( '/blog/create' )->to('Blog#create' )->name('show_blog_create' ); $auth->get ( '/blog/create' )->to('Blog#create' )->name('show_blog_create' );

@ -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; 1;

@ -0,0 +1,47 @@
% layout 'standard', title => 'Dashboard', sb_active => 'dashboard';
<nav aria-label="breadcrumb" class="mt-3 mb-3">
<ol class="breadcrumb">
<li class="breadcrumb-item"><a href="<%= $c->url_for( 'show_dashboard' ) %>">Dashboard</a></li>
<li class="breadcrumb-item active" aria-current="page"><%= $blog->domain->name %></li>
</ol>
</nav>
% if ( $blog ) {
<h3 class="h3 mt-5 mb-3">My Websites</h3>
<table style="border: 1px solid #ccc" class="table mb-5">
<tbody>
<tr>
<th class="text-nowrap">Domain</th>
<th class="text-nowrap">Status Link</th>
</tr>
</thead>
<tbody>
<tr>
<td><a href="https://<%= $blog->domain->name %>"><%= $blog->domain->name %></a></td>
<td><a href="<%= $c->url_for( 'show_dashboard_blog', { id => $blog->id } ) %>">Manage Blog</a></td>
</tr>
</tbody>
</table>
% }
% if ( $blog_posts ) {
<h3 class="h3 mt-5 mb-3">My Posts</h3>
<table style="border: 1px solid #ccc" class="table mb-5">
<tbody>
<tr>
<th class="text-nowrap">Title</th>
<th class="text-nowrap">Date</th>
</tr>
</thead>
<tbody>
% for my $post ( @{$blog_posts} ) {
<tr>
<td><a href="<%= $c->url_for( 'show_dashboard_blog', { id => $blog->id } ) %>"><%= $post->headers->{title} %></a></td>
<td><a href="<%= $c->url_for( 'show_dashboard_blog', { id => $blog->id } ) %>"><%= $post->headers->{date} %></a></td>
</tr>
% }
</tbody>
</table>
% }

@ -104,7 +104,7 @@ sub get_title_of_post {
open my $lf, "<", $file open my $lf, "<", $file
or die "Failed to open $file for reading: $!"; or die "Failed to open $file for reading: $!";
while ( defined( my $line = <$lf> ) ) { while ( defined( my $line = <$lf> ) ) {
if ( $line =~ /^title: "(.+)"$/ ) { if ( $line =~ /^title: (.+)$/ ) {
close $lf; close $lf;
return $1; return $1;
} }

@ -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;
Loading…
Cancel
Save