diff --git a/app/models/bulletin_feed.rb b/app/models/bulletin_feed.rb index c7d6943..b33d64b 100644 --- a/app/models/bulletin_feed.rb +++ b/app/models/bulletin_feed.rb @@ -34,7 +34,7 @@ class BulletinFeed feed_cache = self.generate_one_cache(startdt: startdt,enddt: enddt,dt: dt,base_url: base_url) feed_cache.content else - Timeout::timeout(timeout) { + Timeout::timeout(timeout) do feed_cache = nil Thread.new do feed_cache = self.generate_one_cache(startdt: startdt,enddt: enddt,dt: dt,base_url: base_url) @@ -44,7 +44,7 @@ class BulletinFeed break if !feed_cache.nil? end feed_cache.content - } + end end rescue=> e puts [e,e.backtrace] @@ -100,7 +100,7 @@ class BulletinFeed first_postdate = anns_before_filter.open_in_future.limit(1).pluck(:postdate)[0] first_deadline = nil - announcements.each do |anns| + announcements.each_with_index do |anns,i| deadline = anns.deadline if !deadline.blank? if first_deadline.nil? || first_deadline>deadline @@ -109,7 +109,7 @@ class BulletinFeed end user = User.find(anns.create_user_id) rescue nil if !user.nil? - author = user.member_profile && user.member_profile.name == "" ? user.user_name : user.member_profile.name + author = user.member_name || user.user_name else author = "" end @@ -118,8 +118,9 @@ class BulletinFeed a["title_translations"] = anns.title_translations a["subtitle_translations"] = anns.subtitle_translations a["text_translations"] = {} - a["text_translations"]["en"] = self.class.smart_convertor(anns.text_translations["en"],base_url) if !anns.text_translations["en"].blank? - a["text_translations"]["zh_tw"] = self.class.smart_convertor(anns.text_translations["zh_tw"],base_url) if !anns.text_translations["zh_tw"].blank? + text_translations = anns.text_translations + a["text_translations"]["en"] = self.class.smart_convertor(text_translations["en"],base_url) if !text_translations["en"].blank? + a["text_translations"]["zh_tw"] = self.class.smart_convertor(text_translations["zh_tw"],base_url) if !text_translations["zh_tw"].blank? a["postdate"] = anns.postdate a["image_description_translations"] = anns.image_description_translations a["image"] = {} @@ -173,9 +174,26 @@ class BulletinFeed feed_cache = BulletinFeedCache.create(uid: uid,content: anns,start: startdt,end: enddt,date: dt,invalid_date: invalid_date) end def self.smart_convertor(text,url) - html_string = text - html_string = html_string.gsub(/img.*?src="(?=\/)(.*?)|a.*?href="(?=\/)(.*?)/i){|w| w+url} - html_string = html_string.gsub(/img.*?src="\.\.(?=\/)(.*?)|a.*?href="\.\.(?=\/)(.*?)/i){|w| w[0...-2]+url} - return html_string + doc = Nokogiri.HTML(text) + doc.search('a[href]').each do |link| + if link['href'].nil? + link.delete 'href' + elsif link['href'].start_with?('/') + link['href'] = url + link['href'] + elsif link['href'].start_with?('../') + link['href'] = url + link['href'][3..-1] + end + end + doc.search('img[src]').each do |link| + if link['src'].nil? + link.delete 'src' + elsif link['src'].start_with?('/') + link['src'] = url + link['src'] + elsif link['src'].start_with?('../') + link['src'] = url + link['src'][3..-1] + end + end + + return doc.css('body').inner_html end end \ No newline at end of file