Class: JekyllOpenSdgPlugins::SearchIndex

Inherits:
Jekyll::Generator
  • Object
show all
Defined in:
lib/jekyll-open-sdg-plugins/search_index.rb

Instance Method Summary collapse

Instance Method Details

#generate(site) ⇒ Object



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/jekyll-open-sdg-plugins/search_index.rb', line 31

def generate(site)

  # Generate a hash of items to include in the search index.
  search_items = {}

  site.collections.keys.each do |collection|
    site.collections[collection].docs.each do |doc|
      # Do not index configuration forms.
      if doc.data.has_key?('layout') && (doc.data['layout'] == 'config-builder' or doc.data['layout'] == 'config-builder-2')
        next
      end
      # We segregate the search items by language.
      language = doc.data['language']
      if !search_items.has_key? language
        search_items[language] = {}
      end
      # We'll be adding properties to this basic hash.
      item = {
        # The 'type' can be used on the front-end to describe a search result.
        # It is assumed that all the collection names are translated in the
        # "general" translation group. Eg: general.indicators, general.goals
        'type' => opensdg_translate_key('general.' + collection, site.data['translations'], language)
      }
      if collection == 'indicators'
        # For indicators, we assign the following properties for each item.
        # The URL of the page.
        item['url'] = doc.data['indicator']['url']
        # For the title, use the indicator name.
        indicator_label = opensdg_translate_key('general.indicator', site.data['translations'], language)
        item['title'] = indicator_label + ' ' + doc.data['indicator']['number'] + ' - ' + doc.data['indicator']['name']
        # For the content, use the 'page_content' field.
        item['content'] = prepare_content(site, doc.data['indicator']['page_content'], language)
        # For the id field, use the ID number.
        item['id'] = doc.data['indicator']['number']
        # Also index any additional metadata fields.
        if site.config['search_index_extra_fields']
          site.config['search_index_extra_fields'].each do |field|
            if doc.data['indicator'].has_key? field
              item[field] = prepare_content(site, doc.data['indicator'][field], language)
            end
          end
        end
      elsif collection == 'goals'
        # For goals, we assign the following properties for each item.
        # The URL of the page.
        item['url'] = doc.data['goal']['url']
        # For the title we use the goal name.
        goal_label = opensdg_translate_key('general.goal', site.data['translations'], language)
        item['title'] = goal_label + ' ' + doc.data['goal']['number'] + ' - ' + doc.data['goal']['name']
        # For the content, currently nothing here.
        item['content'] = ''
        # For the id field, use the ID number.
        item['id'] = doc.data['goal']['number']
      else
        # Otherwise assume it is a normal Jekyll document.
        item['url'] = File.join(doc.data['baseurl'], doc.url)
        item['title'] = prepare_content(site, doc.data['title'], language)
        item['content'] = prepare_content(site, doc.content, language)
        item['id'] = ''
      end

      # Save this item in the language-specific search index.
      search_items[language][item['url']] = item
    end
  end

  # Stow the data for later use in Jekyll templates.
  site.data['search_items'] = search_items

end

#prepare_content(site, content, language) ⇒ Object

Helper function to prepare content for the search index.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/jekyll-open-sdg-plugins/search_index.rb', line 12

def prepare_content(site, content, language)

  # Handle nil content.
  if !content
    content = ''
  end

  # Strip whitespace.
  content = content.strip
  # Translate if needed.
  content = opensdg_translate_key(content, site.data['translations'], language)
  # Next compile any Markdown.
  converter = site.find_converter_instance(::Jekyll::Converters::Markdown)
  content = converter.convert(content)
  # Now strip any HTML.
  content = content.gsub(/<\/?[^>]*>/, "")
  return content
end