-
Notifications
You must be signed in to change notification settings - Fork 0
/
prettyprint.py
executable file
·117 lines (100 loc) · 3.24 KB
/
prettyprint.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/env python
import sys
from pygments import highlight
from pygments.lexers import get_lexer_for_filename
from pygments.formatters import HtmlFormatter, LatexFormatter
from opster import command
css_styles = """
h1 {
font-family: "Source Code Pro", monospace;
font-size: 18pt;
background: #bbb;
padding: .5em .3em;
border: 1px solid black;
}
.source pre {
font-family: "Source Code Pro", monospace;
font-size: 10pt;
}
"""
latex_preamble = """
\\documentclass[10pt]{article}
\\usepackage{fancyvrb}
\\usepackage{color}
\\usepackage{%s}
\\usepackage{vmargin}
\\setpapersize{A4}
\\setmarginsrb{1.2cm}{1.2cm}{1.2cm}{1.2cm}{0cm}{0cm}{.75cm}{0cm}
\\begin{document}
"""
options = [
('s', 'style', 'default', 'Pygments style used'),
('o', 'output', None, 'Output file'),
('w', 'width', 100, 'Text width'),
('l', 'latex', False, 'Produce LaTeX output'),
('f', 'fontpkg', 'bera', 'LaTeX font package'),
('p', 'pagebreak', False, 'Insert pagebreaks before each file'),
]
def html_output(filenames, outfile, width, style):
if not outfile:
outfile = "output.html"
lines_wrapped = 0
formatter = HtmlFormatter(linenos=False, cssclass="source", style=style)
output = open(outfile, "w")
output.write('<html><head><style type="text/css">')
output.write(css_styles)
output.write(formatter.get_style_defs())
output.write('\n</style></head><body>')
W = width
for filename in filenames:
output.write('<h1>' + filename + '</h1>\n')
lexer = get_lexer_for_filename(filename)
F = open(filename, "r")
code = ""
for line in F:
while len(line) > W:
lines_wrapped += 1
code += line[:W] + '\n'
line = line[W:]
code += line[:W]
result = highlight(code, lexer, formatter, output)
F.close()
output.write('</body></html>')
if lines_wrapped > 0:
print "(wrapped " + str(lines_wrapped) + " lines)"
output.close()
def latex_output(filenames, outfile, style, fontpkg, pagebreak):
if not outfile:
outfile = "output.tex"
lines_wrapped = 0
formatter = LatexFormatter(linenos=False, style=style)
output = open(outfile, "w")
output.write(latex_preamble % fontpkg)
output.write(formatter.get_style_defs())
for filename in filenames:
output.write('\\hrule\n')
output.write('\\section*{' + filename + '}\n')
lexer = get_lexer_for_filename(filename)
F = open(filename, "r")
result = highlight(F.read(), lexer, formatter, output)
F.close()
if pagebreak:
output.write("\\pagebreak")
output.write('\end{document}')
if lines_wrapped > 0:
print "(wrapped " + str(lines_wrapped) + " lines)"
output.close()
@command(options, usage="%name [options] file...")
def main(*filenames, **opts):
"Pretty print source code using Pygments to an HTML file (output.html)"
if len(filenames) == 0:
print "No input files"
sys.exit(0)
if opts['latex']:
latex_output(filenames, opts['output'], opts['style'],
opts['fontpkg'], opts['pagebreak'])
else:
html_output(filenames, opts['output'], opts['width'],
opts['style'])
if __name__ == '__main__':
main.command()