contentful_converter 0.0.1.22 → 0.0.1.27

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: ff50772e7cf3d60d6587047360fd5f2aa1f87eb3e7110c731b1fdf6862b70cbe
4
- data.tar.gz: 82c222abe96c9d3ac4ccf0e1e0f95f9dcc9d1e11dfda819bbd24737007b3512b
3
+ metadata.gz: a9ca6680d2a5379db77f5230554f63ee7a254c8b5ec96b347546759b9009ac94
4
+ data.tar.gz: e5a32099b44d7379deb294f23a97164c00e6e7f9ab97b016e6fc285f7f85472e
5
5
  SHA512:
6
- metadata.gz: f9bc85b65ff13a53db3bc3c21050287799c273be82778d2a9378f18bc83d8b80af3d5b3b7f43db921743c66e3bd5125b26ce14252586c13b40938222f072d165
7
- data.tar.gz: 37a4f52a6b29bad99ddfc7618b5706e6ae80512633993daf179b4f79810db54925b5c324bd6911015e5ce5b60abf5717e362ef6139091d51084984cda395d1d2
6
+ metadata.gz: f6a4fd4d43be7bb779e36ce9958f6b2f475a2a36cfa1e035dc7468d129fb02bd46bf42f5dc5c56043c9ea71ede9fdf94c8b56f3098cb5214d5ee8542d0bc7c21
7
+ data.tar.gz: c527b643b6249d90d6147b3242b7569a0a840ed349281d5c75ecae0e9cf5c056a3ed4577268dba0a3c1c8df50984310fd111c149ee586606a5090c068d75e7c9
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Build Status](https://travis-ci.org/AlexAvlonitis/contentful_converter.svg?branch=master)](https://travis-ci.org/AlexAvlonitis/contentful_converter)
4
4
 
5
- Converts plain html string to contentful specific rich_text hash structure.
5
+ Converts plain html string to contentful specific rich text hash
6
6
 
7
7
  ### Install
8
8
  ```ruby
@@ -44,6 +44,16 @@ ContentfulConverter.convert('<h3>hello world</h3>')
44
44
 
45
45
  ### Additional info
46
46
 
47
+ **Exclude Nodes**
48
+
49
+ Add nodes to be removed from the conversion
50
+
51
+ ```ruby
52
+ ContentfulConverter.configure do |config|
53
+ config.forbidden_nodes = ['table', 'script', 'iframe']
54
+ end
55
+ ```
56
+
47
57
  **`<a>`**
48
58
 
49
59
  * HTML hyperlinks with full URL e.g: (`<a href="https://google.com"></a>`), will be converted into URL hyperlinks
@@ -100,7 +110,7 @@ ContentfulConverter.convert('<h3>hello world</h3>')
100
110
  }
101
111
  ```
102
112
 
103
- * 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.
104
114
  ```ruby
105
115
  {
106
116
  nodeType: "paragraph",
@@ -111,7 +121,7 @@ ContentfulConverter.convert('<h3>hello world</h3>')
111
121
  data: {
112
122
  target: {
113
123
  sys: {
114
- id: "/path/to_file",
124
+ id: "file.docx",
115
125
  type: "Link",
116
126
  linkType: "Entry"
117
127
  }
@@ -190,13 +200,4 @@ rspec ./spec/features/*
190
200
 
191
201
  ### License
192
202
 
193
- Copyright (C) 2019 Alex Avlonitis
194
-
195
- This program is free software: you can redistribute it and/or modify
196
- it under the terms of the GNU General Public License as published by
197
- the Free Software Foundation, version 3.
198
-
199
- This program is distributed in the hope that it will be useful,
200
- but WITHOUT ANY WARRANTY; without even the implied warranty of
201
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
202
- GNU General Public License for more details.
203
+ [MIT LICENSE](LICENSE)
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ContentfulConverter
4
+ class << self
5
+ attr_accessor :configuration
6
+ end
7
+
8
+ def self.configure
9
+ self.configuration ||= Configuration.new
10
+ yield(configuration) if block_given?
11
+ end
12
+
13
+ class Configuration
14
+ attr_reader :forbidden_nodes
15
+
16
+ def initialize
17
+ @forbidden_nodes = []
18
+ end
19
+
20
+ def forbidden_nodes=(*nodes)
21
+ @forbidden_nodes = nodes.flatten.uniq
22
+ end
23
+ end
24
+ end
@@ -10,14 +10,14 @@ module ContentfulConverter
10
10
 
11
11
  def type
12
12
  return 'asset-hyperlink' if !uri_scheme? && uri_extension?
13
- return 'entry-hyperlink' unless uri_scheme?
13
+ return 'entry-hyperlink' if !uri_scheme? && !parsed_link.to_s.include?("#")
14
14
 
15
15
  'hyperlink'
16
16
  end
17
17
 
18
18
  def options
19
19
  return hyperlink_entry_option('Asset') if !uri_scheme? && uri_extension?
20
- return hyperlink_entry_option('Entry') unless uri_scheme?
20
+ return hyperlink_entry_option('Entry') if !uri_scheme? && !parsed_link.to_s.include?("#")
21
21
 
22
22
  hyperlink_option
23
23
  end
@@ -31,7 +31,7 @@ module ContentfulConverter
31
31
  data: {
32
32
  target: {
33
33
  sys: {
34
- id: parsed_link.to_s.split('.').first,
34
+ id: parsed_link.to_s,
35
35
  type: 'Link',
36
36
  linkType: type
37
37
  }
@@ -1,34 +1,58 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'nokogiri'
4
+ require 'contentful_converter/configuration'
4
5
 
5
6
  module ContentfulConverter
6
7
  class NokogiriBuilder
7
8
  class << self
9
+ # By transforming the elements at this point,
10
+ # nokogiri creates a tree that is accepted by contentful.
8
11
  def build(html)
9
- doc = create_nokogiri_fragment(transform(html))
10
- normalize_lists(doc)
11
- normalize_embeds(doc)
12
- doc
12
+ html_node = nokogiri_fragment(html)
13
+ remove_forbidden_elements(html_node)
14
+ normalize_nodes(html_node)
15
+ html_node
13
16
  end
14
17
 
15
18
  private
16
19
 
17
- # By transforming the elements at this point,
18
- # nokogiri creates a tree that is accepted by contentful.
19
- def transform(html)
20
- doc = create_nokogiri_fragment(html)
21
- find_nodes(doc, %w[section div]).each { |elem| elem.name = 'p' }
22
- find_nodes(doc, 'img').each { |elem| elem.name = 'embed' }
23
- doc.to_html
20
+ def nokogiri_fragment(html)
21
+ Nokogiri::HTML.fragment(html)
24
22
  end
25
23
 
26
- def create_nokogiri_fragment(html)
27
- Nokogiri::HTML.fragment(html)
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
29
+ return if forbidden_nodes.empty?
30
+
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' }
28
49
  end
29
50
 
30
51
  def normalize_lists(nokogiri_fragment)
31
- 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
32
56
  end
33
57
 
34
58
  def normalize_embeds(nokogiri_fragment)
@@ -37,8 +61,8 @@ module ContentfulConverter
37
61
  end
38
62
  end
39
63
 
40
- def find_nodes(doc, element)
41
- doc.css(*element)
64
+ def remove_empty_links(html_node)
65
+ find_nodes(html_node, 'a').each { |n| n.remove unless n['href'] }
42
66
  end
43
67
 
44
68
  def wrap_parents_in_ul(node)
@@ -46,6 +70,38 @@ module ContentfulConverter
46
70
 
47
71
  node.wrap('<ul>')
48
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
49
105
  end
50
106
  end
51
107
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ContentfulConverter
4
- VERSION = '0.0.1.22'
4
+ VERSION = '0.0.1.27'
5
5
  end
metadata CHANGED
@@ -1,11 +1,11 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contentful_converter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.22
4
+ version: 0.0.1.27
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Avlonitis
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
  date: 2019-12-07 00:00:00.000000000 Z
@@ -39,13 +39,14 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3.9'
41
41
  description: Converts HTML text to Rich Text Contentful specific JSON structure
42
- email:
42
+ email:
43
43
  executables: []
44
44
  extensions: []
45
45
  extra_rdoc_files: []
46
46
  files:
47
47
  - README.md
48
48
  - lib/contentful_converter.rb
49
+ - lib/contentful_converter/configuration.rb
49
50
  - lib/contentful_converter/converter.rb
50
51
  - lib/contentful_converter/node_builder.rb
51
52
  - lib/contentful_converter/nodes/base.rb
@@ -68,11 +69,11 @@ files:
68
69
  - lib/contentful_converter/stack.rb
69
70
  - lib/contentful_converter/tree_cloner.rb
70
71
  - lib/contentful_converter/version.rb
71
- homepage: https://github.com/AlexAvlonitis/contentful_converter
72
+ homepage: https://github.com/AlexAvlonitis/contentful_converter.git
72
73
  licenses:
73
74
  - MIT
74
75
  metadata: {}
75
- post_install_message:
76
+ post_install_message:
76
77
  rdoc_options: []
77
78
  require_paths:
78
79
  - lib
@@ -87,8 +88,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
88
  - !ruby/object:Gem::Version
88
89
  version: '0'
89
90
  requirements: []
90
- rubygems_version: 3.0.3
91
- signing_key:
91
+ rubygems_version: 3.1.4
92
+ signing_key:
92
93
  specification_version: 4
93
94
  summary: Contentful HTML to Rich Text Converter
94
95
  test_files: []