bash pip install Pygments python from pygments import highlight from pygments.lexers import get_lexer_by_name from pygments.formatters import HtmlFormatter def highlight_code(code, language): lexer = get_lexer_by_name(language) formatter = HtmlFormatter() highlighted_code = highlight(code, lexer, formatter) return highlighted_code code = ''' def hello_world(): print("Hello, World!") hello_world() ''' highlighted_code = highlight_code(code, 'python') print(highlighted_code) python from pygments import highlight from pygments.lexers import get_lexer_by_name from pygments.formatters import HtmlFormatter def highlight_code(code, language): lexer = get_lexer_by_name(language) formatter = HtmlFormatter(style='monokai', full=True) highlighted_code = highlight(code, lexer, formatter) return highlighted_code code = ''' def hello_world(): print("Hello, World!") hello_world() ''' highlighted_code = highlight_code(code, 'python') print(highlighted_code)


上一篇:
下一篇:
切换中文