jekyll-generator-single-source 0.0.5 → 0.0.6

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: 48cda7ea8aab0b172ac8a43d6e1f994d9dbf00cbba8489ad4d886d11c752675a
4
- data.tar.gz: 1725101cef6faad0f034db3bb0c4f4feb427905f8bf1aac58510a279031ee997
3
+ metadata.gz: 6417de38be88c7bc196b618d92f9e1cd22ede431636f801d037ae8d9370d90b9
4
+ data.tar.gz: 1584dc91a34618375f740b22b68ee3e4f297bb0d004eb47a94076063f36ba5fc
5
5
  SHA512:
6
- metadata.gz: dd24705318c18e46b31f632dff3577f94ec8e74a10c02895304df31d030ae67b70dd85d114101d24c908cbbe217c527bde70abf5d3f4706dc700f17f4eff68a6
7
- data.tar.gz: 2a8aab8f8ca538c99287fcd936ef98f885a136da3a920f0f70bb608b6c06fd06fc5c6c70bd49be44125029f6248676176e5139c4a5db23e320ef0e84477905e0
6
+ metadata.gz: 117c7d2fe0b1edf16e19ecaf941e48cf215ce801d2acc5b7ebbf004a3b2a5a114d336f14d524f7d9c367b40db32415441fa5d79b66fa829851c8b34281cd7a9b
7
+ data.tar.gz: 68d4f7f238f240ba854a97c8861c2da3ba446da7bac0ee86f23b7e07c355bc8e3acf3f5a540bb3e9bb3b5fd41b56e4719eab640760ee02ad1622539ed0cabb5a
data/README.md CHANGED
@@ -56,3 +56,108 @@ jekyll-generator-single-source:
56
56
  multiple_products: false
57
57
  base_dest_path: 'docs'
58
58
  ```
59
+
60
+ ### Inheriting configuration
61
+
62
+ It is sometimes useful to inherit documentation from else where.
63
+
64
+ To do this you can set in your config:
65
+
66
+ ```yaml
67
+ inherit:
68
+ path: /.repos/_src/
69
+ nav: ../_src/.repos/_data/docs_nav_kuma_2.0.x.yml
70
+ patches: [] # Array of modifications on your existing nav applied sequentially
71
+ ```
72
+
73
+ patches identify the section of the docs with an array `path` which lists the title in the doc tree.
74
+ For example if I have a doc:
75
+
76
+ ```yaml
77
+ items:
78
+ - title: foo
79
+ items:
80
+ - text: bar
81
+ - text: foo
82
+ ```
83
+ I can use the path: `[foo, bar]` to modify this subsection of the docs.
84
+
85
+ There are currently 4 types of patches:
86
+
87
+ #### delete
88
+
89
+ Here's a patch to delete that `bar` subsection of the docs.
90
+ ```yaml
91
+ action: delete
92
+ path: [foo]
93
+ entries: [bar] # list of subsection to delete
94
+ ```
95
+
96
+ The docs would look like:
97
+
98
+ ```yaml
99
+ items:
100
+ - title: foo
101
+ items:
102
+ - text: foo
103
+ ```
104
+
105
+ #### modify
106
+
107
+ Here's a patch to modify the `foo` section by changing the title.
108
+ ```yaml
109
+ action: modify
110
+ path: [foo]
111
+ title: foo2
112
+ ```
113
+
114
+ The docs would look like:
115
+
116
+ ```yaml
117
+ items:
118
+ - title: foo2
119
+ items:
120
+ - text: bar
121
+ - text: foo
122
+ ```
123
+
124
+ ### append
125
+
126
+ Add a subsection to the doc
127
+ ```yaml
128
+ action: append
129
+ path: [foo]
130
+ title: foo2
131
+ ```
132
+
133
+ The docs would look like:
134
+
135
+ ```yaml
136
+ items:
137
+ - title: foo
138
+ items:
139
+ - text: bar
140
+ - text: foo
141
+ - title: foo2
142
+ ```
143
+
144
+ ### insert
145
+
146
+ Add a subsection to the doc
147
+ ```yaml
148
+ action: insert
149
+ path: [foo]
150
+ index: 1 # where to insert the entry (if negative inserts from the end)
151
+ text: foo2
152
+ ```
153
+
154
+ The docs would look like:
155
+
156
+ ```yaml
157
+ items:
158
+ - title: foo
159
+ items:
160
+ - text: bar
161
+ - text: foo2
162
+ - text: foo
163
+ ```
@@ -45,8 +45,12 @@ module Jekyll
45
45
  items.map(&:generate_pages)
46
46
  end
47
47
 
48
- def nav
49
- @nav ||= file_path
48
+ def config
49
+ @config ||= begin
50
+ config = NavConfig::Base.process(@file_path)
51
+ Jekyll.logger.debug("generated config: #{config.to_json}")
52
+ config
53
+ end
50
54
  end
51
55
 
52
56
  private
@@ -56,11 +60,6 @@ module Jekyll
56
60
  v['edition'] == product && v['release'] == release
57
61
  end
58
62
  end
59
-
60
- def config
61
- @config ||= SafeYAML.load(File.read(@file_path))
62
- end
63
-
64
63
  end
65
64
  end
66
65
  end
@@ -17,7 +17,7 @@ module Jekyll
17
17
  end
18
18
  end
19
19
 
20
- def_delegators :@top_level_config, :site, :nav, :product,
20
+ def_delegators :@top_level_config, :site, :config, :product,
21
21
  :versions, :assume_generated?, :release
22
22
 
23
23
  def initialize(item_config, top_level_config)
@@ -71,11 +71,11 @@ module Jekyll
71
71
  end
72
72
 
73
73
  def source_path
74
- raise NotImplementedError.new('implement this in subclass')
74
+ raise NotImplementedError.new("implement this in subclass #{self.class}")
75
75
  end
76
76
 
77
77
  def dest_path
78
- raise NotImplementedError.new('implement this in subclass')
78
+ raise NotImplementedError.new("implement this in subclass #{self.class}")
79
79
  end
80
80
  end
81
81
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module GeneratorSingleSource
5
+ module NavConfig
6
+ class Base
7
+ def self.process(file_path)
8
+ new(file_path).process
9
+ end
10
+
11
+ def initialize(file_path)
12
+ @file_path = file_path
13
+ end
14
+
15
+ def process
16
+ if config.is_a?(Array)
17
+ config
18
+ else
19
+ HashConfig.new(file_path: @file_path, config: config).process
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def config
26
+ @config = SafeYAML.load(File.read(@file_path))
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module GeneratorSingleSource
5
+ module NavConfig
6
+ class HashConfig
7
+ REGEXP = /docs_nav_(?<product>.+)_(?<release>.+).ya?ml/
8
+
9
+ def initialize(file_path:, config:)
10
+ @file_path = file_path
11
+ @config = config
12
+ end
13
+
14
+ def process
15
+ @config['product'] = product
16
+ @config['release'] = release
17
+
18
+ inherit!
19
+ trim_config!
20
+
21
+ @config
22
+ end
23
+
24
+ private
25
+
26
+ def product
27
+ @product ||= @config.fetch('product') do |_|
28
+ REGEXP.match(File.basename(@file_path))[:product]
29
+ end
30
+ end
31
+
32
+ def release
33
+ @release ||= @config.fetch('release') do |_|
34
+ REGEXP.match(File.basename(@file_path))[:release]
35
+ end
36
+ end
37
+
38
+ def inherit?
39
+ @config.fetch('inherit', false)
40
+ end
41
+
42
+ def inherit!
43
+ return unless inherit?
44
+
45
+ InheritedConfig.new(
46
+ base_config: @config,
47
+ file_path: @file_path
48
+ ).inherit!
49
+ end
50
+
51
+ def trim_config!
52
+ if @config.key?('max_depth')
53
+ Trimmer.new(@config).trim!
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module GeneratorSingleSource
5
+ module NavConfig
6
+ class InheritedConfig
7
+ def initialize(base_config:, file_path:)
8
+ @base_config = base_config
9
+ @file_path = file_path
10
+ end
11
+
12
+ def inherit!
13
+ rewrite_paths!
14
+ rewrite_items!
15
+ apply_patches!
16
+
17
+ @base_config.delete('inherit')
18
+
19
+ @base_config
20
+ end
21
+
22
+ private
23
+
24
+ def inherited_nav
25
+ # Load the base config as a relative path to the current config
26
+ @inherited_nav ||= SafeYAML.load(
27
+ File.read(
28
+ File.join(File.dirname(@file_path), @base_config['inherit']['nav'])
29
+ )
30
+ )
31
+ end
32
+
33
+ def inherit_path
34
+ @inherit_path ||= @base_config['inherit']['path']
35
+ end
36
+
37
+ def rewrite_paths!
38
+ inherited_nav['items'].map do |entry|
39
+ PathRewriter.new(new_path: inherit_path, entry: entry).rewrite!
40
+ end
41
+ end
42
+
43
+ def rewrite_items!
44
+ @base_config['items'] = inherited_nav['items']
45
+ end
46
+
47
+ def apply_patches!
48
+ @base_config['inherit']['patches']&.each do |patch|
49
+ Patch.new(patch, @base_config).apply
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module GeneratorSingleSource
5
+ module NavConfig
6
+ class Patch
7
+ ACTIONS = ['append', 'delete', 'modify', 'insert'].freeze
8
+
9
+ def initialize(patch, items)
10
+ @path = patch.fetch('path', [])
11
+ @action = patch.fetch('action', 'append')
12
+ @idx = patch.fetch('index', nil)
13
+ @entries = patch.fetch('entries', nil)
14
+ @items = items
15
+
16
+ unless ACTIONS.include? @action
17
+ raise ArgumentError.new("action can only be #{ACTIONS.join(', ')} got `#{@action}` for patch #{patch.inspect}")
18
+ end
19
+
20
+ if @action == 'insert' && @idx == nil
21
+ raise ArgumentError.new("when using insert action you must set 'index' for patch #{patch.inspect}")
22
+ end
23
+
24
+ if @action == 'delete' && @entries == nil
25
+ raise ArgumentError.new("when using delete action you must set 'entries' for patch #{patch.inspect}")
26
+ end
27
+
28
+ raise ArgumentError.new("path must be an array for patch #{patch.inspect}") unless @path.is_a?(Array)
29
+
30
+ @patch = patch.except('action', 'path', 'index')
31
+ end
32
+
33
+ def apply
34
+ @path.each do |p|
35
+ all = @items.fetch('items', []).select do |i|
36
+ (i.fetch('title') {|_| i.fetch('text')}) == p
37
+ end
38
+ @items = all.fetch(0) { raise "Couldn't find docs section with title or text '#{p}' to apply delete patch #{self.inspect}" }
39
+ end
40
+
41
+ if @action == "delete"
42
+ @entries.each do |entry|
43
+ idx = @items['items'].index {|item| (item.fetch('title') {|_| item.fetch('text')}) == entry}
44
+ raise "Couldn't find docs section with title or text '#{entry}' to apply delete patch #{self.inspect}" unless idx != nil
45
+ @items['items'].delete_at(idx)
46
+ end
47
+ elsif @action == "modify"
48
+ @items.merge!(@patch)
49
+ elsif @action == "insert"
50
+ @items['items'].insert(@idx, @patch)
51
+ elsif @action == "append"
52
+ @items['items'].push(@patch)
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module GeneratorSingleSource
5
+ module NavConfig
6
+ class PathRewriter
7
+ def initialize(new_path:, entry:)
8
+ @new_path = new_path
9
+ @entry = entry
10
+ end
11
+
12
+ def rewrite_rec(entry)
13
+ entry['items'].map do |v|
14
+ next if v['absolute_url']
15
+ if v.has_key?('src')
16
+ original = v['src']
17
+ v['src'] = File.join(@new_path, original)
18
+ Jekyll.logger.debug("rewrote #{original} to #{v['src']}")
19
+ elsif v.has_key?('url')
20
+ original = v['url']
21
+ parts = v['url'].split('#', 2)
22
+ v['src'] = File.join(@new_path, parts[0])
23
+ Jekyll.logger.debug("rewrote #{original} to #{v['src']}")
24
+ end
25
+ rewrite_rec(v) if v.has_key?('items')
26
+ end
27
+ end
28
+
29
+ def rewrite!
30
+ rewrite_rec(@entry)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Jekyll
4
+ module GeneratorSingleSource
5
+ module NavConfig
6
+ class Trimmer
7
+ def initialize(config)
8
+ @config = config
9
+ end
10
+
11
+ def trim!
12
+ trim_depth(@config, max_depth)
13
+ end
14
+
15
+ private
16
+
17
+ def max_depth
18
+ @max_depth ||= @config.fetch('max_depth')
19
+ end
20
+
21
+ def trim_depth(entry, left)
22
+ if left == 0
23
+ entry.delete('items')
24
+ end
25
+ if entry['items']
26
+ entry['items'].each { |x| trim_depth(x, left - 1) }
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -32,6 +32,7 @@ module Jekyll
32
32
  @data['release'] = nav_item.release
33
33
  @data['version'] = nav_item.versions['default']
34
34
  @data['versions'] = nav_item.versions
35
+ @data['nav_items'] = nav_item.config
35
36
 
36
37
  # Set the layout if it's not already provided
37
38
  @data['layout'] ||= GeneratorConfig.new(@site).layout
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jekyll
4
4
  module GeneratorSingleSource
5
- VERSION = '0.0.5'
5
+ VERSION = '0.0.6'
6
6
  end
7
7
  end
@@ -8,6 +8,12 @@ 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/nav_config/base'
12
+ require_relative 'generator-single-source/nav_config/hash_config'
13
+ require_relative 'generator-single-source/nav_config/inherited_config'
14
+ require_relative 'generator-single-source/nav_config/patch'
15
+ require_relative 'generator-single-source/nav_config/path_rewriter'
16
+ require_relative 'generator-single-source/nav_config/trimmer'
11
17
  require_relative 'generator-single-source/generator_config'
12
18
  require_relative 'generator-single-source/generator'
13
19
  require_relative 'generator-single-source/single_source_page'
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.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabian Rodriguez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-25 00:00:00.000000000 Z
11
+ date: 2023-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -94,6 +94,12 @@ files:
94
94
  - lib/jekyll/generator-single-source/generator_config.rb
95
95
  - lib/jekyll/generator-single-source/hooks/version_is.rb
96
96
  - lib/jekyll/generator-single-source/liquid/tags/version_is.rb
97
+ - lib/jekyll/generator-single-source/nav_config/base.rb
98
+ - lib/jekyll/generator-single-source/nav_config/hash_config.rb
99
+ - lib/jekyll/generator-single-source/nav_config/inherited_config.rb
100
+ - lib/jekyll/generator-single-source/nav_config/patch.rb
101
+ - lib/jekyll/generator-single-source/nav_config/path_rewriter.rb
102
+ - lib/jekyll/generator-single-source/nav_config/trimmer.rb
97
103
  - lib/jekyll/generator-single-source/single_source_page.rb
98
104
  - lib/jekyll/generator-single-source/source/base.rb
99
105
  - lib/jekyll/generator-single-source/source/dir_source.rb