class Storyblok::Richtext::HtmlRenderer

Public Class Methods

new() click to toggle source
# File lib/storyblok/richtext/html_renderer.rb, line 30
def initialize
  @marks = [
    Storyblok::Richtext::Marks::Bold,
    Storyblok::Richtext::Marks::Strike,
    Storyblok::Richtext::Marks::Underline,
    Storyblok::Richtext::Marks::Strong,
    Storyblok::Richtext::Marks::Code,
    Storyblok::Richtext::Marks::Italic,
    Storyblok::Richtext::Marks::Link,
    Storyblok::Richtext::Marks::Styled
  ]
  @nodes = [
    Storyblok::Richtext::Nodes::HorizontalRule,
    Storyblok::Richtext::Nodes::Blockquote,
    Storyblok::Richtext::Nodes::BulletList,
    Storyblok::Richtext::Nodes::CodeBlock,
    Storyblok::Richtext::Nodes::HardBreak,
    Storyblok::Richtext::Nodes::Heading,
    Storyblok::Richtext::Nodes::Image,
    Storyblok::Richtext::Nodes::ListItem,
    Storyblok::Richtext::Nodes::OrderedList,
    Storyblok::Richtext::Nodes::Paragraph,
    Storyblok::Richtext::Nodes::Text,
    Storyblok::Richtext::Nodes::Blok
  ]
end

Public Instance Methods

add_mark(mark) click to toggle source
# File lib/storyblok/richtext/html_renderer.rb, line 65
def add_mark(mark)
  @marks.push(mark)
end
add_node(node) click to toggle source
# File lib/storyblok/richtext/html_renderer.rb, line 61
def add_node(node)
  @nodes.push(node)
end
render(data) click to toggle source
# File lib/storyblok/richtext/html_renderer.rb, line 69
def render(data)
  html = ""
  data['content'].each do |node|
    html += render_node(node)
  end

  html
end
set_component_resolver(component_resolver) click to toggle source
# File lib/storyblok/richtext/html_renderer.rb, line 57
def set_component_resolver(component_resolver)
  Storyblok::Richtext::Nodes::Blok.send :define_method, :component_resolver, component_resolver
end

Private Instance Methods

get_matching_class(node, classes) click to toggle source
# File lib/storyblok/richtext/html_renderer.rb, line 153
def get_matching_class(node, classes)
  found = classes.select do |clazz|
    instance = clazz.new(node)
    if (instance.matching())
      return instance
    end
  end
  found.first
end
get_matching_mark(item) click to toggle source
# File lib/storyblok/richtext/html_renderer.rb, line 149
def get_matching_mark(item)
  return get_matching_class(item, @marks)
end
get_matching_node(item) click to toggle source
# File lib/storyblok/richtext/html_renderer.rb, line 145
def get_matching_node(item)
  return get_matching_class(item, @nodes)
end
render_closing_tag(tags) click to toggle source
# File lib/storyblok/richtext/html_renderer.rb, line 132
def render_closing_tag(tags)
  return "</#{tags}>" if tags.is_a? String

  all = tags.reverse.map do |tag|
    if tag.is_a? String
      "</#{tag}>"
    else
      "</#{tag[:tag]}>"
    end
  end
  return all.join('')
end
render_node(item) click to toggle source
# File lib/storyblok/richtext/html_renderer.rb, line 80
def render_node(item)
  html = []
  if item['marks']
    item['marks'].each do |m|
      mark = get_matching_mark(m)
      html.push(render_opening_tag(mark.tag)) if mark
    end
  end

  node = get_matching_node(item)
  html.push(render_opening_tag(node.tag)) if node and node.tag

  if item['content']
    item['content'].each do |content|
      html.push(render_node(content))
    end
  elsif node and node.text
    html.push(CGI.escapeHTML(node.text))
  elsif node and node.single_tag
    html.push(render_tag(node.single_tag))
  elsif node and node.html
    html.push(node.html)
  end

  html.push(render_closing_tag(node.tag)) if node and node.tag
  if item['marks']
    item['marks'].reverse.each do |m|
      mark = get_matching_mark(m)
      html.push(render_closing_tag(mark.tag)) if mark
    end
  end
  return html.join("")
end
render_opening_tag(tags) click to toggle source
# File lib/storyblok/richtext/html_renderer.rb, line 128
def render_opening_tag(tags)
  render_tag(tags, '')
end
render_tag(tags, ending = ' /') click to toggle source
# File lib/storyblok/richtext/html_renderer.rb, line 114
def render_tag(tags, ending = ' /')
  return "<#{tags}#{ending}>" if tags.is_a? String
  all = tags.map do |tag|
    if tag.is_a? String
      "<#{tag}#{ending}>"
    else
      h = "<#{tag[:tag]}"
      tag[:attrs].to_h.each_pair { |key, value| h += " #{key}=\"#{CGI.escapeHTML(value)}\"" if !value.nil? } if tag[:attrs]
      "#{h}#{ending}>"
    end
  end
  return all.join('')
end