From 01a9de8ed97b22b5764f32046e94e24703321241 Mon Sep 17 00:00:00 2001 From: Arnaud Levy <contact@arnaudlevy.com> Date: Wed, 6 Jul 2022 21:45:05 +0200 Subject: [PATCH] fix code --- app/helpers/admin/application_helper.rb | 28 ++++++++++++------- .../communication/block/component/code.rb | 3 -- .../blocks/components/code/_static.html.erb | 4 +-- 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/app/helpers/admin/application_helper.rb b/app/helpers/admin/application_helper.rb index cc6a12f4c..9820cf3fb 100644 --- a/app/helpers/admin/application_helper.rb +++ b/app/helpers/admin/application_helper.rb @@ -77,7 +77,7 @@ module Admin::ApplicationHelper end def prepare_html_for_static(html, university) - text = html.to_s + text = html.to_s.dup text = sanitize text text.gsub! "\r", '' text.gsub! "\n", ' ' @@ -86,19 +86,27 @@ module Admin::ApplicationHelper end def prepare_text_for_static(text, depth = 1) + text = strip_tags text.to_s.dup + text = indent text, depth + CGI.unescapeHTML text + end + + def prepare_code_for_static(code, depth = 1) + text = code.to_s.dup + text = indent text, depth + raw text + end + + def indent(text, depth) indentation = ' ' * depth - text = text.to_s.dup - text = strip_tags text - text = text.strip # Remove useless \r - text = text.gsub "\r\n", "\n" + text.gsub! "\r\n", "\n" # Replace lonely \r - text = text.gsub "\r", "\n" + text.gsub! "\r", "\n" # Indent properly to avoid broken frontmatter, with 2 lines so the linebreak work - text = text.gsub "\n", "\n#{indentation}\n#{indentation}" - # Remove extra newlines - text = text.chomp - CGI.unescapeHTML text + text.gsub! "\n", "\n#{indentation}\n#{indentation}" + text.chomp! + text end def prepare_media_for_static(object, key) diff --git a/app/models/communication/block/component/code.rb b/app/models/communication/block/component/code.rb index e85840968..49f21bde0 100644 --- a/app/models/communication/block/component/code.rb +++ b/app/models/communication/block/component/code.rb @@ -1,6 +1,3 @@ class Communication::Block::Component::Code < Communication::Block::Component::Base - def data=(value) - @data = value - end end \ No newline at end of file diff --git a/app/views/admin/communication/blocks/components/code/_static.html.erb b/app/views/admin/communication/blocks/components/code/_static.html.erb index c4afc9949..2d3119af2 100644 --- a/app/views/admin/communication/blocks/components/code/_static.html.erb +++ b/app/views/admin/communication/blocks/components/code/_static.html.erb @@ -1,7 +1,7 @@ <% if list %> <%= indentation %>- <%= property %>: >- -<%= indentation %> <%= prepare_html_for_static value, @university, depth + 2 %> +<%= indentation %> <%= prepare_code_for_static value, depth + 2 %> <% else %> <%= indentation %><%= property %>: >- -<%= indentation %> <%= prepare_html_for_static value, @university %> +<%= indentation %> <%= prepare_code_for_static value, depth + 1 %> <% end %> -- GitLab