Sha256: 823eb9f8fcdc1428589f098e51e0605af6693ee6b1d06bfba187f941c58d0feb

Contents?: true

Size: 1.19 KB

Versions: 63

Compression:

Stored size: 1.19 KB

Contents

module Sunspot
  module Search
    # 
    # A Highlight represents a single highlighted fragment of text from a
    # document. Depending on the highlighting parameters used for search, there
    # may be more than one Highlight object for a given field in a given result.
    #
    class Highlight
      HIGHLIGHT_MATCHER = /@@@hl@@@(.*?)@@@endhl@@@/ #:nodoc:
      
      #
      # The name of the field in which the highlight appeared.
      #
      attr_reader :field_name
            
      def initialize(field_name, highlight) #:nodoc:
        @field_name = field_name.to_sym
        @highlight = highlight.to_s.strip
      end
      
      #
      # Returns the highlighted text with formatting according to the template given in &block.
      # When no block is given, <em> and </em> are used to surround the highlight.
      #
      # ==== Example
      #
      #   search.highlights(:body).first.format { |word| "<strong>#{word}</strong>" }
      #
      def format(&block)
        block ||= proc { |word| "<em>#{word}</em>" }
        @highlight.gsub(HIGHLIGHT_MATCHER) do
          block.call(Regexp.last_match[1])
        end
      end
      alias_method :formatted, :format
    end
  end
end

Version data entries

63 entries across 63 versions & 12 rubygems

Version Path
sunspot-2.7.1 lib/sunspot/search/highlight.rb
sunspot-2.7.0 lib/sunspot/search/highlight.rb
sunspot-2.6.0 lib/sunspot/search/highlight.rb
sunspot-2.5.0 lib/sunspot/search/highlight.rb
sunspot-2.4.0 lib/sunspot/search/highlight.rb
sunspot-2.3.0 lib/sunspot/search/highlight.rb
sunspot-2.2.8 lib/sunspot/search/highlight.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/sunspot-2.2.7/lib/sunspot/search/highlight.rb
sunspot-2.2.7 lib/sunspot/search/highlight.rb
sunspot-2.2.6 lib/sunspot/search/highlight.rb
sunspot-2.2.5 lib/sunspot/search/highlight.rb
sunspot-2.2.4 lib/sunspot/search/highlight.rb
sunspot-2.2.3 lib/sunspot/search/highlight.rb
sunspot-2.2.2 lib/sunspot/search/highlight.rb
sunspot-2.2.1 lib/sunspot/search/highlight.rb
sunspot-2.2.0 lib/sunspot/search/highlight.rb
sunspot-2.1.1 lib/sunspot/search/highlight.rb
sunspot-2.1.0 lib/sunspot/search/highlight.rb
sunspot-2.0.0 lib/sunspot/search/highlight.rb
sunspot-2.0.0.pre.130115 lib/sunspot/search/highlight.rb