Class: BuilderQuillContent

Inherits:
Object
  • Object
show all
Defined in:
lib/builder_quill_content.rb,
lib/builder_quill_content/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

EMBED_KEYS =
%w[wk-image wk-youtube wk-tweet wk-instagram wk-divider waku-post wk-maps].freeze
VERSION =
'1.2.1'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, args = {}) ⇒ BuilderQuillContent

Returns a new instance of BuilderQuillContent.



13
14
15
16
17
# File 'lib/builder_quill_content.rb', line 13

def initialize(input, args = {})
  @input = input
  @args  = args
  @line  = @content = ''
end

Instance Attribute Details

#argsObject

Your code goes here…



9
10
11
# File 'lib/builder_quill_content.rb', line 9

def args
  @args
end

#inputObject

Your code goes here…



9
10
11
# File 'lib/builder_quill_content.rb', line 9

def input
  @input
end

Instance Method Details

#break_line(insert) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# File 'lib/builder_quill_content.rb', line 45

def break_line(insert)
  insert.split(/(?<=\n)/).each do |text|
    if text.end_with?("\n")
      @content += "<p>#{@line}#{text.delete("\n")}</p>"
      @line = ''
    else
      @line += text
    end
  end
end

#embed_node?(node) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
# File 'lib/builder_quill_content.rb', line 60

def embed_node?(node)
  return false if node['insert'].is_a?(String)

  node['insert'].keys.find { |k| EMBED_KEYS.include?(k) }
end

#end_of_line(attributes) ⇒ Object



40
41
42
43
# File 'lib/builder_quill_content.rb', line 40

def end_of_line(attributes)
  @content += attributes.nil? ? "<p>#{@line}</p>" : ConvertInline.new({ 'insert' => @line, 'attributes' => attributes }, args).convert
  @line = ''
end

#inline(node) ⇒ Object



56
57
58
# File 'lib/builder_quill_content.rb', line 56

def inline(node)
  embed_node?(node) ? @content += ConvertInline.new(node, args).convert : @line += ConvertInline.new(node, args).convert
end

#to_htmlObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/builder_quill_content.rb', line 19

def to_html
  content_json = JSON.parse(@input)

  while content_json.length.positive?
    node = content_json.shift

    if node['insert'] == "\n"
      end_of_line(node['attributes'])
    elsif node['insert'].include?("\n")
      break_line(node['insert'])
    else
      inline(node)
    end
  end

  @content += @line unless @line.strip.empty?
  @content.gsub('</ul><ul>', '')
rescue JSON::ParserError
  'No content'
end