class ProsemirrorToHtml::Renderer

Your code goes here…

Public Class Methods

new() click to toggle source
# File lib/prosemirror_to_html.rb, line 27
def initialize()
  @storedMarks = []
  @marks = [
    ProsemirrorToHtml::Marks::Bold,
    ProsemirrorToHtml::Marks::Code,
    ProsemirrorToHtml::Marks::Italic,
    ProsemirrorToHtml::Marks::Link
  ]
  @nodes = [
    ProsemirrorToHtml::Nodes::BulletList,
    ProsemirrorToHtml::Nodes::CodeBlockWrapper,
    ProsemirrorToHtml::Nodes::CodeBlock,
    ProsemirrorToHtml::Nodes::HardBreak,
    ProsemirrorToHtml::Nodes::Heading,
    ProsemirrorToHtml::Nodes::Image,
    ProsemirrorToHtml::Nodes::ListItem,
    ProsemirrorToHtml::Nodes::OrderedList,
    ProsemirrorToHtml::Nodes::Paragraph,
    # ProsemirrorToHtml::Nodes::Text,
    ProsemirrorToHtml::Nodes::User
  ]
end

Public Instance Methods

addMark(mark) click to toggle source
# File lib/prosemirror_to_html.rb, line 58
def addMark(mark)
  @marks.push(mark)
end
addMarks(marks) click to toggle source
# File lib/prosemirror_to_html.rb, line 61
def addMarks(marks)
  marks.each do |mark|
    addMark(mark);
  end
end
addNode(node) click to toggle source
# File lib/prosemirror_to_html.rb, line 50
def addNode(node)
  @nodes.push(node)
end
addNodes(nodes) click to toggle source
# File lib/prosemirror_to_html.rb, line 53
def addNodes(nodes)
  nodes.each do |node|
    addNode(node);
  end
end
render(hash) click to toggle source
# File lib/prosemirror_to_html.rb, line 67
def render(hash)
  html = ""
  json = hash.to_json
  object = JSON.parse(json, object_class: OpenStruct)
  object.content.each do |node|
    html += renderNode(node)
  end
  
  html
end

Private Instance Methods

get_matching_class(node, classes) click to toggle source

Find which class matches the HtmlElement

# File lib/prosemirror_to_html.rb, line 137
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

Find which Mark matches the HtmlElement

# File lib/prosemirror_to_html.rb, line 133
def get_matching_mark(item)
  return get_matching_class(item, @marks)
end
get_matching_node(item) click to toggle source

Find which Node matches the Html Node

# File lib/prosemirror_to_html.rb, line 129
def get_matching_node(item)
  return get_matching_class(item, @nodes)
end
renderClosingTag(tags) click to toggle source
# File lib/prosemirror_to_html.rb, line 119
def renderClosingTag(tags)
  return "</#{tags}>" if tags.is_a? String
  all = tags.reverse.map do |tag|
    return "</#{tag}>" if tag.is_a? String
    "</#{tag[:tag]}>"
  end
  return all.join('')
end
renderNode(item) click to toggle source
# File lib/prosemirror_to_html.rb, line 78
def renderNode(item)
    html = []
    if item.marks
      item.marks.each do |m|
        mark = get_matching_mark(m)
        html.push(renderOpeningTag(mark.tag)) if mark
      end
    end
    
    node = get_matching_node(item)
    html.push(renderOpeningTag(node.tag)) if node

    if item.content
      item.content.each do |content|
        html.push(renderNode(content))
      end
    elsif item.text
      html.push(item.text)
    end
    
    html.push(renderClosingTag(node.tag)) if node
    if item.marks
      item.marks.reverse.each do |m|
        mark = get_matching_mark(m)
        html.push(renderClosingTag(mark.tag)) if mark
      end
    end
    return html.join("");
  end
renderOpeningTag(tags) click to toggle source
# File lib/prosemirror_to_html.rb, line 108
def renderOpeningTag(tags)
  return "<#{tags}>" if tags.is_a? String
  all = tags.map do |tag|
    return "<#{tags}>" if tag.is_a? String
    h = "<#{tag[:tag]}"
    tag[:attrs].to_h.each_pair { |key, value| h += " #{key}=\"#{value}\"" } if tag[:attrs]
    "#{h}>"
  end
  return all.join('')
end