from pygments formatters import HtmlFormatter BBCodeFormatter from pyg

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from pygments.formatters import HtmlFormatter, BBCodeFormatter
from pygments import lexers
from pygments import highlight
def generate_styles():
"""
Generate CSS styles for rendering the pygments HTML.
"""
return HtmlFormatter().get_style_defs('.highlight')
def lexer_title(code):
title = lexers.get_lexer_by_name(code).name
if title == 'Delphi':
title = 'Pascal/Delphi'
return title
def render(lexer_name, code, numbers=True, inline_styles=False, format='html'):
try:
lexer = lexers.get_lexer_by_name(lexer_name)
except lexers.ClassNotFound:
lexer_name = 'text'
lexer = lexers.get_lexer_by_name(lexer_name)
if 'php' == lexer_name:
lexer.startinline = True
linenos = numbers and 'table' or False
if format == 'html':
formatter = HtmlFormatter(linenos=linenos, noclasses=inline_styles)
else:
formatter = BBCodeFormatter()
output = highlight(code, lexer, formatter)
return output