Skip to content
Snippets Groups Projects
Commit 5cc82cd3 authored by pabois's avatar pabois
Browse files

fix static generation

parent c5aca243
No related branches found
No related tags found
No related merge requests found
......@@ -6,7 +6,6 @@ class Static::Html < Static::Default
@prepared.gsub! "\r", ''
@prepared.gsub! "\n", ''
@prepared = clean_empty_paragraphs_at_beginning_and_end @prepared
@prepared.gsub! "\n", ' '
@prepared = @prepared.ortho(locale: locale)
@prepared.gsub! "/rails/active_storage", "#{@university.url}/rails/active_storage"
@prepared = sanitize @prepared
......@@ -17,23 +16,21 @@ class Static::Html < Static::Default
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?
doc = Nokogiri::HTML(text)
ps = doc.xpath '/html/body/p'
first_text = -1
last_text = 0
ps.each_with_index do |p, i|
unless p.at_xpath('child::text()').nil? || p.at_xpath('child::text()').text.strip.empty? #then found some text
first_text = i if first_text == -1
last_text = i
end
doc = Nokogiri::HTML::DocumentFragment.parse(text)
while(doc.children.first.name == 'p' && doc.children.first.text.strip == '')
doc.children.first.remove
end
new_text = ps.slice(first_text .. last_text)
new_text.nil? ? '' : new_text.to_html
while(doc.children.last.name == 'p' && doc.children.last.text.strip == '')
doc.children.last.remove
end
doc.to_html
end
end
\ 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