content_block_tools 1.4.1 → 1.5.0

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: b821972df0a116f521f39a5658515719023e8f716e4b1c2197ec9390bcbec220
4
- data.tar.gz: 81ae6c7245eab5a667901a47630b24f846bad2e0034021a51ee5c9fa4922922c
3
+ metadata.gz: c34b07afe5921c6615c4b9e8a7411361ade415837086f38d1536c90f440f6f62
4
+ data.tar.gz: c71bc6c809d2633613aedd8e4b1c35d22cba35c6307ca1f12ec9da6410293678
5
5
  SHA512:
6
- metadata.gz: 1d27748f20ac56e0ef0b29103d6cff133a61b72fdec00d80547094feb6f7b05cebff2be498150aed636232ae61d0e538a5eb99d5e7c0cd466c4cbeef23c57cf8
7
- data.tar.gz: 6ab320367c385d02a641d4693dc393a67258d94856964d7c46a34046c78aa882752cf2c306c581ef86e8f746b963c7d41f924c35b6c1157c7b13cf34f6f26823
6
+ metadata.gz: d8141f6186ec06d0890dea636d293d650aa3ef3e1e69b86fa88bac0c937e61e9e3a5bd299b2e6dd66c8bd390dc7213c1b2b2a21dd7251fdae03108433b8cf0be
7
+ data.tar.gz: 11b9f5f84654032e9cabb1d3aed0498bebc3e1396359bde3fc56683c6a79c931f47ca753b05022e23bf86f50c35ec55427975c484eb5834e415f50a952ee2ded
@@ -51,13 +51,57 @@ module ContentBlockTools
51
51
  #
52
52
  # @return [Array<ContentBlockReference>] An array of content block references
53
53
  def find_all_in_document(document)
54
- document.scan(ContentBlockReference::EMBED_REGEX).map do |match|
55
- match = prepare_match(match)
56
- ContentBlockTools.logger.info("Found Content Block Reference: #{match}")
57
- ContentBlockReference.new(document_type: match[1], identifier: match[2], embed_code: match[0])
54
+ document.scan(EMBED_REGEX).map do |match_data|
55
+ ContentBlockReference.from_match_data(match_data)
58
56
  end
59
57
  end
60
58
 
59
+ # Converts a single embed code string into a ContentBlockReference object
60
+ #
61
+ # Parses an embed code string using {EMBED_REGEX} to extract the document type,
62
+ # identifier, and embed code, then creates a ContentBlockReference instance.
63
+ #
64
+ # @param embed_code [String] the embed code to parse
65
+ # @example Parse an embed code with a UUID
66
+ # ContentBlockReference.from_string("{{embed:content_block_pension:2b92cade-549c-4449-9796-e7a3957f3a86}}")
67
+ # #=> #<ContentBlockReference document_type="content_block_pension" identifier="2b92cade-549c-4449-9796-e7a3957f3a86" embed_code="{{embed:content_block_pension:2b92cade-549c-4449-9796-e7a3957f3a86}}">
68
+ # @example Parse an embed code with a slug
69
+ # ContentBlockReference.from_string("{{embed:content_block_contact:some-slug}}")
70
+ # #=> #<ContentBlockReference document_type="content_block_contact" identifier="some-slug" embed_code="{{embed:content_block_contact:some-slug}}">
71
+ # @return [ContentBlockReference] a new ContentBlockReference instance
72
+ # @raise [InvalidEmbedCodeError] if the embed_code doesn't match {EMBED_REGEX} (match_data will be nil)
73
+ # @see from_match_data
74
+ def from_string(embed_code)
75
+ match_data = embed_code.match(/^#{EMBED_REGEX}$/)
76
+ raise InvalidEmbedCodeError unless match_data
77
+
78
+ ContentBlockReference.from_match_data(match_data.captures)
79
+ end
80
+
81
+ # Converts match data from a regex scan into a ContentBlockReference object
82
+ #
83
+ # This method is used internally by {find_all_in_document} and {from_string} to create
84
+ # ContentBlockReference instances from regex match data. It normalizes the match data
85
+ # by replacing en/em dashes with double/triple dashes (which can occur due to Kramdown's
86
+ # markdown parsing) before creating the object.
87
+ #
88
+ # @param match_data [MatchData, Array] the match data from scanning with {EMBED_REGEX}
89
+ # Expected to contain: [full_match, document_type, identifier, field]
90
+ # @example Creating from match data
91
+ # match_data = "{{embed:content_block_pension:2b92cade-549c-4449-9796-e7a3957f3a86}}".match(EMBED_REGEX)
92
+ # ContentBlockReference.from_match_data(match_data)
93
+ # #=> #<ContentBlockReference document_type="content_block_pension" identifier="2b92cade-549c-4449-9796-e7a3957f3a86" embed_code="{{embed:content_block_pension:2b92cade-549c-4449-9796-e7a3957f3a86}}">
94
+ # @return [ContentBlockReference] a new ContentBlockReference instance
95
+ # @api private
96
+ # @see find_all_in_document
97
+ # @see from_string
98
+ # @see prepare_match
99
+ def from_match_data(match_data)
100
+ match = prepare_match(match_data)
101
+ ContentBlockTools.logger.info("Found Content Block Reference: #{match}")
102
+ ContentBlockReference.new(document_type: match[1], identifier: match[2], embed_code: match[0])
103
+ end
104
+
61
105
  private
62
106
 
63
107
  # This replaces an en / em dashes in content block references with double or triple dashes. This can occur
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ContentBlockTools
4
- VERSION = "1.4.1"
4
+ VERSION = "1.5.0"
5
5
  end
@@ -18,6 +18,8 @@ require "content_block_tools/version"
18
18
 
19
19
  module ContentBlockTools
20
20
  class Error < StandardError; end
21
+ class InvalidEmbedCodeError < StandardError; end
22
+
21
23
  module Presenters; end
22
24
 
23
25
  module Components
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: content_block_tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.1
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - GOV.UK Dev