lib/zemanta/enhancer.rb in zemanta_client-0.0.5 vs lib/zemanta/enhancer.rb in zemanta_client-0.0.6
- old
+ new
@@ -1,10 +1,12 @@
module Zemanta
class Enhancer
# Options:
#
# no_duplicates (default: false) - ensures links are used once
+ # skip - regexp for URLs not to be hot-linked
+ # strip_query_string - remove the query string from urls
#
def initialize(text, opts = {})
@text = text
@opts = opts
end
@@ -16,11 +18,19 @@
private
def enhance!
words_to_anchor(@opts).each do |dictionary|
- link = "<a href=#{dictionary[:link]}>#{dictionary[:word]}</a>"
+ url = dictionary[:link]
+
+ if @opts[:skip]
+ next if url =~ @opts[:skip]
+ end
+
+ url = strip_query_string(url) if @opts[:strip_query_string]
+
+ link = "<a href=#{url}>#{dictionary[:word]}</a>"
if @opts[:no_duplicates]
@text.sub!(dictionary[:word], link)
else
@text.gsub!(dictionary[:word], link)
end
@@ -33,8 +43,13 @@
end
end
def suggest_markup(opts = {})
Markup.fetch(@text, opts)
+ end
+
+ def strip_query_string(url)
+ return nil unless url
+ url.gsub(/\?.*$/, '')
end
end
end