"Fossies" - the Fresh Open Source Software Archive 
Member "reportlab-3.5.32/src/reportlab/lib/pygments2xpre.py" (1 Oct 2019, 2727 Bytes) of package /linux/privat/reportlab-3.5.32.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style:
standard) with prefixed line numbers.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "pygments2xpre.py" see the
Fossies "Dox" file reference documentation.
1 """Helps you output colourised code snippets in ReportLab documents.
2
3 Platypus has an 'XPreformatted' flowable for handling preformatted
4 text, with variations in fonts and colors. If Pygments is installed,
5 calling 'pygments2xpre' will return content suitable for display in
6 an XPreformatted object. If it's not installed, you won't get colours.
7
8 For a list of available lexers see http://pygments.org/docs/
9
10 """
11 __all__ = ('pygments2xpre',)
12 from reportlab.lib.utils import isPy3, asBytes, getBytesIO, getStringIO, asUnicode, isUnicode
13 import re
14
15 def _2xpre(s,styles):
16 "Helper to transform Pygments HTML output to ReportLab markup"
17 s = s.replace('<div class="highlight">','')
18 s = s.replace('</div>','')
19 s = s.replace('<pre>','')
20 s = s.replace('</pre>','')
21 for k,c in styles+[('p','#000000'),('n','#000000'),('err','#000000')]:
22 s = s.replace('<span class="%s">' % k,'<span color="%s">' % c)
23 s = re.sub(r'<span class="%s\s+.*">'% k,'<span color="%s">' % c,s)
24 s = re.sub(r'<span class=".*">','<span color="#0f0f0f">',s)
25 return s
26
27 def pygments2xpre(s, language="python"):
28 "Return markup suitable for XPreformatted"
29 try:
30 from pygments import highlight
31 from pygments.formatters import HtmlFormatter
32 except ImportError:
33 return s
34
35 from pygments.lexers import get_lexer_by_name
36 rconv = lambda x: x
37 if isPy3:
38 out = getStringIO()
39 else:
40 if isUnicode(s):
41 s = asBytes(s)
42 rconv = asUnicode
43 out = getBytesIO()
44
45 l = get_lexer_by_name(language)
46
47 h = HtmlFormatter()
48 highlight(s,l,h,out)
49 styles = [(cls, style.split(';')[0].split(':')[1].strip())
50 for cls, (style, ttype, level) in h.class2style.items()
51 if cls and style and style.startswith('color:')]
52 return rconv(_2xpre(out.getvalue(),styles))
53
54 def convertSourceFiles(filenames):
55 "Helper function - makes minimal PDF document"
56
57 from reportlab.platypus import Paragraph, SimpleDocTemplate, Spacer, XPreformatted
58 from reportlab.lib.styles import getSampleStyleSheet
59 styT=getSampleStyleSheet()["Title"]
60 styC=getSampleStyleSheet()["Code"]
61 doc = SimpleDocTemplate("pygments2xpre.pdf")
62 S = [].append
63 for filename in filenames:
64 S(Paragraph(filename,style=styT))
65 src = open(filename, 'r').read()
66 fmt = pygments2xpre(src)
67 S(XPreformatted(fmt, style=styC))
68 doc.build(S.__self__)
69 print('saved pygments2xpre.pdf')
70
71 if __name__=='__main__':
72 import sys
73 filenames = sys.argv[1:]
74 if not filenames:
75 print('usage: pygments2xpre.py file1.py [file2.py] [...]')
76 sys.exit(0)
77 convertSourceFiles(filenames)