module KeywordReplacer::Core::InstanceMethods
Attributes
custom_class[R]
keywords[R]
keywords_matcher[R]
keywords_with_links[R]
max_matches[R]
options[RW]
removed_links[RW]
Private Instance Methods
paste_links(target)
click to toggle source
# File lib/keyword_replacer/core.rb, line 39 def paste_links(target) reverse_links_matcher = Regexp.new('(' + removed_links.keys.join('|') + ')') target.gsub(reverse_links_matcher) do |match| removed_links[match] end end
replace_keywords(target)
click to toggle source
# File lib/keyword_replacer/core.rb, line 46 def replace_keywords(target) target.gsub(keywords_matcher) do |match| if options[match] >= 2 || options.sum { |_, v| v } >= max_matches match else options[match] += 1 keyword_link = keywords_with_links[match.downcase] "<a target='_blank' class='#{custom_class}' href='#{keyword_link}' >#{match}</a>" end end end
replace_links(target)
click to toggle source
# File lib/keyword_replacer/core.rb, line 31 def replace_links(target) target.gsub(%r{(<a).*?(\/a>)}) do |match| key = SecureRandom.hex removed_links[key] = match key end end