jekyll-language-plugin 1.1.8 → 1.2.0

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
  SHA1:
3
- metadata.gz: 9f68ac9191d9f6cb48cf3543c56a77b7e4feab60
4
- data.tar.gz: 361f0c76aaba69215a66c1aed0137cee6224e25c
3
+ metadata.gz: 1654e2f4bf11914d49db7c2d585ad30e82087f55
4
+ data.tar.gz: 6043c92dbc51bf59825cd72e24a0e422fc7c3ef6
5
5
  SHA512:
6
- metadata.gz: 4221a4fda1d0042a4af2ac0a7be3a78214e0ee80571470425dc9813fd4f33e3a66d5c0026d17a3c3ca38793421b00413bd17ab5f79e5c81da51b2ced57144728
7
- data.tar.gz: 8337952640d00526dd6f98421a05943f1676586535145ba9667bccf384c90296fd634e37fca823ecf4a25f80d4aa65df36966eba4d5bd31565aabb6da051a080
6
+ metadata.gz: a0389df0dc98678798a902e8dbc560666cf3fc2767ca5be77234018d038d0995932a74e1a821d8c550f1353c9115d0164b55cb45be6c1368ca8595b772b515f3
7
+ data.tar.gz: b1137b4c476b9ffd3be89dfbd6499464c4d632bafcd19521639d8491d153390f30dceb4b47de8aaf2656a6444361d89ac1e28b202bddd2f217c442618f7a4496
@@ -2,6 +2,7 @@ module Jekyll
2
2
 
3
3
  # internal requires for plugin
4
4
  autoload :LanguageReader, 'jekyll/language_reader.rb'
5
+ autoload :LanguageProperties, 'jekyll/language_properties.rb'
5
6
  autoload :LanguagePage, 'jekyll/language_page.rb'
6
7
  autoload :LanguageDocument, 'jekyll/language_document.rb'
7
8
  autoload :LanguagePageReader, 'jekyll/readers/language_page_reader.rb'
@@ -15,9 +16,27 @@ module Jekyll
15
16
  autoload :LiquidContext, 'jekyll/language-plugin/liquid_context.rb'
16
17
  autoload :DateLocalizer, 'jekyll/language-plugin/date_localizer.rb'
17
18
  autoload :VERSION, 'jekyll/language-plugin/version'
19
+
20
+ module Loaders
21
+ autoload :BaseLoader, 'jekyll/language-plugin/loaders/base_loader.rb'
22
+ end
23
+
24
+ class << self
25
+ def loaders
26
+ @loaders ||= []
27
+ end
28
+
29
+ def register_loader(loader)
30
+ loaders.push(loader)
31
+ end
32
+ end
18
33
  end
19
34
  end
20
35
 
36
+ # require data built-in loaders
37
+ require 'jekyll/language-plugin/loaders/jekyll_data_loader.rb'
38
+ require 'jekyll/language-plugin/loaders/rails_i18n_loader.rb'
39
+
21
40
  # require liquid tags and filters
22
41
  Dir[File.join(File.dirname(__FILE__), 'jekyll/language-plugin/tags/*.rb')].each{ |f| require f }
23
42
  require 'jekyll/language-plugin/filters/language_date.rb'
@@ -5,11 +5,13 @@ module Jekyll
5
5
  language_data = Jekyll::LanguagePlugin::LiquidContext.get_language_data(context)
6
6
  translation = language_data.get('date')
7
7
 
8
+ raise Jekyll::LanguagePlugin::PluginError.new('No localized date available.') if translation.nil?
9
+
8
10
  # validate language translation
9
11
  if !['abbr_daynames', 'daynames', 'abbr_monthnames', 'monthnames'].all? {|s| translation.key?(s) && translation[s].is_a?(Array) } ||
10
12
  translation['abbr_daynames'].size < 7 || translation['daynames'].size < 7 ||
11
13
  translation['abbr_monthnames'].size < 12 || translation['monthnames'].size < 12
12
- raise Jekyll::LanguagePlugin::PluginError.new('Localized date is missing translation.')
14
+ raise Jekyll::LanguagePlugin::PluginError.new('Invalid localized date translation.')
13
15
  end
14
16
 
15
17
  date.strftime(
@@ -1,57 +1,23 @@
1
1
  module Jekyll
2
2
  module LanguagePlugin
3
3
  class LanguageData
4
- attr_reader :site, :Language
4
+ attr_reader :language
5
5
 
6
6
  def initialize(site, language)
7
- @site = site
8
7
  @language = language
8
+ @loaders = Jekyll::LanguagePlugin.loaders.map{ |l| l.new(site) }
9
9
  end
10
10
 
11
- def get(keys)
12
- obj = retrieve_obj(keys)
13
- raise Jekyll::LanguagePlugin::PluginError.new("Invalid language data configuration. #{keys.join('.')} not found.") if language_data.nil?
14
- obj
15
- end
16
-
17
- def has?(keys)
18
- !retrieve_obj(keys).nil?
19
- end
20
-
21
- def retrieve_obj(keys)
22
- return nil if @language.to_s.empty?
23
- keys = resolve_dot_notation([language_data, keys])
24
-
25
- obj = site.send(keys.shift)
26
- traverse_hash(obj, keys)
27
- end
28
-
29
- def language_data()
30
- @language_data ||= site.config['language_data'].to_s || 'data.lang.%%'
31
- @language_data.gsub("%%", @language)
32
- end
33
-
34
- def traverse_hash(hash, keys)
35
- return nil if hash.nil?
36
-
37
- for key in keys
38
- if !hash.is_a?(Hash)
39
- return hash
40
- elsif !hash.key?(key)
41
- return nil
42
- end
43
- hash = hash[key]
11
+ def get(key)
12
+ @loaders.inject(nil) do |result, loader|
13
+ loader.load unless loader.loaded?
14
+ result = loader.get(key, @language)
15
+ break result unless result.nil?
44
16
  end
45
- hash
46
17
  end
47
18
 
48
- def resolve_dot_notation(keys)
49
- return keys.split('.') if keys.is_a?(String)
50
- return [] if !keys.is_a?(Enumerable)
51
-
52
- keys.flat_map do |key|
53
- resolve_dot_notation(key)
54
- end
19
+ def has?(key)
20
+ !get(key).nil?
55
21
  end
56
22
  end
57
23
  end
@@ -18,15 +18,12 @@ module Jekyll
18
18
  def self.get_language_string(context, key)
19
19
  language_data = self.get_language_data(context)
20
20
 
21
- if language_data.nil?
22
- $stdout.puts("language data is nil")
23
- end
24
21
  subset = context.registers[:page]['subset']
25
- if !subset.to_s.empty? && language_data.has?([subset, key])
26
- return language_data.get([subset, key])
27
- end
22
+ str = language_data.get([subset, key]) unless subset.to_s.empty?
23
+ str ||= language_data.get(key)
28
24
 
29
- language_data.get(key)
25
+ raise Jekyll::LanguagePlugin::PluginError.new('Key #{key} not found intranslation.') if str.nil?
26
+ str
30
27
  end
31
28
 
32
29
  def self.get_language_name(context, name)
@@ -0,0 +1,47 @@
1
+ module Jekyll
2
+ module LanguagePlugin
3
+ module Loaders
4
+ class BaseLoader
5
+ attr_reader :site, :is_loaded
6
+
7
+ def initialize(site)
8
+ @site = site
9
+ @is_loaded = false
10
+ end
11
+
12
+ def loaded?
13
+ @is_loaded
14
+ end
15
+
16
+ def load
17
+ @is_loaded
18
+ end
19
+
20
+ def get(key, language)
21
+ nil
22
+ end
23
+
24
+ def traverse_hash(hash, keys)
25
+ for key in keys
26
+ if !hash.is_a?(Hash)
27
+ return hash
28
+ elsif !hash.key?(key)
29
+ return nil
30
+ end
31
+ hash = hash[key]
32
+ end
33
+ hash
34
+ end
35
+
36
+ def resolve_dot_notation(keys)
37
+ return keys.split('.') if keys.is_a?(String)
38
+ return [] if !keys.is_a?(Enumerable)
39
+
40
+ keys.flat_map do |key|
41
+ resolve_dot_notation(key)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,33 @@
1
+ module Jekyll
2
+ module LanguagePlugin
3
+ module Loaders
4
+ class JekyllDataLoader < BaseLoader
5
+ def initialize(site)
6
+ super
7
+ @is_loaded = true
8
+ end
9
+
10
+ def get(key, language)
11
+ key = resolve_dot_notation(key)
12
+
13
+ data = get_language_data(language)
14
+ traverse_hash(data, key)
15
+ end
16
+
17
+ def get_language_data(language)
18
+ @language_data ||= site.config['language_data'].to_s || 'data.lang.%%'
19
+ language_data_l = @language_data.gsub("%%", language)
20
+
21
+ key = resolve_dot_notation(language_data_l)
22
+
23
+ obj = site.send(key.shift)
24
+ obj = traverse_hash(obj, key)
25
+ raise Jekyll::LanguagePlugin::PluginError.new("Invalid language data configuration. Cannot retrieve data for language #{language} at #{language_data_l}") if obj.nil?
26
+ obj
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ Jekyll::LanguagePlugin.register_loader(Jekyll::LanguagePlugin::Loaders::JekyllDataLoader)
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'yaml'
3
+
4
+ module Jekyll
5
+ module LanguagePlugin
6
+ module Loaders
7
+ class RailsI18nLoader < BaseLoader
8
+ @@data = nil
9
+
10
+ def load
11
+ if !@is_loaded
12
+ self.class.load_data if @@data.nil?
13
+ @is_loaded = true
14
+ end
15
+ end
16
+
17
+ def get(key, language)
18
+ return nil unless loaded?
19
+ traverse_hash(@@data, [language, key])
20
+ end
21
+
22
+ def self.load_data
23
+ gem_root = Gem.loaded_specs['rails-i18n'].full_gem_path
24
+ files = Dir[File.join(gem_root, 'rails', '**', '*.yml')]
25
+ @@data = files.inject(Hash.new) do |data, file|
26
+ key = File.basename(file, '.yml')
27
+ source = YAML.load_file(file)
28
+ break data if !source.has_key?(key) || source[key]['date'].nil?
29
+
30
+ # extract date object
31
+ date = source[key]['date']
32
+ date.delete_if { |k, _| k !~ /\A(?:abbr_day_names|abbr_month_names|day_names|month_names)\z/ }
33
+ date = Hash[date.map { |k, v| [k.sub(/_names\Z/, 'names'), v] }]
34
+ date['abbr_monthnames'].shift
35
+ date['monthnames'].shift
36
+
37
+ data.merge!({
38
+ key => { 'date' => date }
39
+ })
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ begin
48
+ require 'rails-i18n'
49
+ Jekyll::LanguagePlugin.register_loader(Jekyll::LanguagePlugin::Loaders::RailsI18nLoader)
50
+ rescue LoadError
51
+ end
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
2
  module LanguagePlugin
3
- VERSION = '1.1.8'
3
+ VERSION = '1.2.0'
4
4
  end
5
5
  end
@@ -1,23 +1,10 @@
1
1
  module Jekyll
2
2
  class LanguageDocument < Document
3
+ include LanguageProperties
4
+
3
5
  alias_method :url_template_orig, :url_template
4
6
  alias_method :url_placeholders_orig, :url_placeholders
5
7
 
6
- def language
7
- return nil if data.nil? || data['language'].nil?
8
- data['language']
9
- end
10
-
11
- def languages
12
- return nil if data.nil? || data['languages'].nil?
13
- data['languages']
14
- end
15
-
16
- def subset
17
- return nil if data.nil? || data['subset'].nil?
18
- data['subset']
19
- end
20
-
21
8
  def url_template
22
9
  return "/:language" + url_template_orig if !language.nil?
23
10
  url_template_orig
@@ -1,23 +1,10 @@
1
1
  module Jekyll
2
2
  class LanguagePage < Page
3
+ include LanguageProperties
4
+
3
5
  alias_method :template_orig, :template
4
6
  alias_method :url_placeholders_orig, :url_placeholders
5
7
 
6
- def language
7
- return nil if data.nil? || data['language'].nil?
8
- data['language']
9
- end
10
-
11
- def languages
12
- return nil if data.nil? || data['languages'].nil?
13
- data['languages']
14
- end
15
-
16
- def subset
17
- return nil if data.nil? || data['subset'].nil?
18
- data['subset']
19
- end
20
-
21
8
  def template
22
9
  return "/:language" + template_orig if !language.nil?
23
10
  template_orig
@@ -32,8 +19,12 @@ module Jekyll
32
19
  if !language.nil? && !subset.nil?
33
20
  @language_data ||= LanguagePlugin::LanguageData.new(@site, language)
34
21
 
35
- data = @language_data.get(subset).reject{ |k, v| v.is_a?(Enumerable) }
36
- result.merge!(Hash[data.map{ |k, v| ["t.#{k}", v] }])
22
+ data = @language_data.get(subset)
23
+
24
+ if !data.nil?
25
+ filtered = data.reject{ |k, v| v.is_a?(Enumerable) }
26
+ result.merge!(Hash[filtered.map{ |k, v| ["t.#{k}", v] }])
27
+ end
37
28
  end
38
29
 
39
30
  result
@@ -0,0 +1,18 @@
1
+ module Jekyll
2
+ module LanguageProperties
3
+ def language
4
+ return nil if data.nil? || data['language'].nil?
5
+ data['language']
6
+ end
7
+
8
+ def languages
9
+ return nil if data.nil? || data['languages'].nil?
10
+ data['languages']
11
+ end
12
+
13
+ def subset
14
+ return nil if data.nil? || data['subset'].nil?
15
+ data['subset']
16
+ end
17
+ end
18
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-language-plugin
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.8
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vincent Wochnik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-11 00:00:00.000000000 Z
11
+ date: 2015-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -78,6 +78,9 @@ files:
78
78
  - lib/jekyll/language-plugin/filters/language_date.rb
79
79
  - lib/jekyll/language-plugin/language_data.rb
80
80
  - lib/jekyll/language-plugin/liquid_context.rb
81
+ - lib/jekyll/language-plugin/loaders/base_loader.rb
82
+ - lib/jekyll/language-plugin/loaders/jekyll_data_loader.rb
83
+ - lib/jekyll/language-plugin/loaders/rails_i18n_loader.rb
81
84
  - lib/jekyll/language-plugin/plugin_error.rb
82
85
  - lib/jekyll/language-plugin/tags/language.rb
83
86
  - lib/jekyll/language-plugin/tags/language_include.rb
@@ -85,6 +88,7 @@ files:
85
88
  - lib/jekyll/language-plugin/version.rb
86
89
  - lib/jekyll/language_document.rb
87
90
  - lib/jekyll/language_page.rb
91
+ - lib/jekyll/language_properties.rb
88
92
  - lib/jekyll/language_reader.rb
89
93
  - lib/jekyll/readers/language_page_reader.rb
90
94
  - lib/jekyll/readers/language_post_reader.rb