Class: Formalist::RichText::Rendering::HTMLRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/formalist/rich_text/rendering/html_renderer.rb

Constant Summary collapse

BLOCK_ELEMENTS_MAP =

block and entity must iterate over the children and yield each of the children back to the compiler

{
  "header-one" => "h1",
  "header-two" => "h2",
  "header-three" => "h3",
  "header-four" => "h4",
  "header-five" => "h5",
  "header-six" => "h6",
  "unordered-list-item" => "li",
  "ordered-list-item" => "li",
  "blockquote" => "blockquote",
  "pullquote" => "aside",
  "code-block" => "pre",
  "horizontal-rule" => "hr",
}.freeze
DEFAULT_BLOCK_ELEMENT =
"p".freeze
INLINE_ELEMENTS_MAP =
{
  "bold" => "strong",
  "italic" => "em",
  "strikethrough" => "del",
  "code" => "code",
  "underline" => "u",
}
DEFAULT_INLINE_ELEMENT =
"span".freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ HTMLRenderer

Returns a new instance of HTMLRenderer.



41
42
43
# File 'lib/formalist/rich_text/rendering/html_renderer.rb', line 41

def initialize(options = {})
  @options = options
end

Instance Method Details

#block(type, key, children) ⇒ Object

Defines how to handle a block node



52
53
54
55
56
57
58
59
60
# File 'lib/formalist/rich_text/rendering/html_renderer.rb', line 52

def block(type, key, children)
  rendered_children = children.map { |child| yield(child) }

  if type == 'atomic'
    block_atomic(key, rendered_children)
  else
    render_block_element(type, rendered_children)
  end
end

#entity(type, key, data, children) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/formalist/rich_text/rendering/html_renderer.rb', line 80

def entity(type, key, data, children)
  rendered_children = children.map { |child| yield(child) }

  handler = :"entity_#{type.downcase}"
  if respond_to?(handler, _include_private=true)
    send(handler, data, rendered_children)
  else
    rendered_children
  end
end

#inline(styles, content) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/formalist/rich_text/rendering/html_renderer.rb', line 71

def inline(styles, content)
  return content if styles.nil? || styles.empty?
  out = content
  styles.each do |style|
    out = render_inline_element(style, out)
  end
  out
end

#nodes(nodes) ⇒ Object

Defines how to handle a list of nodes



46
47
48
49
# File 'lib/formalist/rich_text/rendering/html_renderer.rb', line 46

def nodes(nodes)
  nodes = nodes.map { |node| yield(node) } if block_given?
  nodes.join
end

#wrapper(type, children) ⇒ Object

Defines how to handle a list of blocks with a list type



63
64
65
66
67
68
69
# File 'lib/formalist/rich_text/rendering/html_renderer.rb', line 63

def wrapper(type, children)
  type_for_method = type.gsub("-", "_")

  rendered_children = children.map { |child| yield(child) }

  send(:"wrapper_#{type_for_method}", rendered_children)
end