Expanded the create blog.

master
Blog Manager Robot 3 years ago
parent 7c97d1ca16
commit fc90a21d16
  1. 13
      Web/lib/MJB/Web.pm
  2. 53
      Web/lib/MJB/Web/Controller/Dashboard.pm
  3. 4
      Web/templates/dashboard/blog_post_create.html.ep
  4. 52
      libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm

@ -114,12 +114,13 @@ 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/blog/:id/posts' )->to('Dashboard#blog_posts' )->name('show_dashboard_blog_posts' );
$auth->get ( '/dashboard/blog/:id/post/*mdfile' )->to('Dashboard#blog_post' )->name('show_dashboard_blog_post' );
$auth->post( '/dashboard/blog/:id/post/*mdfile' )->to('Dashboard#do_blog_post' )->name('do_dashboard_blog_post' );
$auth->get ( '/dashboard/blog/:id/post' )->to('Dashboard#blog_post_create' )->name('show_dashboard_blog_post_create' );
$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' );
$auth->get ( '/dashboard/blog/:id/post/*mdfile' )->to('Dashboard#blog_post' )->name('show_dashboard_blog_post' );
$auth->post( '/dashboard/blog/:id/post/*mdfile' )->to('Dashboard#do_blog_post' )->name('do_dashboard_blog_post' );
$auth->get ( '/dashboard/blog/:id/post' )->to('Dashboard#blog_post_create' )->name('show_dashboard_blog_post_create' );
$auth->post( '/dashboard/blog/:id/post' )->to('Dashboard#do_blog_post_create' )->name('do_dashboard_blog_post_create' );
# Blog Management
$auth->get ( '/blog/create' )->to('Blog#create' )->name('show_blog_create' );

@ -77,7 +77,7 @@ sub do_blog_post ( $c ) {
$post->headers->{title} = $title;
$post->headers->{date} = $date;
$jekyll->update_post( $post );
$jekyll->write_post( $post );
$c->minion->enqueue( 'deploy_blog', [ $blog->id ] );
@ -96,7 +96,58 @@ sub blog_post_create ( $c ) {
);
return;
}
}
sub _make_slug ( $date, $title ) {
my $slug;
if ( $date =~ /^(\d{4}-\d{2}-\d{2})/ ) {
$slug = $1;
}
s/[^a-zA-Z0-9_-]+/-/g, s/^[^a-zA-Z0-9]+//, s/[^a-zA-Z0-9]+$// for $title;
$slug .= "-$title.markdown";
return $slug;
}
sub do_blog_post_create ( $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 $title = $c->stash->{form_title} = $c->param('postTitle');
my $date = $c->stash->{form_date} = $c->param('postDate');
my $content = $c->stash->{form_content} = $c->param('postContent');
my $jekyll = $c->jekyll($blog->domain->name);
my $post = $c->stash->{post} = $jekyll->new_post( _make_slug( $date, $title ) );
$post->markdown( $content );
$post->headers->{title} = $title;
$post->headers->{date} = $date;
$post->headers->{layout} = 'post';
$jekyll->write_post( $post );
$c->minion->enqueue( 'deploy_blog', [ $blog->id ] );
$c->flash( confirmation => "Created <strong>$title</strong>!" );
$c->redirect_to( $c->url_for( 'show_dashboard_blog_posts', { id => $blog->id } ) );
}
1;

@ -21,7 +21,7 @@
</ul>
<form>
<form method="POST" action="<%= $c->url_for( 'do_dashboard_blog_post_create' ) %>">
<div class="mt-3 mb-3">
<label for="postTitle" class="col-sm-2 col-form-label">Title</label>
<input type="text" class="form-control" id="postTitle" name="postTitle" value="<%= $c->stash->{form_title} %>">
@ -38,7 +38,7 @@
</div>
<div class="mb-3">
<input type="submit" class="btn btn-primary btn-sm float-end" width="100%" value="Update Post">
<input type="submit" class="btn btn-primary btn-sm float-end" width="100%" value="Create New Post">
</div>
</form>

@ -105,6 +105,14 @@ sub get_post {
)->read;
}
sub new_post {
my ( $self, $filename ) = @_;
return MJB::Backend::Jekyll::MarkdownFile->new(
path => $self->repo_path . "/_posts/" . $filename,
);
}
sub get_title_of_post {
my ( $self, $file ) = @_;
@ -136,49 +144,7 @@ sub _post_path {
return $self->repo_path . "/_posts/" . $title . ".markdown";
}
sub create_post {
my ( $self, $headers, $content ) = @_;
# Check if the repo exists, and Update the repo if needed
$self->_ensure_repository_is_latest;
# Get the file path to write to.
my $post_path = $self->_post_path( $headers );
# Ensure the post doesn't exist
die "Error: Cannot create post that already exists at " . $post_path
if -f $post_path;
# Create the post
open my $sf, ">", $post_path
or die "Failed to open $post_path for writing: $!";
print $sf "---\n";
print $sf join( "\n", map { "$_: " . $headers->{$_} } keys %$headers );
print $sf "\n---\n";
print $sf $content;
close $sf;
# Add the file to git
$self->system_command( [ qw( git add ), $post_path ], {
chdir => $self->repo_path,
});
# Commit the file
$self->system_command( [ qw( git commit -m ), "Created " . $headers->{title} ], {
chdir => $self->repo_path,
});
# Push the repo to the store server
$self->system_command( [ qw( git push origin master ) ], {
chdir => $self->repo_path,
});
return 1;
}
sub update_post {
sub write_post {
my ( $self, $md_file ) = @_;
$md_file->write;

Loading…
Cancel
Save