From 6b0c2197cedcfca0e311860337175d419f176672 Mon Sep 17 00:00:00 2001 From: Arnaud Levy Date: Wed, 21 Aug 2024 18:58:58 +0200 Subject: [PATCH] Fix #2151 --- app/services/static/html.rb | 59 ++++++++++++++++++++++++------------- config/locales/en.yml | 2 ++ config/locales/fr.yml | 2 ++ config/locales/pt.yml | 3 ++ 4 files changed, 46 insertions(+), 20 deletions(-) create mode 100644 config/locales/pt.yml diff --git a/app/services/static/html.rb b/app/services/static/html.rb index ebdfdbef3..8d873fa75 100644 --- a/app/services/static/html.rb +++ b/app/services/static/html.rb @@ -3,38 +3,57 @@ class Static::Html < Static::Default def prepared unless @prepared @prepared = @text.to_s.strip.dup - @prepared.gsub! "\r", '' - # if no whitespace in the next line html in static won't be on one line - @prepared.gsub! "\n", ' ' - @prepared = clean_empty_paragraphs_at_beginning_and_end @prepared - # TODO ça ne doit plus être utile depuis un siècle - @prepared.gsub! "/rails/active_storage", "#{@university.url}/rails/active_storage" - # clean_empty_paragraphs_at_beginning_and_end re-send \n, because of Nokogiri. - # Can be changed with a weird hack (Nokogiri(format: 0)) but a gsub is easiest (PAB) - @prepared.gsub! "\n", '' + # Sanitize before clean_code, otherwise we remove the spans! @prepared = sanitize @prepared + @prepared = remove_line_breaks @prepared + @prepared = clean_code @prepared + # clean_empty_paragraphs_at_beginning_and_end re-sends \n, because of Nokogiri. + # Can be changed with a weird hack (Nokogiri(format: 0)) but a gsub is easiest (PAB) + @prepared = remove_line_breaks @prepared end @prepared end private - # based on https://stackoverflow.com/questions/17479135/how-do-i-trim-the-head-and-tail-of-empty-tags-in-html - # and https://stackoverflow.com/questions/16417292/how-do-i-remove-white-space-between-html-nodes - def clean_empty_paragraphs_at_beginning_and_end(text) - return text unless text.present? + def remove_line_breaks(html) + clean = html.gsub "\r", '' + # if no whitespace in the next line html in static won't be on one line + clean = clean.gsub "\n", ' ' + clean + end - doc = Nokogiri::HTML::DocumentFragment.parse(text) + def clean_code(html) + return html unless html.present? + @doc = Nokogiri::HTML::DocumentFragment.parse(html) + clean_empty_paragraphs_at_beginning_and_end! + add_html_tags_to_external_links! + @doc.to_html + end - while(doc.children.any? && doc.children.first.name == 'p' && doc.children.first.text.strip == '') - doc.children.first.remove + # based on https://stackoverflow.com/questions/17479135/how-do-i-trim-the-head-and-tail-of-empty-tags-in-html + # and https://stackoverflow.com/questions/16417292/how-do-i-remove-white-space-between-html-nodes + def clean_empty_paragraphs_at_beginning_and_end! + while(@doc.children.any? && + @doc.children.first.name == 'p' && + @doc.children.first.text.strip == '') + @doc.children.first.remove end - - while(doc.children.any? && doc.children.last.name == 'p' && doc.children.last.text.strip == '') - doc.children.last.remove + while(@doc.children.any? && + @doc.children.last.name == 'p' && + @doc.children.last.text.strip == '') + @doc.children.last.remove end + end - doc.to_html + # Each external link needs a - lien externe in it + # https://github.com/osunyorg/admin/issues/2151 + def add_html_tags_to_external_links! + hint = I18n.t('html.external_link', locale: locale) + span = " - #{hint}" + @doc.css('a[target=_blank]').each do |link| + link << span + end end end \ No newline at end of file diff --git a/config/locales/en.yml b/config/locales/en.yml index ce31f8767..8eac3528e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -251,6 +251,8 @@ en: close: Close folder hello: "Hello %{name}!" home: Home + html: + external_link: external link imports: deleted_user: Deleted user error_msg: "Line %{line}: %{error}" diff --git a/config/locales/fr.yml b/config/locales/fr.yml index 5c1d080f8..46feb2612 100644 --- a/config/locales/fr.yml +++ b/config/locales/fr.yml @@ -251,6 +251,8 @@ fr: close: Fermer le dossier hello: "Bonjour %{name} !" home: Accueil + html: + external_link: lien externe imports: deleted_user: Utilisateur supprimé error_msg: "Ligne %{line} : %{error}" diff --git a/config/locales/pt.yml b/config/locales/pt.yml new file mode 100644 index 000000000..31e2f088e --- /dev/null +++ b/config/locales/pt.yml @@ -0,0 +1,3 @@ +pt: + html: + external_link: link externo \ No newline at end of file