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
| 26 | |
| 27 | |
| 28 | class RendererHTML(RendererProtocol): |
| 29 | """Contains render rules for tokens. Can be updated and extended. |
| 30 | |
| 31 | Example: |
| 32 | |
| 33 | Each rule is called as independent static function with fixed signature: |
| 34 | |
| 35 | :: |
| 36 | |
| 37 | class Renderer: |
| 38 | def token_type_name(self, tokens, idx, options, env) { |
| 39 | # ... |
| 40 | return renderedHTML |
| 41 | |
| 42 | :: |
| 43 | |
| 44 | class CustomRenderer(RendererHTML): |
| 45 | def strong_open(self, tokens, idx, options, env): |
| 46 | return '<b>' |
| 47 | def strong_close(self, tokens, idx, options, env): |
| 48 | return '</b>' |
| 49 | |
| 50 | md = MarkdownIt(renderer_cls=CustomRenderer) |
| 51 | |
| 52 | result = md.render(...) |
| 53 | |
| 54 | See https://github.com/markdown-it/markdown-it/blob/master/lib/renderer.js |
| 55 | for more details and examples. |
| 56 | """ |
| 57 | |
| 58 | __output__ = "html" |
| 59 | |
| 60 | def __init__(self, parser: Any = None): |
| 61 | self.rules = { |
| 62 | k: v |
| 63 | for k, v in inspect.getmembers(self, predicate=inspect.ismethod) |
| 64 | if not (k.startswith("render") or k.startswith("_")) |
| 65 | } |
| 66 | |
| 67 | def render( |
| 68 | self, tokens: Sequence[Token], options: OptionsDict, env: EnvType |
| 69 | ) -> str: |
| 70 | """Takes token stream and generates HTML. |
| 71 | |
| 72 | :param tokens: list on block tokens to render |
| 73 | :param options: params of parser instance |
| 74 | :param env: additional data from parsed input |
| 75 | |
| 76 | """ |
| 77 | result = "" |
| 78 | |
| 79 | for i, token in enumerate(tokens): |
| 80 | if token.type == "inline": |
| 81 | if token.children: |
| 82 | result += self.renderInline(token.children, options, env) |
| 83 | elif token.type in self.rules: |
| 84 | result += self.rules[token.type](tokens, i, options, env) |
| 85 | else: |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…