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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 55620781f48fc401fb5f27ec0d5395656d6c07399065ae1a09d2fc0934ecf7e3
4
- data.tar.gz: 1163a511e7a0a60fd0014d2bfbdb2f6a8efa4a0753d2e9e0caedd14105188c57
3
+ metadata.gz: 480e7aa1a4ac08a0d34be704e52cad378e4eac7cdbc0d5b3ed8ec59ba0469248
4
+ data.tar.gz: 40b848df177728953954d4e840ac5bfe7dfdbefcd2df66c0179236f90486bfb3
5
5
  SHA512:
6
- metadata.gz: d90af11d08e5baac9374a9a69213ea04911fc8be7c9b9aee077a12e525fea2e74acc8642f9d80f846e40f110bdc04627c0fc66fb282a812825005cd17e4db253
7
- data.tar.gz: 0b611c027bf603c5e3b029e397e37cf8e65ef0aef4a320cbe248978b767060ebe6579616668ed293cdb76fd7924e6707ce1ce49b6f1bdfb08fa68bccce2f3566
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(sanitize(html))
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
- def sanitize(html)
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.css('section', 'div').each { |elem| elem.name = 'p' }
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.css('li').each { |li| wrap_parents_in_ul(li) }
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.css('p embed').each do |embed_node|
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
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ContentfulConverter
4
- VERSION = '0.0.1.20'
4
+ VERSION = '0.0.1.21'
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.20
4
+ version: 0.0.1.21
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Avlonitis