jekyll-generator-single-source 0.0.14 → 0.0.15

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e3d73df7a11546176e1abbb848a3e19abb3f7eb6e19d93eb7279de71eb420319
4
- data.tar.gz: e21334cabb7fca2ec15a0a8b477f315a526c061fb7c5f90c6fe174092160c363
3
+ metadata.gz: ac862876c4b0e34ef850bd8b001b68207f6288c6f224dabd9377a3ea0c759477
4
+ data.tar.gz: 196a02a6f09e9dab22bfb4a7e2b2f955ca8d8e183a35c3733c64919b127a6ba7
5
5
  SHA512:
6
- metadata.gz: 27dd0027533708af634579e6f865286344e50081ee7aec99e3e90d180b7b43eeaf5839827517c8c955d9fe2cdbe4ef1736f06fecc5371f1fefe553d4c3a29c9c
7
- data.tar.gz: af2050ed5376650c6957c659261cae93076653de7836db13909cefd90e9e1177ec1e7aacfeee26105733e3211d69400cfcb375e5945a6eb518f965d9077b3636
6
+ metadata.gz: 6143209587a0aed21f1a1a3ed245618e1e8a4b18db273652b50b22f9f50afd30ff3e69b272a44ab2e7cde268be2bd6c29a0c52776b94aabb2f6683a7ffd0e062
7
+ data.tar.gz: e5a9766a4bc1f9b8727f5961e99cf90d4072d7de57cef73037e84868f231c17feb4c1435612a2e58d431814ebbc8a07198d1debf87e7980482f8a9a9ef1902e9
@@ -3,13 +3,17 @@
3
3
  module Jekyll
4
4
  module GeneratorSingleSource
5
5
  class DocNavConfig
6
- attr_reader :file_path, :site
6
+ attr_reader :site
7
7
 
8
- def initialize(file_path, site)
9
- @file_path = file_path
8
+ def initialize(i18n_file:, site:)
9
+ @i18n_file = i18n_file
10
10
  @site = site
11
11
  end
12
12
 
13
+ def missing_translation?
14
+ @i18n_file.missing_translation?
15
+ end
16
+
13
17
  def skip_generation?
14
18
  # skip if release is not available because it
15
19
  # was skipped using an ENV variable
@@ -49,12 +53,24 @@ module Jekyll
49
53
 
50
54
  def config
51
55
  @config ||= begin
52
- config = NavConfig::Base.process(@file_path)
56
+ config = NavConfig::Base.process(@i18n_file.full_file_path_in_default_locale)
53
57
  Jekyll.logger.debug("generated config: #{config.to_json}")
54
58
  config
55
59
  end
56
60
  end
57
61
 
62
+ def nav_items_in_locale(locale)
63
+ @nav_items_in_locale ||= Hash.new do |h, key|
64
+ h[key] = if key == I18n.default_locale.to_s
65
+ config
66
+ else
67
+ NavConfig::Base.process(@i18n_file.full_file_path_in_locale)
68
+ end
69
+
70
+ end
71
+ @nav_items_in_locale[locale]
72
+ end
73
+
58
74
  private
59
75
 
60
76
  def edition
@@ -17,7 +17,7 @@ module Jekyll
17
17
  end
18
18
  end
19
19
 
20
- def_delegators :@top_level_config, :site, :config, :product,
20
+ def_delegators :@top_level_config, :site, :nav_items_in_locale, :product,
21
21
  :versions, :assume_generated?, :release
22
22
 
23
23
  def initialize(item_config, top_level_config)
@@ -53,15 +53,45 @@ module Jekyll
53
53
  end
54
54
 
55
55
  def source
56
- @source ||= Source::Base.make_for(path: source_path, site: site)
56
+ @source ||= Source::Base.make_for(
57
+ path: source_path,
58
+ site: site,
59
+ locale: site.config['locale']
60
+ )
57
61
  end
58
62
 
59
63
  def dest
60
64
  @dest ||= OpenStruct.new(file_path: dest_path)
61
65
  end
62
66
 
67
+ def translation_fallback?
68
+ return false if site.config['locale'] == I18n.default_locale.to_s
69
+
70
+ missing_translation?
71
+ end
72
+
73
+ def relative_path
74
+ @relative_path ||= begin
75
+ if !site.config['locale'] || site.config['locale'] == I18n.default_locale.to_s
76
+ source.file_path
77
+ else
78
+ if translation_fallback?
79
+ source.file_path
80
+ else
81
+ File.join(site.config['translated_content_path'], source.file_path)
82
+ end
83
+ end
84
+ end
85
+ end
86
+
63
87
  private
64
88
 
89
+ def missing_translation?
90
+ return true if @top_level_config.missing_translation?
91
+
92
+ source.missing_translation?
93
+ end
94
+
65
95
  def multiple_products?
66
96
  GeneratorConfig.new(site).multiple_products?
67
97
  end
@@ -1,17 +1,32 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require'pathname'
4
+
3
5
  module Jekyll
4
6
  module GeneratorSingleSource
5
7
  class Generator < Jekyll::Generator
6
- priority :highest
8
+ priority :high
7
9
 
8
10
  def generate(site)
9
11
  config = GeneratorConfig.new(site)
10
12
 
11
13
  Dir.glob(File.join(config.docs_nav_folder, 'docs_nav_*.yml')).each do |f|
12
- DocNavConfig.new(f, site).generate_pages
14
+ i18n_file = build_i18n_file(site:, file: f, config:)
15
+
16
+ # Use docs_nav_files in english as source of truth
17
+ # to generate the pages
18
+ DocNavConfig.new(i18n_file:, site:).generate_pages
13
19
  end
14
20
  end
21
+
22
+ def build_i18n_file(site:, file:, config:)
23
+ I18nFile.new(
24
+ file: Pathname.new(file).basename.to_s,
25
+ src_path: config.docs_nav_folder.gsub("#{site.source}/", ''),
26
+ locale: site.config['locale'],
27
+ site:
28
+ )
29
+ end
15
30
  end
16
31
  end
17
32
  end
@@ -37,6 +37,18 @@ module Jekyll
37
37
  config['base_dest_path']
38
38
  end
39
39
 
40
+ def build_docs_nav(edition:, release: nil)
41
+ file = release.nil? ? "docs_nav_#{edition}.yml" :"docs_nav_#{edition}_#{release}.yml"
42
+ i18n_file = I18nFile.new(
43
+ file: file,
44
+ src_path: config.fetch('docs_nav_folder'),
45
+ locale: @site.config['locale'],
46
+ site:
47
+ )
48
+
49
+ DocNavConfig.new(i18n_file: i18n_file, site: @site)
50
+ end
51
+
40
52
  private
41
53
 
42
54
  def config
@@ -0,0 +1,73 @@
1
+ module Jekyll
2
+ module GeneratorSingleSource
3
+ class I18nFile
4
+ class MissingI18nFile < StandardError; end
5
+
6
+ attr_reader :locale, :src_path
7
+
8
+ def self.exists?(file:, locale:, src_path:, site:)
9
+ new(file:, locale:, src_path:, site:).exists?
10
+ end
11
+
12
+ def initialize(file:, locale:, src_path:, site:)
13
+ @file = file # unlocalized file format
14
+ @locale = locale # locale in which we want the file
15
+ @src_path = src_path # src_path relative to app
16
+ @site = site
17
+ end
18
+
19
+ def full_file_path_in_default_locale
20
+ @full_file_path_in_default_locale ||= File.join(@site.source, @src_path, @file)
21
+ end
22
+
23
+ def full_file_path_in_locale
24
+ @full_file_path_in_locale ||= if @locale == I18n.default_locale.to_s
25
+ full_file_path_in_default_locale
26
+ else
27
+ full_file_path_in_locale_with_fallback
28
+ end
29
+ end
30
+
31
+ def missing_translation?
32
+ !exists_in_locale?
33
+ end
34
+
35
+ def exists?
36
+ File.exist?(full_file_path_in_locale)
37
+ end
38
+
39
+ def exists_in_locale?
40
+ if @locale == I18n.default_locale.to_s
41
+ File.exist?(full_file_path_in_default_locale)
42
+ else
43
+ File.exist?(full_file_path_in_locale_folder)
44
+ end
45
+ end
46
+
47
+ # path to the file relative to src_path including
48
+ def relative_file_path
49
+ @relative_file_path ||= if @src_path.to_s.empty?
50
+ @file
51
+ else
52
+ File.join(@src_path, @file)
53
+ end
54
+ end
55
+
56
+ private
57
+
58
+ def full_file_path_in_locale_with_fallback
59
+ if File.exist?(full_file_path_in_locale_folder)
60
+ full_file_path_in_locale_folder
61
+ else
62
+ full_file_path_in_default_locale
63
+ end
64
+ end
65
+
66
+ def full_file_path_in_locale_folder
67
+ @full_file_path_in_locale_folder ||= File.join(
68
+ File.expand_path(@site.config['translated_content_path'] || '', @site.source), @src_path, @file
69
+ )
70
+ end
71
+ end
72
+ end
73
+ end
@@ -22,7 +22,6 @@ module Jekyll
22
22
  private
23
23
 
24
24
  def inherited_nav
25
- # Load the base config as a relative path to the current config
26
25
  @inherited_nav ||= SafeYAML.load(
27
26
  File.read(
28
27
  File.join(File.dirname(@file_path), @base_config['inherit']['nav'])
@@ -14,7 +14,12 @@ module Jekyll
14
14
  # This is the directory that we're going to write the output file to
15
15
  @dir = nav_item.dest.file_path
16
16
 
17
- content = File.read(nav_item.source.full_file_path)
17
+ file_path = if nav_item.translation_fallback?
18
+ nav_item.source.full_file_path_in_default_locale
19
+ else
20
+ nav_item.source.full_file_path_in_locale
21
+ end
22
+ content = File.read(file_path)
18
23
 
19
24
  # Load content + frontmatter from the file
20
25
  if content =~ Jekyll::Document::YAML_FRONT_MATTER_REGEXP
@@ -22,9 +27,6 @@ module Jekyll
22
27
  @data = SafeYAML.load(Regexp.last_match(1))
23
28
  end
24
29
 
25
- # Set the "Edit on GitHub" link url
26
- @data['edit_link'] = nav_item.source.file_path
27
-
28
30
  # Make it clear that this content comes from a generated file
29
31
  @data['is_dir_index'] = nav_item.source.dir?
30
32
 
@@ -35,7 +37,21 @@ module Jekyll
35
37
  @data['version'] = release.default_version
36
38
  @data['versions'] = release.versions
37
39
 
38
- @data['nav_items'] = nav_item.config
40
+ # Set i18n related data
41
+ # locale in which the page is
42
+ locale = if nav_item.translation_fallback?
43
+ I18n.default_locale.to_s
44
+ else
45
+ @site.config['locale']
46
+ end
47
+ @data['translation_fallback'] = nav_item.translation_fallback?
48
+ @data['full_file_path_in_locale'] = nav_item.source.full_file_path_in_locale
49
+ @data['file_path'] = nav_item.source.file_path
50
+ @data['locale'] = locale
51
+ @data['nav_items'] = nav_item.nav_items_in_locale(locale)
52
+
53
+ # Set the "Edit on GitHub" link url
54
+ @data['edit_link'] = nav_item.source.file_path unless nav_item.translation_fallback?
39
55
 
40
56
  # Set the layout if it's not already provided
41
57
  @data['layout'] ||= GeneratorConfig.new(@site).layout
@@ -63,7 +79,7 @@ module Jekyll
63
79
  # Needed so that regeneration works for single sourced pages
64
80
  # It must be set to the source file
65
81
  # Also, @path MUST NOT be set, it falls back to @relative_path
66
- @relative_path = nav_item.source.file_path
82
+ @relative_path = nav_item.relative_path
67
83
  end
68
84
 
69
85
  def to_version(input)
@@ -4,38 +4,34 @@ module Jekyll
4
4
  module GeneratorSingleSource
5
5
  module Source
6
6
  class Base
7
- PATH = '_src'
7
+ extend Forwardable
8
8
 
9
- def self.make_for(path:, site:)
10
- # Normalize path
11
- file_path = File.join(
12
- PATH,
13
- path.delete_prefix('/').delete_suffix('/')
14
- )
9
+ SRC_PATH = '_src'
15
10
 
16
- src_dir = File.expand_path(site.source)
11
+ def self.make_for(path:, site:, locale:)
12
+ md_path = "#{path.delete_suffix('/')}.md"
13
+ dir_path = File.join(path.delete_suffix('/'), "index.md")
17
14
 
18
15
  # Read the source file, either `<src>.md or <src>/index.md`
19
- if File.exist?(File.join(src_dir, "#{file_path}.md"))
20
- FileSource.new("#{file_path}.md", src_dir)
21
- elsif File.exist?(File.join(src_dir, "#{file_path}/index.md"))
22
- DirSource.new("#{file_path}/index.md", src_dir)
16
+ if I18nFile.exists?(file: md_path, locale:, src_path: SRC_PATH, site:)
17
+ FileSource.new(I18nFile.new(file: md_path, locale:, src_path: SRC_PATH, site:), site)
18
+ elsif I18nFile.exists?(file: dir_path, locale:, src_path: SRC_PATH, site:)
19
+ DirSource.new(I18nFile.new(file: dir_path, locale:, src_path: SRC_PATH, site:), site)
23
20
  else
24
- raise "Could not find a source file at '#{file_path}.md' or '#{file_path}/index.md'"
21
+ raise "Could not find a source file at '_src/#{md_path}' or '_src/#{dir_path}'"
25
22
  end
26
23
  end
27
24
 
28
- def initialize(path, source_dir)
29
- @path = path
30
- @source_dir = source_dir
31
- end
25
+ def_delegators :@i18n_file, :missing_translation?, :full_file_path_in_locale,
26
+ :full_file_path_in_default_locale
32
27
 
33
- def file_path
34
- @file_path ||= @path
28
+ def initialize(i18n_file, site)
29
+ @i18n_file = i18n_file
30
+ @site = site
35
31
  end
36
32
 
37
- def full_file_path
38
- @full_file_path ||= File.expand_path(@path, @source_dir)
33
+ def file_path
34
+ @file_path ||= @i18n_file.relative_file_path
39
35
  end
40
36
  end
41
37
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jekyll
4
4
  module GeneratorSingleSource
5
- VERSION = '0.0.14'
5
+ VERSION = '0.0.15'
6
6
  end
7
7
  end
@@ -8,6 +8,7 @@ require_relative 'generator-single-source/source/base'
8
8
  require_relative 'generator-single-source/source/dir_source'
9
9
  require_relative 'generator-single-source/source/file_source'
10
10
  require_relative 'generator-single-source/doc_nav_config'
11
+ require_relative 'generator-single-source/i18n_file'
11
12
  require_relative 'generator-single-source/nav_config/base'
12
13
  require_relative 'generator-single-source/nav_config/hash_config'
13
14
  require_relative 'generator-single-source/nav_config/inherited_config'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-generator-single-source
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.14
4
+ version: 0.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabian Rodriguez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-04-29 00:00:00.000000000 Z
11
+ date: 2024-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -30,6 +30,20 @@ dependencies:
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
32
  version: '5.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: i18n
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '1'
33
47
  - !ruby/object:Gem::Dependency
34
48
  name: bundler
35
49
  requirement: !ruby/object:Gem::Requirement
@@ -93,6 +107,7 @@ files:
93
107
  - lib/jekyll/generator-single-source/generator.rb
94
108
  - lib/jekyll/generator-single-source/generator_config.rb
95
109
  - lib/jekyll/generator-single-source/hooks/version_is.rb
110
+ - lib/jekyll/generator-single-source/i18n_file.rb
96
111
  - lib/jekyll/generator-single-source/liquid/drops/release.rb
97
112
  - lib/jekyll/generator-single-source/liquid/tags/version_is.rb
98
113
  - lib/jekyll/generator-single-source/nav_config/base.rb