Class: Jekyll::Commands::Index

Inherits:
Command
  • Object
show all
Defined in:
lib/jekyll_search.rb

Class Method Summary collapse

Class Method Details

.collect_content(site, elements) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/jekyll_search.rb', line 67

def collect_content(site, elements)
  elements.
    select { |p| p['name'] and p['name'] =~ /\.(html|md)$/ }.
    select { |p| p.data['searchable'].nil? or p.data['searchable'] != false }.
    map do |p|
      {
        :url => site.baseurl + p.url,
        :title => p.data['title'],
        :content => p.content
      }
    end
end

.create_index(client, settings) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/jekyll_search.rb', line 59

def create_index(client, settings)
  if client.indices.exists index: settings['index']['name']
    client.indices.delete index: settings['index']['name']
  end

  client.indices.create index: settings['index']['name'], body: (settings['index']['settings'] or {})
end

.init_with_program(prog) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/jekyll_search.rb', line 11

def init_with_program(prog)
  prog.command(:index) do |c|
    c.syntax "index"
    c.description 'Creates a search index in Elasticsearch.'

    c.action do |args, options|
      options["serving"] = false
      Index.process(options)
    end
  end
end

.process(options) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/jekyll_search.rb', line 23

def process(options)
  options = configuration_from_options(options)
  destination = options['destination']

  site = Jekyll::Site.new(options)
  site.process
  settings = site.config['search']

  host = ENV['JEKYLL_SEARCH_HOST'] || settings['host']
  client = Elasticsearch::Client.new host: host, log: false
  create_index(client, settings)

  pages = collect_content(site, site.pages)
  posts = collect_content(site, site.posts)

  for page in pages + posts
    page_body = {
      url: page[:url],
      title: page[:title],
      content: JekyllSearch::HtmlProcessor.strip_html(page[:content])
    }

    client.index index: settings['index']['name'], type: 'page', body: page_body

    for section in JekyllSearch::HtmlProcessor.detect_sections(page[:content])
      section_body = {
        url: if section[:id] != nil then site.baseurl + page[:url] + '#' + section[:id] else site.baseurl + page[:url] end,
        title: if section[:title] != nil then section[:title] else page[:title] end,
        content: JekyllSearch::HtmlProcessor.strip_html(section[:content])
      }

      client.index index: settings['index']['name'], type: 'section', body: section_body
    end
  end
end