Module: Odania::Filter

Defined in:
lib/odania/filter.rb

Class Method Summary collapse

Class Method Details

.filter_html(obj, html, host) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/odania/filter.rb', line 8

def filter_html(obj, html, host)
	# 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, host)
			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

.get_click_counter_url(obj, target_url, host) ⇒ Object



30
31
32
# File 'lib/odania/filter.rb', line 30

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