Class: CommonMarker::HtmlRenderer

Inherits:
Renderer
  • Object
show all
Defined in:
lib/commonmarker/renderer/html_renderer.rb

Instance Attribute Summary

Attributes inherited from Renderer

#in_plain, #in_tight, #warnings

Instance Method Summary collapse

Methods inherited from Renderer

#block, #blocksep, #container, #containersep, #cr, #initialize, #out, #plain, #reference_def, #render

Constructor Details

This class inherits a constructor from CommonMarker::Renderer

Instance Method Details

#blockquote(node) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/commonmarker/renderer/html_renderer.rb', line 77

def blockquote(node)
  block do
    container("<blockquote#{sourcepos(node)}>\n", '</blockquote>') do
      out(:children)
    end
  end
end

#code(node) ⇒ Object



155
156
157
158
159
# File 'lib/commonmarker/renderer/html_renderer.rb', line 155

def code(node)
  out('<code>')
  out(escape_html(node.string_content))
  out('</code>')
end

#code_block(node) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/commonmarker/renderer/html_renderer.rb', line 91

def code_block(node)
  block do
    if option_enabled?(:GITHUB_PRE_LANG)
      out("<pre#{sourcepos(node)}")
      out(' lang="', node.fence_info.split(/\s+/)[0], '"') if node.fence_info && !node.fence_info.empty?
      out('><code>')
    else
      out("<pre#{sourcepos(node)}><code")
      if node.fence_info && !node.fence_info.empty?
        out(' class="language-', node.fence_info.split(/\s+/)[0], '">')
      else
        out('>')
      end
    end
    out(escape_html(node.string_content))
    out('</code></pre>')
  end
end

#document(_) ⇒ Object



5
6
7
8
# File 'lib/commonmarker/renderer/html_renderer.rb', line 5

def document(_)
  super
  out("</ol>\n</section>\n") if @written_footnote_ix
end

#emph(_) ⇒ Object



128
129
130
# File 'lib/commonmarker/renderer/html_renderer.rb', line 128

def emph(_)
  out('<em>', :children, '</em>')
end

#footnote_definition(_) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/commonmarker/renderer/html_renderer.rb', line 219

def footnote_definition(_)
  unless @footnote_ix
    out("<section class=\"footnotes\">\n<ol>\n")
    @footnote_ix = 0
  end

  @footnote_ix += 1
  out("<li id=\"fn#{@footnote_ix}\">\n", :children)
  out("\n") if out_footnote_backref
  out("</li>\n")
  # </ol>
  # </section>
end

#footnote_reference(node) ⇒ Object



215
216
217
# File 'lib/commonmarker/renderer/html_renderer.rb', line 215

def footnote_reference(node)
  out("<sup class=\"footnote-ref\"><a href=\"#fn#{node.string_content}\" id=\"fnref#{node.string_content}\">#{node.string_content}</a></sup>")
end

#header(node) ⇒ Object



10
11
12
13
14
15
# File 'lib/commonmarker/renderer/html_renderer.rb', line 10

def header(node)
  block do
    out('<h', node.header_level, "#{sourcepos(node)}>", :children,
        '</h', node.header_level, '>')
  end
end

#hrule(node) ⇒ Object



85
86
87
88
89
# File 'lib/commonmarker/renderer/html_renderer.rb', line 85

def hrule(node)
  block do
    out("<hr#{sourcepos(node)} />")
  end
end

#html(node) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/commonmarker/renderer/html_renderer.rb', line 110

def html(node)
  block do
    if option_enabled?(:UNSAFE)
      out(tagfilter(node.string_content))
    else
      out('<!-- raw HTML omitted -->')
    end
  end
end

#image(node) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/commonmarker/renderer/html_renderer.rb', line 142

def image(node)
  out('<img src="', escape_href(node.url), '"')
  plain do
    out(' alt="', :children, '"')
  end
  out(' title="', escape_html(node.title), '"') if node.title && !node.title.empty?
  out(' />')
end

#inline_html(node) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/commonmarker/renderer/html_renderer.rb', line 120

def inline_html(node)
  if option_enabled?(:UNSAFE)
    out(tagfilter(node.string_content))
  else
    out('<!-- raw HTML omitted -->')
  end
end

#linebreak(_node) ⇒ Object



161
162
163
# File 'lib/commonmarker/renderer/html_renderer.rb', line 161

def linebreak(_node)
  out("<br />\n")
end


136
137
138
139
140
# File 'lib/commonmarker/renderer/html_renderer.rb', line 136

def link(node)
  out('<a href="', node.url.nil? ? '' : escape_href(node.url), '"')
  out(' title="', escape_html(node.title), '"') if node.title && !node.title.empty?
  out('>', :children, '</a>')
end

#list(node) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/commonmarker/renderer/html_renderer.rb', line 33

def list(node)
  old_in_tight = @in_tight
  @in_tight = node.list_tight

  block do
    if node.list_type == :bullet_list
      container("<ul#{sourcepos(node)}>\n", '</ul>') do
        out(:children)
      end
    else
      start = if node.list_start == 1
                "<ol#{sourcepos(node)}>\n"
              else
                "<ol start=\"#{node.list_start}\"#{sourcepos(node)}>\n"
              end
      container(start, '</ol>') do
        out(:children)
      end
    end
  end

  @in_tight = old_in_tight
end

#list_item(node) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/commonmarker/renderer/html_renderer.rb', line 57

def list_item(node)
  block do
    tasklist_data = tasklist(node)
    container("<li#{sourcepos(node)}#{tasklist_data}>#{' ' if tasklist?(node)}", '</li>') do
      out(:children)
    end
  end
end

#paragraph(node) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/commonmarker/renderer/html_renderer.rb', line 17

def paragraph(node)
  if @in_tight && node.parent.type != :blockquote
    out(:children)
  else
    block do
      container("<p#{sourcepos(node)}>", '</p>') do
        out(:children)
        if node.parent.type == :footnote_definition && node.next.nil?
          out(' ')
          out_footnote_backref
        end
      end
    end
  end
end

#softbreak(_) ⇒ Object



165
166
167
168
169
170
171
172
173
# File 'lib/commonmarker/renderer/html_renderer.rb', line 165

def softbreak(_)
  if option_enabled?(:HARDBREAKS)
    out("<br />\n")
  elsif option_enabled?(:NOBREAKS)
    out(' ')
  else
    out("\n")
  end
end

#strikethrough(_) ⇒ Object



211
212
213
# File 'lib/commonmarker/renderer/html_renderer.rb', line 211

def strikethrough(_)
  out('<del>', :children, '</del>')
end

#strong(_) ⇒ Object



132
133
134
# File 'lib/commonmarker/renderer/html_renderer.rb', line 132

def strong(_)
  out('<strong>', :children, '</strong>')
end

#table(node) ⇒ Object



175
176
177
178
179
180
181
# File 'lib/commonmarker/renderer/html_renderer.rb', line 175

def table(node)
  @alignments = node.table_alignments
  @needs_close_tbody = false
  out("<table#{sourcepos(node)}>\n", :children)
  out("</tbody>\n") if @needs_close_tbody
  out("</table>\n")
end

#table_cell(node) ⇒ Object



200
201
202
203
204
205
206
207
208
209
# File 'lib/commonmarker/renderer/html_renderer.rb', line 200

def table_cell(node)
  align = case @alignments[@column_index]
          when :left then ' align="left"'
          when :right then ' align="right"'
          when :center then ' align="center"'
          else; ''
          end
  out(@in_header ? "<th#{align}#{sourcepos(node)}>" : "<td#{align}#{sourcepos(node)}>", :children, @in_header ? "</th>\n" : "</td>\n")
  @column_index += 1
end

#table_header(node) ⇒ Object



183
184
185
186
187
188
189
# File 'lib/commonmarker/renderer/html_renderer.rb', line 183

def table_header(node)
  @column_index = 0

  @in_header = true
  out("<thead>\n<tr#{sourcepos(node)}>\n", :children, "</tr>\n</thead>\n")
  @in_header = false
end

#table_row(node) ⇒ Object



191
192
193
194
195
196
197
198
# File 'lib/commonmarker/renderer/html_renderer.rb', line 191

def table_row(node)
  @column_index = 0
  if !@in_header && !@needs_close_tbody
    @needs_close_tbody = true
    out("<tbody>\n")
  end
  out("<tr#{sourcepos(node)}>\n", :children, "</tr>\n")
end

#tasklist(node) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/commonmarker/renderer/html_renderer.rb', line 66

def tasklist(node)
  return '' unless tasklist?(node)

  state = if checked?(node)
            'checked="" disabled=""'
          else
            'disabled=""'
          end
  "><input type=\"checkbox\" #{state} /"
end

#text(node) ⇒ Object



151
152
153
# File 'lib/commonmarker/renderer/html_renderer.rb', line 151

def text(node)
  out(escape_html(node.string_content))
end