class Formalist::RichText::Rendering::HTMLRenderer
Constants
- BLOCK_ELEMENTS_MAP
block and entity must iterate over the children and yield each of the children back to the compiler
- DEFAULT_BLOCK_ELEMENT
- DEFAULT_INLINE_ELEMENT
- INLINE_ELEMENTS_MAP
Public Class Methods
new(options = {})
click to toggle source
# File lib/formalist/rich_text/rendering/html_renderer.rb, line 41 def initialize(options = {}) @options = options end
Public Instance Methods
block(type, key, children) { |child| ... }
click to toggle source
Defines how to handle a block node
# 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) { |child| ... }
click to toggle source
# 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)
click to toggle source
# 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) { |node| ... }
click to toggle source
Defines how to handle a list of nodes
# 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) { |child| ... }
click to toggle source
Defines how to handle a list of blocks with a list type
# 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
Private Instance Methods
block_atomic(key, children)
click to toggle source
# File lib/formalist/rich_text/rendering/html_renderer.rb, line 93 def block_atomic(key, children) children.join end
entity_default(attrs, children)
click to toggle source
# File lib/formalist/rich_text/rendering/html_renderer.rb, line 130 def entity_default(attrs, children) html_tag(:div, attrs) do children.join end end
entity_image(data, children)
click to toggle source
# File lib/formalist/rich_text/rendering/html_renderer.rb, line 122 def entity_image(data, children) html_tag(:img, src: data[:src]) end
entity_link(data, children)
click to toggle source
# File lib/formalist/rich_text/rendering/html_renderer.rb, line 109 def entity_link(data, children) link_attrs = { href: data[:url] } link_attrs = link_attrs.merge( target: "_blank", rel: "noopener" ) if data[:newWindow] html_tag(:a, link_attrs) do children.join end end
entity_video(data, children)
click to toggle source
# File lib/formalist/rich_text/rendering/html_renderer.rb, line 126 def entity_video(data, children) html_tag(:video, src: data[:src]) end
html_options_string(options)
click to toggle source
# File lib/formalist/rich_text/rendering/html_renderer.rb, line 173 def html_options_string(options) opts = options.map do |key, val| "#{key}='#{val}'" end opts.join(" ") end
html_tag(tag, options = {}) { |: ""| ... }
click to toggle source
# File lib/formalist/rich_text/rendering/html_renderer.rb, line 160 def html_tag(tag, options = {}) options_string = html_options_string(options) out = "<#{tag} #{options_string}".strip content = block_given? ? yield : "" if content.nil? || content.empty? out << "/>" else out << ">#{replace_soft_newlines(content)}</#{tag}>" end end
render_block_element(type, content)
click to toggle source
# File lib/formalist/rich_text/rendering/html_renderer.rb, line 136 def render_block_element(type, content) elem = BLOCK_ELEMENTS_MAP.fetch(type.downcase, DEFAULT_BLOCK_ELEMENT) html_tag(elem) do if content.is_a?(Array) content.join else content end end end
render_inline_element(type, content)
click to toggle source
# File lib/formalist/rich_text/rendering/html_renderer.rb, line 148 def render_inline_element(type, content) elem = INLINE_ELEMENTS_MAP.fetch(type.downcase, DEFAULT_INLINE_ELEMENT) html_tag(elem, class: "inline--#{type.downcase}") do if content.is_a?(Array) content.join else content end end end
replace_soft_newlines(content)
click to toggle source
# File lib/formalist/rich_text/rendering/html_renderer.rb, line 180 def replace_soft_newlines(content) content.gsub(/\n/, '<br/>') end
wrapper_ordered_list_item(children)
click to toggle source
# File lib/formalist/rich_text/rendering/html_renderer.rb, line 103 def wrapper_ordered_list_item(children) html_tag(:ol) do children.join end end
wrapper_unordered_list_item(children)
click to toggle source
# File lib/formalist/rich_text/rendering/html_renderer.rb, line 97 def wrapper_unordered_list_item(children) html_tag(:ul) do children.join end end