Class: DocsKit::SearchIndex::Snippet

Inherits:
Object
  • Object
show all
Defined in:
lib/docs_kit/search_index/snippet.rb

Overview

Builds the HTML-safe excerpt shown under a search result: a short window of text centered on the first query-token match, with every token wrapped in . Surrounding text is HTML-escaped so an angle bracket in the source can never inject markup — the returned String is safe to render.

Constant Summary collapse

RADIUS =

Characters of context on either side of the first match.

80

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, tokens) ⇒ Snippet

Returns a new instance of Snippet.



19
20
21
22
# File 'lib/docs_kit/search_index/snippet.rb', line 19

def initialize(text, tokens)
  @flat = text.to_s.gsub(/\s+/, " ").strip
  @tokens = tokens
end

Class Method Details

.build(text, tokens) ⇒ Object



15
16
17
# File 'lib/docs_kit/search_index/snippet.rb', line 15

def self.build(text, tokens)
  new(text, tokens).build
end

Instance Method Details

#buildObject



24
25
26
# File 'lib/docs_kit/search_index/snippet.rb', line 24

def build
  highlight(window)
end