Skip to content
Snippets Groups Projects
Commit c3f2b359 authored by Arnaud Levy's avatar Arnaud Levy
Browse files

better style load for preview

parent eb39b12b
No related branches found
No related tags found
No related merge requests found
......@@ -2,18 +2,20 @@
#
# Table name: communication_websites
#
# id :uuid not null, primary key
# about_type :string indexed => [about_id]
# access_token :string
# git_endpoint :string
# git_provider :integer default("github")
# name :string
# repository :string
# url :string
# created_at :datetime not null
# updated_at :datetime not null
# about_id :uuid indexed => [about_type]
# university_id :uuid not null, indexed
# id :uuid not null, primary key
# about_type :string indexed => [about_id]
# access_token :string
# git_endpoint :string
# git_provider :integer default("github")
# name :string
# repository :string
# style :text
# style_updated_at :date
# url :string
# created_at :datetime not null
# updated_at :datetime not null
# about_id :uuid indexed => [about_type]
# university_id :uuid not null, indexed
#
# Indexes
#
......@@ -35,6 +37,7 @@ class Communication::Website < ApplicationRecord
include WithMenuItems
include WithProgramCategories
include WithSpecialPages
include WithStyle
enum git_provider: {
github: 0,
......@@ -73,21 +76,4 @@ class Communication::Website < ApplicationRecord
dependencies += about.git_dependencies(website) if about.present?
dependencies
end
def preview_style
return '' if url.blank?
html = Nokogiri::HTML open(url)
css_files = html.xpath '//link[@rel="stylesheet"]/@href'
css = ''
css_files.each do |css_url|
css_uri = URI.parse css_url
data = Net::HTTP.get css_uri
data = data.force_encoding("UTF-8")
data = data.gsub "src:url(../", "src:url(#{url}/assets/"
data = data.gsub ",url(../", ",url(#{url}/assets/"
css << data
end
css
end
end
......@@ -2,18 +2,20 @@
#
# Table name: communication_websites
#
# id :uuid not null, primary key
# about_type :string indexed => [about_id]
# access_token :string
# git_endpoint :string
# git_provider :integer default("github")
# name :string
# repository :string
# url :string
# created_at :datetime not null
# updated_at :datetime not null
# about_id :uuid indexed => [about_type]
# university_id :uuid not null, indexed
# id :uuid not null, primary key
# about_type :string indexed => [about_id]
# access_token :string
# git_endpoint :string
# git_provider :integer default("github")
# name :string
# repository :string
# style :text
# style_updated_at :date
# url :string
# created_at :datetime not null
# updated_at :datetime not null
# about_id :uuid indexed => [about_type]
# university_id :uuid not null, indexed
#
# Indexes
#
......
......@@ -2,18 +2,20 @@
#
# Table name: communication_websites
#
# id :uuid not null, primary key
# about_type :string indexed => [about_id]
# access_token :string
# git_endpoint :string
# git_provider :integer default("github")
# name :string
# repository :string
# url :string
# created_at :datetime not null
# updated_at :datetime not null
# about_id :uuid indexed => [about_type]
# university_id :uuid not null, indexed
# id :uuid not null, primary key
# about_type :string indexed => [about_id]
# access_token :string
# git_endpoint :string
# git_provider :integer default("github")
# name :string
# repository :string
# style :text
# style_updated_at :date
# url :string
# created_at :datetime not null
# updated_at :datetime not null
# about_id :uuid indexed => [about_type]
# university_id :uuid not null, indexed
#
# Indexes
#
......
module Communication::Website::WithStyle
extend ActiveSupport::Concern
def preview_style
return '' if url.blank?
load_style if style_outdated?
style
end
protected
def load_style
@style = ''
load_style_from url
load_style_from "#{url}/fr" if @style.blank?
load_style_from "#{url}/en" if @style.blank?
substitute_fonts_urls_in_style!
self.update_columns style: @style,
style_updated_at: Date.today
end
def load_style_from(url)
data = URI.open url
html = Nokogiri::HTML data
css_files = html.xpath '//link[@rel="stylesheet"]/@href'
css_files.each do |css_url|
add_css_url_to_style css_url.to_s
end
end
def add_css_url_to_style(css_url)
@style << URI.open(css_url).read
end
def substitute_fonts_urls_in_style!
@style.gsub! "src:url(../", "src:url(#{url}/assets/"
@style.gsub ",url(../", ",url(#{url}/assets/"
end
def style_outdated?
style_updated_at.nil? || style_updated_at < Date.yesterday
end
end
\ No newline at end of file
class AddStyleToWebsite < ActiveRecord::Migration[6.1]
def change
add_column :communication_websites, :style, :text
add_column :communication_websites, :style_updated_at, :date
end
end
This diff is collapsed.
......@@ -2,18 +2,20 @@
#
# Table name: communication_websites
#
# id :uuid not null, primary key
# about_type :string indexed => [about_id]
# access_token :string
# git_endpoint :string
# git_provider :integer default("github")
# name :string
# repository :string
# url :string
# created_at :datetime not null
# updated_at :datetime not null
# about_id :uuid indexed => [about_type]
# university_id :uuid not null, indexed
# id :uuid not null, primary key
# about_type :string indexed => [about_id]
# access_token :string
# git_endpoint :string
# git_provider :integer default("github")
# name :string
# repository :string
# style :text
# style_updated_at :date
# url :string
# created_at :datetime not null
# updated_at :datetime not null
# about_id :uuid indexed => [about_type]
# university_id :uuid not null, indexed
#
# Indexes
#
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment