bookshop-array-structures 2.0.0.pre.alpha.4 → 2.0.0.pre.alpha.9

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
  SHA256:
3
- metadata.gz: dd6a9ea3509e93567041804ab0c7ee85cf90056f67038447a5aa12990018f2cf
4
- data.tar.gz: bb0837acf12d13e09b4d29b19f871d23ef70f16776e4080bc5546c84fa21d6b5
3
+ metadata.gz: 1e462ed292655141cffd66539af92a6d4f4cabadf23ff19d37fb94fe53b83254
4
+ data.tar.gz: 0d9836c8ad1816e22fb6cbd8c4001c3289ea8b32576e8538a79721d7366d481c
5
5
  SHA512:
6
- metadata.gz: b669694686d087649024baf90a72dfc6ac6badeeed62d493ea2ead466f5695cb501eacd2c6731617d043411e86873bf6e02a5414056d941537f9569e36999ce2
7
- data.tar.gz: 8fe61bd48e56d11621f6903f00bf4d73d1dc66215d8ea0f38e85ac17ed77a5ba19fd08cfd919df1eb62b5696a9def20c51f913ba619ae2145bd303ccd5bf4866
6
+ metadata.gz: 669c00b48bfa5819817c0d86804dfee927b3a923d4203afddb58c821450cb2341081b91c93ad07b2aeb3069bcff2db10f766d8f57a1fee83caef1a2f30029983
7
+ data.tar.gz: f4d365d566d66c731720db57ada3306228dfd0196c481bf40ea1d84c7ace0432956cc516c851ffc011be1c1f9b8e4449dedb70f07de6039eab99d4401bce8a7b
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- bookshop-array-structures (2.0.0.pre.alpha.3)
4
+ bookshop-array-structures (2.0.0.pre.alpha.7)
5
5
  dry-inflector (>= 0.1, < 1.0)
6
6
  jekyll (>= 3.7, < 5.0)
7
7
  toml-rb (>= 2.0, < 3.0)
@@ -16,7 +16,7 @@ GEM
16
16
  builder (3.2.4)
17
17
  citrus (3.0.2)
18
18
  colorator (1.1.0)
19
- concurrent-ruby (1.1.8)
19
+ concurrent-ruby (1.1.9)
20
20
  dry-inflector (0.2.0)
21
21
  em-websocket (0.5.2)
22
22
  eventmachine (>= 0.12.9)
@@ -3,8 +3,8 @@ require_relative 'lib/bookshop-array-structures/version'
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "bookshop-array-structures"
5
5
  spec.version = Bookshop::Arraystructures::VERSION
6
- spec.authors = ["Tate"]
7
- spec.email = ["tate@cloudcannon.com"]
6
+ spec.authors = ["CloudCannon"]
7
+ spec.email = ["support@cloudcannon.com"]
8
8
  spec.homepage = "https://github.com/cloudcannon/bookshop"
9
9
  spec.summary = "A Jekyll plugin to generate array structures from bookshop"
10
10
 
@@ -171,7 +171,103 @@ module Bookshop
171
171
  result["array_structures"].push("bookshop_components")
172
172
  end
173
173
  result.delete("_hidden") unless result["_hidden"].nil?
174
- return result
174
+ results = [result]
175
+ if result["_template"]
176
+ results.push(transform_template_component(result))
177
+ end
178
+ result.delete("_template")
179
+ return results
180
+ end
181
+
182
+ def self.transform_template_component(result)
183
+ schema_result = Marshal.load(Marshal.dump(result))
184
+ unwrap_structure_template(schema_result)
185
+ schema_result["value"] = templatize_values(schema_result["value"])
186
+ schema_result["_comments"] = templatize_comments(schema_result["_comments"])
187
+ schema_result["value"]["_bookshop_name"] = "#{schema_result["value"]["_bookshop_name"]}.__template"
188
+ schema_result["label"] = "Templated #{schema_result["label"]}"
189
+ schema_result["_array_structures"] = {}
190
+ schema_result["_select_data"] = {}
191
+ schema_result.delete("_template")
192
+ schema_result
193
+ end
194
+
195
+ # Breadth-first search through the structure, looking for keys
196
+ # that will match array structure or comments and flattening them
197
+ # into the root structure
198
+ def self.unwrap_structure_template(structure)
199
+ flattened_keys = {}
200
+ structure["value"].each_pair do |base_key, base_value|
201
+ flattened_keys[base_key] = base_value if base_key.start_with? "_"
202
+ end
203
+ flatten_hash(structure["value"]).each do |flat_key|
204
+ cascade_key = flat_key.split(".").last
205
+ matched_comment = structure.dig("_comments", cascade_key)
206
+ matched_substructure = structure.dig("_array_structures", cascade_key, "values", 0)
207
+
208
+ if matched_substructure
209
+ unwrap_structure_template(matched_substructure)
210
+ matched_substructure["value"].each_pair do |subkey, subvalue|
211
+ # Pull substructure's flat keys into this structure
212
+ flattened_keys["#{flat_key}.#{subkey}"] = subvalue
213
+ end
214
+ matched_substructure["_comments"].each_pair do |subkey, subcomment|
215
+ # Pull substructure's comments into this structure
216
+ structure["_comments"]["#{flat_key}.#{subkey}"] = subcomment
217
+ end
218
+ # Mark this key as an array so the include plugin knows to return
219
+ # a value and not a string
220
+ flattened_keys["#{flat_key}.__array_template"] = "{{#{cascade_key}}}"
221
+ if matched_comment
222
+ structure["_comments"].delete(cascade_key)
223
+ structure["_comments"]["#{flat_key}.__array_template"] = matched_comment
224
+ end
225
+ else
226
+ flattened_keys[flat_key] = ""
227
+ if matched_comment
228
+ structure["_comments"].delete(cascade_key)
229
+ structure["_comments"][flat_key] = matched_comment
230
+ end
231
+ end
232
+ end
233
+ structure["value"] = flattened_keys
234
+ end
235
+
236
+ # Recursively convert a hash into flat dot-notation keys
237
+ def self.flatten_hash(hash)
238
+ keys = [];
239
+ hash.each_pair do |k, v|
240
+ next if k.start_with? "_"
241
+ if v.is_a? Hash
242
+ flatten_hash(v).each do |ik|
243
+ keys.push("#{k}.#{ik}")
244
+ end
245
+ else
246
+ keys.push(k)
247
+ end
248
+ end
249
+ keys
250
+ end
251
+
252
+ def self.templatize_values(hash)
253
+ templated_hash = hash.dup
254
+ hash.each_key do |k|
255
+ next if k.start_with? "_"
256
+ next if k.end_with? "__array_template"
257
+ templated_hash.delete(k)
258
+ templated_hash["#{k}.__template"] = "{{#{k.split('.').last}}}"
259
+ end
260
+ templated_hash
261
+ end
262
+
263
+ def self.templatize_comments(hash)
264
+ templated_hash = hash.dup
265
+ hash.each_pair do |k, comment|
266
+ next if k.end_with? "__array_template"
267
+ templated_hash.delete(k)
268
+ templated_hash["#{k}.__template"] = comment
269
+ end
270
+ templated_hash
175
271
  end
176
272
 
177
273
  def self.transform_legacy_component(path, component, site)
@@ -192,7 +288,7 @@ module Bookshop
192
288
  result["array_structures"].push("components")
193
289
  end
194
290
  result.delete("_hidden") unless result["_hidden"].nil?
195
- return result
291
+ return [result]
196
292
  end
197
293
 
198
294
  def self.rewrite_bookshop_toml(content)
@@ -235,24 +331,26 @@ module Bookshop
235
331
  puts exception
236
332
  next
237
333
  end
238
- transformed_component = transform_component(f, component, site)
239
- array_structures = transformed_component.delete("array_structures")
240
- array_structures.each{|key|
241
- begin
242
- site.config["_array_structures"][key] ||= {}
243
- site.config["_array_structures"][key]["values"] ||= []
244
- site.config["_array_structures"][key]["values"].push(transformed_component)
245
- rescue => exception
246
- puts "❌ Error Adding Story to Array Structures: " + f
247
- puts "🤔 Maybe your current _config.yml has conflicting array structures?"
248
- end
334
+ transformed_components = transform_component(f, component, site)
335
+ transformed_components.each{|transformed_component|
336
+ array_structures = transformed_component.delete("array_structures")
337
+ array_structures.each{|key|
338
+ begin
339
+ site.config["_array_structures"][key] ||= {}
340
+ site.config["_array_structures"][key]["values"] ||= []
341
+ site.config["_array_structures"][key]["values"].push(transformed_component)
342
+ rescue => exception
343
+ puts " Error Adding Story to Array Structures: " + f
344
+ puts "🤔 Maybe your current _config.yml has conflicting array structures?"
345
+ end
346
+ }
249
347
  }
250
348
  end
251
349
  end
252
350
 
253
351
  def self.build_array_structures(site)
254
352
  bookshop_locations = site.config['bookshop_locations']&.collect do |location|
255
- Pathname.new(location + "/components").cleanpath.to_s
353
+ Pathname.new("#{site.source}/#{location}/components").cleanpath.to_s
256
354
  end
257
355
  bookshop_locations = bookshop_locations.select do |location|
258
356
  Dir.exist?(location)
@@ -1,5 +1,5 @@
1
1
  module Bookshop
2
2
  module Arraystructures
3
- VERSION = "2.0.0.pre.alpha.4"
3
+ VERSION = "2.0.0.pre.alpha.9"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bookshop-array-structures
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.pre.alpha.4
4
+ version: 2.0.0.pre.alpha.9
5
5
  platform: ruby
6
6
  authors:
7
- - Tate
7
+ - CloudCannon
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-05-28 00:00:00.000000000 Z
11
+ date: 2021-06-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -72,7 +72,7 @@ dependencies:
72
72
  version: '1.0'
73
73
  description:
74
74
  email:
75
- - tate@cloudcannon.com
75
+ - support@cloudcannon.com
76
76
  executables: []
77
77
  extensions: []
78
78
  extra_rdoc_files: []