Module: BuilderQuillContent
- Defined in:
- lib/builder_quill_content.rb,
lib/builder_quill_content/version.rb
Constant Summary collapse
- VALID_INLINE_KEYS =
%w[italic bold header blockquote link list].freeze
- VALID_MEDIA_KEYS =
%w[wk-image wk-youtube wk-tweet wk-instagram wk-divider waku-post wk-maps].freeze
- START_VALID_HTML_TAGS =
%w[<h2 <div <ul <hr <blockquote].freeze
- END_VALID_HTML_TAGS =
%w[/h2> /div> /ul> hr> /blockquote>].freeze
- VERSION =
"0.1.0"
Instance Method Summary collapse
- #convert_attribute(pre_node, node) ⇒ Object
- #convert_inline ⇒ Object
- #convert_media(node) ⇒ Object
- #in_valid_html_tag(text) ⇒ Object
- #merge_array(text) ⇒ Object
- #preprocess ⇒ Object
- #to_html(content) ⇒ Object
- #valid_inline_node?(node) ⇒ Boolean
- #valid_media_node?(node) ⇒ Boolean
Instance Method Details
#convert_attribute(pre_node, node) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/builder_quill_content.rb', line 59 def convert_attribute(pre_node, node) return node['insert'] if node['attributes'].nil? text_array = node['insert'].split("\n") text = text_array.last node['attributes'].each_pair do |key, value| case key when 'bold' text.replace("<strong>#{text}</strong>") when 'italic' text.replace("<em>#{text}</em>") when 'header' text.replace("<h#{value}>#{text}</h#{value}>") when 'blockquote' text.replace("<blockquote>#{text}</blockquote>") when 'list' if pre_node.end_with?('</ul>') pre_node.gsub!('</ul>', "<li>#{text}</li></ul>") text.replace('') else text.replace("<ul><li>#{text}</li></ul>") end when 'link' text.replace('<a href="' + value + '" target="_blank">' + text + '</a>') end end text_array.join("\n") end |
#convert_inline ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/builder_quill_content.rb', line 36 def convert_inline preprocess.each_with_object([]) do |node, arr| arr << if node['insert'].is_a?(Hash) convert_media(node) else convert_attribute(arr.last, node) end end end |
#convert_media(node) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/builder_quill_content.rb', line 46 def convert_media(node) key = node['insert'].keys.first case key when 'wk-image' '<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>' when 'wk-divider' '<hr>' else '<div data-id="' + key + '" data-src="' + node['insert'][key] + '"></div>' end end |
#in_valid_html_tag(text) ⇒ Object
120 121 122 |
# File 'lib/builder_quill_content.rb', line 120 def in_valid_html_tag(text) text.start_with?(*START_VALID_HTML_TAGS) && text.end_with?(*END_VALID_HTML_TAGS) end |
#merge_array(text) ⇒ Object
102 103 104 |
# File 'lib/builder_quill_content.rb', line 102 def merge_array(text) text.split("\n").reject(&:blank?).map { |e| in_valid_html_tag(e) ? e : "<p>#{e}</p>" }.join end |
#preprocess ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/builder_quill_content.rb', line 90 def preprocess JSON.parse(@content).each_with_object([]) do |node, arr| next if node['insert'].blank? && node['attributes'].nil? if valid_inline_node?(node) arr.last.merge!(node) { |key, _| key == 'insert' ? arr.last[key] + node[key] : arr.last[key].merge(node[key]) } else arr << node end end end |
#to_html(content) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/builder_quill_content.rb', line 9 def to_html(content) @content = content save_tag_p = result = '' convert_inline.each_with_index do |text, i| next if text.blank? if text.include?("\n") result += merge_array(text) elsif in_valid_html_tag(text) result += text elsif !in_valid_html_tag(text) if save_tag_p.blank? result += "<p>#{text}" save_tag_p = '<p>' elsif save_tag_p == '<p>' && in_valid_html_tag(convert_inline[i + 1]) result += "#{text}</p>" save_tag_p = '' else result += text end end end result end |
#valid_inline_node?(node) ⇒ Boolean
106 107 108 109 110 111 112 |
# File 'lib/builder_quill_content.rb', line 106 def valid_inline_node?(node) return false if node['insert'].is_a?(Hash) || node['insert'].present? return false if node['attributes'].nil? return false unless node['attributes'].keys.find { |k| VALID_INLINE_KEYS.include?(k) } true end |
#valid_media_node?(node) ⇒ Boolean
114 115 116 117 118 |
# File 'lib/builder_quill_content.rb', line 114 def valid_media_node?(node) return false unless node['insert'].keys.find { |k| VALID_MEDIA_KEYS.include?(k) } true end |