markdown_it.renderer module

class Renderer

Generates HTML from parsed token stream. Each instance has independent copy of rules. Those can be rewritten with ease. Also, you can add new rules if you create plugin and adds new token types.

class markdown_it.renderer.RendererHTML(parser=None)[源代码]

基类:markdown_it.renderer.RendererProtocol

Contains render rules for tokens. Can be updated and extended.

Example:

Each rule is called as independent static function with fixed signature:

class Renderer:
    def token_type_name(self, tokens, idx, options, env) {
        # ...
        return renderedHTML
class CustomRenderer(RendererHTML):
    def strong_open(self, tokens, idx, options, env):
        return '<b>'
    def strong_close(self, tokens, idx, options, env):
        return '</b>'

md = MarkdownIt(renderer_cls=CustomRenderer)

result = md.render(...)

See https://github.com/markdown-it/markdown-it/blob/master/lib/renderer.js for more details and examples.

code_block(tokens: Sequence[markdown_it.token.Token], idx: int, options: markdown_it.utils.OptionsDict, env: MutableMapping) str[源代码]
code_inline(tokens: Sequence[markdown_it.token.Token], idx: int, options, env) str[源代码]
fence(tokens: Sequence[markdown_it.token.Token], idx: int, options: markdown_it.utils.OptionsDict, env: MutableMapping) str[源代码]
hardbreak(tokens: Sequence[markdown_it.token.Token], idx: int, options: markdown_it.utils.OptionsDict, *args) str[源代码]
html_block(tokens: Sequence[markdown_it.token.Token], idx: int, *args) str[源代码]
html_inline(tokens: Sequence[markdown_it.token.Token], idx: int, *args) str[源代码]
image(tokens: Sequence[markdown_it.token.Token], idx: int, options: markdown_it.utils.OptionsDict, env: MutableMapping) str[源代码]
render(tokens: Sequence[markdown_it.token.Token], options: markdown_it.utils.OptionsDict, env: MutableMapping) str[源代码]

Takes token stream and generates HTML.

参数
  • tokens – list on block tokens to render

  • options – params of parser instance

  • env – additional data from parsed input

static renderAttrs(token: markdown_it.token.Token) str[源代码]

Render token attributes to string.

renderInline(tokens: Sequence[markdown_it.token.Token], options: markdown_it.utils.OptionsDict, env: MutableMapping) str[源代码]

The same as render, but for single token of inline type.

参数
  • tokens – list on block tokens to render

  • options – params of parser instance

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

renderInlineAsText(tokens: Optional[Sequence[markdown_it.token.Token]], options: markdown_it.utils.OptionsDict, env: MutableMapping) str[源代码]

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.

参数
  • tokens – list on block tokens to render

  • options – params of parser instance

  • env – additional data from parsed input

renderToken(tokens: Sequence[markdown_it.token.Token], idx: int, options: markdown_it.utils.OptionsDict, env: MutableMapping) str[源代码]

Default token renderer.

Can be overridden by custom function

参数
  • idx – token index to render

  • options – params of parser instance

softbreak(tokens: Sequence[markdown_it.token.Token], idx: int, options: markdown_it.utils.OptionsDict, *args) str[源代码]
text(tokens: Sequence[markdown_it.token.Token], idx: int, *args) str[源代码]
class markdown_it.renderer.RendererProtocol(*args, **kwargs)[源代码]

基类:Protocol

render(tokens: Sequence[markdown_it.token.Token], options: markdown_it.utils.OptionsDict, env: MutableMapping) Any[源代码]