summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/page.rb33
1 files changed, 12 insertions, 21 deletions
diff --git a/app/models/page.rb b/app/models/page.rb
index fff044e..1a98e08 100644
--- a/app/models/page.rb
+++ b/app/models/page.rb
@@ -243,31 +243,22 @@ class Page < ApplicationRecord
243 end 243 end
244 244
245 def rewrite_links_in_body 245 def rewrite_links_in_body
246 begin 246 return unless self.body
247 if self.body
248 tmp_body = "<div>#{self.body}</div>"
249 xml_string = XML::Parser.string( tmp_body )
250 xml_doc = xml_string.parse
251 links = xml_doc.find("//a[not(starts-with(@href, 'http://'))]")
252 links = links.reject { |l| l[:href] =~ /system\/uploads/ }
253 locales = I18n.available_locales.reject {|l| l == :root}
254 247
255 links.each do |link| 248 doc = Nokogiri::HTML::DocumentFragment.parse(self.body)
256 unless locales.include? link[:href].slice(1,2).to_sym 249 locales = I18n.available_locales.reject { |l| l == :root }
257 unless link[:href] =~ /sytem\/uploads/
258 link[:href] = link[:href].sub(/^\//, "/#{I18n.locale}/")
259 end
260 end
261 end
262 250
263 tmp_body = xml_doc.to_s.gsub(/(\n\<div\>|\<\/div\>\n)/, "") 251 doc.css('a').each do |link|
264 tmp_body.gsub!("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "") 252 href = link['href']
253 next unless href
254 next if href.start_with?('http://', 'https://')
255 next if href =~ /system\/uploads/
265 256
266 self.body = tmp_body 257 unless locales.include?(href.slice(1, 2)&.to_sym)
258 link['href'] = href.sub(/^\//, "/#{I18n.locale}/")
267 end 259 end
268 rescue
269 nil
270 end 260 end
271 end
272 261
262 self.body = doc.to_html
263 end
273end 264end