From ef8290954b6c05c68f0595509a44f8ce0419e9b5 Mon Sep 17 00:00:00 2001 From: Blog Manager Robot Date: Sat, 29 Oct 2022 06:08:02 +0000 Subject: [PATCH] Blog page UI. --- Web/lib/MJB/Web.pm | 6 + Web/lib/MJB/Web/Controller/Dashboard.pm | 123 ++++++++++++++++++ Web/templates/dashboard/_blog_nav.html.ep | 15 +++ Web/templates/dashboard/blog_page.html.ep | 28 ++++ .../dashboard/blog_page_edit.html.ep | 25 ++++ Web/templates/dashboard/blog_pages.html.ep | 50 +++++++ .../lib/MJB/Backend/Jekyll.pm | 35 ++++- .../lib/MJB/Backend/Jekyll/MarkdownFile.pm | 30 +++++ 8 files changed, 311 insertions(+), 1 deletion(-) create mode 100644 Web/templates/dashboard/blog_page.html.ep create mode 100644 Web/templates/dashboard/blog_page_edit.html.ep create mode 100644 Web/templates/dashboard/blog_pages.html.ep diff --git a/Web/lib/MJB/Web.pm b/Web/lib/MJB/Web.pm index f925c7e..4fea1b6 100644 --- a/Web/lib/MJB/Web.pm +++ b/Web/lib/MJB/Web.pm @@ -132,6 +132,12 @@ sub startup ($self) { $auth->get ( '/dashboard/blog/:id/history' )->to('Dashboard#blog_history' )->name('show_dashboard_blog_history' ); $auth->post( '/dashboard/blog/:id/history' )->to('Dashboard#do_blog_history' )->name('do_dashboard_blog_history' ); + $auth->get ( '/dashboard/blog/:id/pages' )->to('Dashboard#blog_pages' )->name('show_dashboard_blog_pages' ); + $auth->get ( '/dashboard/blog/:id/page' )->to('Dashboard#blog_page' )->name('show_dashboard_blog_page' ); + $auth->post( '/dashboard/blog/:id/page' )->to('Dashboard#do_blog_page' )->name('do_dashboard_blog_page' ); + $auth->get ( '/dashboard/blog/:id/page/edit' )->to('Dashboard#blog_page_edit' )->name('show_dashboard_blog_page_edit' ); + $auth->post( '/dashboard/blog/:id/page/edit' )->to('Dashboard#do_blog_page_edit' )->name('do_dashboard_blog_page_edit' ); + # Blog Management $auth->get ( '/blog' )->to('Blog#create' )->name('show_blog_create' ); $auth->post( '/blog' )->to('Blog#do_create' )->name('do_blog_create' ); diff --git a/Web/lib/MJB/Web/Controller/Dashboard.pm b/Web/lib/MJB/Web/Controller/Dashboard.pm index 39141b9..97b9f27 100644 --- a/Web/lib/MJB/Web/Controller/Dashboard.pm +++ b/Web/lib/MJB/Web/Controller/Dashboard.pm @@ -392,5 +392,128 @@ sub do_blog_history ( $c ) { } } +sub blog_pages ( $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_pages = $c->stash->{blog_pages} = [ map { $_->read } @{$c->jekyll($blog->domain->name)->list_pages} ]; +} + +sub blog_page_edit ( $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; + } + + # This is a dumb-expensive way of loading the file. TODO, fix this. + my $blog_pages = $c->stash->{blog_pages} = [ map { $_->read } @{$c->jekyll($blog->domain->name)->list_pages} ]; + + my $rel_path = $c->param('file'); + + ( $c->stash->{blog_page} ) = grep { $_->rel_path eq $rel_path } @{$blog_pages}; + +} + +sub do_blog_page_edit ( $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 $jekyll = $c->jekyll($blog->domain->name); + + # This is a dumb-expensive way of loading the file. TODO, fix this. + my $blog_pages = $c->stash->{blog_pages} = [ map { $_->read } @{$jekyll->list_pages} ]; + + my $rel_path = $c->param('file'); + my $content = $c->param('pageContent'); + my $headers = $c->param('pageHeaders'); + + my ( $blog_page ) = grep { $_->rel_path eq $rel_path } @{$blog_pages}; + + $blog_page->set_headers_from_string( $headers ); + $blog_page->markdown( $content ); + + $jekyll->write_post( $blog_page ); + + my $build_job_id = $c->minion->enqueue( 'deploy_blog', [ $blog->id ], { + notes => { '_bid_' . $blog->id => 1 }, + priority => $blog->build_priority, + }); + $blog->create_related( 'builds', { job_id => $build_job_id } ); + + $c->flash( confirmation => "Updated Page " . $blog_page->filename . "!" ); + $c->redirect_to( $c->url_for( 'show_dashboard_blog_pages', { id => $blog->id } ) ); +} + +sub blog_page ( $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; + } +} + +sub do_blog_page ( $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 $path = $c->param('pagePath'); + my $content = $c->param('pageContent'); + my $headers = $c->param('pageHeaders'); + + my $jekyll = $c->jekyll($blog->domain->name); + + my $page = $jekyll->new_page( $path ); + + $page->set_headers_from_string( $headers ); + $page->markdown( $content ); + + $jekyll->write_post( $page ); + + my $build_job_id = $c->minion->enqueue( 'deploy_blog', [ $blog->id ], { + notes => { '_bid_' . $blog->id => 1 }, + priority => $blog->build_priority, + }); + $blog->create_related( 'builds', { job_id => $build_job_id } ); + + $c->flash( confirmation => "Created Page " . $page->filename . "!" ); + $c->redirect_to( $c->url_for( 'show_dashboard_blog_pages', { id => $blog->id } ) ); + + +} 1; diff --git a/Web/templates/dashboard/_blog_nav.html.ep b/Web/templates/dashboard/_blog_nav.html.ep index 671ff8c..6d13479 100644 --- a/Web/templates/dashboard/_blog_nav.html.ep +++ b/Web/templates/dashboard/_blog_nav.html.ep @@ -36,6 +36,21 @@ Post Editor % } + + + + % if ( $page eq 'page-create' ) { + + % } + % if ( $page eq 'page-edit' ) { + + % }