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

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: a9c4b7006d6acc1a01ccb184fb5d9122e03acc278d40a9194c5a51e5e53c39ad
4
- data.tar.gz: aff6856e93b8c304e5003fad6b943148e24253ec2e30a566b4912c12f5150d52
3
+ metadata.gz: dd6a9ea3509e93567041804ab0c7ee85cf90056f67038447a5aa12990018f2cf
4
+ data.tar.gz: bb0837acf12d13e09b4d29b19f871d23ef70f16776e4080bc5546c84fa21d6b5
5
5
  SHA512:
6
- metadata.gz: a1fb28bef8d8e90f581be164b8dec8fe3689b0801bad16fdad31b2b8cebc7a287d4c46edd48805928a3e6abd4d494016ae5f405dfb3119b1a9033232fb966797
7
- data.tar.gz: ca588fddf15bc24e5814c259e36d89fc9b20beb86a2dd5b204f05d4535c444af710e5a30eca0507e9682c190705cc87f9fc6f95dc69bcaa52a12cf468e02f1c5
6
+ metadata.gz: b669694686d087649024baf90a72dfc6ac6badeeed62d493ea2ead466f5695cb501eacd2c6731617d043411e86873bf6e02a5414056d941537f9569e36999ce2
7
+ data.tar.gz: 8fe61bd48e56d11621f6903f00bf4d73d1dc66215d8ea0f38e85ac17ed77a5ba19fd08cfd919df1eb62b5696a9def20c51f913ba619ae2145bd303ccd5bf4866
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ group :test do
6
+ gem "rubocop", "~> 0.51.0"
7
+ gem "hashdiff"
8
+ gem "rake"
9
+ gem "minitest"
10
+ gem 'minitest-reporters'
11
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,109 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bookshop-array-structures (2.0.0.pre.alpha.3)
5
+ dry-inflector (>= 0.1, < 1.0)
6
+ jekyll (>= 3.7, < 5.0)
7
+ toml-rb (>= 2.0, < 3.0)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ addressable (2.7.0)
13
+ public_suffix (>= 2.0.2, < 5.0)
14
+ ansi (1.5.0)
15
+ ast (2.4.2)
16
+ builder (3.2.4)
17
+ citrus (3.0.2)
18
+ colorator (1.1.0)
19
+ concurrent-ruby (1.1.8)
20
+ dry-inflector (0.2.0)
21
+ em-websocket (0.5.2)
22
+ eventmachine (>= 0.12.9)
23
+ http_parser.rb (~> 0.6.0)
24
+ eventmachine (1.2.7)
25
+ ffi (1.15.1)
26
+ forwardable-extended (2.6.0)
27
+ hashdiff (1.0.1)
28
+ http_parser.rb (0.6.0)
29
+ i18n (1.8.10)
30
+ concurrent-ruby (~> 1.0)
31
+ jekyll (4.2.0)
32
+ addressable (~> 2.4)
33
+ colorator (~> 1.0)
34
+ em-websocket (~> 0.5)
35
+ i18n (~> 1.0)
36
+ jekyll-sass-converter (~> 2.0)
37
+ jekyll-watch (~> 2.0)
38
+ kramdown (~> 2.3)
39
+ kramdown-parser-gfm (~> 1.0)
40
+ liquid (~> 4.0)
41
+ mercenary (~> 0.4.0)
42
+ pathutil (~> 0.9)
43
+ rouge (~> 3.0)
44
+ safe_yaml (~> 1.0)
45
+ terminal-table (~> 2.0)
46
+ jekyll-sass-converter (2.1.0)
47
+ sassc (> 2.0.1, < 3.0)
48
+ jekyll-watch (2.2.1)
49
+ listen (~> 3.0)
50
+ kramdown (2.3.1)
51
+ rexml
52
+ kramdown-parser-gfm (1.1.0)
53
+ kramdown (~> 2.0)
54
+ liquid (4.0.3)
55
+ listen (3.5.1)
56
+ rb-fsevent (~> 0.10, >= 0.10.3)
57
+ rb-inotify (~> 0.9, >= 0.9.10)
58
+ mercenary (0.4.0)
59
+ minitest (5.14.4)
60
+ minitest-reporters (1.4.3)
61
+ ansi
62
+ builder
63
+ minitest (>= 5.0)
64
+ ruby-progressbar
65
+ parallel (1.20.1)
66
+ parser (2.7.2.0)
67
+ ast (~> 2.4.1)
68
+ pathutil (0.16.2)
69
+ forwardable-extended (~> 2.6)
70
+ powerpack (0.1.3)
71
+ public_suffix (4.0.6)
72
+ rainbow (2.2.2)
73
+ rake
74
+ rake (10.5.0)
75
+ rb-fsevent (0.11.0)
76
+ rb-inotify (0.10.1)
77
+ ffi (~> 1.0)
78
+ rexml (3.2.5)
79
+ rouge (3.26.0)
80
+ rubocop (0.51.0)
81
+ parallel (~> 1.10)
82
+ parser (>= 2.3.3.1, < 3.0)
83
+ powerpack (~> 0.1)
84
+ rainbow (>= 2.2.2, < 3.0)
85
+ ruby-progressbar (~> 1.7)
86
+ unicode-display_width (~> 1.0, >= 1.0.1)
87
+ ruby-progressbar (1.11.0)
88
+ safe_yaml (1.0.5)
89
+ sassc (2.4.0)
90
+ ffi (~> 1.9)
91
+ terminal-table (2.0.0)
92
+ unicode-display_width (~> 1.1, >= 1.1.1)
93
+ toml-rb (2.0.1)
94
+ citrus (~> 3.0, > 3.0)
95
+ unicode-display_width (1.7.0)
96
+
97
+ PLATFORMS
98
+ ruby
99
+
100
+ DEPENDENCIES
101
+ bookshop-array-structures!
102
+ hashdiff
103
+ minitest
104
+ minitest-reporters
105
+ rake
106
+ rubocop (~> 0.51.0)
107
+
108
+ BUNDLED WITH
109
+ 2.1.4
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs.push 'lib'
6
+ t.libs.push 'specs'
7
+ t.verbose = true
8
+ t.warning = false
9
+ t.pattern = "spec/**/*_spec.rb"
10
+ t.test_files = FileList['spec/**/*_spec.rb']
11
+ end
12
+
13
+ desc "Run tests"
14
+ task :default => [:test]
@@ -22,4 +22,5 @@ Gem::Specification.new do |spec|
22
22
 
23
23
  spec.add_dependency "jekyll", ">= 3.7", "< 5.0"
24
24
  spec.add_dependency "toml-rb", ">= 2.0", "< 3.0"
25
+ spec.add_dependency "dry-inflector", ">= 0.1", "< 1.0"
25
26
  end
@@ -1,11 +1,13 @@
1
1
  require "jekyll"
2
+ require "pathname"
2
3
  require "toml-rb"
4
+ require "dry/inflector"
3
5
 
4
6
  module Bookshop
5
7
  class ArrayStructures
6
8
  def self.get_story_name(path)
7
- basename = path.split("/").last.split(".").first
8
- return basename.split("-").map(&:capitalize).join(" ")
9
+ basename = get_component_type(path)
10
+ return basename.split(/-|\//).map(&:capitalize).join(" ")
9
11
  end
10
12
 
11
13
  def self.get_component_type(path)
@@ -18,16 +20,93 @@ module Bookshop
18
20
  return result
19
21
  end
20
22
 
23
+ def self.handle_props_object(props, result)
24
+ end
21
25
 
22
- def self.transform_component(path, component, site)
23
- result = {}
24
- result['_component_type'] = get_component_type(path);
25
- component.each_pair { |storyname, story|
26
- story.each_pair {|key, value|
27
- if result.has_key?(key) && storyname != "defaults"
26
+ def self.handle_bookprops(value_obj, structure, value_context = nil)
27
+ structure["_select_data"] ||= {}
28
+ structure["_array_structures"] ||= {}
29
+ structure["_comments"] ||= {}
30
+ structure["value"] ||= {}
31
+ value_context = structure["value"] if value_context.nil?
32
+ inflector = Dry::Inflector.new
33
+ value_obj.each_pair {|key, value|
34
+ if value_context.has_key?(key)
35
+ next
36
+ end
37
+
38
+ if value.is_a? Hash
39
+ comment = value["--bookshop_comment"] ||
40
+ value["default--bookshop_comment"] ||
41
+ value["select--bookshop_comment"] ||
42
+ value["preview--bookshop_comment"]
43
+ if comment
44
+ structure["_comments"][key] = comment
45
+ end
46
+ unless value["default"].nil?
47
+ value_context[key] = value["default"]
48
+ next
49
+ end
50
+ if value["select"]
51
+ value_context[key] = nil
52
+ plural_key = inflector.pluralize(key)
53
+ structure["_select_data"][plural_key] = value["select"]
54
+ next
55
+ end
56
+ if value["preview"]
57
+ value_context[key] = nil
58
+ next
59
+ end
60
+ value_context[key] ||= {}
61
+ handle_bookprops(value, structure, value_context[key])
28
62
  next
29
63
  end
30
64
 
65
+ if value.is_a? Array
66
+ if value[0]&.is_a? Hash
67
+ if value[0]["--bookshop_comment"]
68
+ structure["_comments"][key] = value[0]["--bookshop_comment"]
69
+ end
70
+
71
+ singular_title = inflector.classify(key).gsub(/(.)([A-Z])/, '\1 \2')
72
+ structure["_array_structures"][key] ||= {
73
+ "values" => [{
74
+ "label" => singular_title,
75
+ "icon" => "add_box",
76
+ "value" => {}
77
+ }]
78
+ }
79
+ handle_bookprops( value[0],
80
+ structure["_array_structures"][key]["values"][0],
81
+ structure["_array_structures"][key]["values"][0]["value"])
82
+ value_context[key] = []
83
+ next
84
+ end
85
+ value_context[key] = value
86
+ end
87
+
88
+ if [true, false].include? value
89
+ value_context[key] = value
90
+ next
91
+ end
92
+
93
+ if key.include? "--bookshop_comment"
94
+ actual_key = key.split("--").first
95
+ next if actual_key == ""
96
+ structure["_comments"][actual_key] = value
97
+ next
98
+ end
99
+
100
+ value_context[key] = nil
101
+ }
102
+ end
103
+
104
+
105
+ def self.handle_legacy_story(story, site)
106
+ result = {};
107
+ story.each_pair {|key, value|
108
+ next unless result.has_key?(key) && storyname != "defaults"
109
+
31
110
  if key.include? "--repeat"
32
111
  new_key = key.split("--").first
33
112
  result[new_key] = []
@@ -63,31 +142,125 @@ module Bookshop
63
142
  end
64
143
  site.config["_select_data"][new_key].push(option)
65
144
  }
145
+ elsif key.include? "--"
146
+ new_key = key.split("--").first
147
+ result[new_key] = nil
66
148
  else
67
149
  result[key] = value
68
150
  end
69
151
  }
70
- }
152
+ return result
153
+ end
154
+
155
+ def self.transform_component(path, component, site)
156
+ unless component["defaults"].nil?
157
+ return transform_legacy_component(path, component, site)
158
+ end
159
+ result = { "value" => {} }
160
+ result["label"] = get_story_name(path)
161
+ result["value"]["_bookshop_name"] = get_component_type(path)
162
+ unless component["component"].nil?
163
+ result.merge!(component["component"])
164
+ end
165
+ unless component["props"].nil?
166
+ handle_bookprops(component["props"] || {}, result)
167
+ end
168
+ result["array_structures"] ||= [];
169
+ already_in_global_array = result["array_structures"].select{|value| value == 'bookshop_components'}.length > 0
170
+ if !already_in_global_array && !result["_hidden"]
171
+ result["array_structures"].push("bookshop_components")
172
+ end
173
+ result.delete("_hidden") unless result["_hidden"].nil?
71
174
  return result
72
175
  end
73
176
 
74
- def self.build_array_structures(site)
75
- base_path = ""
76
- if !site.theme.nil?
77
- base_path = site.theme.root + "/_bookshop/components/"
177
+ def self.transform_legacy_component(path, component, site)
178
+ puts "📚 Parsing legacy stories.toml config file"
179
+ result = { "value" => {} }
180
+ result["label"] = get_story_name(path)
181
+ result["value"]["_component_type"] = get_component_type(path)
182
+ component.each_pair { |storyname, story|
183
+ if storyname == "meta"
184
+ result.merge!(story)
185
+ else
186
+ result["value"].merge!(handle_legacy_story(story, site))
187
+ end
188
+ }
189
+ result["array_structures"] ||= [];
190
+ contains_component = result["array_structures"].select{|value| value == 'components'}.length > 0
191
+ if !contains_component && !result["_hidden"]
192
+ result["array_structures"].push("components")
78
193
  end
194
+ result.delete("_hidden") unless result["_hidden"].nil?
195
+ return result
196
+ end
197
+
198
+ def self.rewrite_bookshop_toml(content)
199
+ rewritten_lines = content.split("\n")&.collect { |line|
200
+ if line =~ /^[a-z0-9\-_\.\s]+=.*?#.+?$/i
201
+ /#:(?<comment>[^#]+)$/i =~ line
202
+ /^\s*?(?<variable_name>[a-z0-9\-_\.]+)\s?=/i =~ line
203
+ next line unless comment && variable_name
204
+
205
+ next "#{variable_name}--bookshop_comment = \"#{comment.strip}\"\n#{line}"
206
+ elsif line =~ /^\s*?\[.*?#.+?$/i
207
+ /#:(?<comment>[^#]+)$/i =~ line
208
+ next line unless comment
209
+ next "#{line}\n--bookshop_comment = \"#{comment.strip}\""
210
+ end
211
+ line
212
+ }
213
+ rewritten_lines.join("\n")
214
+ end
215
+
216
+ def self.parse_bookshop_toml(content)
217
+ rewritten_content = rewrite_bookshop_toml(content)
218
+ return TomlRB.parse(rewritten_content)
219
+ end
220
+
221
+ def self.build_from_location(base_path, site)
79
222
  site.config["_select_data"] ||= {}
80
223
  site.config["_array_structures"] ||= {}
81
- site.config["_array_structures"]["components"] ||= {
82
- "values" => []
83
- }
84
- Dir.glob("**/*.stories.{toml,tml,tom,tm}", base: base_path).each do |f|
85
- component = TomlRB.load_file(base_path + f)
86
- site.config["_array_structures"]["components"]["values"].push({
87
- "label" => get_story_name(f),
88
- "value" => transform_component(f, component, site)
89
- })
224
+ puts "📚 Parsing Stories from #{base_path}"
225
+ Dir.glob("**/*.{bookshop,stories}.{toml,tml,tom,tm}", base: base_path).each do |f|
226
+ begin
227
+ if f =~ /bookshop/
228
+ raw_file = File.read(base_path + "/" + f)
229
+ component = parse_bookshop_toml(raw_file)
230
+ else
231
+ component = TomlRB.load_file(base_path + f)
232
+ end
233
+ rescue => exception
234
+ puts "❌ Error Parsing Story: " + f
235
+ puts exception
236
+ next
237
+ 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
249
+ }
250
+ end
251
+ end
252
+
253
+ def self.build_array_structures(site)
254
+ bookshop_locations = site.config['bookshop_locations']&.collect do |location|
255
+ Pathname.new(location + "/components").cleanpath.to_s
256
+ end
257
+ bookshop_locations = bookshop_locations.select do |location|
258
+ Dir.exist?(location)
259
+ end
260
+ bookshop_locations.each do |base_path|
261
+ build_from_location(base_path, site)
90
262
  end
263
+ puts "✅ Finshed Parsing Stories"
91
264
  end
92
265
  end
93
266
  end
@@ -1,5 +1,5 @@
1
1
  module Bookshop
2
2
  module Arraystructures
3
- VERSION = "1.4.6"
3
+ VERSION = "2.0.0.pre.alpha.4"
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: 1.4.6
4
+ version: 2.0.0.pre.alpha.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tate
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-10-22 00:00:00.000000000 Z
11
+ date: 2021-05-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -50,13 +50,36 @@ dependencies:
50
50
  - - "<"
51
51
  - !ruby/object:Gem::Version
52
52
  version: '3.0'
53
- description:
53
+ - !ruby/object:Gem::Dependency
54
+ name: dry-inflector
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '0.1'
60
+ - - "<"
61
+ - !ruby/object:Gem::Version
62
+ version: '1.0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0.1'
70
+ - - "<"
71
+ - !ruby/object:Gem::Version
72
+ version: '1.0'
73
+ description:
54
74
  email:
55
75
  - tate@cloudcannon.com
56
76
  executables: []
57
77
  extensions: []
58
78
  extra_rdoc_files: []
59
79
  files:
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - Rakefile
60
83
  - bookshop-array-structures.gemspec
61
84
  - lib/bookshop-array-structures.rb
62
85
  - lib/bookshop-array-structures/version.rb
@@ -64,7 +87,7 @@ homepage: https://github.com/cloudcannon/bookshop
64
87
  licenses:
65
88
  - MIT
66
89
  metadata: {}
67
- post_install_message:
90
+ post_install_message:
68
91
  rdoc_options: []
69
92
  require_paths:
70
93
  - lib
@@ -75,12 +98,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
75
98
  version: 2.3.0
76
99
  required_rubygems_version: !ruby/object:Gem::Requirement
77
100
  requirements:
78
- - - ">="
101
+ - - ">"
79
102
  - !ruby/object:Gem::Version
80
- version: '0'
103
+ version: 1.3.1
81
104
  requirements: []
82
105
  rubygems_version: 3.0.3
83
- signing_key:
106
+ signing_key:
84
107
  specification_version: 4
85
108
  summary: A Jekyll plugin to generate array structures from bookshop
86
109
  test_files: []