Skip to content
Snippets Groups Projects
Unverified Commit 77ddb765 authored by Arnaud Levy's avatar Arnaud Levy Committed by GitHub
Browse files

Fix #2151 (#2152)

parent e7f1ddad
No related branches found
No related tags found
No related merge requests found
......@@ -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 <span class="sr-only"> - lien externe</span> 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 = "<span class=\"sr-only\"> - #{hint}</span>"
@doc.css('a[target=_blank]').each do |link|
link << span
end
end
end
\ No newline at end of file
......@@ -261,6 +261,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}"
......
......@@ -262,6 +262,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}"
......
pt:
html:
external_link: link externo
\ No newline at end of file
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