MCPcopy Create free account
hub / github.com/Python-Markdown/markdown / HiliteTreeprocessor

Class HiliteTreeprocessor

markdown/extensions/codehilite.py:254–289  ·  view source on GitHub ↗

Highlight source code in code blocks.

Source from the content-addressed store, hash-verified

252
253
254class HiliteTreeprocessor(Treeprocessor):
255 """ Highlight source code in code blocks. """
256
257 config: dict[str, Any]
258
259 def code_unescape(self, text: str) -> str:
260 """Unescape code."""
261 text = text.replace("&lt;", "<")
262 text = text.replace("&gt;", ">")
263 # Escaped '&' should be replaced at the end to avoid
264 # conflicting with < and >.
265 text = text.replace("&amp;", "&")
266 return text
267
268 def run(self, root: etree.Element) -> None:
269 """ Find code blocks and store in `htmlStash`. """
270 blocks = root.iter('pre')
271 for block in blocks:
272 if len(block) == 1 and block[0].tag == 'code':
273 local_config = self.config.copy()
274 text = block[0].text
275 if text is None:
276 continue
277 code = CodeHilite(
278 self.code_unescape(text),
279 tab_length=self.md.tab_length,
280 style=local_config.pop('pygments_style', 'default'),
281 **local_config
282 )
283 placeholder = self.md.htmlStash.store(code.hilite())
284 # Clear code block in `etree` instance
285 block.clear()
286 # Change to `p` element which will later
287 # be removed when inserting raw html
288 block.tag = 'p'
289 block.text = placeholder
290
291
292class CodeHiliteExtension(Extension):

Callers 1

extendMarkdownMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected