diff --git a/app/models/communication/website/page.rb b/app/models/communication/website/page.rb index ce2fd2b36453e8b9a1f17b39645e8a18e6311b6f..ad4e52d14a72e60b17fc268a1a6fa6adb3af4a9f 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 e7d4f38837556509c24bec9df7d5ddd4ea9c1a98..3b1440215a9e3edb87db2642669401e92a05f712 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 0000000000000000000000000000000000000000..ed89d0e8cb6d392621670d46b04408a09c324993 --- /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 9b33158fda7897082abd6a61c5b6c1c1486968db..eac664d343dd3b9efe9d555af52b5fef1220d81a 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 98963fb5e351602fe42c67f13d9a832ce258560f..a989bb177b51b90bd5fcdcda932d9897450d595b 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 0000000000000000000000000000000000000000..6f0aab1756b1d727f6aacb399a0ac2fcd37d3d52 --- /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 310601d1fde4627058d955c34f41e7f626bbf7be..b7a299368a5162ccc160eb1836bf49936c2e3c1b 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>