Sha256: 97bb000f64340fd3f5db6d049d3cc76031e1d2f9536d5bb8b80999487424792b

Contents?: true

Size: 1.17 KB

Versions: 2

Compression:

Stored size: 1.17 KB

Contents

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

    def enhance
      enhance!
      @text
    end

    private

    def enhance!
      words_to_anchor(@opts).each do |dictionary|
        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
      end
    end

    def words_to_anchor(opts)
      suggest_markup(opts).links.map do |link|
        { word: link.anchor, link: link.target.first.url }
      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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
zemanta_client-0.0.9 lib/zemanta/enhancer.rb
zemanta_client-0.0.8 lib/zemanta/enhancer.rb