Class: DraftjsHtml::ToHtml

Inherits:
Object
  • Object
show all
Defined in:
lib/draftjs_html/to_html.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ToHtml

Returns a new instance of ToHtml.



8
9
10
11
12
# File 'lib/draftjs_html/to_html.rb', line 8

def initialize(options)
  @options = ensure_options!(options)
  @document = Nokogiri::HTML::Builder.new(encoding: @options.fetch(:encoding, 'UTF-8'))
  @current_bidi_direction = CurrentBidiDirection.new
end

Instance Method Details

#convert(raw_draftjs) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/draftjs_html/to_html.rb', line 14

def convert(raw_draftjs)
  draftjs = Draftjs.parse(raw_draftjs)

  @document.html do |html|
    html.body do |body|
      @html_depth = HtmlDepth.new(body)

      draftjs.blocks.each do |block|
        @html_depth.apply(block)

        body.public_send(block_element_for(block)) do |block_body|
          block.each_range do |char_range|
            squeeze_newlines(char_range)
            content = try_apply_entity_to(draftjs, char_range)

            apply_styles_to(block_body, char_range.style_names, Node.of(content))
          end
        end
      end
    end
  end

  @document.doc.css('body').first.children.to_html.strip
end