Top Level Namespace

Defined Under Namespace

Modules: Jekyll, JekyllOpenSdgPlugins

Instance Method Summary collapse

Instance Method Details

#opensdg_error(message) ⇒ Object

Print an error that should halt the build.



117
118
119
120
# File 'lib/jekyll-open-sdg-plugins/helpers.rb', line 117

def opensdg_error(message)
  Jekyll.logger.error message.red
  exit 1
end

#opensdg_is_path_remote(path) ⇒ Object

Is this path a remote path?



174
175
176
177
178
179
# File 'lib/jekyll-open-sdg-plugins/helpers.rb', line 174

def opensdg_is_path_remote(path)
  if path.nil?
    return false
  end
  return path.start_with?('http')
end

#opensdg_languages_public(site) ⇒ Object

Get the public language codes for a site, keyed by the actual language codes.



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/helpers.rb', line 123

def opensdg_languages_public(site)
  languages_public = site.config['languages_public']

  # The current structure of the setting is an array of hashes, each containing
  # keys for "language" and "language_public".
  if languages_public.is_a?(Array)
    converted_languages_public = Hash.new
    languages_public.each do |language_public|
      language_code = language_public['language']
      language_code_public = language_public['language_public']
      converted_languages_public[language_code] = language_code_public
    end
    return converted_languages_public
  end

  # Fallback to exactly what was retrieved from site.confg['languages_public'],
  # since the deprecated structure is exactly what this function wants.
  return languages_public
end

#opensdg_notice(message) ⇒ Object

Print a notice during compilation.



112
113
114
# File 'lib/jekyll-open-sdg-plugins/helpers.rb', line 112

def opensdg_notice(message)
  Jekyll.logger.warn message.yellow
end

#opensdg_parse_site_config(key, site) ⇒ Object

Takes a string that might be a textual representation of a site configuration, such as “baseurl” or “country.name”.



7
8
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
# File 'lib/jekyll-open-sdg-plugins/helpers.rb', line 7

def opensdg_parse_site_config(key, site)

  # Safety code - abort now if key is nil.
  if key.nil?
    return ""
  end

  # Also make sure it is a string, and other just return it.
  if not key.is_a? String
    return key
  end

  # More safety code - abort now if key is empty.
  if key.empty?
    return ""
  end

  # Keep track of the last thing we drilled to.
  drilled = site.config

  # Keep track of how many levels we have drilled.
  levels_drilled = 0
  levels = key.split('.')

  # Loop through each level.
  levels.each do |level|

    # If we have drilled down to a scalar value too soon, abort.
    break if drilled.class != Hash and drilled.class != Jekyll::Configuration

    if drilled.has_key? level
      # If we find something, continue drilling.
      drilled = drilled[level]
      levels_drilled += 1
    end

  end

  # If we didn't drill the right number of levels, return the
  # original string.
  if levels.length != levels_drilled
    return key
  end

  # Otherwise we must have drilled all they way.
  return drilled
end

#opensdg_translate_key(key, translations, language) ⇒ Object

Takes a translation key and returns a translated string according to the language of the current page. Or if none is found, returns the original key.



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
# File 'lib/jekyll-open-sdg-plugins/helpers.rb', line 58

def opensdg_translate_key(key, translations, language)

  # Safety code - abort now if key is nil.
  if key.nil?
    return ""
  end

  # Also make sure it is a string, and other just return it.
  if not key.is_a? String
    return key
  end

  # More safety code - abort now if key is empty.
  if key.empty?
    return ""
  end

  # Keep track of the last thing we drilled to.
  drilled = translations[language]

  # Keep track of how many levels we have drilled.
  levels_drilled = 0
  levels = key.split('.')

  # Loop through each level.
  levels.each do |level|

    # If we have drilled down to a scalar value too soon, abort.
    break if drilled.class != Hash

    if drilled.has_key? level
      # If we find something, continue drilling.
      drilled = drilled[level]
      levels_drilled += 1
    end

  end

  # If we didn't drill the right number of levels, return the
  # original string.
  if levels.length != levels_drilled
    return key
  end

  # If the result is still not a string, return the original string.
  if drilled.class != String
    return key
  end

  # Otherwise we must have drilled all they way.
  return drilled
end

#opensdg_validation_error(error) ⇒ Object

Print notices about a validation error.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/jekyll-open-sdg-plugins/helpers.rb', line 144

def opensdg_validation_error(error)
  if error['type'] == 'required'
    missing = []
    error['schema']['required'].each do |required_property|
      unless error['data'].has_key?(required_property)
        message = 'Missing configuration setting: ' + required_property
        if error['schema'].has_key?('title')
          message += ' (' + error['schema']['title'] + ')'
        end
        opensdg_notice(message)
      end
    end
  else
    message = 'Validation error of type: ' + error['type']
    if error['schema'] && error['schema'].has_key?('title')
      message += ' (' + error['schema']['title'] + ')'
    end
    opensdg_notice(message)
    if error['schema']
      opensdg_notice('Expected schema:')
      puts error['schema'].inspect
    end
    if error['data']
      opensdg_notice('Actual data:')
      puts error['data'].inspect
    end
  end
end