-
Notifications
You must be signed in to change notification settings - Fork 1
/
outputter.py
85 lines (64 loc) · 2 KB
/
outputter.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
from classes import *
latexBegin = r'''
\newcommand{\includecodelang}[2]{\lstinputlisting[escapechar=, language=#2]{#1}}
\newcommand{\includecode}[1]{\lstinputlisting[escapechar=]{#1}}
'''
latexIncludeCode = "\\includecode{%s}"
latexIncludeCodeLang = "\\includecodelang{%s}{%s}"
latexFileHeading = "\\%s{%s\label{%s:%s}}"
latexFileHeadingNoLabel = "\\%s{%s}"
latexReplacements = {
'\t': '\\ ' * 4,
'&': '\\&',
'%': '\\%',
'$': '\\$',
'#': '\\#',
'_': '\\_',
'{': '\\{',
'}': '\\}',
'~': '\\textasciitilde ',
'^': '\\textasciicircum '
}
def escapeForLatex(text):
text = text.replace('\\', '\\textbackslash')
text = text.replace(' ', '\\ ')
text = text.replace('\\textbackslash', '\\textbackslash ')
for i, j in latexReplacements.items():
text = text.replace(i, j)
text = text.replace('"', '\char`\"{}')
return text
def output_start(out_file):
out_file.write(latexBegin)
def output(filename, rel_path, out_file):
out_file.write("%" * 80)
out_file.write("\n")
out_file.write("%% %s\n\n" % rel_path)
if settings.shouldAddLabel:
# apparently, no escape in labels
heading = latexFileHeading % (settings.headingStyle, escapeForLatex(rel_path), settings.labelPrefix, rel_path)
else:
heading = latexFileHeadingNoLabel % (settings.headingStyle, escapeForLatex(rel_path) )
out_file.write(heading)
out_file.write("\n")
language = None
for key in fileExtensionMap:
if filename.endswith(key):
language = fileExtensionMap[key]
break
if language is None:
include_line = latexIncludeCode % (filename)
else:
include_line = latexIncludeCodeLang % (filename, language)
out_file.write(include_line)
out_file.write("\n")
out_file.write("\n")
fileExtensionMap = {
'.erl' : 'erlang'
, '.hs' : 'Haskell'
, '.py' : 'Python'
, '.java' : 'Java'
, '.sh' : 'sh'
, '.bash' : 'bash'
, '.sml' : 'ML'
, '.sig' : 'ML'
}