jekyll-open-sdg-plugins 1.0.0.rc10 → 1.0.0.rc11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 15ab6b5be8eeff35429449800edc336a66e125dd0e4ea8818890998a278cee4d
4
- data.tar.gz: 6d728c433f257d54d37a5b714bfec1a08446b4843bfe31e856145e4b21f1cb03
3
+ metadata.gz: 3668b012dcfdd9957e10f37855cc46b01ca36fa7da976f45797c15272ebb4086
4
+ data.tar.gz: 3a87d3869abbe9286df6cf1656c1b0d7f2ab7d6aac033c959216a151fc741e9f
5
5
  SHA512:
6
- metadata.gz: 640399d80ef3c0354592fa9c5071df0978a3faf297303a2bc99f61f18a21079558c31560d331340a4f277de3dc7eef9dbaaa03724813f082672eccbe3b069417
7
- data.tar.gz: ff0c4a04cf8566480df015b6dc9355c8bab6dda18cf3f572b7ad961e43e32918206fae058ab5120758737274a76c997349373a0a5c49bb2739986de93b48d2bf
6
+ metadata.gz: a589b05b8c085a70ece51a29ba5435b00757ccd91b87f75b3e56c6f3fd9b22f3c3cb868a6a13b73472fee32f2ed6a4e0943ecdc5579eaf7396070db058964616
7
+ data.tar.gz: a330617aee360e7ad4ccbd0795bfcca9b5ae1943f79eca35814b34e963e2614ed74cb1d5bbf461377a4651a60f6d159ed6121e513c9df4bdfbd229cbf87bc581
@@ -9,111 +9,130 @@ module JekyllOpenSdgPlugins
9
9
  safe true
10
10
  priority :highest
11
11
 
12
- def download_build(prefix)
12
+ # Fix a Unix path in case we are on Windows.
13
+ def fix_path(path)
14
+ path_parts = path.split('/')
15
+ return path_parts.join(File::SEPARATOR)
16
+ end
13
17
 
14
- endpoints = {
18
+ # Our hardcoded list of pieces of the build that we expect.
19
+ def get_endpoints()
20
+ return {
15
21
  'meta' => 'meta/all.json',
16
22
  'headlines' => 'headline/all.json',
17
23
  'schema' => 'meta/schema.json',
18
24
  'reporting' => 'stats/reporting.json',
19
25
  'translations' => 'translations/translations.json'
20
26
  }
27
+ end
28
+
29
+ # Is this path a remote path?
30
+ def is_path_remote(path)
31
+ return path.start_with?('http')
32
+ end
33
+
34
+ # Get a build from a local folder on disk or a remote URL on the Internet.
35
+ def fetch_build(path)
36
+
37
+ is_remote = is_path_remote(path)
21
38
  build = {}
22
- endpoints.each do |key, value|
23
- endpoint = prefix + '/' + value
39
+ get_endpoints().each do |key, value|
40
+ endpoint = is_remote ? path + '/' + value : File.join(path, fix_path(value))
41
+
24
42
  begin
25
- source = JSON.load(open(endpoint))
26
- build[key] = source
43
+ json_file = is_remote ? open(endpoint) : File.open(endpoint)
44
+ build[key] = JSON.load(json_file)
27
45
  rescue StandardError => e
28
46
  # For backwards compatibility, we allow 'translations' to be missing.
29
47
  if key != 'translations'
30
48
  puts e.message
31
- abort 'Unable to fetch remote data from: ' + endpoint
49
+ abort 'Unable to read data from: ' + endpoint
32
50
  end
33
51
  end
34
52
  end
35
- build
53
+
54
+ return build
55
+ end
56
+
57
+ # Predict (before data has been fetched) whether the site is using
58
+ # translated builds or not.
59
+ def site_uses_translated_builds(path)
60
+
61
+ is_remote = is_path_remote(path)
62
+ endpoints = get_endpoints()
63
+ # For a quick test, we just use 'meta'.
64
+ meta = endpoints['meta']
65
+ endpoint = is_remote ? path + '/' + meta : File.join(path, fix_path(meta))
66
+
67
+ begin
68
+ json_file = is_remote ? open(endpoint) : File.open(endpoint)
69
+ rescue StandardError => e
70
+ # If we didn't find an untranslated 'meta', we assume translated builds.
71
+ return true
72
+ end
73
+
74
+ # Other wise assume untranslated builds.
75
+ return false
36
76
  end
37
77
 
38
78
  def generate(site)
39
79
 
40
- if site.config['remote_data_prefix']
41
- prefix = site.config['remote_data_prefix']
80
+ # For below, make sure there is at least an empty hash at
81
+ # site.data.translations.
82
+ if !site.data.has_key?('translations')
83
+ site.data['translations'] = {}
84
+ end
42
85
 
43
- # For below, make sure there is at least an empty hash at
44
- # site.data.translations.
45
- if !site.data.has_key?('translations')
46
- site.data['translations'] = {}
47
- end
86
+ remote = site.config['remote_data_prefix']
87
+ local = site.config['local_data_folder']
48
88
 
49
- # How do we tell, before data has been fetched, whether the site is
50
- # using translated builds? Quick and dirty way - attempt a download
51
- # of the non-tranlsated build. If it fails, assume translated builds.
52
- translated_builds = false
53
- begin
54
- JSON.load(open(prefix + '/meta/all.json'))
55
- rescue StandardError => e
56
- translated_builds = true
57
- end
89
+ if !remote && !local
90
+ abort 'Site config must include either "remote_data_prefix" or "local_data_folder".'
91
+ end
58
92
 
59
- if translated_builds
60
- # For translated builds, we download a build for each language, and
61
- # place them in "subfolders" (so to speak) of site.data.
62
- site.config['languages'].each do |language|
63
- data_target = site.data[language]
64
- data_source = download_build(prefix + '/' + language)
65
- if data_target
66
- data_target.deep_merge(data_source)
67
- else
68
- site.data[language] = data_source
69
- end
70
- # Additionally, we move the language-specific translations to the
71
- # site.data.translations location, where all translations are kept.
72
- translation_target = site.data['translations'][language]
73
- translation_source = site.data[language]['translations']
74
- if translation_target
75
- translation_target.deep_merge(translation_source)
76
- else
77
- site.data['translations'][language] = translation_source
78
- end
93
+ build_location = remote ? remote : File.join(Dir.pwd, local)
94
+ translated_builds = site_uses_translated_builds(build_location)
95
+
96
+ if translated_builds
97
+ # For translated builds, we get a build for each language, and
98
+ # place them in "subfolders" (so to speak) of site.data.
99
+ site.config['languages'].each do |language|
100
+ data_target = site.data[language]
101
+ translated_build = remote ? build_location + '/' + language : File.join(build_location, language)
102
+ data_source = fetch_build(translated_build)
103
+ if data_target
104
+ data_target.deep_merge(data_source)
105
+ else
106
+ site.data[language] = data_source
79
107
  end
80
- else
81
- # For untranslated builds, we download one build only, and place it
82
- # in the "root" (so to speak) of site.data. Nothing else is needed.
83
- target = site.data
84
- source = download_build(prefix)
85
- target.deep_merge(source)
86
108
  end
87
- else
88
- # If remote data is not configured, check to see if Jekyll's data folder
89
- # already contains the data.
90
- data_is_local = false
91
- # Data will either be directly in site.data, or in a folder per language,
92
- # depending on whether we are using translated builds.
93
- if site.data.has_key? 'meta'
94
- # We could be more thorough and check all the endpoints, but for now
95
- # just assume that if 'meta' is there, all the endpoints are.
96
- data_is_local = true
97
- else
98
- data_for_all_languages = true
99
- site.config['languages'].each do |language|
100
- if !site.data.has_key? language || !site.data[language].has_key? 'meta'
101
- data_for_all_languages = false
102
- end
109
+ # We move the language-specific translations to the
110
+ # site.data.translations location, where all translations are kept.
111
+ site.config['languages'].each do |language|
112
+ translation_target = site.data['translations'][language]
113
+ translation_source = site.data[language]['translations']
114
+ if translation_target
115
+ translation_target.deep_merge(translation_source)
116
+ else
117
+ site.data['translations'][language] = translation_source
103
118
  end
104
- if data_for_all_languages
105
- data_is_local = true
106
- end
107
- end
108
- # Finally give an error is there is no local data.
109
- if !data_is_local
110
- abort 'The "remote_data_prefix" configuration setting is missing and there is no local data.'
111
119
  end
120
+ # And there are some parts of the build that don't need to be translated
121
+ # and should be moved to the top level.
122
+ first_language = site.config['languages'][0]
123
+ site.data['reporting'] = site.data[first_language]['reporting']
124
+ site.data['schema'] = site.data[first_language]['schema']
125
+ else
126
+ # For untranslated builds, we download one build only, and place it
127
+ # in the "root" (so to speak) of site.data. Nothing else is needed.
128
+ target = site.data
129
+ source = fetch_build(build_location)
130
+ target.deep_merge(source)
112
131
  end
113
132
 
114
133
  # Finally support the deprecated 'remote_translations' option.
115
134
  # This is deprecated because translations should now be in the
116
- # data repository, where they will be fetched in download_build().
135
+ # data repository, where they will be fetched in fetch_build().
117
136
  if site.config['remote_translations']
118
137
  key = 'translations'
119
138
  target = site.data[key]
@@ -133,4 +152,14 @@ module JekyllOpenSdgPlugins
133
152
  end
134
153
  end
135
154
  end
155
+
156
+ # This makes sure that the contents of the "local_data_folder" get copied
157
+ # into the Jekyll build, so that they can be served from the website.
158
+ Jekyll::Hooks.register :site, :post_write do |site|
159
+ if site.config['local_data_folder']
160
+ source = File.join(Dir.pwd, site.config['local_data_folder'], '.')
161
+ destination = site.config['destination']
162
+ FileUtils.cp_r(source, destination)
163
+ end
164
+ end
136
165
  end
@@ -318,7 +318,11 @@ module JekyllOpenSdgPlugins
318
318
  doc.data['t'] = site.data['translations'][language]
319
319
 
320
320
  # Set the remote_data_prefix for this indicator.
321
- doc.data['remote_data_prefix'] = site.config['remote_data_prefix']
321
+ if site.config.has_key? 'remote_data_prefix'
322
+ doc.data['remote_data_prefix'] = site.config['remote_data_prefix']
323
+ elsif site.config.has_key? 'local_data_folder'
324
+ doc.data['remote_data_prefix'] = doc.data['baseurl']
325
+ end
322
326
  if opensdg_translated_builds(site)
323
327
  doc.data['remote_data_prefix'] += '/' + language
324
328
  end
@@ -1,3 +1,3 @@
1
1
  module JekyllOpenSdgPlugins
2
- VERSION = "1.0.0.rc10".freeze
2
+ VERSION = "1.0.0.rc11".freeze
3
3
  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: 1.0.0.rc10
4
+ version: 1.0.0.rc11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brock Fanning
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-08 00:00:00.000000000 Z
11
+ date: 2020-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll