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, #document, #initialize, #out, #plain, #reference_def

Constructor Details

This class inherits a constructor from CommonMarker::Renderer

Instance Method Details

#blockquote(node) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/commonmarker/renderer/html_renderer.rb', line 55

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

#code(node) ⇒ Object



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

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

#code_block(node) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/commonmarker/renderer/html_renderer.rb', line 69

def code_block(node)
  block do
    out('<pre><code')
    if node.fence_info && node.fence_info.length > 0
      out(' class="language-', node.fence_info.split(/\s+/)[0], '">')
    else
      out(">")
    end
    out(escape_html(node.string_content))
    out('</code></pre>')
  end
end

#emph(node) ⇒ Object



92
93
94
# File 'lib/commonmarker/renderer/html_renderer.rb', line 92

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

#escape_href(str) ⇒ Object



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

def escape_href(str)
  CommonMarker::Node.html_escape_href(str)
end

#escape_html(str) ⇒ Object



142
143
144
# File 'lib/commonmarker/renderer/html_renderer.rb', line 142

def escape_html(str)
  CommonMarker::Node.html_escape_html(str)
end

#header(node) ⇒ Object



7
8
9
10
11
12
# File 'lib/commonmarker/renderer/html_renderer.rb', line 7

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

#hrule(node) ⇒ Object



63
64
65
66
67
# File 'lib/commonmarker/renderer/html_renderer.rb', line 63

def hrule(node)
  block do
    out('<hr />')
  end
end

#html(node) ⇒ Object



82
83
84
85
86
# File 'lib/commonmarker/renderer/html_renderer.rb', line 82

def html(node)
  block do
    out(node.string_content)
  end
end

#image(node) ⇒ Object



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

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

#inline_html(node) ⇒ Object



88
89
90
# File 'lib/commonmarker/renderer/html_renderer.rb', line 88

def inline_html(node)
  out(node.string_content)
end

#linebreak(node) ⇒ Object



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

def linebreak(node)
  out('<br />')
  softbreak(node)
end


100
101
102
103
104
105
106
# File 'lib/commonmarker/renderer/html_renderer.rb', line 100

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

#list(node) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/commonmarker/renderer/html_renderer.rb', line 26

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

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

  @in_tight = old_in_tight
end

#list_item(node) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/commonmarker/renderer/html_renderer.rb', line 47

def list_item(node)
  block do
    container('<li>', '</li>') do
      out(:children)
    end
  end
end

#paragraph(node) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/commonmarker/renderer/html_renderer.rb', line 14

def paragraph(node)
  if @in_tight && node.parent.type != :blockquote
    out(:children)
  else
    block do
      container('<p>', '</p>') do
        out(:children)
      end
    end
  end
end

#render(node) ⇒ Object



3
4
5
# File 'lib/commonmarker/renderer/html_renderer.rb', line 3

def render(node)
  super(node)
end

#softbreak(node) ⇒ Object



134
135
136
# File 'lib/commonmarker/renderer/html_renderer.rb', line 134

def softbreak(node)
  out("\n")
end

#strong(node) ⇒ Object



96
97
98
# File 'lib/commonmarker/renderer/html_renderer.rb', line 96

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

#text(node) ⇒ Object



119
120
121
# File 'lib/commonmarker/renderer/html_renderer.rb', line 119

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