jekyll-open-sdg-plugins 1.0.0.rc22 → 1.2.0.pre.beta2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,92 +1,102 @@
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, language)
13
+
14
+ # Handle nil content.
15
+ if !content
16
+ content = ''
17
+ end
18
+
19
+ # Strip whitespace.
20
+ content = content.strip
21
+ # Translate if needed.
22
+ content = opensdg_translate_key(content, site.data['translations'], language)
23
+ # Next compile any Markdown.
24
+ converter = site.find_converter_instance(::Jekyll::Converters::Markdown)
25
+ content = converter.convert(content)
26
+ # Now strip any HTML.
27
+ content = content.gsub(/<\/?[^>]*>/, "")
28
+ return content
29
+ end
30
+
31
+ def generate(site)
32
+
33
+ # Generate a hash of items to include in the search index.
34
+ search_items = {}
35
+
36
+ site.collections.keys.each do |collection|
37
+ site.collections[collection].docs.each do |doc|
38
+ # Do not index configuration forms.
39
+ if doc.data.has_key?('layout') && doc.data['layout'] == 'config-builder'
40
+ next
41
+ end
42
+ # We segregate the search items by language.
43
+ language = doc.data['language']
44
+ if !search_items.has_key? language
45
+ search_items[language] = {}
46
+ end
47
+ # We'll be adding properties to this basic hash.
48
+ item = {
49
+ # The 'type' can be used on the front-end to describe a search result.
50
+ # It is assumed that all the collection names are translated in the
51
+ # "general" translation group. Eg: general.indicators, general.goals
52
+ 'type' => opensdg_translate_key('general.' + collection, site.data['translations'], language)
53
+ }
54
+ if collection == 'indicators'
55
+ # For indicators, we assign the following properties for each item.
56
+ # The URL of the page.
57
+ item['url'] = doc.data['indicator']['url']
58
+ # For the title, use the indicator name.
59
+ indicator_label = opensdg_translate_key('general.indicator', site.data['translations'], language)
60
+ item['title'] = indicator_label + ' ' + doc.data['indicator']['number'] + ' - ' + doc.data['indicator']['name']
61
+ # For the content, use the 'page_content' field.
62
+ item['content'] = prepare_content(site, doc.data['indicator']['page_content'], language)
63
+ # For the id field, use the ID number.
64
+ item['id'] = doc.data['indicator']['number']
65
+ # Also index any additional metadata fields.
66
+ if site.config['search_index_extra_fields']
67
+ site.config['search_index_extra_fields'].each do |field|
68
+ if doc.data['indicator'].has_key? field
69
+ item[field] = prepare_content(site, doc.data['indicator'][field], language)
70
+ end
71
+ end
72
+ end
73
+ elsif collection == 'goals'
74
+ # For goals, we assign the following properties for each item.
75
+ # The URL of the page.
76
+ item['url'] = doc.data['goal']['url']
77
+ # For the title we use the goal name.
78
+ goal_label = opensdg_translate_key('general.goal', site.data['translations'], language)
79
+ item['title'] = goal_label + ' ' + doc.data['goal']['number'] + ' - ' + doc.data['goal']['name']
80
+ # For the content, currently nothing here.
81
+ item['content'] = ''
82
+ # For the id field, use the ID number.
83
+ item['id'] = doc.data['goal']['number']
84
+ else
85
+ # Otherwise assume it is a normal Jekyll document.
86
+ item['url'] = File.join(doc.data['baseurl'], doc.url)
87
+ item['title'] = prepare_content(site, doc.data['title'], language)
88
+ item['content'] = prepare_content(site, doc.content, language)
89
+ item['id'] = ''
90
+ end
91
+
92
+ # Save this item in the language-specific search index.
93
+ search_items[language][item['url']] = item
94
+ end
95
+ end
96
+
97
+ # Stow the data for later use in Jekyll templates.
98
+ site.data['search_items'] = search_items
99
+
100
+ end
101
+ end
102
+ 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)