CommonMark-py icon indicating copy to clipboard operation
CommonMark-py copied to clipboard

Add support for syntax highlighting

Open eigenein opened this issue 11 years ago • 0 comments

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))

eigenein avatar Oct 16 '14 20:10 eigenein