contentful_converter 0.0.1.25 → 0.0.1.26
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +2 -2
- data/lib/contentful_converter/configuration.rb +0 -1
- data/lib/contentful_converter/nokogiri_builder.rb +68 -26
- data/lib/contentful_converter/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 60f068e6ba05414012bc0bacd531648585829e96d17afed00513fc1fdef31801
|
4
|
+
data.tar.gz: 91a6276c212b1684bd84d4361ed8bbcb198f4281bef5c642842465361ec0b4c2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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="
|
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: "
|
124
|
+
id: "file.docx",
|
125
125
|
type: "Link",
|
126
126
|
linkType: "Entry"
|
127
127
|
}
|
@@ -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
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
-
|
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(
|
33
|
-
|
34
|
-
|
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(
|
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
|
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(
|
51
|
-
find_nodes(
|
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
|