Sha256: cec8965180a2de11d05232eb3b2ea2886979661cc91964252a2e0491c6cb7f06

Contents?: true

Size: 1.19 KB

Versions: 18

Compression:

Stored size: 1.19 KB

Contents

module Sunspot
  class 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

18 entries across 18 versions & 3 rubygems

Version Path
sunspot-1.0.5 lib/sunspot/search/highlight.rb
sunspot-1.0.4 lib/sunspot/search/highlight.rb
sunspot-1.0.3 lib/sunspot/search/highlight.rb
sunspot-1.0.2 lib/sunspot/search/highlight.rb
sunspot-1.0.1 lib/sunspot/search/highlight.rb
sunspot-1.0.0 lib/sunspot/search/highlight.rb
sunspot-0.10.9 lib/sunspot/search/highlight.rb
sunspot-0.10.8 lib/sunspot/search/highlight.rb
nxa-sunspot-0.10.7 lib/sunspot/search/highlight.rb
sunspot-0.10.7 lib/sunspot/search/highlight.rb
sunspot-0.10.6 lib/sunspot/search/highlight.rb
sunspot-0.10.5 lib/sunspot/search/highlight.rb
sunspot-0.10.4 lib/sunspot/search/highlight.rb
kuahyeow-sunspot-0.10.3 lib/sunspot/search/highlight.rb
sunspot-0.10.3 lib/sunspot/search/highlight.rb
sunspot-0.10.2 lib/sunspot/search/highlight.rb
sunspot-0.10.1 lib/sunspot/search/highlight.rb
sunspot-0.10.0 lib/sunspot/search/highlight.rb