Sha256: 32e4af4d7560a62f324c3cd79dab75f2cbcb7972a2c9c0e320364c7ec63f2087

Contents?: true

Size: 1.17 KB

Versions: 1

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

1 entries across 1 versions & 1 rubygems

Version Path
zemanta_client-0.0.6 lib/zemanta/enhancer.rb