Class: BuilderQuillContent
- Inherits:
-
Object
- Object
- BuilderQuillContent
- 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
-
#args ⇒ Object
Your code goes here…
-
#input ⇒ Object
Your code goes here…
Instance Method Summary collapse
- #break_line(insert) ⇒ Object
- #embed_node?(node) ⇒ Boolean
- #end_of_line(attributes) ⇒ Object
-
#initialize(input, args = {}) ⇒ BuilderQuillContent
constructor
A new instance of BuilderQuillContent.
- #inline(node) ⇒ Object
- #to_html ⇒ Object
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
#args ⇒ Object
Your code goes here…
9 10 11 |
# File 'lib/builder_quill_content.rb', line 9 def args @args end |
#input ⇒ Object
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
60 61 62 63 64 |
# File 'lib/builder_quill_content.rb', line 60 def (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) (node) ? @content += ConvertInline.new(node, args).convert : @line += ConvertInline.new(node, args).convert end |
#to_html ⇒ Object
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 |