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
# 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' => '/',
        'layout' => 'frontpage'
      },
      {
        'folder' => '/reporting-status',
        'layout' => 'reportingstatus',
        'title' => 'status.reporting_status',
      },
      {
        'filename' => 'indicators.json',
        'folder' => '/',
        'layout' => 'indicator-json',
      },
      {
        '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')
      # Backwards compatability to support the deprecated "pages" key.

      pages = site.config['create_pages']['pages']
    elsif 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

    # Hardcode the site configuration page if it's not already there.

    config_page = pages.find { |page| page['layout'] == 'config-builder' }
    if config_page == nil
      if site.config['create_config_forms'] && site.config['create_config_forms'].key?('layout') && site.config['create_config_forms']['layout'] != ''
        pages.push({
          'folder' => '/config',
          'layout' => site.config['create_config_forms']['layout'],
          'title' => 'Open SDG site configuration',
          'config_type' => 'site',
          'config_filename' => 'site_config.yml'
        })
      end
    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
end