Class: Prosereflect::Output::DOMSerializer

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

Overview

DOMSerializer provides configurable document serialization to HTML

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, options = {}) ⇒ DOMSerializer

Returns a new instance of DOMSerializer.



405
406
407
408
409
# File 'lib/prosereflect/output/html.rb', line 405

def initialize(schema, options = {})
  @schema = schema
  @options = options
  @marks = build_mark_serializers
end

Instance Attribute Details

#marksObject (readonly)

Returns the value of attribute marks.



403
404
405
# File 'lib/prosereflect/output/html.rb', line 403

def marks
  @marks
end

#optionsObject (readonly)

Returns the value of attribute options.



403
404
405
# File 'lib/prosereflect/output/html.rb', line 403

def options
  @options
end

#schemaObject (readonly)

Returns the value of attribute schema.



403
404
405
# File 'lib/prosereflect/output/html.rb', line 403

def schema
  @schema
end

Instance Method Details

#apply_mark(mark, content) ⇒ Object



440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/prosereflect/output/html.rb', line 440

def apply_mark(mark, content)
  mark_handler = @marks[mark.type]
  return content unless mark_handler

  case mark.type
  when "bold"
    "<strong>#{content}</strong>"
  when "italic"
    "<em>#{content}</em>"
  when "code"
    "<code>#{content}</code>"
  when "link"
    href = extract_mark_attr(mark, "href")
    "<a href=\"#{href}\">#{content}</a>"
  when "strike"
    "<del>#{content}</del>"
  when "underline"
    "<u>#{content}</u>"
  when "subscript"
    "<sub>#{content}</sub>"
  when "superscript"
    "<sup>#{content}</sup>"
  else
    content
  end
end

#render_node(node) ⇒ Object



419
420
421
422
423
424
425
# File 'lib/prosereflect/output/html.rb', line 419

def render_node(node)
  return render_text(node.text, node.marks) if node.text?

  builder = Nokogiri::HTML::Builder.new
  render_node_to_builder(node, builder)
  builder.doc.root.children.to_html
end

#render_node_to_builder(node, builder) ⇒ Object



427
428
429
430
# File 'lib/prosereflect/output/html.rb', line 427

def render_node_to_builder(node, builder)
  content = render_node_content(node)
  wrap_node(node, content, builder)
end

#render_text(text, node_marks = nil) ⇒ Object



432
433
434
435
436
437
438
# File 'lib/prosereflect/output/html.rb', line 432

def render_text(text, node_marks = nil)
  marks_to_apply = node_marks || []
  marks_to_apply.each do |mark|
    text = apply_mark(mark, text)
  end
  text
end

#serialize(document) ⇒ Object



411
412
413
# File 'lib/prosereflect/output/html.rb', line 411

def serialize(document)
  render_node(document)
end

#serialize_node(node) ⇒ Object



415
416
417
# File 'lib/prosereflect/output/html.rb', line 415

def serialize_node(node)
  render_node(node)
end