CommonMark-py
CommonMark-py copied to clipboard
Add support for syntax highlighting
In my opinion it is useful to be able to integrate syntax highlighting for code blocks. This commit will allow to use CommonMark.HTMLRenderer in a way like:
from pygments import highlight
from pygments.formatters import HtmlFormatter
from pygments.lexers import get_lexer_by_name, guess_lexer
def highlight_syntax(block_type, code, info_words):
if info_words:
lexer = get_lexer_by_name(info_words[0])
else:
lexer = guess_lexer(code)
return highlight(code, lexer, HtmlFormatter(nowrap=True))
print(CommonMark.HTMLRenderer(highlight_syntax).render(ast))