Sha256: e72374220efb24ea5ae33032e6a9db88c7768b6f5c0546a5b62fa31afcef3a45

Contents?: true

Size: 928 Bytes

Versions: 2

Compression:

Stored size: 928 Bytes

Contents

require 'nokogiri'

module Odania
	module Filter
		class << self
			include Rails.application.routes.url_helpers

			def filter_html(obj, html)
				# Add nofollow to links
				doc = Nokogiri::HTML.fragment(html)
				doc.css('a').each do |link|
					unless link.attributes['href'].nil?
						link.attributes['href'].value = get_click_counter_url(obj, link.attributes['href'].value)
						link['rel'] = 'nofollow'
					end
				end

				# Retrieve tags
				filtered_html = doc.to_s
				tags = []
				html.gsub(/#[a-z0-9\-]*[^< ]/i) do |match|
					tag = match[1, match.length]
					filtered_html.gsub!(match, "<a href=\"/#{obj.language.iso_639_1}/tags/#{tag.parameterize}\">#{tag}</a>")
					tags << tag
				end

				return tags.join(','), filtered_html
			end

			def get_click_counter_url(obj, target_url)
				deliver_click_url(type: obj.class.to_s, id: obj.id.to_s, target: Rack::Utils.escape(target_url))
			end
		end
	end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
odania_core-0.0.7 lib/odania/filter.rb
odania_core-0.0.6 lib/odania/filter.rb