bookshop-array-structures 2.0.0.pre.alpha.6 → 2.0.0.pre.alpha.11

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: b3711c17e1471fd6edbf0ea003a9acf57dfefd5a9f63bf782dc513e2017a68b0
4
- data.tar.gz: ab98a4c6242250a4fee5bfc65f942b77668e2d9b50a9a1e28edb212571753af9
3
+ metadata.gz: bc64af97dba6b97d2887320793f66249a7b17754eb3f21e3007857232bba53a9
4
+ data.tar.gz: ac861a05afe512c38ecf28f45e8da9262ef433683b9b12f59274c503d1a869e9
5
5
  SHA512:
6
- metadata.gz: a93e89d72c800dddfcd45485f9693b02ea2f2068cea4aff19e63e088986dc79c140eb40531ee76df909040836642401377ede3c50a41ec5ee059d786cc1bce71
7
- data.tar.gz: 3cd104e743326297fd72e43d46a43e93dd60bd485e77e2585860985eacc6d2a499e75a127918f64ad4ff210300bcd5be16c1c815d4fa1c2a7df0b2cbdb54d8fc
6
+ metadata.gz: '0957f4491873f58ac95b6569ed762939ccae6cbeeecb3cbf6fb7d4a2f1b5c34ef00207a189411f7666ead8a0e8bc81a524bdd0af1a2ca43a13778cefddca3450'
7
+ data.tar.gz: 4f959f0b3e6dfd264745fa5a55f355311e1e31857b469c0e894ee4cea76e17588e51451dd598ae79bbee62a3251dffd2372a923145357b7215d9e74403033a97
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.9)
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,115 @@ 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, "site")
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, parent_scope)
199
+ inflector = Dry::Inflector.new
200
+ singular_parent_scope = inflector.singularize(parent_scope)
201
+ flattened_keys = {}
202
+ structure["value"].each_pair do |base_key, base_value|
203
+ flattened_keys[base_key] = base_value if base_key.start_with? "_"
204
+ end
205
+ flatten_hash(structure["value"]).each do |flat_key|
206
+ cascade_key = flat_key.split(".").last
207
+ matched_comment = structure.dig("_comments", cascade_key)
208
+ matched_substructure = structure.dig("_array_structures", cascade_key, "values", 0)
209
+
210
+ if matched_substructure
211
+ # Mark this key as an array so the include plugin knows to return
212
+ # a value and not a string
213
+ flattened_keys["#{flat_key}.__array_template"] = "{% assign #{cascade_key} = #{singular_parent_scope}.#{cascade_key} %}"
214
+ if matched_comment
215
+ structure["_comments"].delete(cascade_key)
216
+ structure["_comments"]["#{flat_key}.__array_template"] = matched_comment
217
+ end
218
+
219
+ unwrap_structure_template(matched_substructure, cascade_key)
220
+ matched_substructure["value"].each_pair do |subkey, subvalue|
221
+ # Pull substructure's flat keys into this structure
222
+ flattened_keys["#{flat_key}.#{subkey}"] = subvalue
223
+ end
224
+ matched_substructure["_comments"].each_pair do |subkey, subcomment|
225
+ # Pull substructure's comments into this structure
226
+ structure["_comments"]["#{flat_key}.#{subkey}"] = subcomment
227
+ end
228
+ else
229
+ key_parent_scope = ""
230
+ unless singular_parent_scope == "site"
231
+ key_parent_scope = "#{singular_parent_scope}."
232
+ end
233
+ flattened_keys[flat_key] = "{{ #{key_parent_scope}#{flat_key.split('.').last} }}"
234
+ if matched_comment
235
+ structure["_comments"].delete(cascade_key)
236
+ structure["_comments"][flat_key] = matched_comment
237
+ end
238
+ end
239
+ end
240
+ structure["value"] = flattened_keys
241
+ end
242
+
243
+ # Recursively convert a hash into flat dot-notation keys
244
+ def self.flatten_hash(hash)
245
+ keys = [];
246
+ hash.each_pair do |k, v|
247
+ next if k.start_with? "_"
248
+ if v.is_a? Hash
249
+ flatten_hash(v).each do |ik|
250
+ keys.push("#{k}.#{ik}")
251
+ end
252
+ else
253
+ keys.push(k)
254
+ end
255
+ end
256
+ keys
257
+ end
258
+
259
+ def self.templatize_values(hash)
260
+ templated_hash = hash.dup
261
+ hash.each_pair do |k, v|
262
+ next if k.start_with? "_"
263
+ if k.end_with? "__array_template"
264
+ # Remove and re-add the array so position is preserved
265
+ templated_hash.delete(k)
266
+ templated_hash[k] = v
267
+ else
268
+ templated_hash.delete(k)
269
+ templated_hash["#{k}.__template"] = v
270
+ end
271
+ end
272
+ templated_hash
273
+ end
274
+
275
+ def self.templatize_comments(hash)
276
+ templated_hash = hash.dup
277
+ hash.each_pair do |k, comment|
278
+ next if k.end_with? "__array_template"
279
+ templated_hash.delete(k)
280
+ templated_hash["#{k}.__template"] = comment
281
+ end
282
+ templated_hash
175
283
  end
176
284
 
177
285
  def self.transform_legacy_component(path, component, site)
@@ -192,7 +300,7 @@ module Bookshop
192
300
  result["array_structures"].push("components")
193
301
  end
194
302
  result.delete("_hidden") unless result["_hidden"].nil?
195
- return result
303
+ return [result]
196
304
  end
197
305
 
198
306
  def self.rewrite_bookshop_toml(content)
@@ -235,17 +343,19 @@ module Bookshop
235
343
  puts exception
236
344
  next
237
345
  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
346
+ transformed_components = transform_component(f, component, site)
347
+ transformed_components.each{|transformed_component|
348
+ array_structures = transformed_component.delete("array_structures")
349
+ array_structures.each{|key|
350
+ begin
351
+ site.config["_array_structures"][key] ||= {}
352
+ site.config["_array_structures"][key]["values"] ||= []
353
+ site.config["_array_structures"][key]["values"].push(transformed_component)
354
+ rescue => exception
355
+ puts " Error Adding Story to Array Structures: " + f
356
+ puts "🤔 Maybe your current _config.yml has conflicting array structures?"
357
+ end
358
+ }
249
359
  }
250
360
  end
251
361
  end
@@ -1,5 +1,5 @@
1
1
  module Bookshop
2
2
  module Arraystructures
3
- VERSION = "2.0.0.pre.alpha.6"
3
+ VERSION = "2.0.0.pre.alpha.11"
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.6
4
+ version: 2.0.0.pre.alpha.11
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-06-02 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: []