Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/markdown_it/renderer.py: 96%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

123 statements  

1""" 

2class Renderer 

3 

4Generates HTML from parsed token stream. Each instance has independent 

5copy of rules. Those can be rewritten with ease. Also, you can add new 

6rules if you create plugin and adds new token types. 

7""" 

8 

9from __future__ import annotations 

10 

11from collections.abc import Sequence 

12import inspect 

13from typing import Any, ClassVar, Protocol 

14 

15from .common.utils import escapeHtml, unescapeAll 

16from .token import Token 

17from .utils import EnvType, OptionsDict 

18 

19 

20class RendererProtocol(Protocol): 

21 __output__: ClassVar[str] 

22 

23 def render( 

24 self, tokens: Sequence[Token], options: OptionsDict, env: EnvType 

25 ) -> Any: ... 

26 

27 

28class 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", "_")) 

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: 

86 result += self.renderToken(tokens, i, options, env) 

87 

88 return result 

89 

90 def renderInline( 

91 self, tokens: Sequence[Token], options: OptionsDict, env: EnvType 

92 ) -> str: 

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

94 

95 :param tokens: list on block tokens to render 

96 :param options: params of parser instance 

97 :param env: additional data from parsed input (references, for example) 

98 """ 

99 result = "" 

100 

101 for i, token in enumerate(tokens): 

102 if token.type in self.rules: 

103 result += self.rules[token.type](tokens, i, options, env) 

104 else: 

105 result += self.renderToken(tokens, i, options, env) 

106 

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 

167 

168 result += ">\n" if needLf else ">" 

169 

170 return result 

171 

172 @staticmethod 

173 def renderAttrs(token: Token) -> str: 

174 """Render token attributes to string.""" 

175 result = "" 

176 

177 for key, value in token.attrItems(): 

178 result += " " + escapeHtml(key) + '="' + escapeHtml(str(value)) + '"' 

179 

180 return result 

181 

182 def renderInlineAsText( 

183 self, 

184 tokens: Sequence[Token] | None, 

185 options: OptionsDict, 

186 env: EnvType, 

187 ) -> str: 

188 """Special kludge for image `alt` attributes to conform CommonMark spec. 

189 

190 Don't try to use it! Spec requires to show `alt` content with stripped markup, 

191 instead of simple escaping. 

192 

193 :param tokens: list on block tokens to render 

194 :param options: params of parser instance 

195 :param env: additional data from parsed input 

196 """ 

197 result = "" 

198 

199 for token in tokens or []: 

200 if token.type == "text": 

201 result += token.content 

202 elif token.type == "image": 

203 if token.children: 

204 result += self.renderInlineAsText(token.children, options, env) 

205 elif token.type in ("html_inline", "html_block"): 

206 result += token.content 

207 elif token.type in ("softbreak", "hardbreak"): 

208 result += "\n" 

209 

210 return result 

211 

212 ################################################### 

213 

214 def list_item_open( 

215 self, 

216 tokens: Sequence[Token], 

217 idx: int, 

218 options: OptionsDict, 

219 env: EnvType, 

220 ) -> str: 

221 token = tokens[idx] 

222 result = self.renderToken(tokens, idx, options, env) 

223 if token.meta and "checked" in token.meta: 

224 checked_attr = ' checked=""' if token.meta["checked"] else "" 

225 disabled_attr = ( 

226 "" if options.get("tasklists_editable", False) else ' disabled=""' 

227 ) 

228 result += ( 

229 '<input class="task-list-item-checkbox"' 

230 f'{disabled_attr} type="checkbox"{checked_attr}> ' 

231 ) 

232 return result 

233 

234 def code_inline( 

235 self, tokens: Sequence[Token], idx: int, options: OptionsDict, env: EnvType 

236 ) -> str: 

237 token = tokens[idx] 

238 return ( 

239 "<code" 

240 + self.renderAttrs(token) 

241 + ">" 

242 + escapeHtml(tokens[idx].content) 

243 + "</code>" 

244 ) 

245 

246 def code_block( 

247 self, 

248 tokens: Sequence[Token], 

249 idx: int, 

250 options: OptionsDict, 

251 env: EnvType, 

252 ) -> str: 

253 token = tokens[idx] 

254 

255 return ( 

256 "<pre" 

257 + self.renderAttrs(token) 

258 + "><code>" 

259 + escapeHtml(tokens[idx].content) 

260 + "</code></pre>\n" 

261 ) 

262 

263 def fence( 

264 self, 

265 tokens: Sequence[Token], 

266 idx: int, 

267 options: OptionsDict, 

268 env: EnvType, 

269 ) -> str: 

270 token = tokens[idx] 

271 info = unescapeAll(token.info).strip() if token.info else "" 

272 langName = "" 

273 langAttrs = "" 

274 

275 if info: 

276 arr = info.split(maxsplit=1) 

277 langName = arr[0] 

278 if len(arr) == 2: 

279 langAttrs = arr[1] 

280 

281 if options.highlight: 

282 highlighted = options.highlight( 

283 token.content, langName, langAttrs 

284 ) or escapeHtml(token.content) 

285 else: 

286 highlighted = escapeHtml(token.content) 

287 

288 if highlighted.startswith("<pre"): 

289 return highlighted + "\n" 

290 

291 # If language exists, inject class gently, without modifying original token. 

292 # May be, one day we will add .deepClone() for token and simplify this part, but 

293 # now we prefer to keep things local. 

294 if info: 

295 # Fake token just to render attributes 

296 tmpToken = Token(type="", tag="", nesting=0, attrs=token.attrs.copy()) 

297 tmpToken.attrJoin("class", options.langPrefix + langName) 

298 

299 return ( 

300 "<pre><code" 

301 + self.renderAttrs(tmpToken) 

302 + ">" 

303 + highlighted 

304 + "</code></pre>\n" 

305 ) 

306 

307 return ( 

308 "<pre><code" 

309 + self.renderAttrs(token) 

310 + ">" 

311 + highlighted 

312 + "</code></pre>\n" 

313 ) 

314 

315 def image( 

316 self, 

317 tokens: Sequence[Token], 

318 idx: int, 

319 options: OptionsDict, 

320 env: EnvType, 

321 ) -> str: 

322 token = tokens[idx] 

323 

324 # "alt" attr MUST be set, even if empty. Because it's mandatory and 

325 # should be placed on proper position for tests. 

326 if token.children: 

327 token.attrSet("alt", self.renderInlineAsText(token.children, options, env)) 

328 else: 

329 token.attrSet("alt", "") 

330 

331 return self.renderToken(tokens, idx, options, env) 

332 

333 def hardbreak( 

334 self, tokens: Sequence[Token], idx: int, options: OptionsDict, env: EnvType 

335 ) -> str: 

336 return "<br />\n" if options.xhtmlOut else "<br>\n" 

337 

338 def softbreak( 

339 self, tokens: Sequence[Token], idx: int, options: OptionsDict, env: EnvType 

340 ) -> str: 

341 return ( 

342 ("<br />\n" if options.xhtmlOut else "<br>\n") if options.breaks else "\n" 

343 ) 

344 

345 def text( 

346 self, tokens: Sequence[Token], idx: int, options: OptionsDict, env: EnvType 

347 ) -> str: 

348 return escapeHtml(tokens[idx].content) 

349 

350 def html_block( 

351 self, tokens: Sequence[Token], idx: int, options: OptionsDict, env: EnvType 

352 ) -> str: 

353 return tokens[idx].content 

354 

355 def html_inline( 

356 self, tokens: Sequence[Token], idx: int, options: OptionsDict, env: EnvType 

357 ) -> str: 

358 return tokens[idx].content