/
pygmentize.py
41 lines (32 loc) · 1.38 KB
/
pygmentize.py
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
37
38
39
40
41
from pygments import highlight
from pygments.lexers.jvm import JavaLexer
from pygments.formatters.html import HtmlFormatter
import re
def pygmentize(code, linenos=False, tofile=False, outputfile=''):
style = 'colorful'
defstyles = 'overflow:auto;width:auto;'
formatter = HtmlFormatter(style=style,
linenos=False,
noclasses=True,
cssclass='',
cssstyles=defstyles + get_default_style(),
prestyles='margin: 0')
html = highlight(code, JavaLexer(), formatter)
if linenos:
html = insert_line_numbers(html)
html = "<!-- Rapport généré avec TPGen -->" + html
return html
def insert_line_numbers(html):
match = re.search('(<pre[^>]*>)(.*)(</pre>)', html, re.DOTALL)
if not match: return html
pre_open = match.group(1)
pre = match.group(2)
pre_close = match.group(3)
html = html.replace(pre_close, '</pre></td></tr></table>')
numbers = range(1, pre.count('\n') + 1)
format = '%' + str(len(str(numbers[-1]))) + 'i'
lines = '\n'.join(format % i for i in numbers)
html = html.replace(pre_open, '<table><tr><td>' + pre_open + lines + '</pre></td><td>' + pre_open)
return html
def get_default_style():
return 'border:solid gray;border-width:.1em .1em .1em .8em;padding:.2em .6em;'