diff --git a/Gemfile.lock b/Gemfile.lock index 54f6ebefdcc4d6adcab65ffaace07e8f2d3d40af..4cfbe89dcbd26526510676bc5fc525af40aeb966 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -79,17 +79,17 @@ GEM autoprefixer-rails (10.3.3.0) execjs (~> 2) aws-eventstream (1.2.0) - aws-partitions (1.515.0) - aws-sdk-core (3.121.1) + aws-partitions (1.517.0) + aws-sdk-core (3.121.3) aws-eventstream (~> 1, >= 1.0.2) aws-partitions (~> 1, >= 1.239.0) aws-sigv4 (~> 1.1) jmespath (~> 1.0) - aws-sdk-kms (1.49.0) - aws-sdk-core (~> 3, >= 3.120.0) + aws-sdk-kms (1.50.0) + aws-sdk-core (~> 3, >= 3.121.2) aws-sigv4 (~> 1.1) - aws-sdk-s3 (1.103.0) - aws-sdk-core (~> 3, >= 3.120.0) + aws-sdk-s3 (1.104.0) + aws-sdk-core (~> 3, >= 3.121.2) aws-sdk-kms (~> 1) aws-sigv4 (~> 1.4) aws-sigv4 (1.4.0) @@ -247,7 +247,7 @@ GEM public_suffix (4.0.6) puma (5.5.2) nio4r (~> 2.0) - racc (1.5.2) + racc (1.6.0) rack (2.2.3) rack-mini-profiler (2.3.3) rack (>= 1.2.0) @@ -298,7 +298,7 @@ GEM ruby-vips (2.1.3) ffi (~> 1.12) ruby2_keywords (0.0.5) - rubyzip (1.3.0) + rubyzip (2.3.2) sanitize (6.0.0) crass (~> 1.0.2) nokogiri (>= 1.12.0) @@ -313,7 +313,7 @@ GEM sawyer (0.8.2) addressable (>= 2.3.5) faraday (> 0.8, < 2.0) - selenium-webdriver (4.0.0) + selenium-webdriver (4.0.3) childprocess (>= 0.5, < 5.0) rexml (~> 3.2, >= 3.2.5) rubyzip (>= 1.2.2) @@ -361,15 +361,16 @@ GEM activemodel (>= 6.0.0) bindex (>= 0.4.0) railties (>= 6.0.0) - webdrivers (2.4.0) + webdrivers (5.0.0) nokogiri (~> 1.6) - rubyzip (~> 1.0) + rubyzip (>= 1.3.0) + selenium-webdriver (~> 4.0) websocket-driver (0.7.5) websocket-extensions (>= 0.1.0) websocket-extensions (0.1.5) xpath (3.2.0) nokogiri (~> 1.8) - zeitwerk (2.4.2) + zeitwerk (2.5.1) PLATFORMS ruby 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 a3211613cf529142bd1a68131e0afd5614156e99..1a146d187481888ec496d019a611964221680804 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 has_rich_text :text @@ -45,4 +46,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/models/research/journal/article.rb b/app/models/research/journal/article.rb index 0410c664d3b6e9859bf11e853a325e9043904631..20a415c655deaf08befa1c4a77cf15c2faa94b1f 100644 --- a/app/models/research/journal/article.rb +++ b/app/models/research/journal/article.rb @@ -10,7 +10,7 @@ # text :text # title :string # created_at :datetime not null -# updated_at :datetime not null +# updated_at :date not null # research_journal_id :uuid not null # research_journal_volume_id :uuid # university_id :uuid not null 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 1ef214923f097dba7597a0c50d8c94244a854991..678c8142572da33e7a11940829d388c3355e260b 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> diff --git a/db/schema.rb b/db/schema.rb index 84ec856e3dcd325301e07961061964a644bbc816..a8b5b57149f48e017de43ae1bc14845f09c2adb6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -250,7 +250,7 @@ ActiveRecord::Schema.define(version: 2021_10_21_095157) do t.uuid "research_journal_id", null: false t.uuid "research_journal_volume_id" t.datetime "created_at", precision: 6, null: false - t.datetime "updated_at", precision: 6, null: false + t.date "updated_at", null: false t.uuid "updated_by_id" t.text "abstract" t.text "references"