jekyll-open-sdg-plugins 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,92 +1,92 @@
1
- require "jekyll"
2
- require_relative "helpers"
3
-
4
- module JekyllOpenSdgPlugins
5
- class SearchIndex < Jekyll::Generator
6
- safe true
7
- priority :lowest
8
- # NOTE: This must be executed **after** the sdg_variables.rb file, since it
9
- # relies heavily on the variables created there.
10
-
11
- # Helper function to prepare content for the search index.
12
- def prepare_content(site, content)
13
-
14
- # Handle nil content.
15
- if !content
16
- content = ''
17
- end
18
-
19
- # First compile any Markdown.
20
- converter = site.find_converter_instance(::Jekyll::Converters::Markdown)
21
- content = converter.convert(content)
22
- # Now strip any HTML.
23
- content = content.gsub(/<\/?[^>]*>/, "")
24
- return content
25
- end
26
-
27
- def generate(site)
28
-
29
- # Generate a hash of items to include in the search index.
30
- search_items = {}
31
-
32
- site.collections.keys.each do |collection|
33
- site.collections[collection].docs.each do |doc|
34
- # We segregate the search items by language.
35
- language = doc.data['language']
36
- if !search_items.has_key? language
37
- search_items[language] = {}
38
- end
39
- # We'll be adding properties to this basic hash.
40
- item = {
41
- # The 'type' can be used on the front-end to describe a search result.
42
- # It is assumed that all the collection names are translated in the
43
- # "general" translation group. Eg: general.indicators, general.goals
44
- 'type' => opensdg_translate_key('general.' + collection, site.data['translations'], language)
45
- }
46
- if collection == 'indicators'
47
- # For indicators, we assign the following properties for each item.
48
- # The URL of the page.
49
- item['url'] = doc.data['indicator']['url']
50
- # For the title, use the indicator name.
51
- item['title'] = doc.data['indicator']['name']
52
- # For the content, use the 'page_content' field.
53
- item['content'] = prepare_content(site, doc.data['indicator']['page_content'])
54
- # For the id field, use the ID number.
55
- item['id'] = doc.data['indicator']['number']
56
- # Also index any additional metadata fields.
57
- if site.config['search_index_extra_fields']
58
- site.config['search_index_extra_fields'].each do |field|
59
- if doc.data['indicator'].has_key? field
60
- item[field] = prepare_content(site, doc.data['indicator'][field])
61
- end
62
- end
63
- end
64
- elsif collection == 'goals'
65
- # For goals, we assign the following properties for each item.
66
- # The URL of the page.
67
- item['url'] = doc.data['goal']['url']
68
- # For the title we use the goal name.
69
- item['title'] = doc.data['goal']['name']
70
- # For the content, currently nothing here.
71
- item['content'] = ''
72
- # For the id field, use the ID number.
73
- item['id'] = doc.data['goal']['number']
74
- else
75
- # Otherwise assume it is a normal Jekyll document.
76
- item['url'] = File.join(doc.data['baseurl'], doc.url)
77
- item['title'] = doc.data['title']
78
- item['content'] = prepare_content(site, doc.content)
79
- item['id'] = ''
80
- end
81
-
82
- # Save this item in the language-specific search index.
83
- search_items[language][item['url']] = item
84
- end
85
- end
86
-
87
- # Stow the data for later use in Jekyll templates.
88
- site.data['search_items'] = search_items
89
-
90
- end
91
- end
92
- end
1
+ require "jekyll"
2
+ require_relative "helpers"
3
+
4
+ module JekyllOpenSdgPlugins
5
+ class SearchIndex < Jekyll::Generator
6
+ safe true
7
+ priority :lowest
8
+ # NOTE: This must be executed **after** the sdg_variables.rb file, since it
9
+ # relies heavily on the variables created there.
10
+
11
+ # Helper function to prepare content for the search index.
12
+ def prepare_content(site, content)
13
+
14
+ # Handle nil content.
15
+ if !content
16
+ content = ''
17
+ end
18
+
19
+ # First compile any Markdown.
20
+ converter = site.find_converter_instance(::Jekyll::Converters::Markdown)
21
+ content = converter.convert(content)
22
+ # Now strip any HTML.
23
+ content = content.gsub(/<\/?[^>]*>/, "")
24
+ return content
25
+ end
26
+
27
+ def generate(site)
28
+
29
+ # Generate a hash of items to include in the search index.
30
+ search_items = {}
31
+
32
+ site.collections.keys.each do |collection|
33
+ site.collections[collection].docs.each do |doc|
34
+ # We segregate the search items by language.
35
+ language = doc.data['language']
36
+ if !search_items.has_key? language
37
+ search_items[language] = {}
38
+ end
39
+ # We'll be adding properties to this basic hash.
40
+ item = {
41
+ # The 'type' can be used on the front-end to describe a search result.
42
+ # It is assumed that all the collection names are translated in the
43
+ # "general" translation group. Eg: general.indicators, general.goals
44
+ 'type' => opensdg_translate_key('general.' + collection, site.data['translations'], language)
45
+ }
46
+ if collection == 'indicators'
47
+ # For indicators, we assign the following properties for each item.
48
+ # The URL of the page.
49
+ item['url'] = doc.data['indicator']['url']
50
+ # For the title, use the indicator name.
51
+ item['title'] = doc.data['indicator']['name']
52
+ # For the content, use the 'page_content' field.
53
+ item['content'] = prepare_content(site, doc.data['indicator']['page_content'])
54
+ # For the id field, use the ID number.
55
+ item['id'] = doc.data['indicator']['number']
56
+ # Also index any additional metadata fields.
57
+ if site.config['search_index_extra_fields']
58
+ site.config['search_index_extra_fields'].each do |field|
59
+ if doc.data['indicator'].has_key? field
60
+ item[field] = prepare_content(site, doc.data['indicator'][field])
61
+ end
62
+ end
63
+ end
64
+ elsif collection == 'goals'
65
+ # For goals, we assign the following properties for each item.
66
+ # The URL of the page.
67
+ item['url'] = doc.data['goal']['url']
68
+ # For the title we use the goal name.
69
+ item['title'] = doc.data['goal']['name']
70
+ # For the content, currently nothing here.
71
+ item['content'] = ''
72
+ # For the id field, use the ID number.
73
+ item['id'] = doc.data['goal']['number']
74
+ else
75
+ # Otherwise assume it is a normal Jekyll document.
76
+ item['url'] = File.join(doc.data['baseurl'], doc.url)
77
+ item['title'] = doc.data['title']
78
+ item['content'] = prepare_content(site, doc.content)
79
+ item['id'] = ''
80
+ end
81
+
82
+ # Save this item in the language-specific search index.
83
+ search_items[language][item['url']] = item
84
+ end
85
+ end
86
+
87
+ # Stow the data for later use in Jekyll templates.
88
+ site.data['search_items'] = search_items
89
+
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,43 @@
1
+ require "jekyll"
2
+ require_relative "helpers"
3
+
4
+ module JekyllOpenSdgPlugins
5
+ class SiteConfiguration < Jekyll::Generator
6
+ safe true
7
+ priority :highest
8
+
9
+ # This looks for site configuration in the data directory, and if found, copies it to
10
+ # the "site" object, as if it had been in _config.yml. It looks in "site_config" for
11
+ # configuration to move. In addition, if jekyll.environment or site.environment is
12
+ # specifically "production", then it also moves data from "site_config_prod".
13
+ #
14
+ # This allows you to keep all OpenSDG-specific config out of _config.yml, and instead
15
+ # place it in site_config and/or site_config_prod in your data directory.
16
+ def generate(site)
17
+
18
+ if site.data.has_key?('site_config')
19
+ hash_to_hash(site.data['site_config'], site.config)
20
+ end
21
+
22
+ production = false
23
+ if Jekyll.env == 'production'
24
+ production = true
25
+ end
26
+ if site.config.has_key?('environment') && site.config['environment'] == 'production'
27
+ production = true
28
+ end
29
+
30
+ if production && site.data.has_key?('site_config_prod')
31
+ hash_to_hash(site.data['site_config_prod'], site.config)
32
+ end
33
+
34
+ end
35
+
36
+ # Copy properties from a hash onto another hash.
37
+ def hash_to_hash(hash_from, hash_to)
38
+ hash_from.each do |key, value|
39
+ hash_to[key] = value
40
+ end
41
+ end
42
+ end
43
+ end
@@ -1,72 +1,98 @@
1
- require "jekyll"
2
- require_relative "helpers"
3
-
4
- module Jekyll
5
- module TranslateDate
6
-
7
- # Takes a raw Jekyll date and returns a translated string according to the
8
- # language of the current page and a date format given.
9
- def t_date(date, format_type)
10
-
11
- # Determine the language of the current page.
12
- config = @context.registers[:site].config
13
- translations = @context.registers[:site].data['translations']
14
- language = @context.environments.first['page']['language']
15
-
16
- # Try to find the specified date format in the site config. It needs to be
17
- # something like this, assuming the "format_type" param is "standard":
18
- # date_formats:
19
- # standard:
20
- # en: "%b %d, %Y"
21
- # es: "%d de %b de %Y"
22
- # etc...
23
- date_format = '%b %d, %Y'
24
- if config.has_key? 'date_formats'
25
- if config['date_formats'].has_key? format_type
26
- if config['date_formats'][format_type].has_key? language
27
- date_format = config['date_formats'][format_type][language]
28
- end
29
- end
30
- end
31
-
32
- # Support timestamps.
33
- if date.is_a? Integer
34
- date = Time.at(date)
35
- end
36
-
37
- # Convert the date into English.
38
- english = date.strftime(date_format)
39
-
40
- # Now "tokenize" that date by spaces.
41
- parts = english.split(' ')
42
-
43
- translated_parts = []
44
- parts.each do |part|
45
- # Special case: see if we need to remove a comma from the end.
46
- removed_comma = false
47
- if part.end_with? ','
48
- part = part.delete_suffix(',')
49
- removed_comma = true
50
- end
51
- # Look for a translation in the "calendar" translation group.
52
- key = 'calendar.' + part
53
- translated_part = opensdg_translate_key(key, translations, language)
54
- # If it changed from the key, that means it was a working key.
55
- if key != translated_part
56
- part = translated_part
57
- end
58
-
59
- # Add back the comma if needed.
60
- if removed_comma
61
- part = part + ','
62
- end
63
-
64
- translated_parts.push(part)
65
- end
66
-
67
- return translated_parts.join(' ')
68
- end
69
- end
70
- end
71
-
72
- Liquid::Template.register_filter(Jekyll::TranslateDate)
1
+ require "jekyll"
2
+ require_relative "helpers"
3
+
4
+ module Jekyll
5
+ module TranslateDate
6
+
7
+ # Takes a raw Jekyll date and returns a translated string according to the
8
+ # language of the current page and a date format given.
9
+ def t_date(date, format_type)
10
+
11
+ # Determine the language of the current page.
12
+ config = @context.registers[:site].config
13
+ translations = @context.registers[:site].data['translations']
14
+ language = @context.environments.first['page']['language']
15
+
16
+ # Try to find the specified date format in the site config. It needs to be
17
+ # something like this, assuming the "format_type" param is "standard":
18
+ #
19
+ # date_formats:
20
+ # - type: standard
21
+ # language: en
22
+ # format: "%b %d, %Y"
23
+ # - type: standard
24
+ # language: es
25
+ # format: "%d de %b de %Y"
26
+ #
27
+ # However the following deprecated structure is also supported:
28
+ #
29
+ # date_formats:
30
+ # standard:
31
+ # en: "%b %d, %Y"
32
+ # es: "%d de %b de %Y"
33
+ # etc...
34
+ date_format = '%b %d, %Y'
35
+ if config.has_key?('date_formats')
36
+
37
+ # @deprecated start
38
+ # In a deprecated form of date_formats, it was a nested hash keyed first
39
+ # by the format type and then by the language.
40
+ if config['date_formats'].is_a?(Hash) && config['date_formats'].has_key?(format_type)
41
+ if config['date_formats'][format_type].has_key?(language)
42
+ date_format = config['date_formats'][format_type][language]
43
+ end
44
+ end
45
+ # @deprecated end
46
+
47
+ # In the current form of data_formats, it is an array of hashes, each
48
+ # containing "type", "language", and "format" keys.
49
+ if config['date_formats'].is_a?(Array)
50
+ date_format_config = config['date_formats'].find {|d| d['type'] == format_type && d['language'] == language }
51
+ if date_format_config
52
+ date_format = date_format_config['format']
53
+ end
54
+ end
55
+
56
+ end
57
+
58
+ # Support timestamps.
59
+ if date.is_a? Integer
60
+ date = Time.at(date)
61
+ end
62
+
63
+ # Convert the date into English.
64
+ english = date.strftime(date_format)
65
+
66
+ # Now "tokenize" that date by spaces.
67
+ parts = english.split(' ')
68
+
69
+ translated_parts = []
70
+ parts.each do |part|
71
+ # Special case: see if we need to remove a comma from the end.
72
+ removed_comma = false
73
+ if part.end_with? ','
74
+ part = part.delete_suffix(',')
75
+ removed_comma = true
76
+ end
77
+ # Look for a translation in the "calendar" translation group.
78
+ key = 'calendar.' + part
79
+ translated_part = opensdg_translate_key(key, translations, language)
80
+ # If it changed from the key, that means it was a working key.
81
+ if key != translated_part
82
+ part = translated_part
83
+ end
84
+
85
+ # Add back the comma if needed.
86
+ if removed_comma
87
+ part = part + ','
88
+ end
89
+
90
+ translated_parts.push(part)
91
+ end
92
+
93
+ return translated_parts.join(' ')
94
+ end
95
+ end
96
+ end
97
+
98
+ Liquid::Template.register_filter(Jekyll::TranslateDate)
@@ -1,20 +1,20 @@
1
- require "jekyll"
2
- require_relative "helpers"
3
-
4
- module Jekyll
5
- module TranslateKey
6
- # Takes a translation key and returns a translated string according to the
7
- # language of the current page. Or if none is found, returns the original
8
- # key.
9
- def t(key)
10
-
11
- # Determine the language of the current page.
12
- translations = @context.registers[:site].data['translations']
13
- language = @context.environments.first["page"]['language']
14
-
15
- return opensdg_translate_key(key, translations, language)
16
- end
17
- end
18
- end
19
-
20
- Liquid::Template.register_filter(Jekyll::TranslateKey)
1
+ require "jekyll"
2
+ require_relative "helpers"
3
+
4
+ module Jekyll
5
+ module TranslateKey
6
+ # Takes a translation key and returns a translated string according to the
7
+ # language of the current page. Or if none is found, returns the original
8
+ # key.
9
+ def t(key)
10
+
11
+ # Determine the language of the current page.
12
+ translations = @context.registers[:site].data['translations']
13
+ language = @context.environments.first["page"]['language']
14
+
15
+ return opensdg_translate_key(key, translations, language)
16
+ end
17
+ end
18
+ end
19
+
20
+ Liquid::Template.register_filter(Jekyll::TranslateKey)
@@ -1,111 +1,111 @@
1
- require "jekyll"
2
- require_relative "helpers"
3
-
4
- module Jekyll
5
- module TranslateMetadataField
6
- # Takes a metadata field (machine name) and returns a translated string
7
- # according to the language of the current page, suitable for displaying to
8
- # the public. It gets this string by looking in the site's "schema" for a
9
- # "translation_key" property, and running that through the
10
- # opensdg_translate_key() helper function.
11
- #
12
- # Temporary backwards compatibility: If the check fails, it falls back to
13
- # checking for a translation in the 'metadata_fields' translation group.
14
- #
15
- # More backwards compatibility: If all of the above fails, it falls back to
16
- # using whatever is in a "label" property in the schema.
17
- #
18
- # Parameters
19
- # ----------
20
- # field_name : string
21
- # The machine name of a metadata field.
22
- def translate_metadata_field(field_name)
23
-
24
- # Determine the language of the current page.
25
- t = @context.registers[:site].data['translations']
26
- lang = @context.environments.first['page']['language']
27
- # Get the schema.
28
- schema = @context.registers[:site].data['schema']
29
-
30
- # Find the field.
31
- field = schema.select {|x| x['name'] == field_name }
32
- if field
33
- field = field.first()
34
- end
35
-
36
- to_translate = ''
37
- # First choice - use the 'translation_key' property from the schema.
38
- if field && field['field'].has_key?('translation_key')
39
- to_translate = field['field']['translation_key']
40
- # Next choice - try the 'metadata_fields' translation group.
41
- elsif t[lang].has_key?('metadata_fields') && t[lang]['metadata_fields'].has_key?(field_name)
42
- to_translate = 'metadata_fields.' + field_name
43
- # Next choice - use the 'label' from the schema.
44
- elsif field && field['field'].has_key?('label')
45
- to_translate = field['field']['label']
46
- # Last choice - just use the field name.
47
- else
48
- to_translate = field_name
49
- end
50
-
51
- return opensdg_translate_key(to_translate, t, lang)
52
- end
53
- end
54
-
55
- module TranslateMetadataFieldOption
56
- # Takes a metadata field (machine name) and option (value) and returns a
57
- # translated string according to the language of the current page, suitable
58
- # for displaying to the public.
59
- #
60
- # By contrast to TranslateMetadataField above, this is for translating the
61
- # options of multiple-choice schema fields. But similar to
62
- # TranslateMetadataField, this looks for a "translation_key" property on
63
- # the option in the schema.
64
- #
65
- # Temporary backwards compatibility: If the check fails, it falls back to
66
- # whatever is in a "name" property in the schema.
67
- #
68
- # Parameters
69
- # ----------
70
- # field_name : string
71
- # The machine name of a metadata field.
72
- # value : string
73
- # The 'value' of the option to use.
74
- def translate_metadata_field_option(field_name, value)
75
-
76
- # Determine the language of the current page.
77
- t = @context.registers[:site].data['translations']
78
- lang = @context.environments.first['page']['language']
79
- # Get the schema.
80
- schema = @context.registers[:site].data['schema']
81
-
82
- # Find the field.
83
- field = schema.select {|x| x['name'] == field_name}
84
- if field
85
- field = field.first()
86
- end
87
-
88
- # Fall back to the value itself.
89
- to_translate = value
90
-
91
- # Look for the 'translation_key' property from the schema.
92
- if field && field['field'].has_key?('options')
93
- option = field['field']['options'].select {|x| x['value'] == value}
94
- if option
95
- option = option.first()
96
- if option.has_key?('translation_key')
97
- to_translate = option['translation_key']
98
- else
99
- to_translate = option['name']
100
- end
101
- end
102
- end
103
-
104
- return opensdg_translate_key(to_translate, t, lang)
105
-
106
- end
107
- end
108
- end
109
-
110
- Liquid::Template.register_filter(Jekyll::TranslateMetadataField)
111
- Liquid::Template.register_filter(Jekyll::TranslateMetadataFieldOption)
1
+ require "jekyll"
2
+ require_relative "helpers"
3
+
4
+ module Jekyll
5
+ module TranslateMetadataField
6
+ # Takes a metadata field (machine name) and returns a translated string
7
+ # according to the language of the current page, suitable for displaying to
8
+ # the public. It gets this string by looking in the site's "schema" for a
9
+ # "translation_key" property, and running that through the
10
+ # opensdg_translate_key() helper function.
11
+ #
12
+ # Temporary backwards compatibility: If the check fails, it falls back to
13
+ # checking for a translation in the 'metadata_fields' translation group.
14
+ #
15
+ # More backwards compatibility: If all of the above fails, it falls back to
16
+ # using whatever is in a "label" property in the schema.
17
+ #
18
+ # Parameters
19
+ # ----------
20
+ # field_name : string
21
+ # The machine name of a metadata field.
22
+ def translate_metadata_field(field_name)
23
+
24
+ # Determine the language of the current page.
25
+ t = @context.registers[:site].data['translations']
26
+ lang = @context.environments.first['page']['language']
27
+ # Get the schema.
28
+ schema = @context.registers[:site].data['schema']
29
+
30
+ # Find the field.
31
+ field = schema.select {|x| x['name'] == field_name }
32
+ if field
33
+ field = field.first()
34
+ end
35
+
36
+ to_translate = ''
37
+ # First choice - use the 'translation_key' property from the schema.
38
+ if field && field['field'].has_key?('translation_key')
39
+ to_translate = field['field']['translation_key']
40
+ # Next choice - try the 'metadata_fields' translation group.
41
+ elsif t[lang].has_key?('metadata_fields') && t[lang]['metadata_fields'].has_key?(field_name)
42
+ to_translate = 'metadata_fields.' + field_name
43
+ # Next choice - use the 'label' from the schema.
44
+ elsif field && field['field'].has_key?('label')
45
+ to_translate = field['field']['label']
46
+ # Last choice - just use the field name.
47
+ else
48
+ to_translate = field_name
49
+ end
50
+
51
+ return opensdg_translate_key(to_translate, t, lang)
52
+ end
53
+ end
54
+
55
+ module TranslateMetadataFieldOption
56
+ # Takes a metadata field (machine name) and option (value) and returns a
57
+ # translated string according to the language of the current page, suitable
58
+ # for displaying to the public.
59
+ #
60
+ # By contrast to TranslateMetadataField above, this is for translating the
61
+ # options of multiple-choice schema fields. But similar to
62
+ # TranslateMetadataField, this looks for a "translation_key" property on
63
+ # the option in the schema.
64
+ #
65
+ # Temporary backwards compatibility: If the check fails, it falls back to
66
+ # whatever is in a "name" property in the schema.
67
+ #
68
+ # Parameters
69
+ # ----------
70
+ # field_name : string
71
+ # The machine name of a metadata field.
72
+ # value : string
73
+ # The 'value' of the option to use.
74
+ def translate_metadata_field_option(field_name, value)
75
+
76
+ # Determine the language of the current page.
77
+ t = @context.registers[:site].data['translations']
78
+ lang = @context.environments.first['page']['language']
79
+ # Get the schema.
80
+ schema = @context.registers[:site].data['schema']
81
+
82
+ # Find the field.
83
+ field = schema.select {|x| x['name'] == field_name}
84
+ if field
85
+ field = field.first()
86
+ end
87
+
88
+ # Fall back to the value itself.
89
+ to_translate = value
90
+
91
+ # Look for the 'translation_key' property from the schema.
92
+ if field && field['field'].has_key?('options')
93
+ option = field['field']['options'].select {|x| x['value'] == value}
94
+ if option
95
+ option = option.first()
96
+ if option.has_key?('translation_key')
97
+ to_translate = option['translation_key']
98
+ else
99
+ to_translate = option['name']
100
+ end
101
+ end
102
+ end
103
+
104
+ return opensdg_translate_key(to_translate, t, lang)
105
+
106
+ end
107
+ end
108
+ end
109
+
110
+ Liquid::Template.register_filter(Jekyll::TranslateMetadataField)
111
+ Liquid::Template.register_filter(Jekyll::TranslateMetadataFieldOption)