Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajout de span HTML aux liens externes pour les lecteurs vocaux #2152

Merged
merged 8 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 39 additions & 20 deletions app/services/static/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
2 changes: 2 additions & 0 deletions config/locales/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
3 changes: 3 additions & 0 deletions config/locales/pt.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pt:
html:
external_link: link externo
Loading