jekyll-open-sdg-plugins 0.0.7 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/jekyll-open-sdg-plugins.rb +2 -0
- data/lib/jekyll-open-sdg-plugins/create_pages.rb +87 -0
- data/lib/jekyll-open-sdg-plugins/get_indicator_name.rb +119 -0
- data/lib/jekyll-open-sdg-plugins/helpers.rb +52 -0
- data/lib/jekyll-open-sdg-plugins/translate_key.rb +2 -44
- data/lib/jekyll-open-sdg-plugins/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 484259f4b5f990b178a77150a142b26d9cd9b2cd9e69e0f18b3b42b9d2e76ac2
|
4
|
+
data.tar.gz: 4721d1f5fb4c309d9383f060a5b1c663fd0812972cb5fc5f321dfa6514ea80cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 76176dafc41bb541052d31f7379eb4064339c6c745a8b937b72dd90ab5f9a34cf8e8782e9e8dcffc918cdd0fd283ede45fcaf2d0ae9f13b152618dcf1158d7d0
|
7
|
+
data.tar.gz: 76166c2cb90fea4000065805853be8077dacb3dca414315aec90d20f09c963106a14b18782e94cc00dc7d8f8ae97cf21ef41a93fb6fd6fb932781ef5ce8e873f
|
@@ -3,6 +3,8 @@ require_relative "jekyll-open-sdg-plugins/multilingual_metadata"
|
|
3
3
|
require_relative "jekyll-open-sdg-plugins/translate_key"
|
4
4
|
require_relative "jekyll-open-sdg-plugins/create_indicators"
|
5
5
|
require_relative "jekyll-open-sdg-plugins/create_goals"
|
6
|
+
require_relative "jekyll-open-sdg-plugins/create_pages"
|
7
|
+
require_relative "jekyll-open-sdg-plugins/get_indicator_name"
|
6
8
|
|
7
9
|
module JekyllOpenSdgPlugins
|
8
10
|
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
require "jekyll"
|
2
|
+
|
3
|
+
module JekyllOpenSdgPlugins
|
4
|
+
class CreatePages < Jekyll::Generator
|
5
|
+
safe true
|
6
|
+
priority :low
|
7
|
+
|
8
|
+
def generate(site)
|
9
|
+
# If site.create_pages is set, create the 4 required pages. These include:
|
10
|
+
# - the home page: /
|
11
|
+
# - the indicators json page: /indicators.json
|
12
|
+
# - the search results page: /search
|
13
|
+
# - the reporting status page: /reporting-status
|
14
|
+
#
|
15
|
+
# These can be overridden though, with a create_pages.pages setting in
|
16
|
+
# _config.yml, like so:
|
17
|
+
#
|
18
|
+
# create_pages:
|
19
|
+
# pages:
|
20
|
+
# - folder: ''
|
21
|
+
# layout: frontpage
|
22
|
+
# - filename: my-json-file.json
|
23
|
+
# folder: my-subfolder
|
24
|
+
# layout: indicator-json
|
25
|
+
#
|
26
|
+
# Note the optional "filename" setting for when the page needs a specific
|
27
|
+
# filename (as opposed to being "index.html" inside a named folder).
|
28
|
+
#
|
29
|
+
# To use the default 4 pages, simply put:
|
30
|
+
#
|
31
|
+
# create_pages: true
|
32
|
+
if site.config['languages'] and site.config['create_pages']
|
33
|
+
default_pages = [
|
34
|
+
{
|
35
|
+
'folder' => '/',
|
36
|
+
'layout' => 'frontpage'
|
37
|
+
},
|
38
|
+
{
|
39
|
+
'folder' => '/reporting-status',
|
40
|
+
'layout' => 'reportingstatus'
|
41
|
+
},
|
42
|
+
{
|
43
|
+
'filename' => 'indicators.json',
|
44
|
+
'folder' => '/',
|
45
|
+
'layout' => 'indicator-json',
|
46
|
+
},
|
47
|
+
{
|
48
|
+
'folder' => '/search',
|
49
|
+
'layout' => 'search'
|
50
|
+
}
|
51
|
+
]
|
52
|
+
pages = default_pages
|
53
|
+
if site.config['create_pages'].is_a?(Hash) and site.config['create_pages'].key?('pages')
|
54
|
+
pages = site.config['create_pages']['pages']
|
55
|
+
end
|
56
|
+
|
57
|
+
# Loop through the languages.
|
58
|
+
site.config['languages'].each_with_index do |language, index|
|
59
|
+
# Loop through the pages.
|
60
|
+
pages.each do |page|
|
61
|
+
# Add the language subfolder for all except the default (first) language.
|
62
|
+
dir = index == 0 ? page['folder'] : File.join(language, page['folder'])
|
63
|
+
# Create the page.
|
64
|
+
site.pages << OpenSdgPage.new(site, site.source, dir, page, language)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# A Page subclass used in the `CreatePages` class.
|
72
|
+
class OpenSdgPage < Jekyll::Page
|
73
|
+
def initialize(site, base, dir, page, language)
|
74
|
+
@site = site
|
75
|
+
@base = base
|
76
|
+
|
77
|
+
index_files = (!page.key?('filename') or page['filename'] == 'index.html')
|
78
|
+
@dir = index_files ? File.join(dir, '/') : dir
|
79
|
+
@name = index_files ? 'index.html' : page['filename']
|
80
|
+
|
81
|
+
self.process(@name)
|
82
|
+
self.data = {}
|
83
|
+
self.data['layout'] = page['layout']
|
84
|
+
self.data['language'] = language
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
require "jekyll"
|
2
|
+
require_relative "helpers"
|
3
|
+
|
4
|
+
module Jekyll
|
5
|
+
module IndicatorName
|
6
|
+
|
7
|
+
# Takes an indicator ID (dot-delimited or dash-delimited) and returns the
|
8
|
+
# translated indicator name (according to the current language). This lookup
|
9
|
+
# is as forgiving as possible, to make sure that something is always there.
|
10
|
+
#
|
11
|
+
# The order of preference in the lookup is:
|
12
|
+
#
|
13
|
+
# 1. "indicator_name" or "title" in translated metadata
|
14
|
+
# 2. If the default language, "indicator_name" or "title" in non-translated metadata
|
15
|
+
# 3. if global, translated global indicator name
|
16
|
+
# 4. "indicator_name" or "title" in non-translated metadata
|
17
|
+
# 5. indicator ID
|
18
|
+
def get_indicator_name(inid)
|
19
|
+
|
20
|
+
# Safety code - abort now if id is nil.
|
21
|
+
if inid.nil?
|
22
|
+
return ""
|
23
|
+
end
|
24
|
+
|
25
|
+
# Also make sure it is a string, and otherwise just return it.
|
26
|
+
if not inid.is_a? String
|
27
|
+
return inid
|
28
|
+
end
|
29
|
+
|
30
|
+
# More safety code - abort now if inid is empty.
|
31
|
+
if inid.empty?
|
32
|
+
return ""
|
33
|
+
end
|
34
|
+
|
35
|
+
# Normalize around dash-delimited inids.
|
36
|
+
inid = inid.gsub('.', '-')
|
37
|
+
|
38
|
+
# Some variables to help our lookups later.
|
39
|
+
page = @context.environments.first['page']
|
40
|
+
language = page['language']
|
41
|
+
languages = @context.registers[:site].config['languages']
|
42
|
+
data = @context.registers[:site].data
|
43
|
+
translations = data['translations']
|
44
|
+
meta = data['meta'][inid]
|
45
|
+
metadata_fields = ['indicator_name', 'title']
|
46
|
+
|
47
|
+
name = false
|
48
|
+
|
49
|
+
# First choice, is there a subfolder translation of any metadata fields?
|
50
|
+
if meta and meta.has_key? language
|
51
|
+
metadata_fields.each do |field|
|
52
|
+
if !name and meta[language].has_key? field
|
53
|
+
name = meta[language][field]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# Next choice, are any of the metadata fields a translation key?
|
59
|
+
if !name
|
60
|
+
metadata_fields.each do |field|
|
61
|
+
if !name and meta and meta.has_key? field
|
62
|
+
untranslated = meta[field]
|
63
|
+
translated = opensdg_translate_key(untranslated, translations, language)
|
64
|
+
if untranslated != translated
|
65
|
+
# If the opensdg_translate_key() function returned something else,
|
66
|
+
# that means it was an actual translation key.
|
67
|
+
name = translated
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
# Next, if this is the default language, use the non-translated fields
|
74
|
+
# if available.
|
75
|
+
if !name
|
76
|
+
if language == languages[0]
|
77
|
+
metadata_fields.each do |field|
|
78
|
+
if !name and meta and meta.has_key? field
|
79
|
+
name = meta[field]
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# Next, is this a global indicator with a translation? For this we actually
|
86
|
+
# need the inid dot-delimited.
|
87
|
+
if !name
|
88
|
+
inid_dots = inid.gsub('-', '.')
|
89
|
+
if translations.has_key? language
|
90
|
+
if translations[language].has_key? 'global_indicators'
|
91
|
+
if translations[language]['global_indicators'].has_key? inid_dots
|
92
|
+
name = translations[language]['global_indicators'][inid_dots]['title']
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
# Next just return any untranslated metadata field.
|
99
|
+
if !name
|
100
|
+
metadata_fields.each do |field|
|
101
|
+
if !name and meta and meta.has_key? field
|
102
|
+
name = meta[field]
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# Still here? Just return the inid.
|
108
|
+
if !name
|
109
|
+
name = inid
|
110
|
+
end
|
111
|
+
|
112
|
+
# Finally return the name with key translation for good measure.
|
113
|
+
return opensdg_translate_key(name, translations, language)
|
114
|
+
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
Liquid::Template.register_filter(Jekyll::IndicatorName)
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# Simple collection of helper functions for use in these plugins.
|
2
|
+
|
3
|
+
# Takes a translation key and returns a translated string according to the
|
4
|
+
# language of the current page. Or if none is found, returns the original
|
5
|
+
# key.
|
6
|
+
def opensdg_translate_key(key, translations, language)
|
7
|
+
|
8
|
+
# Safety code - abort now if key is nil.
|
9
|
+
if key.nil?
|
10
|
+
return ""
|
11
|
+
end
|
12
|
+
|
13
|
+
# Also make sure it is a string, and other just return it.
|
14
|
+
if not key.is_a? String
|
15
|
+
return key
|
16
|
+
end
|
17
|
+
|
18
|
+
# More safety code - abort now if key is empty.
|
19
|
+
if key.empty?
|
20
|
+
return ""
|
21
|
+
end
|
22
|
+
|
23
|
+
# Keep track of the last thing we drilled to.
|
24
|
+
drilled = translations[language]
|
25
|
+
|
26
|
+
# Keep track of how many levels we have drilled.
|
27
|
+
levels_drilled = 0
|
28
|
+
levels = key.split('.')
|
29
|
+
|
30
|
+
# Loop through each level.
|
31
|
+
levels.each do |level|
|
32
|
+
|
33
|
+
# If we have drilled down to a scalar value too soon, abort.
|
34
|
+
break if drilled.class != Hash
|
35
|
+
|
36
|
+
if drilled.has_key? level
|
37
|
+
# If we find something, continue drilling.
|
38
|
+
drilled = drilled[level]
|
39
|
+
levels_drilled += 1
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
|
44
|
+
# If we didn't drill the right number of levels, return the
|
45
|
+
# original string.
|
46
|
+
if levels.length != levels_drilled
|
47
|
+
return key
|
48
|
+
end
|
49
|
+
|
50
|
+
# Otherwise we must have drilled all they way.
|
51
|
+
return drilled
|
52
|
+
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
require "jekyll"
|
2
|
+
require_relative "helpers"
|
2
3
|
|
3
4
|
module Jekyll
|
4
5
|
module TranslateKey
|
@@ -7,54 +8,11 @@ module Jekyll
|
|
7
8
|
# key.
|
8
9
|
def t(key)
|
9
10
|
|
10
|
-
# Safety code - abort now if key is nil.
|
11
|
-
if key.nil?
|
12
|
-
return ""
|
13
|
-
end
|
14
|
-
|
15
|
-
# Also make sure it is a string, and other just return it.
|
16
|
-
if not key.is_a? String
|
17
|
-
return key
|
18
|
-
end
|
19
|
-
|
20
|
-
# More safety code - abort now if key is empty.
|
21
|
-
if key.empty?
|
22
|
-
return ""
|
23
|
-
end
|
24
|
-
|
25
11
|
# Determine the language of the current page.
|
26
12
|
translations = @context.registers[:site].data['translations']
|
27
13
|
language = @context.environments.first["page"]['language']
|
28
14
|
|
29
|
-
|
30
|
-
drilled = translations[language]
|
31
|
-
|
32
|
-
# Keep track of how many levels we have drilled.
|
33
|
-
levels_drilled = 0
|
34
|
-
levels = key.split('.')
|
35
|
-
|
36
|
-
# Loop through each level.
|
37
|
-
levels.each do |level|
|
38
|
-
|
39
|
-
# If we have drilled down to a scalar value too soon, abort.
|
40
|
-
break if drilled.class != Hash
|
41
|
-
|
42
|
-
if drilled.has_key? level
|
43
|
-
# If we find something, continue drilling.
|
44
|
-
drilled = drilled[level]
|
45
|
-
levels_drilled += 1
|
46
|
-
end
|
47
|
-
|
48
|
-
end
|
49
|
-
|
50
|
-
# If we didn't drill the right number of levels, return the
|
51
|
-
# original string.
|
52
|
-
if levels.length != levels_drilled
|
53
|
-
return key
|
54
|
-
end
|
55
|
-
|
56
|
-
# Otherwise we must have drilled all they way.
|
57
|
-
return drilled
|
15
|
+
return opensdg_translate_key(key, translations, language)
|
58
16
|
end
|
59
17
|
end
|
60
18
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jekyll-open-sdg-plugins
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brock Fanning
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-05-
|
11
|
+
date: 2019-05-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: jekyll
|
@@ -39,6 +39,9 @@ files:
|
|
39
39
|
- lib/jekyll-open-sdg-plugins.rb
|
40
40
|
- lib/jekyll-open-sdg-plugins/create_goals.rb
|
41
41
|
- lib/jekyll-open-sdg-plugins/create_indicators.rb
|
42
|
+
- lib/jekyll-open-sdg-plugins/create_pages.rb
|
43
|
+
- lib/jekyll-open-sdg-plugins/get_indicator_name.rb
|
44
|
+
- lib/jekyll-open-sdg-plugins/helpers.rb
|
42
45
|
- lib/jekyll-open-sdg-plugins/multilingual_metadata.rb
|
43
46
|
- lib/jekyll-open-sdg-plugins/translate_key.rb
|
44
47
|
- lib/jekyll-open-sdg-plugins/version.rb
|