Class: Protos::Markdown
- Inherits:
-
Component
- Object
- Component
- Protos::Markdown
- Defined in:
- lib/protos/markdown.rb,
lib/protos/markdown/ast.rb,
lib/protos/markdown/table.rb
Overview
rubocop:disable Metrics/ClassLength
Defined Under Namespace
Constant Summary collapse
- Heading =
Data.define(:node) do def id text.downcase.gsub(/[^a-z0-9]+/, "-").chomp("-") end def text buffer = +"" node.walk do |node| buffer << node.string_content rescue TypeError # Ignore non-text nodes end buffer end def header_level node.header_level end end
Instance Method Summary collapse
- #view_template ⇒ Object
- #visit_block_quote(node) ⇒ Object
- #visit_code(node) ⇒ Object
- #visit_code_block(node) ⇒ Object
- #visit_document(_node) ⇒ Object
- #visit_emph(node) ⇒ Object
- #visit_escaped(node) ⇒ Object
- #visit_heading(node) ⇒ Object
- #visit_html(node) ⇒ Object
- #visit_html_block(_node) ⇒ Object
- #visit_html_inline(node) ⇒ Object
- #visit_image(node) ⇒ Object
- #visit_item(node) ⇒ Object
- #visit_link(node) ⇒ Object
- #visit_list(node) ⇒ Object
- #visit_paragraph(node) ⇒ Object
- #visit_softbreak(_node) ⇒ Object
- #visit_strong(node) ⇒ Object
- #visit_table(node) ⇒ Object
- #visit_text(node) ⇒ Object
- #visit_thematic_break(_node) ⇒ Object
Instance Method Details
#view_template ⇒ Object
37 38 39 40 41 |
# File 'lib/protos/markdown.rb', line 37 def view_template return unless root root.accept(self) end |
#visit_block_quote(node) ⇒ Object
136 137 138 |
# File 'lib/protos/markdown.rb', line 136 def visit_block_quote(node) blockquote { visit_children(node) } end |
#visit_code(node) ⇒ Object
116 117 118 119 120 |
# File 'lib/protos/markdown.rb', line 116 def visit_code(node) inline_code do |**attributes| code(**attributes) { plain(node.string_content) } end end |
#visit_code_block(node) ⇒ Object
122 123 124 125 126 127 128 129 130 |
# File 'lib/protos/markdown.rb', line 122 def visit_code_block(node) code_block(node.string_content, node.fence_info) do |**attributes| pre(**attributes) do code(class: "highlight language-#{node.fence_info}") do raw safe(lex(node.string_content, node.fence_info)) end end end end |
#visit_document(_node) ⇒ Object
43 44 45 |
# File 'lib/protos/markdown.rb', line 43 def visit_document(_node) # Do nothing end |
#visit_emph(node) ⇒ Object
96 97 98 |
# File 'lib/protos/markdown.rb', line 96 def visit_emph(node) em { visit_children(node) } end |
#visit_escaped(node) ⇒ Object
156 157 158 |
# File 'lib/protos/markdown.rb', line 156 def visit_escaped(node) plain(node.first_child&.string_content) end |
#visit_heading(node) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/protos/markdown.rb', line 61 def visit_heading(node) heading = Heading.new(node) case heading.header_level in 1 then h1(id: heading.id) { visit_children(node) } in 2 then h2(id: heading.id) { visit_children(node) } in 3 then h3(id: heading.id) { visit_children(node) } in 4 then h4(id: heading.id) { visit_children(node) } in 5 then h5(id: heading.id) { visit_children(node) } in 6 then h6(id: heading.id) { visit_children(node) } end end |
#visit_html(node) ⇒ Object
140 141 142 143 144 |
# File 'lib/protos/markdown.rb', line 140 def visit_html(node) return if @sanitize raw safe(node.string_content) end |
#visit_html_block(_node) ⇒ Object
152 153 154 |
# File 'lib/protos/markdown.rb', line 152 def visit_html_block(_node) nil end |
#visit_html_inline(node) ⇒ Object
146 147 148 149 150 |
# File 'lib/protos/markdown.rb', line 146 def visit_html_inline(node) return if @sanitize raw safe(node.to_html(options: { render: { unsafe: true } })) end |
#visit_image(node) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/protos/markdown.rb', line 88 def visit_image(node) img( src: node.url, alt: node.each.first.string_content, title: node.title ) end |
#visit_item(node) ⇒ Object
112 113 114 |
# File 'lib/protos/markdown.rb', line 112 def visit_item(node) li { visit_children(node) } end |
#visit_link(node) ⇒ Object
84 85 86 |
# File 'lib/protos/markdown.rb', line 84 def visit_link(node) a(href: node.url, title: node.title) { visit_children(node) } end |
#visit_list(node) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/protos/markdown.rb', line 104 def visit_list(node) case node.list_type when :ordered then ol { visit_children(node) } when :bullet then ul { visit_children(node) } else raise ArgumentError, "Unknown list type: #{node.list_type}" end end |
#visit_paragraph(node) ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/protos/markdown.rb', line 74 def visit_paragraph(node) grandparent = node.parent&.parent if grandparent&.type == :list && grandparent&.list_tight visit_children(node) else p { visit_children(node) } end end |
#visit_softbreak(_node) ⇒ Object
53 54 55 |
# File 'lib/protos/markdown.rb', line 53 def visit_softbreak(_node) whitespace end |
#visit_strong(node) ⇒ Object
100 101 102 |
# File 'lib/protos/markdown.rb', line 100 def visit_strong(node) strong { visit_children(node) } end |
#visit_table(node) ⇒ Object
47 48 49 50 51 |
# File 'lib/protos/markdown.rb', line 47 def visit_table(node) render Markdown::Table.new do |table| node.accept(table) end end |
#visit_text(node) ⇒ Object
57 58 59 |
# File 'lib/protos/markdown.rb', line 57 def visit_text(node) plain(node.string_content) end |
#visit_thematic_break(_node) ⇒ Object
132 133 134 |
# File 'lib/protos/markdown.rb', line 132 def visit_thematic_break(_node) hr end |