Default token renderer. Can be overridden by custom function :param idx: token index to render :param options: params of parser instance
(
self,
tokens: Sequence[Token],
idx: int,
options: OptionsDict,
env: EnvType,
)
| 107 | return result |
| 108 | |
| 109 | def renderToken( |
| 110 | self, |
| 111 | tokens: Sequence[Token], |
| 112 | idx: int, |
| 113 | options: OptionsDict, |
| 114 | env: EnvType, |
| 115 | ) -> str: |
| 116 | """Default token renderer. |
| 117 | |
| 118 | Can be overridden by custom function |
| 119 | |
| 120 | :param idx: token index to render |
| 121 | :param options: params of parser instance |
| 122 | """ |
| 123 | result = "" |
| 124 | needLf = False |
| 125 | token = tokens[idx] |
| 126 | |
| 127 | # Tight list paragraphs |
| 128 | if token.hidden: |
| 129 | return "" |
| 130 | |
| 131 | # Insert a newline between hidden paragraph and subsequent opening |
| 132 | # block-level tag. |
| 133 | # |
| 134 | # For example, here we should insert a newline before blockquote: |
| 135 | # - a |
| 136 | # > |
| 137 | # |
| 138 | if token.block and token.nesting != -1 and idx and tokens[idx - 1].hidden: |
| 139 | result += "\n" |
| 140 | |
| 141 | # Add token name, e.g. `<img` |
| 142 | result += ("</" if token.nesting == -1 else "<") + token.tag |
| 143 | |
| 144 | # Encode attributes, e.g. `<img src="foo"` |
| 145 | result += self.renderAttrs(token) |
| 146 | |
| 147 | # Add a slash for self-closing tags, e.g. `<img src="foo" /` |
| 148 | if token.nesting == 0 and options["xhtmlOut"]: |
| 149 | result += " /" |
| 150 | |
| 151 | # Check if we need to add a newline after this tag |
| 152 | if token.block: |
| 153 | needLf = True |
| 154 | |
| 155 | if token.nesting == 1 and (idx + 1 < len(tokens)): |
| 156 | nextToken = tokens[idx + 1] |
| 157 | |
| 158 | if nextToken.type == "inline" or nextToken.hidden: |
| 159 | # Block-level tag containing an inline tag. |
| 160 | # |
| 161 | needLf = False |
| 162 | |
| 163 | elif nextToken.nesting == -1 and nextToken.tag == token.tag: |
| 164 | # Opening tag + closing tag of the same type. E.g. `<li></li>`. |
| 165 | # |
| 166 | needLf = False |
no test coverage detected