Class: RicherText::HTMLVisitor

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TagHelper
Defined in:
lib/richer_text/html_visitor.rb

Instance Method Summary collapse

Instance Method Details

#visit(node) ⇒ Object



5
6
7
# File 'lib/richer_text/html_visitor.rb', line 5

def visit(node)
  node.accept(self)
end

#visit_attachment_figure(node) ⇒ Object



9
10
11
12
13
14
15
# File 'lib/richer_text/html_visitor.rb', line 9

def visit_attachment_figure(node)
  "<figure sgid=#{node.attrs["sgid"]}>
    #{node_previewable?(node) ? "<img src=#{node.url} />" : "<a href=#{node.url} target='_blank'>"}
    <figcaption class='attachment__caption'>#{visit_children(node).join}</figcaption>
    #{node_previewable?(node) ? "" : "</a>"}
  </figure>"
end


17
18
19
# File 'lib/richer_text/html_visitor.rb', line 17

def visit_attachment_gallery(node)
  "<div class='attachment-gallery'>#{visit_children(node).join}</div>"
end

#visit_blockquote(node) ⇒ Object



21
22
23
# File 'lib/richer_text/html_visitor.rb', line 21

def visit_blockquote(node)
  "<blockquote>#{visit_children(node).join}</blockquote>"
end

#visit_bullet_list(node) ⇒ Object



25
26
27
# File 'lib/richer_text/html_visitor.rb', line 25

def visit_bullet_list(node)
  "<ul>#{visit_children(node).join}</ul>"
end

#visit_callout(node) ⇒ Object



29
30
31
# File 'lib/richer_text/html_visitor.rb', line 29

def visit_callout(node)
  "<div class='callout' data-color='#{node.color}'>#{visit_children(node).join}</div>"
end

#visit_children(node) ⇒ Object



33
34
35
# File 'lib/richer_text/html_visitor.rb', line 33

def visit_children(node)
  node.children.map { |child| visit(child) }
end

#visit_code_block(node) ⇒ Object



37
38
39
# File 'lib/richer_text/html_visitor.rb', line 37

def visit_code_block(node)
  "<pre><code>#{visit_children(node).join("\n")}</code></pre>"
end

#visit_doc(node) ⇒ Object



41
42
43
# File 'lib/richer_text/html_visitor.rb', line 41

def visit_doc(node)
  "<div class='richer-text'>#{visit_children(node).join("\n")}</div>".html_safe
end

#visit_hard_break(_node) ⇒ Object



80
81
82
# File 'lib/richer_text/html_visitor.rb', line 80

def visit_hard_break(_node)
  "<br>"
end

#visit_heading(node) ⇒ Object



76
77
78
# File 'lib/richer_text/html_visitor.rb', line 76

def visit_heading(node)
  "<h#{node.level}>#{visit_children(node).join}</h#{node.level}>"
end

#visit_horizontal_rule(_node) ⇒ Object



84
85
86
# File 'lib/richer_text/html_visitor.rb', line 84

def visit_horizontal_rule(_node)
  "<div><hr></div>"
end

#visit_iframely_embed(node) ⇒ Object



45
46
47
# File 'lib/richer_text/html_visitor.rb', line 45

def visit_iframely_embed(node)
  node.html
end

#visit_image(node) ⇒ Object



88
89
90
# File 'lib/richer_text/html_visitor.rb', line 88

def visit_image(node)
  "<img src='#{node.src}' width='#{node.width}' />"
end

#visit_list_item(node) ⇒ Object



68
69
70
# File 'lib/richer_text/html_visitor.rb', line 68

def visit_list_item(node)
  "<li>#{visit_children(node).join}</li>"
end

#visit_mention(node, marks) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/richer_text/html_visitor.rb', line 49

def visit_mention(node, marks)
  if marks.any?
    (marks[0].tag, visit_mention(node, marks[1..]), marks[0].attrs)
  else
    tag.span(
      tag.img(src: node.avatar_url, class: "richer-text--mention-img") +
      tag.span(node.name, class: "richer-text--mention-label"),
      class: "richer-text--mention")
  end
end

#visit_ordered_list(node) ⇒ Object



72
73
74
# File 'lib/richer_text/html_visitor.rb', line 72

def visit_ordered_list(node)
  "<ol>#{visit_children(node).join}</ol>"
end

#visit_paragraph(node) ⇒ Object



60
61
62
# File 'lib/richer_text/html_visitor.rb', line 60

def visit_paragraph(node)
  "<p style='#{node.style}'>#{visit_children(node).join}</p>"
end

#visit_richer_text_embed(node) ⇒ Object



64
65
66
# File 'lib/richer_text/html_visitor.rb', line 64

def visit_richer_text_embed(node)
  node.html
end

#visit_table(node) ⇒ Object



100
101
102
# File 'lib/richer_text/html_visitor.rb', line 100

def visit_table(node)
  "<table>#{visit_children(node).join}</table>"
end

#visit_table_cell(node) ⇒ Object



108
109
110
# File 'lib/richer_text/html_visitor.rb', line 108

def visit_table_cell(node)
  "<td>#{visit_children(node).join}</td>"
end

#visit_table_header(node) ⇒ Object



112
113
114
# File 'lib/richer_text/html_visitor.rb', line 112

def visit_table_header(node)
  "<th>#{visit_children(node).join}</th>"
end

#visit_table_row(node) ⇒ Object



104
105
106
# File 'lib/richer_text/html_visitor.rb', line 104

def visit_table_row(node)
  "<tr>#{visit_children(node).join}</tr>"
end

#visit_text(node, marks) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/richer_text/html_visitor.rb', line 92

def visit_text(node, marks)
  if marks.empty?
    node.text
  else
    (marks[0].tag, visit_text(node, marks[1..]), parse_mark_attrs(marks[0]))
  end
end