rolandshoemaker / CommonMark-py

Depreciated in favor of rtfd/CommonMark-py
Other
125 stars 12 forks source link

Add support for syntax highlighting #9

Closed eigenein closed 1 year ago

eigenein commented 10 years ago

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