wcc-contentful 1.5.0.rc1 → 1.5.1

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: 81257133691833969ac9f77447b91d2698aa0738964b56f80e8533dd403e90d1
4
- data.tar.gz: 424597dcc95a76513d70226d82aafbea7baf614dcc7c5ee55c405e2dc75d4337
3
+ metadata.gz: 5599a00c953fc7b5692bc3f3c5b235d621c2d65bb1965e30a5ef89db3006a1c8
4
+ data.tar.gz: 40731523f1a793e1c85159977e0f3235588fadf82fa2d5b4733f586f33a812fe
5
5
  SHA512:
6
- metadata.gz: 23e37d1e5e4271c0f0a13f8e0aa495473b55164b855b58df1638de5fc88f50e00499a4726908f1bb59856075833ab275fa3a893233fc484ff6c839fd42f023b0
7
- data.tar.gz: 139b207faa2d1f6a3ae44ddff0421d4572f7b93aab04c2fc63bd6136e05faa72886d639ac023c0009af0934a74d8c70b79d7ce23876903706dd9702c04fb5f8a
6
+ metadata.gz: 22ee7a27c1000436dbf3ba622ef1ce412ca62d67bd67e2db0a0cc707a6e85f8e1a62a719729bba7385d8020295ae460779e0e0cc4be7bcac35bea90b855c7baf
7
+ data.tar.gz: cee9e57b7f192501d5cfe46338a34a8db64be596e7ab64872495eab6ad33408439d024a8ad9a60f9754487b460eae9cb31ea26c908c40fd828d87289443e9170
@@ -39,11 +39,11 @@ class WCC::Contentful::RichTextRenderer
39
39
  end
40
40
 
41
41
  attr_reader :document
42
- attr_accessor :config, :store, :model_namespace
42
+ attr_accessor :configuration, :store, :model_namespace
43
43
 
44
- def initialize(document, config: nil, store: nil, model_namespace: nil)
44
+ def initialize(document, configuration: nil, store: nil, model_namespace: nil)
45
45
  @document = document
46
- @config = config if config.present?
46
+ @configuration = configuration if configuration.present?
47
47
  @store = store if store.present?
48
48
  @model_namespace = model_namespace if model_namespace.present?
49
49
  end
@@ -142,7 +142,7 @@ class WCC::Contentful::RichTextRenderer
142
142
  # Check the first row - if it's a header row, render a <thead>
143
143
  first, *rest = node.content
144
144
  if first&.content&.all? { |cell| cell.node_type == 'table-header-cell' }
145
- concat(content_tag(:thead) { render_content([first]) })
145
+ concat(render_table_header(first))
146
146
  else
147
147
  # Otherwise, render it inside the tbody with the rest
148
148
  rest.unshift(first)
@@ -152,6 +152,38 @@ class WCC::Contentful::RichTextRenderer
152
152
  end
153
153
  end
154
154
 
155
+ def render_table_header(table_row_node)
156
+ # roll up blank table-header-cells into the previous cell w/ colspan
157
+ node_contents = []
158
+ table_row_node.content.each do |node|
159
+ if node.node_type == 'table-header-cell' &&
160
+ node_is_blank?(node) &&
161
+ node_contents.last&.node_type == 'table-header-cell'
162
+
163
+ # replace the previous node with a new node with colspan + 1
164
+ last_node = node_contents.pop
165
+ node_contents << WCC::Contentful::RichText.tokenize(
166
+ last_node.as_json.merge(
167
+ 'data' => (last_node['data'] || {}).merge({
168
+ 'colspan' => (last_node['data']&.try('colspan') || 1) + 1
169
+ })
170
+ )
171
+ )
172
+
173
+ # And skip adding this blank node
174
+ next
175
+ end
176
+
177
+ node_contents << node
178
+ end
179
+
180
+ content_tag(:thead) do
181
+ content_tag(:tr) do
182
+ render_content(node_contents)
183
+ end
184
+ end
185
+ end
186
+
155
187
  def render_table_row(node)
156
188
  content_tag(:tr) do
157
189
  render_content(node.content)
@@ -160,16 +192,23 @@ class WCC::Contentful::RichTextRenderer
160
192
 
161
193
  def render_table_cell(node)
162
194
  content_tag(:td) do
163
- render_content(node.content)
195
+ render_table_cell_content(node.content)
164
196
  end
165
197
  end
166
198
 
167
199
  def render_table_header_cell(node)
168
- content_tag(:th) do
169
- render_content(node.content)
200
+ content_tag(:th, colspan: node.data && node.data['colspan']) do
201
+ render_table_cell_content(node.content)
170
202
  end
171
203
  end
172
204
 
205
+ def render_table_cell_content(content)
206
+ # If the content is a single paragraph, render it without the <p> tag
207
+ return render_content(content.first.content) if content.size == 1 && content.first.node_type == 'paragraph'
208
+
209
+ render_content(content)
210
+ end
211
+
173
212
  def render_hyperlink(node)
174
213
  content_tag(:a,
175
214
  href: node.data['uri'],
@@ -249,6 +288,15 @@ class WCC::Contentful::RichTextRenderer
249
288
 
250
289
  private
251
290
 
291
+ def node_is_blank?(node)
292
+ case node.node_type
293
+ when 'text'
294
+ node.value.blank?
295
+ else
296
+ node.content.all? { |n| node_is_blank?(n) }
297
+ end
298
+ end
299
+
252
300
  def resolve_target(target)
253
301
  unless store.present?
254
302
  raise NotConnectedError,
@@ -274,9 +322,9 @@ class WCC::Contentful::RichTextRenderer
274
322
  return false unless uri&.host.present?
275
323
 
276
324
  app_uri =
277
- if config&.app_url.present?
325
+ if configuration&.app_url.present?
278
326
  begin
279
- URI(config.app_url)
327
+ URI(configuration.app_url)
280
328
  rescue StandardError
281
329
  nil
282
330
  end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Constructs new connected RichTextRenderer instances w/ needed dependencies
4
+ class RichTextRendererFactory
5
+ def initialize(implementation_class, services: WCC::Contentful::Services.instance)
6
+ @implementation_class = implementation_class
7
+ @services = services
8
+ end
9
+
10
+ def new(document)
11
+ @implementation_class.new(document).tap do |renderer|
12
+ # Inject any dependencies that the renderer needs (except itself to avoid infinite recursion)
13
+ @services.inject_into(renderer, except: [:rich_text_renderer])
14
+ end
15
+ end
16
+
17
+ def call(document)
18
+ new(document).call
19
+ end
20
+ end
@@ -11,6 +11,10 @@ module WCC::Contentful
11
11
 
12
12
  attr_reader :configuration
13
13
 
14
+ def model_namespace
15
+ @model_namespace || WCC::Contentful::Model
16
+ end
17
+
14
18
  def initialize(configuration, model_namespace: nil)
15
19
  raise ArgumentError, 'Not yet configured!' unless configuration
16
20
 
@@ -131,20 +135,7 @@ module WCC::Contentful
131
135
  def rich_text_renderer
132
136
  @rich_text_renderer ||=
133
137
  if implementation_class = configuration&.rich_text_renderer
134
- store = self.store
135
- config = configuration
136
- model_namespace = @model_namespace || WCC::Contentful::Model
137
-
138
- # Wrap the implementation in a subclass that injects the services
139
- Class.new(implementation_class) do
140
- define_method :initialize do |document, *args, **kwargs|
141
- # Implementation might choose to override these, so call super last
142
- @store = store
143
- @config = config
144
- @model_namespace = model_namespace
145
- super(document, *args, **kwargs)
146
- end
147
- end
138
+ RichTextRendererFactory.new(implementation_class, services: self)
148
139
  else
149
140
  # Create a renderer that renders a more helpful error message, but delay the error message until #to_html
150
141
  # is actually invoked in case the user never actually uses the renderer.
@@ -2,6 +2,6 @@
2
2
 
3
3
  module WCC
4
4
  module Contentful
5
- VERSION = '1.5.0.rc1'
5
+ VERSION = '1.5.1'
6
6
  end
7
7
  end
@@ -22,6 +22,7 @@ require 'wcc/contentful/model_singleton_methods'
22
22
  require 'wcc/contentful/model_builder'
23
23
  require 'wcc/contentful/rich_text'
24
24
  require 'wcc/contentful/rich_text_renderer'
25
+ require 'wcc/contentful/rich_text_renderer_factory'
25
26
  require 'wcc/contentful/sync_engine'
26
27
  require 'wcc/contentful/events'
27
28
  require 'wcc/contentful/middleware'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wcc-contentful
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.0.rc1
4
+ version: 1.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Watermark Dev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-07 00:00:00.000000000 Z
11
+ date: 2023-06-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug
@@ -454,6 +454,7 @@ files:
454
454
  - lib/wcc/contentful/rich_text.rb
455
455
  - lib/wcc/contentful/rich_text/node.rb
456
456
  - lib/wcc/contentful/rich_text_renderer.rb
457
+ - lib/wcc/contentful/rich_text_renderer_factory.rb
457
458
  - lib/wcc/contentful/rspec.rb
458
459
  - lib/wcc/contentful/services.rb
459
460
  - lib/wcc/contentful/simple_client.rb
@@ -511,9 +512,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
511
512
  version: '2.7'
512
513
  required_rubygems_version: !ruby/object:Gem::Requirement
513
514
  requirements:
514
- - - ">"
515
+ - - ">="
515
516
  - !ruby/object:Gem::Version
516
- version: 1.3.1
517
+ version: '0'
517
518
  requirements: []
518
519
  rubygems_version: 3.3.7
519
520
  signing_key: