diff --git a/Web/lib/MJB/Web.pm b/Web/lib/MJB/Web.pm index 752e74b..8134afc 100644 --- a/Web/lib/MJB/Web.pm +++ b/Web/lib/MJB/Web.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' ); diff --git a/Web/lib/MJB/Web/Controller/Dashboard.pm b/Web/lib/MJB/Web/Controller/Dashboard.pm index 545152d..cf0ec4e 100644 --- a/Web/lib/MJB/Web/Controller/Dashboard.pm +++ b/Web/lib/MJB/Web/Controller/Dashboard.pm @@ -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 $title!" ); + $c->redirect_to( $c->url_for( 'show_dashboard_blog_posts', { id => $blog->id } ) ); +} + 1; diff --git a/Web/templates/dashboard/blog_post_create.html.ep b/Web/templates/dashboard/blog_post_create.html.ep index b768d9d..d19482d 100644 --- a/Web/templates/dashboard/blog_post_create.html.ep +++ b/Web/templates/dashboard/blog_post_create.html.ep @@ -21,7 +21,7 @@ -
diff --git a/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm b/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm index db408a3..04dc9de 100644 --- a/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm +++ b/libs/MJB-Backend-Jekyll/lib/MJB/Backend/Jekyll.pm @@ -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;