Class: ProsemirrorToHtml::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/prosemirror_to_html.rb

Instance Method Summary collapse

Constructor Details

#initializeRenderer

Returns a new instance of Renderer.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/prosemirror_to_html.rb', line 34

def initialize
  @document = nil

  @nodes = [
    ProsemirrorToHtml::Nodes::Blockquote,
    ProsemirrorToHtml::Nodes::BulletList,
    ProsemirrorToHtml::Nodes::CodeBlock,
    ProsemirrorToHtml::Nodes::HardBreak,
    ProsemirrorToHtml::Nodes::Heading,
    ProsemirrorToHtml::Nodes::HorizontalRule,
    ProsemirrorToHtml::Nodes::Image,
    ProsemirrorToHtml::Nodes::ListItem,
    ProsemirrorToHtml::Nodes::OrderedList,
    ProsemirrorToHtml::Nodes::Paragraph,
    ProsemirrorToHtml::Nodes::Table,
    ProsemirrorToHtml::Nodes::TableCell,
    ProsemirrorToHtml::Nodes::TableHeader,
    ProsemirrorToHtml::Nodes::TableRow,
  ]

  @marks = [
    ProsemirrorToHtml::Marks::Bold,
    ProsemirrorToHtml::Marks::Code,
    ProsemirrorToHtml::Marks::Italic,
    ProsemirrorToHtml::Marks::Link,
    ProsemirrorToHtml::Marks::Strike,
    ProsemirrorToHtml::Marks::Subscript,
    ProsemirrorToHtml::Marks::Superscript,
    ProsemirrorToHtml::Marks::Underline,
  ]
end

Instance Method Details

#add_mark(mark) ⇒ Object



113
114
115
116
117
# File 'lib/prosemirror_to_html.rb', line 113

def add_mark(mark)
  @marks << mark

  self
end

#add_marks(marks) ⇒ Object



119
120
121
122
123
# File 'lib/prosemirror_to_html.rb', line 119

def add_marks(marks)
  @marks.push marks

  self
end

#add_node(node) ⇒ Object



101
102
103
104
105
# File 'lib/prosemirror_to_html.rb', line 101

def add_node(node)
  @nodes << node

  self
end

#add_nodes(nodes) ⇒ Object



107
108
109
110
111
# File 'lib/prosemirror_to_html.rb', line 107

def add_nodes(nodes)
  @nodes.push nodes

  self
end

#render(value) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/prosemirror_to_html.rb', line 78

def render(value)
  json = case value
         when Hash then value.to_json
         when String then value
         else raise Error
         end

  @document = JSON.parse(json, object_class: OpenStruct)

  html = ''

  content = @document.content.is_a?(Array) ? @document.content : []

  content.each_with_index do |node, index|
    prev_node = content[index - 1]
    next_node = content[index + 1]

    html << render_node(node, prev_node, next_node)
  end

  html
end

#replace_mark(search_mark, replace_mark) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/prosemirror_to_html.rb', line 135

def replace_mark(search_mark, replace_mark)
  @marks.each do |key, mark_klass|
    if mark_klass == search_mark
      @marks[key] = replace_mark
    end
  end

  self
end

#replace_node(search_node, replace_node) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/prosemirror_to_html.rb', line 125

def replace_node(search_node, replace_node)
  @nodes.each do |key, node_klass|
    if node_klass == search_node
      @nodes[key] = replace_node
    end
  end

  self
end

#with_marks(marks = nil) ⇒ Object



66
67
68
69
70
# File 'lib/prosemirror_to_html.rb', line 66

def with_marks(marks = nil)
  @marks = marks if marks.is_a? Array

  self
end

#with_nodes(nodes = nil) ⇒ Object



72
73
74
75
76
# File 'lib/prosemirror_to_html.rb', line 72

def with_nodes(nodes = nil)
  @nodes = nodes if nodes.is_a? Array

  self
end