Class: Prosereflect::Output::Html

Inherits:
Object
  • Object
show all
Defined in:
lib/prosereflect/output/html.rb

Class Method Summary collapse

Class Method Details

.convert(document) ⇒ Object

Convert a Prosereflect::Document to HTML



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
35
36
# File 'lib/prosereflect/output/html.rb', line 10

def convert(document)
  builder = Nokogiri::HTML::Builder.new do |doc|
    doc.div do
      process_node(document, doc)
    end
  end

  doc = Nokogiri::HTML(builder.to_html)
  html = doc.at_css("div").children.to_html

  code_blocks = {}
  html.scan(%r{<code[^>]*>(.*?)</code>}m).each_with_index do |match, i|
    code_content = match[0]
    placeholder = "CODE_BLOCK_#{i}"
    code_blocks[placeholder] = code_content
    html.sub!(code_content, placeholder)
  end

  # Remove newlines and spaces
  html = html.gsub(/\n\s*/, "")

  code_blocks.each do |placeholder, content|
    html.sub!(placeholder, content)
  end

  html
end

.render(document, options = {}) ⇒ Object

Render document with options



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/prosereflect/output/html.rb', line 39

def render(document, options = {})
  options = {
    document: true,
    text: ->(text, _marks) { text },
    mark: ->(_mark, content) { content },
    node: ->(_node, content) { content },
  }.merge(options)

  serializer = DOMSerializer.new(document.schema, options)
  serializer.serialize(document)
end

.render_node(node, options = {}) ⇒ Object

Render single node with marks



52
53
54
55
# File 'lib/prosereflect/output/html.rb', line 52

def render_node(node, options = {})
  serializer = DOMSerializer.new(nil, options)
  serializer.render_node(node)
end

.render_text(text, marks, options = {}) ⇒ Object

Render text with marks applied



58
59
60
61
# File 'lib/prosereflect/output/html.rb', line 58

def render_text(text, marks, options = {})
  serializer = DOMSerializer.new(nil, options)
  serializer.render_text(text, marks)
end