contentful_converter 0.0.1.20 → 0.0.1.21
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/contentful_converter/nokogiri_builder.rb +12 -5
- 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: 480e7aa1a4ac08a0d34be704e52cad378e4eac7cdbc0d5b3ed8ec59ba0469248
|
4
|
+
data.tar.gz: 40b848df177728953954d4e840ac5bfe7dfdbefcd2df66c0179236f90486bfb3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5d9aaa610bd5cc7633a679fd1d3144b12db4412f1739b697bbf53009b2654b9153a251890e4b2782a55a836c09e4bc00068287b1481d68d8b59568c07f035b25
|
7
|
+
data.tar.gz: 79a8db078530d7ccef8851f59c6f1d7c9aa1f61962bccf1c5ab736918ff482f7cadd7d09b5b8a21b7ac0e6697cb684c18c0b8513d04072dcd52f21c585b5b895
|
@@ -6,7 +6,7 @@ module ContentfulConverter
|
|
6
6
|
class NokogiriBuilder
|
7
7
|
class << self
|
8
8
|
def build(html)
|
9
|
-
doc = create_nokogiri_fragment(
|
9
|
+
doc = create_nokogiri_fragment(transform(html))
|
10
10
|
normalize_lists(doc)
|
11
11
|
normalize_embeds(doc)
|
12
12
|
doc
|
@@ -14,9 +14,12 @@ module ContentfulConverter
|
|
14
14
|
|
15
15
|
private
|
16
16
|
|
17
|
-
|
17
|
+
# By transforming the elements at this point,
|
18
|
+
# nokogiri creates a tree that is accepted by contentful.
|
19
|
+
def transform(html)
|
18
20
|
doc = create_nokogiri_fragment(html)
|
19
|
-
doc
|
21
|
+
find_nodes(doc, ['section', 'div']).each { |elem| elem.name = 'p' }
|
22
|
+
find_nodes(doc, 'img').each { |elem| elem.name = 'embed' }
|
20
23
|
doc.to_html
|
21
24
|
end
|
22
25
|
|
@@ -25,15 +28,19 @@ module ContentfulConverter
|
|
25
28
|
end
|
26
29
|
|
27
30
|
def normalize_lists(nokogiri_fragment)
|
28
|
-
nokogiri_fragment
|
31
|
+
find_nodes(nokogiri_fragment, 'li').each { |li| wrap_parents_in_ul(li) }
|
29
32
|
end
|
30
33
|
|
31
34
|
def normalize_embeds(nokogiri_fragment)
|
32
|
-
nokogiri_fragment
|
35
|
+
find_nodes(nokogiri_fragment, 'p embed').each do |embed_node|
|
33
36
|
embed_node.parent.add_next_sibling(embed_node)
|
34
37
|
end
|
35
38
|
end
|
36
39
|
|
40
|
+
def find_nodes(doc, element)
|
41
|
+
doc.css(*element)
|
42
|
+
end
|
43
|
+
|
37
44
|
def wrap_parents_in_ul(node)
|
38
45
|
return if node.parent.name == 'ul' || node.parent.name == 'ol'
|
39
46
|
|