Class: Jekyll::J1LunrSearch::SearchEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/starter_web/_plugins/index/lunr.rb

Overview

noinspection RubyTooManyInstanceVariablesInspection

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title, tagline, url, date, tags, categories, description, is_post, body, collection) ⇒ SearchEntry

Returns a new instance of SearchEntry.



357
358
359
# File 'lib/starter_web/_plugins/index/lunr.rb', line 357

def initialize(title, tagline, url, date, tags, categories, description, is_post, body, collection)
  @title, @tagline, @url, @date,@tags,  @categories, @description, @is_post, @body, @collection = title, tagline, url, date, tags, categories, description, is_post, body, collection
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



355
356
357
# File 'lib/starter_web/_plugins/index/lunr.rb', line 355

def body
  @body
end

#categoriesObject (readonly)

Returns the value of attribute categories.



355
356
357
# File 'lib/starter_web/_plugins/index/lunr.rb', line 355

def categories
  @categories
end

#collectionObject (readonly)

Returns the value of attribute collection.



355
356
357
# File 'lib/starter_web/_plugins/index/lunr.rb', line 355

def collection
  @collection
end

#dateObject (readonly)

Returns the value of attribute date.



355
356
357
# File 'lib/starter_web/_plugins/index/lunr.rb', line 355

def date
  @date
end

#descriptionObject (readonly)

Returns the value of attribute description.



355
356
357
# File 'lib/starter_web/_plugins/index/lunr.rb', line 355

def description
  @description
end

#is_postObject (readonly)

Returns the value of attribute is_post.



355
356
357
# File 'lib/starter_web/_plugins/index/lunr.rb', line 355

def is_post
  @is_post
end

#taglineObject (readonly)

Returns the value of attribute tagline.



355
356
357
# File 'lib/starter_web/_plugins/index/lunr.rb', line 355

def tagline
  @tagline
end

#tagsObject (readonly)

Returns the value of attribute tags.



355
356
357
# File 'lib/starter_web/_plugins/index/lunr.rb', line 355

def tags
  @tags
end

#titleObject (readonly)

Returns the value of attribute title.



355
356
357
# File 'lib/starter_web/_plugins/index/lunr.rb', line 355

def title
  @title
end

#urlObject (readonly)

Returns the value of attribute url.



355
356
357
# File 'lib/starter_web/_plugins/index/lunr.rb', line 355

def url
  @url
end

Class Method Details

.create(site, renderer) ⇒ Object



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/starter_web/_plugins/index/lunr.rb', line 304

def self.create(site, renderer)
  if site.is_a?(Jekyll::Page) or site.is_a?(Jekyll::Document)

    if defined?(site.date)
      date = site.date
    elsif defined?(site.data['date'])
      date = site.data['date']
    else
      date = '2022-01-01 00:00:00'
    end

    tagline           = site.data['tagline']
    tags              = site.data['tags']
    categories        = site.data['categories']
    description       = site.data['description']
    title, url        = extract_title_and_url(site)
    is_post           = site.is_a?(Jekyll::Document)
    body              = renderer.render(site)

    # For posts, the description is taken from the excerpt
    if site.is_a?(Jekyll::Document)
      excerpt = extract_excerpt(site)
      unless excerpt.nil? || excerpt.length == 0
        description = excerpt
      end
    end

    if description.nil? || description.length == 0
      description = 'No description available.'
    end

    SearchEntry.new(title, tagline, url, date, tags, categories, description, is_post, body, renderer)

  else
    raise 'Not supported'
  end
end

.extract_excerpt(item) ⇒ Object



347
348
349
350
351
352
353
# File 'lib/starter_web/_plugins/index/lunr.rb', line 347

def self.extract_excerpt(item)
  data = item.to_liquid
  parsed_data = Nokogiri::HTML.parse(data['excerpt']).text
  parsed_data.gsub!(/\n+/, ' ')
  parsed_data.gsub!(/^\s+/, '')
  parsed_data.gsub!(/\s+$/, '')
end

.extract_title_and_url(item) ⇒ Object



342
343
344
345
# File 'lib/starter_web/_plugins/index/lunr.rb', line 342

def self.extract_title_and_url(item)
  data = item.to_liquid
  [ data['title'], data['url'] ]
end

Instance Method Details

#strip_index_suffix_from_url!Object



361
362
363
# File 'lib/starter_web/_plugins/index/lunr.rb', line 361

def strip_index_suffix_from_url!
  @url.gsub!(/index\.html$/, '')
end

#strip_stopwords!(stopwords, min_length) ⇒ Object

remove anything that is in the stop words list from the text to be indexed



368
369
370
371
372
373
374
# File 'lib/starter_web/_plugins/index/lunr.rb', line 368

def strip_stopwords!(stopwords, min_length)
  #noinspection RubyParenthesesAfterMethodCallInspection
  @body = @body.split.delete_if() do |x|
    t = x.downcase.gsub(/[^a-z]/, '')
    t.length < min_length || stopwords.include?(t)
  end.join(' ')
end