Sha256: f528cac50ff225efe1c96273deaa927588f5aa60050b10655a5bc9452437b422

Contents?: true

Size: 902 Bytes

Versions: 5

Compression:

Stored size: 902 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=\"/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

5 entries across 5 versions & 1 rubygems

Version Path
odania_core-0.0.5 lib/odania/filter.rb
odania_core-0.0.4 lib/odania/filter.rb
odania_core-0.0.3 lib/odania/filter.rb
odania_core-0.0.2 lib/odania/filter.rb
odania_core-0.0.1 lib/odania/filter.rb