builder_quill_content 0.1.1 → 0.1.2

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: c8ae5025ab31bcfa7a716431be579fb1ee7a279724c1d7ffab1a6a1775dd5b64
4
- data.tar.gz: 214ebcbe24cfdc56e13ee64e78c6d30cf594acd63828cd05023c69569aa88c38
3
+ metadata.gz: a2ec3f0ee541302574c92d357c52171813cfbdf405962f65ca229ab09f7a5253
4
+ data.tar.gz: 5e44ce1c2dc121e074e2e6dbfd2147377bddbc6b2903db73bd1b07a81d143c3c
5
5
  SHA512:
6
- metadata.gz: 143de3efae9414d5d22f4b6b26415a925311300224dd8d2385b353ca7f689d71df912bccbde6a76a7fd4a90b3dc4f296d09e0ea200cc410c099f231eaeeebb74
7
- data.tar.gz: 53c35b3645a27631ad7dfac1c742851be2529e04f539b829041e7b3d0ebbfe4ed00a6586b3396a08792d14bf787d3b9c138013e84a9af2e6d170c054529444d3
6
+ metadata.gz: 7fc5ab54a2de0371ca472f7cc2fc1460bded3494f3a85a140fe8518f88faba21cbc426a621334366d7a67a1f8520f47afad3cfb36a2bb884fdd4ee8dea2c0609
7
+ data.tar.gz: df40c78f9bca9bf7fa5edd7cb08ba7f5234fbce315a71269579609544ad5b120b8e028d9da10b897530e88a330f29a22029ad933e3382e1ffbaa908eee4ad057
@@ -6,8 +6,8 @@ Gem::Specification.new do |spec|
6
6
  spec.authors = ["Michael Tran"]
7
7
  spec.email = ["mictran205@gmail.com"]
8
8
 
9
- spec.summary = "Convert quill content to html"
10
- spec.homepage = "https://github.com/michaelt0520/word_censored"
9
+ spec.summary = "Convert quill content to html for wakuwaku"
10
+ spec.homepage = "https://github.com/michaelt0520/builder_quill_content"
11
11
  spec.license = "MIT"
12
12
  # spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
13
 
@@ -1,128 +1,59 @@
1
- # frozen_string_literal: true
1
+ require "builder_quill_content/version"
2
+ require "json"
3
+ require "convert_inline"
2
4
 
3
- class BuilderQuillContent
4
- attr_accessor :content
5
+ module BuilderQuillContent
6
+ class Error < StandardError; end
7
+ # Your code goes here...
5
8
 
6
- VALID_INLINE_KEYS = %w[italic bold header blockquote link list].freeze
7
- VALID_MEDIA_KEYS = %w[wk-image wk-youtube wk-tweet wk-instagram wk-divider waku-post wk-maps].freeze
8
- START_VALID_HTML_TAGS = %w[<h2 <div <ul <hr <blockquote].freeze
9
- END_VALID_HTML_TAGS = %w[/h2> /div> /ul> hr> /blockquote>].freeze
9
+ EMBED_KEYS = %w[wk-image wk-youtube wk-tweet wk-instagram wk-divider waku-post wk-maps].freeze
10
10
 
11
- def initialize(content)
12
- @content = content
13
- end
14
-
15
- def to_html
16
- save_tag_p = result = ''
11
+ def to_html(input)
12
+ content_json = JSON.parse(input)
13
+ line = content = ''
17
14
 
18
- convert_inline.each_with_index do |text, i|
19
- next if text.blank?
15
+ while content_json.length.positive?
16
+ node = content_json.shift
20
17
 
21
- if text.include?("\n")
22
- result += merge_array(text)
23
- elsif in_valid_html_tag(text)
24
- result += text
25
- elsif !in_valid_html_tag(text)
26
- if save_tag_p.blank?
27
- result += "<p>#{text}"
28
- save_tag_p = '<p>'
29
- elsif save_tag_p == '<p>' && in_valid_html_tag(convert_inline[i + 1])
30
- result += "#{text}</p>"
31
- save_tag_p = ''
32
- else
33
- result += text
34
- end
18
+ if node['insert'] == "\n"
19
+ content, line = end_of_line(content, line, node['attributes'])
20
+ elsif node['insert'].include?("\n")
21
+ content, line = break_line(content, line, node['insert'])
22
+ else
23
+ content, line = inline(content, line, node)
35
24
  end
36
25
  end
37
26
 
38
- result
27
+ content.gsub('</ul><ul>', '')
39
28
  end
40
29
 
41
- def convert_inline
42
- preprocess.each_with_object([]) do |node, arr|
43
- arr << if node['insert'].is_a?(Hash)
44
- convert_media(node)
45
- else
46
- convert_attribute(arr.last, node)
47
- end
48
- end
30
+ def end_of_line(content, line, attributes)
31
+ content += attributes.nil? ? "<p>#{line}</p>" : ConvertInline.new('insert' => line, 'attributes' => attributes).convert
32
+ [content, '']
49
33
  end
50
34
 
51
- def convert_media(node)
52
- key = node['insert'].keys.first
53
-
54
- case key
55
- when 'wk-image'
56
- '<div data-id="wk-image" data-src="' + node['insert']['wk-image']['src'] + '" data-caption="' + node['insert']['wk-image']['caption'] + '" data-alt="' + node['attributes']['alt'] + '"></div>'
57
- when 'wk-divider'
58
- '<hr>'
59
- else
60
- '<div data-id="' + key + '" data-src="' + node['insert'][key] + '"></div>'
61
- end
62
- end
63
-
64
- def convert_attribute(pre_node, node)
65
- return node['insert'] if node['attributes'].nil?
66
-
67
- text_array = node['insert'].split("\n")
68
- text = text_array.last
69
-
70
- node['attributes'].each_pair do |key, value|
71
- case key
72
- when 'bold'
73
- text.replace("<strong>#{text}</strong>")
74
- when 'italic'
75
- text.replace("<em>#{text}</em>")
76
- when 'header'
77
- text.replace("<h#{value}>#{text}</h#{value}>")
78
- when 'blockquote'
79
- text.replace("<blockquote>#{text}</blockquote>")
80
- when 'list'
81
- if pre_node.end_with?('</ul>')
82
- pre_node.gsub!('</ul>', "<li>#{text}</li></ul>")
83
- text.replace('')
84
- else
85
- text.replace("<ul><li>#{text}</li></ul>")
86
- end
87
- when 'link'
88
- text.replace('<a href="' + value + '" target="_blank">' + text + '</a>')
89
- end
90
- end
91
-
92
- text_array.join("\n")
93
- end
94
-
95
- def preprocess
96
- JSON.parse(@content).each_with_object([]) do |node, arr|
97
- next if node['insert'].blank? && node['attributes'].nil?
98
-
99
- if valid_inline_node?(node)
100
- arr.last.merge!(node) { |key, _| key == 'insert' ? arr.last[key] + node[key] : arr.last[key].merge(node[key]) }
35
+ def break_line(content, line, insert)
36
+ insert.split(/(?<=\n)/).each do |text|
37
+ if text.end_with?("\n")
38
+ content += "<p>#{line}#{text.delete("\n")}</p>"
39
+ line = ''
101
40
  else
102
- arr << node
41
+ line += text
103
42
  end
104
43
  end
44
+ [content, line]
105
45
  end
106
46
 
107
- def merge_array(text)
108
- text.split("\n").reject(&:blank?).map { |e| in_valid_html_tag(e) ? e : "<p>#{e}</p>" }.join
109
- end
110
-
111
- def valid_inline_node?(node)
112
- return false if node['insert'].is_a?(Hash) || node['insert'].present?
113
- return false if node['attributes'].nil?
114
- return false unless node['attributes'].keys.find { |k| VALID_INLINE_KEYS.include?(k) }
47
+ def inline(content, line, node)
48
+ return [content + ConvertInline.new(node).convert, line] if embed_node?(node)
115
49
 
116
- true
50
+ [content, line + ConvertInline.new(node).convert]
117
51
  end
118
52
 
119
- def valid_media_node?(node)
120
- return false unless node['insert'].keys.find { |k| VALID_MEDIA_KEYS.include?(k) }
53
+ def embed_node?(node)
54
+ return false if node['insert'].is_a?(String)
55
+ return false unless node['insert'].keys.find { |k| EMBED_KEYS.include?(k) }
121
56
 
122
57
  true
123
58
  end
124
-
125
- def in_valid_html_tag(text)
126
- text.start_with?(*START_VALID_HTML_TAGS) && text.end_with?(*END_VALID_HTML_TAGS)
127
- end
128
59
  end
@@ -1,3 +1,3 @@
1
1
  module BuilderQuillContent
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: builder_quill_content
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Tran
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-02-28 00:00:00.000000000 Z
11
+ date: 2020-03-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -28,7 +28,7 @@ files:
28
28
  - builder_quill_content.gemspec
29
29
  - lib/builder_quill_content.rb
30
30
  - lib/builder_quill_content/version.rb
31
- homepage: https://github.com/michaelt0520/word_censored
31
+ homepage: https://github.com/michaelt0520/builder_quill_content
32
32
  licenses:
33
33
  - MIT
34
34
  metadata: {}
@@ -50,5 +50,5 @@ requirements: []
50
50
  rubygems_version: 3.1.2
51
51
  signing_key:
52
52
  specification_version: 4
53
- summary: Convert quill content to html
53
+ summary: Convert quill content to html for wakuwaku
54
54
  test_files: []