Class: JekyllOpenSdgPlugins::CreateIndicators

Inherits:
Jekyll::Generator
  • Object
show all
Defined in:
lib/jekyll-open-sdg-plugins/create_indicators.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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/jekyll-open-sdg-plugins/create_indicators.rb', line 9

def generate(site)
  # Some references to clean up the code below.
  language_config = site.config['languages']
  indicator_config = site.config['create_indicators']
  form_settings_config = site.config['indicator_config_form']
  form_settings_meta = site.config['indicator_metadata_form']
  form_settings_data = site.config['indicator_data_form']
  config_builder_layout = 'config-builder'
  #if site.config['use_new_config_forms']
  #  config_builder_layout = 'config-builder-2'
  #end

  translations = site.data['translations']
  # If site.create_indicators is set, create indicators per the metadata.
  if (language_config and indicator_config)
    # Decide what layout to use for the indicator pages.
    layout = 'indicator'
    # 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.
    language_config.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
       = {}
       = site.data[language]['meta']
      # Loop through the indicators (using metadata as a list).
      .each do |inid, meta|
        permalink = inid
        if (meta.has_key?('permalink') and meta['permalink'] != '')
          permalink = meta['permalink']
        end
        # Add the language subfolder for all except the default (first) language.
        dir = index == 0 ? permalink : File.join(language_public, permalink)

        # Create the indicator page.
        site.collections['indicators'].docs << IndicatorPage.new(site, site.source, dir, inid, language, layout)
        # Also create the iframe version.
        iframe_layout = 'indicator-iframe'
        iframe_permalink = permalink + '-iframe'
        iframe_dir = index == 0 ? iframe_permalink : File.join(language_public, iframe_permalink)
        site.collections['pages'].docs << IndicatorPage.new(site, site.source, iframe_dir, inid, language, iframe_layout)
      end
    end
    # Create the indicator settings configuration/metadata/data pages.
    do_indicator_config_forms = form_settings_config && form_settings_config['enabled']
    do_indicator_meta_forms = form_settings_meta && form_settings_meta['enabled']
    do_indicator_data_forms = form_settings_data && form_settings_data['enabled']
     = form_settings_meta && form_settings_meta['translated']
    if do_indicator_config_forms || do_indicator_meta_forms || do_indicator_data_forms

       = {}
      
      if site.data.has_key?('untranslated')
         = site.data['untranslated']['meta']
      else
        default_language = language_config[0]
         = site.data[default_language]['meta']
      end

       = {}
      language_config.each do |language|
        [language] = site.data[language]['meta']
      end

      # Because we have config forms for indicator config or meta/data, we
      # take over the meta/data_edit_url and configuration_edit_url settings
      # here with simple relative links.
      if do_indicator_config_forms
        site.config['configuration_edit_url'] = 'config'
      end

      if do_indicator_meta_forms
        site.config['metadata_edit_url'] = 'metadata'
      end

      if do_indicator_data_forms
        site.config['data_edit_url'] = 'data'
      end

      # Loop through the indicators (using metadata as a list).
      if !.empty?
        # Loop through the languages.
        language_config.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
          .each do |inid, meta|
            permalink = inid
            if (meta.has_key?('permalink') and meta['permalink'] != '')
              permalink = meta['permalink']
            end
            dir_base = File.join(permalink)
            if index != 0
              dir_base = File.join(language_public, permalink)
            end

            if do_indicator_config_forms
              dir = File.join(dir_base, 'config')
              title = opensdg_translate_key('indicator.edit_configuration', translations, language)
              config_type = 'indicator'
              site.collections['pages'].docs << IndicatorConfigPage.new(site, site.source, dir, inid, language, meta, title, config_type, form_settings_config, config_builder_layout)
            end

            if do_indicator_meta_forms
               = meta
              if 
                 = [language][inid]
              end
              dir = File.join(dir_base, 'metadata')
              title = opensdg_translate_key('indicator.edit_metadata', translations, language)
              config_type = 'metadata'
              site.collections['pages'].docs << IndicatorConfigPage.new(site, site.source, dir, inid, language, , title, config_type, form_settings_meta, config_builder_layout)
            end

            if do_indicator_data_forms
              dir = File.join(dir_base, 'data')
              title = opensdg_translate_key('indicator.edit_data', translations, language)
              site.collections['pages'].docs << IndicatorDataPage.new(site, site.source, dir, inid, language, title, form_settings_data)
            end
          end
        end
      end
    end
  end
end