From 6039e3ebd1adafdd05c4ae246df3355076c39167 Mon Sep 17 00:00:00 2001 From: Arnaud Levy <contact@arnaudlevy.com> Date: Sat, 25 Jun 2022 20:13:57 +0200 Subject: [PATCH] preview POC --- .../websites/pages_controller.rb | 5 +++ .../websites/pages/preview.html.erb | 4 +++ app/views/admin/layouts/preview.html.erb | 31 +++++++++++++++++++ config/routes/admin/communication.rb | 1 + 4 files changed, 41 insertions(+) create mode 100644 app/views/admin/communication/websites/pages/preview.html.erb create mode 100644 app/views/admin/layouts/preview.html.erb diff --git a/app/controllers/admin/communication/websites/pages_controller.rb b/app/controllers/admin/communication/websites/pages_controller.rb index 4d3e9be99..d4c34e112 100644 --- a/app/controllers/admin/communication/websites/pages_controller.rb +++ b/app/controllers/admin/communication/websites/pages_controller.rb @@ -35,6 +35,11 @@ class Admin::Communication::Websites::PagesController < Admin::Communication::We render layout: false end + def preview + @url = @website.url + render layout: 'admin/layouts/preview' + end + def new @page.website = @website breadcrumb diff --git a/app/views/admin/communication/websites/pages/preview.html.erb b/app/views/admin/communication/websites/pages/preview.html.erb new file mode 100644 index 000000000..2aaf52e00 --- /dev/null +++ b/app/views/admin/communication/websites/pages/preview.html.erb @@ -0,0 +1,4 @@ +<%= @page.text.to_s %> +<% @page.blocks.ordered.each do |block| %> +<%= render 'admin/communication/blocks/preview', block: block %> +<% end %> \ No newline at end of file diff --git a/app/views/admin/layouts/preview.html.erb b/app/views/admin/layouts/preview.html.erb new file mode 100644 index 000000000..c0f2336ef --- /dev/null +++ b/app/views/admin/layouts/preview.html.erb @@ -0,0 +1,31 @@ +<% +@html = Nokogiri::HTML open(@url) +@css_files = @html.xpath '//link[@rel="stylesheet"]/@href' +@css = '' +@css_files.each do |url| + uri = URI.parse url + data = Net::HTTP.get 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 +%> +<!DOCTYPE html> +<html> + <head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> + <title><%= yield :title %></title> + </head> + <body class="is-loaded"> + <style> + <%= @css %> + </style> + <main class="page-with-blocks" id="main" role="main" tabindex="-1"> + <div class="blocks"> + <%= yield %> + </div> + </main> + </body> +</html> diff --git a/config/routes/admin/communication.rb b/config/routes/admin/communication.rb index a7ee40e9a..a8673cb02 100644 --- a/config/routes/admin/communication.rb +++ b/config/routes/admin/communication.rb @@ -12,6 +12,7 @@ namespace :communication do member do get :children get :static + get :preview end end resources :categories, controller: 'websites/categories' do -- GitLab