Class: JekyllOpenSdgPlugins::CreatePages

Inherits:
Jekyll::Generator
  • Object
show all
Defined in:
lib/jekyll-open-sdg-plugins/create_pages.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
142
143
144
145
146
147
148
149
150
151
# File 'lib/jekyll-open-sdg-plugins/create_pages.rb', line 9

def generate(site)
  # If site.create_pages is set, create the 4 required pages. These include:
  # - the home page: /
  # - the indicators json page: /indicators.json
  # - the search results page: /search
  # - the reporting status page: /reporting-status
  #
  # These can be overridden though, with a create_pages.pages setting in
  # _config.yml, like so:
  #
  # create_pages:
  #   pages:
  #     - folder: ''
  #       layout: frontpage
  #     - filename: my-json-file.json
  #       folder: my-subfolder
  #       layout: indicator-json
  #
  # Note the optional "filename" setting for when the page needs a specific
  # filename (as opposed to being "index.html" inside a named folder).
  #
  # To use the default 4 pages, simply put:
  #
  # create_pages: true
  if (site.config['languages'] and site.config['create_pages'])

    default_pages = [
      {
        'folder' => '/reporting-status',
        'layout' => 'reportingstatus',
        'title' => 'status.reporting_status',
      },
      {
        'folder' => '/search',
        'layout' => 'search',
        'title' => 'search.search',
      }
    ]
    pages = default_pages
    if (site.config['create_pages'].is_a?(Hash) and site.config['create_pages'].key?('pages'))
      opensdg_error('The create_pages setting is not in the correct format. Please consult the latest documentation: https://open-sdg.readthedocs.io/en/latest/configuration/#create_pages')
    end
    if site.config['create_pages'].is_a?(Array)
      pages = site.config['create_pages']
    end

    # Clone pages so that we don't edit the original.
    pages = pages.clone

    # Automate some required pages if not already there.
    frontpage = pages.find { |page| page['folder'] == '/' && page['filename'] == nil }
    if frontpage == nil || frontpage['layout'] == 'frontpage-alt'
      pages.push({
        'folder' => '/',
        'layout' => 'frontpage',
      })
    end
    indicatorjson = pages.find { |page| page['layout'] == 'indicator-json' }
    if indicatorjson == nil
      pages.push({
        'folder' => '/',
        'layout' => 'indicator-json',
        'filename' => 'indicators.json'
      })
    end
    if site.config.has_key?('progressive_web_app')
      if site.config['progressive_web_app'].is_a?(Hash)
        if site.config['progressive_web_app']['enabled']
          offline = pages.find { |page| page['layout'] == 'offline' }
          if offline == nil
            pages.push({
              'folder' => '/',
              'layout' => 'offline',
              'filename' => 'offline.html'
            })
          end
        end
      end
    end

    # Hardcode the site configuration page if it's not already there.
    form_settings = site.config['site_config_form']
    config_page = pages.find { |page| page['layout'] == 'config-builder' }
    config_builder_layout = 'config-builder'
    if site.config['use_new_config_forms']
      config_builder_layout = 'config-builder-2'
    end
    if config_page == nil
      if form_settings && form_settings['enabled']
        pages.push({
          'folder' => '/config',
          'layout' => config_builder_layout,
          'title' => 'Open SDG site configuration',
          'config_type' => 'site',
          'config_filename' => 'site_config.yml',
        })
      end
    end
    # Make sure the form settings are set.
    config_page = pages.find { |page| page['layout'] == config_builder_layout }
    if config_page != nil && form_settings && form_settings['enabled']
      config_page['form_settings'] = form_settings
    end

    # 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 pages.
      pages.each do |page|
        # Add the language subfolder for all except the default (first) language.
        dir = index == 0 ? page['folder'] : File.join(language_public, page['folder'])
        # Create the page.
        site.collections['pages'].docs << OpenSdgPage.new(site, site.source, dir, page, language)
      end
    end
  end

  if site.config.has_key?('progressive_web_app') and site.config['progressive_web_app']['enabled']
    language = site.config['languages'][0]
    manifest_page = {
      'filename' => 'manifest.json',
      'folder' => '/',
      'layout' => 'manifest',
    }
    service_worker_page = {
      'filename' => 'service-worker.js',
      'folder' => '/',
      'layout' => 'service-worker',
    }
    site.collections['pages'].docs << OpenSdgPage.new(site, site.source, '/', manifest_page, language)
    site.collections['pages'].docs << OpenSdgPage.new(site, site.source, '/', service_worker_page, language)
  end
end