bookshop-array-structures 1.4.4 → 2.0.0.pre.alpha.2

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