From e938b1801a87d5cd761b582377f535d96f75697a Mon Sep 17 00:00:00 2001 From: Arnaud Levy <contact@arnaudlevy.com> Date: Thu, 21 Oct 2021 13:07:13 +0200 Subject: [PATCH] posts --- app/models/communication/website/page.rb | 22 +++-------- app/models/communication/website/post.rb | 22 +++++++++++ .../communication/website/with_github.rb | 39 +++++++++++++++++++ app/services/github.rb | 2 +- .../communication/website/pages/show.html.erb | 6 +-- .../website/posts/jekyll.html.erb | 8 ++++ .../communication/website/posts/show.html.erb | 7 ++-- 7 files changed, 81 insertions(+), 25 deletions(-) create mode 100644 app/models/communication/website/with_github.rb create mode 100644 app/views/admin/communication/website/posts/jekyll.html.erb diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb index ce2fd2b36..ad4e52d14 100644 --- a/app/models/communication/website/page.rb +++ b/app/models/communication/website/page.rb @@ -34,6 +34,7 @@ class Communication::Website::Page < ApplicationRecord include WithSlug + include Communication::Website::WithGithub belongs_to :university belongs_to :website, @@ -52,20 +53,11 @@ class Communication::Website::Page < ApplicationRecord validates :title, presence: true before_save :make_path - after_save :publish_to_github scope :ordered, -> { order(:position) } scope :recent, -> { order(updated_at: :desc).limit(5) } scope :root, -> { where(parent_id: nil) } - def content - @content ||= github.read_file_at "_pages/#{id}.html" - end - - def content_without_frontmatter - frontmatter.content - end - def has_children? children.any? end @@ -76,18 +68,14 @@ class Communication::Website::Page < ApplicationRecord protected - def github - @github ||= Github.with_site(website) - end - - def frontmatter - @frontmatter ||= FrontMatterParser::Parser.new(:md).call(content) - end - def make_path self.path = "#{parent&.path}/#{slug}".gsub('//', '/') end + def github_path + "_pages/#{github_file}" + end + def publish_to_github github.publish kind: :pages, file: "#{ id }.html", diff --git a/app/models/communication/website/post.rb b/app/models/communication/website/post.rb index e7d4f3883..3b1440215 100644 --- a/app/models/communication/website/post.rb +++ b/app/models/communication/website/post.rb @@ -26,6 +26,7 @@ # class Communication::Website::Post < ApplicationRecord include WithSlug + include Communication::Website::WithGithub belongs_to :university belongs_to :website, @@ -43,4 +44,25 @@ class Communication::Website::Post < ApplicationRecord def to_s "#{title}" end + + protected + + def github_file + "#{published_at.year}/#{published_at.month}/#{published_at.strftime "%Y-%m-%d"}-#{id}.html" + end + + def github_path + "_posts/#{github_file}" + end + + def publish_to_github + github.publish kind: :posts, + file: github_file, + title: to_s, + data: ApplicationController.render( + template: 'admin/communication/website/posts/jekyll', + layout: false, + assigns: { post: self } + ) + end end diff --git a/app/models/communication/website/with_github.rb b/app/models/communication/website/with_github.rb new file mode 100644 index 000000000..ed89d0e8c --- /dev/null +++ b/app/models/communication/website/with_github.rb @@ -0,0 +1,39 @@ +module Communication::Website::WithGithub + extend ActiveSupport::Concern + + included do + after_save :publish_to_github + end + + def content + @content ||= github.read_file_at github_path + end + + def frontmatter + @frontmatter ||= FrontMatterParser::Parser.new(:md).call(content) + end + + def content_without_frontmatter + frontmatter.content + end + + def github_file + "#{ id }.html" + end + + # Needs override + def github_path + '' + end + + protected + + def github + @github ||= Github.with_site(website) + end + + # Needs override + def publish_to_github + '' + end +end diff --git a/app/services/github.rb b/app/services/github.rb index 9b33158fd..eac664d34 100644 --- a/app/services/github.rb +++ b/app/services/github.rb @@ -12,8 +12,8 @@ class Github def publish(kind:, file:, title:, data:) local_directory = "tmp/jekyll/#{ kind }" - FileUtils.mkdir_p local_directory local_path = "#{ local_directory }/#{ file }" + Pathname(local_path).dirname.mkpath File.write local_path, data remote_file = "_#{ kind }/#{ file }" begin diff --git a/app/views/admin/communication/website/pages/show.html.erb b/app/views/admin/communication/website/pages/show.html.erb index 98963fb5e..a989bb177 100644 --- a/app/views/admin/communication/website/pages/show.html.erb +++ b/app/views/admin/communication/website/pages/show.html.erb @@ -9,13 +9,13 @@ <div class="card-body"> <p> <strong>Description</strong> - <%= @page.description %> </p> + <%= sanitize @page.description %> <p> <strong>Text</strong> </p> - <%= raw @page.text %> + <%= sanitize @page.text %> </div> </div> </div> @@ -27,7 +27,7 @@ <table class="<%= table_classes %>"> <tbody> <tr> - <td><%= Communication::Website::Page.human_attribute_name('slug') %></td> + <td width="150"><%= Communication::Website::Page.human_attribute_name('slug') %></td> <td><%= @page.slug %></td> </tr> <tr> diff --git a/app/views/admin/communication/website/posts/jekyll.html.erb b/app/views/admin/communication/website/posts/jekyll.html.erb new file mode 100644 index 000000000..6f0aab175 --- /dev/null +++ b/app/views/admin/communication/website/posts/jekyll.html.erb @@ -0,0 +1,8 @@ +--- +title: "<%= @post.title %>" +date: <%= @post.published_at %> UTC +slug: "<%= @post.slug %>" +description: "<%= @post.description %>" +text: "<%= @post.text %>" +--- +<%= @post.content_without_frontmatter.html_safe %> diff --git a/app/views/admin/communication/website/posts/show.html.erb b/app/views/admin/communication/website/posts/show.html.erb index 310601d1f..b7a299368 100644 --- a/app/views/admin/communication/website/posts/show.html.erb +++ b/app/views/admin/communication/website/posts/show.html.erb @@ -9,9 +9,8 @@ <div class="card-body"> <p> <strong><%= Communication::Website::Post.human_attribute_name('description') %></strong> - <%= sanitize @post.description %> </p> - + <%= sanitize @post.description %> <p> <strong><%= Communication::Website::Post.human_attribute_name('text') %></strong> </p> @@ -27,7 +26,7 @@ <table class="<%= table_classes %>"> <tbody> <tr> - <td><%= Communication::Website::Page.human_attribute_name('slug') %></td> + <td width="150"><%= Communication::Website::Page.human_attribute_name('slug') %></td> <td><%= @post.slug %></td> </tr> <tr> @@ -37,7 +36,7 @@ <% if @post.imported_post %> <tr> <td><%= t('communication.website.imported.from') %></td> - <td><a href="<%= @post.imported_post.url %>" target="_blank"><%= @post.imported_post.url %></a></td> + <td><a href="<%= @post.imported_post.url %>" target="_blank">Original URL</a></td> </tr> <% end %> </tbody> -- GitLab