Class: MarkdownIt::Renderer
- Inherits:
-
Object
- Object
- MarkdownIt::Renderer
- 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
-
#rules ⇒ Object
Returns the value of attribute rules.
Class Method Summary collapse
-
.code_block(tokens, idx, options, env, renderer) ⇒ Object
——————————————————————————.
-
.code_inline(tokens, idx, options, env, renderer) ⇒ Object
Default Rules ——————————————————————————.
-
.fence(tokens, idx, options, env, renderer) ⇒ Object
——————————————————————————.
-
.hardbreak(tokens, idx, options) ⇒ Object
——————————————————————————.
-
.html_block(tokens, idx) ⇒ Object
——————————————————————————.
- .html_inline(tokens, idx) ⇒ Object
-
.image(tokens, idx, options, env, renderer) ⇒ Object
——————————————————————————.
- .softbreak(tokens, idx, options) ⇒ Object
-
.text(tokens, idx) ⇒ Object
——————————————————————————.
Instance Method Summary collapse
-
#initialize ⇒ Renderer
constructor
new Renderer().
-
#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).
-
#renderAttrs(token) ⇒ Object
Renderer.renderAttrs(token) -> String.
-
#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).
-
#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).
-
#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.
Methods included from Common::Utils
arrayReplaceAt, assign, charCodeAt, escapeHtml, escapeRE, fromCharCode, fromCodePoint, isMdAsciiPunct, isPunctChar, isSpace, isValidEntityCode, isWhiteSpace, normalizeReference, replaceEntityPattern, unescapeAll, unescapeMd
Constructor Details
#initialize ⇒ Renderer
new Renderer()
Creates new [[Renderer]] instance and fill [[Renderer#rules]] with defaults.
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/motion-markdown-it/renderer.rb', line 122 def initialize @default_rules = { 'code_inline' => lambda {|tokens, idx, , env, renderer| Renderer.code_inline(tokens, idx, , env, renderer)}, 'code_block' => lambda {|tokens, idx, , env, renderer| Renderer.code_block(tokens, idx, , env, renderer)}, 'fence' => lambda {|tokens, idx, , env, renderer| Renderer.fence(tokens, idx, , env, renderer)}, 'image' => lambda {|tokens, idx, , env, renderer| Renderer.image(tokens, idx, , env, renderer)}, 'hardbreak' => lambda {|tokens, idx, , env, renderer| Renderer.hardbreak(tokens, idx, )}, 'softbreak' => lambda {|tokens, idx, , env, renderer| Renderer.softbreak(tokens, idx, )}, 'text' => lambda {|tokens, idx, , env, renderer| Renderer.text(tokens, idx)}, 'html_block' => lambda {|tokens, idx, , env, renderer| Renderer.html_block(tokens, idx)}, 'html_inline' => lambda {|tokens, idx, , 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 independet 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
#rules ⇒ Object
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, options, env, renderer) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/motion-markdown-it/renderer.rb', line 25 def self.code_block(tokens, idx, , env, renderer) token = tokens[idx] return '<pre' + renderer.renderAttrs(token) + '><code>' + escapeHtml(tokens[idx].content) + "</code></pre>\n" end |
.code_inline(tokens, idx, options, env, renderer) ⇒ Object
Default Rules
16 17 18 19 20 21 22 |
# File 'lib/motion-markdown-it/renderer.rb', line 16 def self.code_inline(tokens, idx, , env, renderer) token = tokens[idx] return '<code' + renderer.renderAttrs(token) + '>' + escapeHtml(tokens[idx].content) + '</code>' end |
.fence(tokens, idx, options, env, renderer) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/motion-markdown-it/renderer.rb', line 34 def self.fence(tokens, idx, , env, renderer) token = tokens[idx] info = token.info ? unescapeAll(token.info).strip : '' langName = '' langAttrs = '' if !info.empty? arr = info.split(/\s+/) langName = arr[0] langAttrs = arr[1..-1].join(' ') end if [:highlight] highlighted = [:highlight].call(token.content, langName, langAttrs) || escapeHtml(token.content) else highlighted = escapeHtml(token.content) end if highlighted.start_with?('<pre') return highlighted + "\n" end # If language exists, inject class gently, without modifying original token. # May be, one day we will add .deepClone() for token and simplify this part, but # now we prefer to keep things local. if !info.empty? i = token.attrIndex('class') tmpAttrs = token.attrs ? token.attrs.dup : [] if i < 0 tmpAttrs.push([ 'class', [:langPrefix] + langName ]) else tmpAttrs[i] = tmpAttrs[i].slice(0..-1) tmpAttrs[i][1] += ' ' + [:langPrefix] + langName end # Fake token just to render attributes tmpToken = Token.new(nil, nil, nil) tmpToken.attrs = tmpAttrs return '<pre><code' + renderer.renderAttrs(tmpToken) + '>' + highlighted + "</code></pre>\n" end return '<pre><code' + renderer.renderAttrs(token) + '>' + highlighted + "</code></pre>\n" end |
.hardbreak(tokens, idx, options) ⇒ Object
97 98 99 |
# File 'lib/motion-markdown-it/renderer.rb', line 97 def self.hardbreak(tokens, idx, ) return [:xhtmlOut] ? "<br />\n" : "<br>\n" end |
.html_block(tokens, idx) ⇒ Object
110 111 112 |
# File 'lib/motion-markdown-it/renderer.rb', line 110 def self.html_block(tokens, idx) return tokens[idx].content end |
.html_inline(tokens, idx) ⇒ Object
113 114 115 |
# File 'lib/motion-markdown-it/renderer.rb', line 113 def self.html_inline(tokens, idx) return tokens[idx].content end |
.image(tokens, idx, options, env, renderer) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/motion-markdown-it/renderer.rb', line 83 def self.image(tokens, idx, , 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, , env) return renderer.renderToken(tokens, idx, ); end |
.softbreak(tokens, idx, options) ⇒ Object
100 101 102 |
# File 'lib/motion-markdown-it/renderer.rb', line 100 def self.softbreak(tokens, idx, ) return [:breaks] ? ([:xhtmlOut] ? "<br />\n" : "<br>\n") : "\n" end |
.text(tokens, idx) ⇒ Object
105 106 107 |
# File 'lib/motion-markdown-it/renderer.rb', line 105 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.
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/motion-markdown-it/renderer.rb', line 307 def render(tokens, , env) result = '' rules = @rules 0.upto(tokens.length - 1) do |i| type = tokens[i].type if type == 'inline' result += renderInline(tokens[i].children, , env) elsif rules[type] != nil result += rules[tokens[i].type].call(tokens, i, , env, self) else result += renderToken(tokens, i, ) end end return result end |
#renderAttrs(token) ⇒ Object
Renderer.renderAttrs(token) -> String
Render token attributes to string.
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/motion-markdown-it/renderer.rb', line 169 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.
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/motion-markdown-it/renderer.rb', line 254 def renderInline(tokens, , 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, , env, self) else result += renderToken(tokens, i, ) 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.
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/motion-markdown-it/renderer.rb', line 282 def renderInlineAsText(tokens, , env) result = '' 0.upto(tokens.length - 1) do |i| if tokens[i].type == 'text' result += tokens[i].content elsif tokens[i].type == 'image' result += renderInlineAsText(tokens[i].children, , env) elsif tokens[i].type == 'softbreak' result += "\n" 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]].
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/motion-markdown-it/renderer.rb', line 189 def renderToken(tokens, idx, , 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 && [: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 |