contentful_converter 0.0.1.25 → 0.0.1.26

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: db73e54f2c69e59e6e8226b0fe637923751c6e08e0b6969af6cb1be71ed428e3
4
- data.tar.gz: 03d183bce70c3c68d0e1aeef56b39bf2b6066fc10888958c4eec38b1a8b0322b
3
+ metadata.gz: 60f068e6ba05414012bc0bacd531648585829e96d17afed00513fc1fdef31801
4
+ data.tar.gz: 91a6276c212b1684bd84d4361ed8bbcb198f4281bef5c642842465361ec0b4c2
5
5
  SHA512:
6
- metadata.gz: c5bdd3783d1852e1df52c5d8c2ca1bd059b8e7482ca8bedaff6643b910a01c551c5b6a6ff318a590fbd58957b9cdb23f2ec1b342d00b1b1ecde7bcb52b46735d
7
- data.tar.gz: 21f47df813122e6e3d8618ec713d2d8ef962260465606c0556685a9a8b999f6b974c2c778a77e0984cad25764bec409dbf74a2f9566635c9d244f7f9d88848c8
6
+ metadata.gz: e402d018e413aaaf2e4435a455513ef2b745048d05edbf61d729daf5d8af2f9f32d545c2b84cd18b76a919a6439f30014b70493648c74dd439f791ed32e0397a
7
+ data.tar.gz: d6384e721401bdb87abc7aecdb44166e7fb60eb60331518eeabeed11d3b0d444f87e70f22e12e6eb938886300217ad7c1d08d77a715078b7e971e4038f50e40b
data/README.md CHANGED
@@ -110,7 +110,7 @@ end
110
110
  }
111
111
  ```
112
112
 
113
- * HTML hyperlinks without a scheme but with an extension e.g: (`<a href="/path/to_file.docx">file</a>`), will be converted into ASSET hyperlinks, with the href value as an ID, excluding the extension.
113
+ * HTML hyperlinks without a scheme but with an extension e.g: (`<a href="file.docx">file</a>`), will be converted into ASSET hyperlinks, with the href value as an ID.
114
114
  ```ruby
115
115
  {
116
116
  nodeType: "paragraph",
@@ -121,7 +121,7 @@ end
121
121
  data: {
122
122
  target: {
123
123
  sys: {
124
- id: "/path/to_file",
124
+ id: "file.docx",
125
125
  type: "Link",
126
126
  linkType: "Entry"
127
127
  }
@@ -8,7 +8,6 @@ module ContentfulConverter
8
8
  def self.configure
9
9
  self.configuration ||= Configuration.new
10
10
  yield(configuration) if block_given?
11
- self
12
11
  end
13
12
 
14
13
  class Configuration
@@ -6,39 +6,53 @@ require 'contentful_converter/configuration'
6
6
  module ContentfulConverter
7
7
  class NokogiriBuilder
8
8
  class << self
9
+ # By transforming the elements at this point,
10
+ # nokogiri creates a tree that is accepted by contentful.
9
11
  def build(html)
10
- doc = create_nokogiri_fragment(transform(html))
11
- normalize_lists(doc)
12
- normalize_embeds(doc)
13
- doc
12
+ html_node = nokogiri_fragment(html)
13
+ remove_forbidden_elements(html_node)
14
+ normalize_nodes(html_node)
15
+ html_node
14
16
  end
15
17
 
16
18
  private
17
19
 
18
- # By transforming the elements at this point,
19
- # nokogiri creates a tree that is accepted by contentful.
20
- def transform(html)
21
- doc = create_nokogiri_fragment(html)
22
- remove_forbidden_elements(doc)
23
- find_nodes(doc, %w[section div]).each { |elem| elem.name = 'p' }
24
- find_nodes(doc, 'img').each { |elem| elem.name = 'embed' }
25
- doc.to_html
26
- end
27
-
28
- def create_nokogiri_fragment(html)
20
+ def nokogiri_fragment(html)
29
21
  Nokogiri::HTML.fragment(html)
30
22
  end
31
23
 
32
- def remove_forbidden_elements(doc)
33
- forbidden_nodes = ContentfulConverter.configure.configuration.forbidden_nodes
34
- remove_empty_links(doc)
24
+ def remove_forbidden_elements(html_node)
25
+ remove_empty_links(html_node)
26
+ return unless ContentfulConverter.configuration
27
+
28
+ forbidden_nodes = ContentfulConverter.configuration.forbidden_nodes
35
29
  return if forbidden_nodes.empty?
36
30
 
37
- find_nodes(doc, forbidden_nodes).each(&:remove)
31
+ find_nodes(html_node, forbidden_nodes).each(&:remove)
32
+ end
33
+
34
+ def normalize_nodes(html_node)
35
+ normalize_blocks(html_node)
36
+ normalize_imgs(html_node)
37
+ normalize_lists(html_node)
38
+ normalize_embeds(html_node)
39
+ end
40
+
41
+ def normalize_blocks(html_node)
42
+ find_nodes(html_node, %w[section div]).each do |elem|
43
+ elem.swap(elem.children)
44
+ end
45
+ end
46
+
47
+ def normalize_imgs(html_node)
48
+ find_nodes(html_node, 'img').each { |elem| elem.name = 'embed' }
38
49
  end
39
50
 
40
51
  def normalize_lists(nokogiri_fragment)
41
- find_nodes(nokogiri_fragment, 'li').each { |li| wrap_parents_in_ul(li) }
52
+ find_nodes(nokogiri_fragment, 'li').each do |li|
53
+ wrap_parents_in_ul(li)
54
+ wrap_children_in_single_p(li)
55
+ end
42
56
  end
43
57
 
44
58
  def normalize_embeds(nokogiri_fragment)
@@ -47,12 +61,8 @@ module ContentfulConverter
47
61
  end
48
62
  end
49
63
 
50
- def remove_empty_links(doc)
51
- find_nodes(doc, 'a').each { |n| n.remove unless n['href'] }
52
- end
53
-
54
- def find_nodes(doc, element)
55
- doc.css(*element)
64
+ def remove_empty_links(html_node)
65
+ find_nodes(html_node, 'a').each { |n| n.remove unless n['href'] }
56
66
  end
57
67
 
58
68
  def wrap_parents_in_ul(node)
@@ -60,6 +70,38 @@ module ContentfulConverter
60
70
 
61
71
  node.wrap('<ul>')
62
72
  end
73
+
74
+ def wrap_children_in_single_p(node)
75
+ return if node.children.count == 1 && node.children.first.name == 'p'
76
+
77
+ find_nodes(node, 'p').each { |p_node| p_node.swap(p_node.children) }
78
+ node_children = append_space_to_text_nodes(node.children.remove, node)
79
+
80
+ node.add_child('<p>')
81
+ node.at_css('p').children = node_children
82
+ merge_text_nodes(node)
83
+ end
84
+
85
+ def append_space_to_text_nodes(children, node)
86
+ children.each do |child|
87
+ next if child.text.chomp.empty? || child.text.end_with?(' ')
88
+
89
+ child.content = create_text_node("#{child.text} ", node)
90
+ end
91
+ children
92
+ end
93
+
94
+ def find_nodes(html_node, element)
95
+ html_node.css(*element)
96
+ end
97
+
98
+ def create_text_node(text, html_node)
99
+ Nokogiri::XML::Text.new(text, html_node)
100
+ end
101
+
102
+ def merge_text_nodes(node)
103
+ Nokogiri::XML(node.to_xml)
104
+ end
63
105
  end
64
106
  end
65
107
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ContentfulConverter
4
- VERSION = '0.0.1.25'
4
+ VERSION = '0.0.1.26'
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.25
4
+ version: 0.0.1.26
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Avlonitis