Class: MarkdownIt::Renderer

Inherits:
Object
  • Object
show all
Extended by:
Common::Utils
Includes:
Common::Utils
Defined in:
lib/motion-markdown-it/renderer.rb

Constant Summary

Constants included from Common::Utils

Common::Utils::DIGITAL_ENTITY_TEST_RE, Common::Utils::ENTITY_RE, Common::Utils::HTML_ESCAPE_REPLACE_RE, Common::Utils::HTML_ESCAPE_TEST_RE, Common::Utils::HTML_REPLACEMENTS, Common::Utils::REGEXP_ESCAPE_RE, Common::Utils::UNESCAPE_ALL_RE, Common::Utils::UNESCAPE_MD_RE, Common::Utils::UNICODE_PUNCT_RE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common::Utils

arrayReplaceAt, assign, escapeHtml, escapeRE, fromCodePoint, isMdAsciiPunct, isPunctChar, isValidEntityCode, isWhiteSpace, normalizeReference, replaceEntityPattern, unescapeAll, unescapeMd

Constructor Details

#initializeRenderer

new Renderer()

Creates new [[Renderer]] instance and fill [[Renderer#rules]] with defaults.




84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/motion-markdown-it/renderer.rb', line 84

def initialize
  @default_rules = {
    'code_inline' => lambda {|tokens, idx, options, env, renderer| Renderer.code_inline(tokens, idx)},
    'code_block'  => lambda {|tokens, idx, options, env, renderer| Renderer.code_block(tokens, idx)},
    'fence'       => lambda {|tokens, idx, options, env, renderer| Renderer.fence(tokens, idx, options, env, renderer)},
    'image'       => lambda {|tokens, idx, options, env, renderer| Renderer.image(tokens, idx, options, env, renderer)},
    'hardbreak'   => lambda {|tokens, idx, options, env, renderer| Renderer.hardbreak(tokens, idx, options)},
    'softbreak'   => lambda {|tokens, idx, options, env, renderer| Renderer.softbreak(tokens, idx, options)},
    'text'        => lambda {|tokens, idx, options, env, renderer| Renderer.text(tokens, idx)},
    'html_block'  => lambda {|tokens, idx, options, env, renderer| Renderer.html_block(tokens, idx)},
    'html_inline' => lambda {|tokens, idx, options, env, renderer| Renderer.html_inline(tokens, idx)}
  }
  
  # Renderer#rules -> Object
  #
  # Contains render rules for tokens. Can be updated and extended.
  #
  # ##### Example
  #
  # ```javascript
  # var md = require('markdown-it')();
  #
  # md.renderer.rules.strong_open  = function () { return '<b>'; };
  # md.renderer.rules.strong_close = function () { return '</b>'; };
  #
  # var result = md.renderInline(...);
  # ```
  #
  # Each rule is called as independed static function with fixed signature:
  #
  # ```javascript
  # function my_token_render(tokens, idx, options, env, renderer) {
  #   // ...
  #   return renderedHTML;
  # }
  # ```
  #
  # See [source code](https://github.com/markdown-it/markdown-it/blob/master/lib/renderer.js)
  # for more details and examples.
  @rules = assign({}, @default_rules)
end

Instance Attribute Details

#rulesObject

Returns the value of attribute rules.



12
13
14
# File 'lib/motion-markdown-it/renderer.rb', line 12

def rules
  @rules
end

Class Method Details

.code_block(tokens, idx) ⇒ Object




21
22
23
# File 'lib/motion-markdown-it/renderer.rb', line 21

def self.code_block(tokens, idx)
  return '<pre><code>' + escapeHtml(tokens[idx].content) + "</code></pre>\n"
end

.code_inline(tokens, idx) ⇒ Object

Default Rules




16
17
18
# File 'lib/motion-markdown-it/renderer.rb', line 16

def self.code_inline(tokens, idx)
  return '<code>' + escapeHtml(tokens[idx].content) + '</code>'
end

.fence(tokens, idx, options, env, renderer) ⇒ Object




26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/motion-markdown-it/renderer.rb', line 26

def self.fence(tokens, idx, options, env, renderer)
  token     = tokens[idx]
  langName  = ''

  if !token.info.empty?
    langName = unescapeAll(token.info.strip.split(/\s+/)[0])
    token.attrPush([ 'class', options[:langPrefix] + langName ])
  end

  if options[:highlight]
    highlighted = options[:highlight].call(token.content, langName) || escapeHtml(token.content)
  else
    highlighted = escapeHtml(token.content)
  end

  return  '<pre><code' + renderer.renderAttrs(token) + '>' + highlighted + "</code></pre>\n"
end

.hardbreak(tokens, idx, options) ⇒ Object




59
60
61
# File 'lib/motion-markdown-it/renderer.rb', line 59

def self.hardbreak(tokens, idx, options)
  return options[:xhtmlOut] ? "<br />\n" : "<br>\n"
end

.html_block(tokens, idx) ⇒ Object




72
73
74
# File 'lib/motion-markdown-it/renderer.rb', line 72

def self.html_block(tokens, idx)
  return tokens[idx].content
end

.html_inline(tokens, idx) ⇒ Object



75
76
77
# File 'lib/motion-markdown-it/renderer.rb', line 75

def self.html_inline(tokens, idx)
  return tokens[idx].content
end

.image(tokens, idx, options, env, renderer) ⇒ Object




45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/motion-markdown-it/renderer.rb', line 45

def self.image(tokens, idx, options, env, renderer)
  token = tokens[idx]

  # "alt" attr MUST be set, even if empty. Because it's mandatory and
  # should be placed on proper position for tests.
  #
  # Replace content with actual value

  token.attrs[token.attrIndex('alt')][1] = renderer.renderInlineAsText(token.children, options, env)

  return renderer.renderToken(tokens, idx, options);
end

.softbreak(tokens, idx, options) ⇒ Object



62
63
64
# File 'lib/motion-markdown-it/renderer.rb', line 62

def self.softbreak(tokens, idx, options)
  return options[:breaks] ? (options[:xhtmlOut] ? "<br />\n" : "<br>\n") : "\n"
end

.text(tokens, idx) ⇒ Object




67
68
69
# File 'lib/motion-markdown-it/renderer.rb', line 67

def self.text(tokens, idx)
  return escapeHtml(tokens[idx].content)
end

Instance Method Details

#render(tokens, options, env) ⇒ Object

Renderer.render(tokens, options, env) -> String

  • tokens (Array): list on block tokens to renter

  • options (Object): params of parser instance

  • env (Object): additional data from parsed input (references, for example)

Takes token stream and generates HTML. Probably, you will never need to call this method directly.




268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/motion-markdown-it/renderer.rb', line 268

def render(tokens, options, env)
  result = ''
  rules  = @rules

  0.upto(tokens.length - 1) do |i|
    type = tokens[i].type

    if type == 'inline'
      result += renderInline(tokens[i].children, options, env)
    elsif rules[type] != nil
      result += rules[tokens[i].type].call(tokens, i, options, env, self)
    else
      result += renderToken(tokens, i, options)
    end
  end

  return result
end

#renderAttrs(token) ⇒ Object

Renderer.renderAttrs(token) -> String

Render token attributes to string.




131
132
133
134
135
136
137
138
139
140
# File 'lib/motion-markdown-it/renderer.rb', line 131

def renderAttrs(token)
  return '' if !token.attrs

  result = ''
  0.upto(token.attrs.length - 1) do |i|
    result += ' ' + escapeHtml(token.attrs[i][0]) + '="' + escapeHtml(token.attrs[i][1].to_s) + '"'
  end

  return result
end

#renderInline(tokens, options, env) ⇒ Object

Renderer.renderInline(tokens, options, env) -> String

  • tokens (Array): list on block tokens to renter

  • options (Object): params of parser instance

  • env (Object): additional data from parsed input (references, for example)

The same as [[Renderer.render]], but for single token of ‘inline` type.




216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/motion-markdown-it/renderer.rb', line 216

def renderInline(tokens, options, env)
  result  = ''
  rules   = @rules

  0.upto(tokens.length - 1) do |i|
    type = tokens[i].type
    
    if rules[type] != nil
      result += rules[type].call(tokens, i, options, env, self)
    else
      result += renderToken(tokens, i, options)
    end
  end

  return result;
end

#renderInlineAsText(tokens, options, env) ⇒ Object

internal Renderer.renderInlineAsText(tokens, options, env) -> String

  • tokens (Array): list on block tokens to renter

  • options (Object): params of parser instance

  • env (Object): additional data from parsed input (references, for example)

Special kludge for image ‘alt` attributes to conform CommonMark spec. Don’t try to use it! Spec requires to show ‘alt` content with stripped markup, instead of simple escaping.




244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/motion-markdown-it/renderer.rb', line 244

def renderInlineAsText(tokens, options, env)
  result = ''
  rules  = @rules

  0.upto(tokens.length - 1) do |i|
    if tokens[i].type == 'text'
      result += rules['text'].call(tokens, i, options, env, self)
    elsif tokens[i].type == 'image'
      result += renderInlineAsText(tokens[i].children, options, env)
    end
  end

  return result
end

#renderToken(tokens, idx, options, env = nil, renderer = nil) ⇒ Object

Renderer.renderToken(tokens, idx, options) -> String

  • tokens (Array): list of tokens

  • idx (Numbed): token index to render

  • options (Object): params of parser instance

Default token renderer. Can be overriden by custom function in [[Renderer#rules]].




151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/motion-markdown-it/renderer.rb', line 151

def renderToken(tokens, idx, options, env = nil, renderer = nil)
  result = ''
  needLf = false
  token  = tokens[idx]

  # Tight list paragraphs
  return '' if token.hidden

  # Insert a newline between hidden paragraph and subsequent opening
  # block-level tag.
  #
  # For example, here we should insert a newline before blockquote:
  #  - a
  #    >
  #
  if token.block && token.nesting != -1 && idx && tokens[idx - 1].hidden
    result += "\n"
  end

  # Add token name, e.g. `<img`
  result += (token.nesting == -1 ? '</' : '<') + token.tag

  # Encode attributes, e.g. `<img src="foo"`
  result += renderAttrs(token)

  # Add a slash for self-closing tags, e.g. `<img src="foo" /`
  if token.nesting == 0 && options[:xhtmlOut]
    result += ' /'
  end

  # Check if we need to add a newline after this tag
  if token.block
    needLf = true

    if token.nesting == 1
      if idx + 1 < tokens.length
        nextToken = tokens[idx + 1]

        if nextToken.type == 'inline' || nextToken.hidden
          # Block-level tag containing an inline tag.
          #
          needLf = false

        elsif nextToken.nesting == -1 && nextToken.tag == token.tag
          # Opening tag + closing tag of the same type. E.g. `<li></li>`.
          #
          needLf = false
        end
      end
    end
  end

  result += needLf ? ">\n" : '>'

  return result
end