contentful_converter 0.0.1.21 → 0.0.1.22

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: 480e7aa1a4ac08a0d34be704e52cad378e4eac7cdbc0d5b3ed8ec59ba0469248
4
- data.tar.gz: 40b848df177728953954d4e840ac5bfe7dfdbefcd2df66c0179236f90486bfb3
3
+ metadata.gz: ff50772e7cf3d60d6587047360fd5f2aa1f87eb3e7110c731b1fdf6862b70cbe
4
+ data.tar.gz: 82c222abe96c9d3ac4ccf0e1e0f95f9dcc9d1e11dfda819bbd24737007b3512b
5
5
  SHA512:
6
- metadata.gz: 5d9aaa610bd5cc7633a679fd1d3144b12db4412f1739b697bbf53009b2654b9153a251890e4b2782a55a836c09e4bc00068287b1481d68d8b59568c07f035b25
7
- data.tar.gz: 79a8db078530d7ccef8851f59c6f1d7c9aa1f61962bccf1c5ab736918ff482f7cadd7d09b5b8a21b7ac0e6697cb684c18c0b8513d04072dcd52f21c585b5b895
6
+ metadata.gz: f9bc85b65ff13a53db3bc3c21050287799c273be82778d2a9378f18bc83d8b80af3d5b3b7f43db921743c66e3bd5125b26ce14252586c13b40938222f072d165
7
+ data.tar.gz: 37a4f52a6b29bad99ddfc7618b5706e6ae80512633993daf179b4f79810db54925b5c324bd6911015e5ce5b60abf5717e362ef6139091d51084984cda395d1d2
@@ -13,7 +13,7 @@ module ContentfulConverter
13
13
  end
14
14
 
15
15
  def add_content(node)
16
- @content << node
16
+ @content << (node.needs_p_wrapping? ? wrap_in_p(node) : node)
17
17
  end
18
18
 
19
19
  def to_h(params = options)
@@ -25,9 +25,9 @@ module ContentfulConverter
25
25
 
26
26
  def needs_p_wrapping?
27
27
  if parent.nil? ||
28
- parent&.class == Nodes::Header ||
29
- parent&.class == Nodes::Paragraph ||
30
- parent&.class == Nodes::Hyperlink
28
+ parent&.class == Nodes::Header ||
29
+ parent&.class == Nodes::Paragraph ||
30
+ parent&.class == Nodes::Hyperlink
31
31
 
32
32
  return false
33
33
  end
@@ -37,6 +37,14 @@ module ContentfulConverter
37
37
 
38
38
  private
39
39
 
40
+ attr_writer :content
41
+
42
+ def wrap_in_p(node)
43
+ p_node = Nodes::Paragraph.new(nil, node.parent)
44
+ p_node.content << node
45
+ p_node
46
+ end
47
+
40
48
  def value
41
49
  nokogiri_node.content
42
50
  end
@@ -9,15 +9,15 @@ module ContentfulConverter
9
9
  private
10
10
 
11
11
  def type
12
- return 'asset-hyperlink' if !(uri_scheme?) && uri_extension?
12
+ return 'asset-hyperlink' if !uri_scheme? && uri_extension?
13
13
  return 'entry-hyperlink' unless uri_scheme?
14
14
 
15
15
  'hyperlink'
16
16
  end
17
17
 
18
18
  def options
19
- return hyperlink_entry_option("Asset") if !(uri_scheme?) && uri_extension?
20
- return hyperlink_entry_option("Entry") unless uri_scheme?
19
+ return hyperlink_entry_option('Asset') if !uri_scheme? && uri_extension?
20
+ return hyperlink_entry_option('Entry') unless uri_scheme?
21
21
 
22
22
  hyperlink_option
23
23
  end
@@ -32,7 +32,7 @@ module ContentfulConverter
32
32
  target: {
33
33
  sys: {
34
34
  id: parsed_link.to_s.split('.').first,
35
- type: "Link",
35
+ type: 'Link',
36
36
  linkType: type
37
37
  }
38
38
  }
@@ -18,7 +18,7 @@ module ContentfulConverter
18
18
  # nokogiri creates a tree that is accepted by contentful.
19
19
  def transform(html)
20
20
  doc = create_nokogiri_fragment(html)
21
- find_nodes(doc, ['section', 'div']).each { |elem| elem.name = 'p' }
21
+ find_nodes(doc, %w[section div]).each { |elem| elem.name = 'p' }
22
22
  find_nodes(doc, 'img').each { |elem| elem.name = 'embed' }
23
23
  doc.to_html
24
24
  end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ContentfulConverter
4
+ class Stack
5
+ def initialize
6
+ @stack = []
7
+ end
8
+
9
+ def add(value)
10
+ @stack << value
11
+ end
12
+
13
+ def pop
14
+ @stack.pop
15
+ end
16
+
17
+ def any?
18
+ @stack.any?
19
+ end
20
+ end
21
+ end
@@ -1,53 +1,65 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'contentful_converter/node_builder'
4
+ require 'contentful_converter/stack'
4
5
 
5
6
  module ContentfulConverter
6
7
  class TreeCloner
7
- class << self
8
- def nokogiri_to_rich_text(nokogiri_fragment)
9
- if nokogiri_fragment.children.empty?
10
- return NodeBuilder.build(nokogiri_fragment).to_h
11
- end
12
-
13
- traverse_and_clone(nokogiri_fragment).to_h
8
+ def self.nokogiri_to_rich_text(nokogiri_fragment)
9
+ if nokogiri_fragment.children.empty?
10
+ return NodeBuilder.build(nokogiri_fragment).to_h
14
11
  end
15
12
 
16
- private
13
+ new(nokogiri_fragment, Stack.new, Stack.new).traverse_and_clone
14
+ end
17
15
 
18
- def traverse_and_clone(nokogiri_fragment)
19
- rich_root_node = NodeBuilder.build(nokogiri_fragment)
16
+ def initialize(nokogiri_fragment, noko_stack, rich_stack)
17
+ @nokogiri_fragment = nokogiri_fragment
18
+ @noko_stack = noko_stack
19
+ @rich_stack = rich_stack
20
+ end
20
21
 
21
- noko_stack = [nokogiri_fragment]
22
- rich_stack = [rich_root_node]
22
+ def traverse_and_clone
23
+ initialize_stacks
23
24
 
24
- while noko_stack.any?
25
- noko_node = noko_stack.pop
26
- rich_node = rich_stack.pop
25
+ while noko_stack.any?
26
+ noko_node = noko_stack.pop
27
+ rich_node = rich_stack.pop
27
28
 
28
- next unless noko_node.children.any?
29
+ next unless noko_node.children.any?
29
30
 
30
- noko_node.children.each do |child_node|
31
- rich_child_node = NodeBuilder.build(child_node, rich_node)
31
+ children_traversal(noko_node, rich_node)
32
+ end
32
33
 
33
- noko_stack << child_node
34
- rich_stack << rich_child_node
35
- rich_node.add_content(wrap_in_paragraph(rich_child_node))
36
- end
37
- end
34
+ rich_root_node.to_h
35
+ end
38
36
 
39
- rich_root_node
40
- end
37
+ private
41
38
 
42
- def wrap_in_paragraph(node)
43
- node.needs_p_wrapping? ? p_wrapper(node) : node
44
- end
39
+ attr_reader :noko_stack, :rich_stack, :nokogiri_fragment
45
40
 
46
- def p_wrapper(node)
47
- p_node = Nodes::Paragraph.new(nil, node.parent)
48
- p_node.add_content(node)
49
- p_node
41
+ def initialize_stacks
42
+ noko_stack.add(nokogiri_fragment)
43
+ rich_stack.add(rich_root_node)
44
+ end
45
+
46
+ def rich_root_node
47
+ @rich_root_node ||= NodeBuilder.build(nokogiri_fragment)
48
+ end
49
+
50
+ def children_traversal(noko_node, rich_node)
51
+ noko_node.children.each do |child_node|
52
+ rich_child_node = NodeBuilder.build(child_node, rich_node)
53
+
54
+ add_to_stacks(child_node, rich_child_node)
55
+
56
+ rich_node.add_content(rich_child_node)
50
57
  end
51
58
  end
59
+
60
+ def add_to_stacks(noko_node, rich_node)
61
+ noko_stack.add(noko_node)
62
+ rich_stack.add(rich_node)
63
+ end
52
64
  end
53
65
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ContentfulConverter
4
- VERSION = '0.0.1.21'
4
+ VERSION = '0.0.1.22'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contentful_converter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.21
4
+ version: 0.0.1.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Avlonitis
@@ -65,6 +65,7 @@ files:
65
65
  - lib/contentful_converter/nodes/underline.rb
66
66
  - lib/contentful_converter/nodes/unordered_list.rb
67
67
  - lib/contentful_converter/nokogiri_builder.rb
68
+ - lib/contentful_converter/stack.rb
68
69
  - lib/contentful_converter/tree_cloner.rb
69
70
  - lib/contentful_converter/version.rb
70
71
  homepage: https://github.com/AlexAvlonitis/contentful_converter