Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does mistune support code/syntax highlighting? [question] #54

Closed
avinassh opened this issue Jun 4, 2015 · 2 comments
Closed

Does mistune support code/syntax highlighting? [question] #54

avinassh opened this issue Jun 4, 2015 · 2 comments

Comments

@avinassh
Copy link

avinassh commented Jun 4, 2015

^question. If so, how can I use Pygments or some other library? Thanks!

@avinassh
Copy link
Author

avinassh commented Jun 4, 2015

aha, I should have read the README, oops.

@avinassh avinassh closed this as completed Jun 4, 2015
@xzava
Copy link

xzava commented Nov 22, 2019

This is the top link in google search, so ill add this for anyone else looking for it.

        import mistune
	from pygments import highlight
	from pygments.lexers import get_lexer_by_name
	from pygments.formatters import html

	class HighlightRenderer(mistune.Renderer):
	    def block_code(self, code, lang):
	        if not lang:
	            return '\n<pre><code>%s</code></pre>\n' % \
	                mistune.escape(code)
	        lexer = get_lexer_by_name(lang, stripall=True)
	        formatter = html.HtmlFormatter(lineseparator="<br>")
	        return highlight(code, lexer, formatter)

	renderer = HighlightRenderer()
	markdown = mistune.Markdown(renderer=renderer)
	output_html = markdown(content_document)
	print(output_html)

https://mistune.readthedocs.io/en/latest/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants