Sha256: ea2e6454a56f7759e434032af29141f51faff0a26526d2578b269cedacce14d2

Contents?: true

Size: 822 Bytes

Versions: 1

Compression:

Stored size: 822 Bytes

Contents

module Zemanta
  class Enhancer
    # Options:
    #
    #  no_duplicates (default: false) - ensures links are used once
    #
    def initialize(text, opts = {})
      @text = text
      @opts = opts
    end

    def enhance
      enhance!
      @text
    end

    private

    def enhance!
      words_to_anchor(@opts).each do |dictionary|
        link = "<a href=#{dictionary[:link]}>#{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
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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