Class: JekyllOpenSdgPlugins::CreateGoals

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

Instance Method Summary collapse

Instance Method Details

#generate(site) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
# File 'lib/jekyll-open-sdg-plugins/create_goals.rb', line 9

def generate(site)
  # If site.create_goals is set, create goals per the metadata.
  if site.config['languages'] and site.config['create_goals']
    # Compile the list of goals.
    goals = {}
     = {}
    default_language = site.config['languages'][0]
     = site.data[default_language]['meta']
    .each do |inid, indicator|
      if indicator.has_key?('standalone') and indicator['standalone']
        next
      end
      goal = inid.split('-')[0]
      goals[goal] = true
    end
    # Decide what layout to use for the goal pages.
    layout = 'goal'
    # See if we need to "map" any language codes.
    languages_public = Hash.new
    if site.config['languages_public']
      languages_public = opensdg_languages_public(site)
    end
    # Loop through the languages.
    site.config['languages'].each_with_index do |language, index|
      # Get the "public language" (for URLs) which may be different.
      language_public = language
      if languages_public[language]
        language_public = languages_public[language]
      end
      # Loop through the goals.
      goal_index = 0
      # In the SDGs the goals are numeric, so try to sort them numerically.
      begin
        goals_sorted = goals.keys.sort_by { |k| k.to_i if Float(k) rescue k }
      rescue
        goals_sorted = goals.keys.sort
      end
      goals_sorted.each do |goal|
        # Add the language subfolder for all except the default (first) language.
        dir = index == 0 ? goal.to_s : File.join(language_public, goal.to_s)
        # Create the goal page.
        site.collections['goals'].docs << GoalPage.new(site, site.source, dir, goal, language, layout, goal_index)
        goal_index += 1
      end
    end
  end
end