Class: JekyllOpenSdgPlugins::Routes

Inherits:
Jekyll::Generator
  • Object
show all
Defined in:
lib/jekyll-open-sdg-plugins/routes.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
# File 'lib/jekyll-open-sdg-plugins/routes.rb', line 9

def generate(site)

  routes = {}
  baseurl = ''
  if site.config.has_key?('baseurl') and site.config['baseurl'].is_a?(String)
    baseurl = site.config['baseurl']
  end
  unless baseurl.end_with?('/')
    baseurl = baseurl + '/'
  end

  routes['pages'] = []
  site.collections['pages'].docs.each do |doc|
    route = baseurl + doc.url
    route = route.gsub('//', '/')

    unless route.end_with?('/') or route.end_with?('.html') or route.end_with?('.json')
      route = route + '/'
    end

    unless doc.data['layout'] == 'config-builder' or doc.data['layout'] == 'data-editor' or doc.data['layout'] == 'config-builder-2'
      routes['pages'].append(route)
    end
  end

  routes['config'] = []
  site.collections['pages'].docs.each do |doc|
    route = baseurl + doc.url
    route = route.gsub('//', '/')

    unless route.end_with?('/') or route.end_with?('.html') or route.end_with?('.json')
      route = route + '/'
    end

    if doc.data['layout'] == 'config-builder' or doc.data['layout'] == 'data-editor' or doc.data['layout'] == 'config-builder-2'
      routes['config'].append(route)
    end
  end

  routes['indicators'] = []
  site.collections['indicators'].docs.each do |doc|
    route = baseurl + doc.url
    route = route.gsub('//', '/')

    unless route.end_with?('/') or route.end_with?('.html') or route.end_with?('.json')
      route = route + '/'
    end
    routes['indicators'].append(route)
  end

  routes['goals'] = []
  site.collections['goals'].docs.each do |doc|
    route = baseurl + doc.url
    route = route.gsub('//', '/')

    unless route.end_with?('/') or route.end_with?('.html') or route.end_with?('.json')
      route = route + '/'
    end
    routes['goals'].append(route)
  end

  routes['posts'] = []
  site.collections['posts'].docs.each do |doc|
    route = baseurl + doc.url
    route = route.gsub('//', '/')

    unless route.end_with?('/') or route.end_with?('.html') or route.end_with?('.json')
      route = route + '/'
    end
    routes['posts'].append(route)
  end

  routes['images'] = []
  if site.config.has_key?('logos') and site.config['logos'].length > 0
    site.config['logos'].each do ||
      route = baseurl + ['src']
      routes['images'].append(route)
    end
  end

  goal_image_base = 'https://open-sdg.org/sdg-translations/assets/img/goals'
  if site.config.has_key?('goal_image_base')
    goal_image_base = site.config['goal_image_base']
  end
  goal_image_extension = 'png'
  if site.config.has_key?('goal_image_extension') && site.config['goal_image_extension'] != ''
    goal_image_extension = site.config['goal_image_extension']
  end
  goal_image_base_contrast = goal_image_base.gsub('img/goals', 'img/high-contrast/goals')

  site.collections['goals'].docs.each do |doc|
    goal_number = doc.data['goal']['number']
    language = doc.data['language']
    if goal_number.is_a? Numeric
      goal_number = goal_number.to_s
    end
    route = goal_image_base + '/' + language + '/' + goal_number + '.' + goal_image_extension
    routes['images'].append(route)
    unless goal_image_extension == 'svg'
      route = goal_image_base_contrast + '/' + language + '/' + goal_number + '.' + goal_image_extension
      routes['images'].append(route)
    end
  end

  data_prefix = site.config['remote_data_prefix']
  unless data_prefix.start_with?('http')
    data_prefix = baseurl
  end
  unless data_prefix.end_with?('/')
    data_prefix = data_prefix + '/'
  end

  routes['json'] = []
  routes['csv'] = []
  routes['zip'] = []
  site.config['languages'].each do |language|
    site.collections['indicators'].docs.each do |doc|
      route = data_prefix + language + '/comb/' + doc.data['indicator']['slug'] + '.json'
      routes['json'].append(route)
      route = data_prefix + language + '/data/' + doc.data['indicator']['slug'] + '.csv'
      routes['csv'].append(route)
    end
    route = data_prefix + language + '/zip/' + site.data[language]['zip']['filename']
    routes['zip'].append(route)
  end

  site.data['routes'] = routes
end