-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_profile_latex.py
executable file
·89 lines (76 loc) · 2.17 KB
/
format_profile_latex.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
#!/usr/bin/python
import sys
import re
import codecs
RE = {
'indentation': '[ ]{10}',
'comment': '^#',
'dashes': '--+',
'pipes': '^([ |]+)',
'percentage': '(\D+)(\d+\.\d+)%',
'symbol': '^([^%]+%)* ([^%]+)$'
}
VSPACE = r"(*@\vspace{-0.15cm}@*)"
RE['pipes_only'] = RE['pipes'] + '$'
for k, v in RE.iteritems():
RE[k] = re.compile(v)
def format_to_latex(filename):
f = codecs.open(filename, 'r', 'utf8')
print r"""
\lstset{
basicstyle=\linespread{0}\rmfamily\tiny,
escapeinside={(*@}{@*)},
columns=fixed,
basewidth=0.5em,
mathescape,
breaklines=false,breakatwhitespace=false,
moredelim=[is][\underbar]{<<@<<}{>>@>>}
}
\begin{lstlisting}
"""
def p(line):
print line.rstrip().encode('utf8')
print VSPACE
for line in f:
line = RE['dashes'].sub('', line)
line = RE['indentation'].sub(' ', line)
if RE['comment'].match(line):
continue
if RE['pipes_only'].match(line):
p(line)
p(line)
continue
pipe_part = None
m = RE['pipes'].match(line)
if m: pipe_part = m.group(1)
else: pipe_part = None
percentage = ""
m = RE['percentage'].search(line)
if m:
percentage_value = m.group(2)
if len(percentage_value) < 5:
phantom = '00'
else:
phantom = '0'
percentage = '$\\underline{{\\phantom{{{}}}{}\\%}}$'.format(phantom,percentage_value)
symbol = "\n"
m = RE['symbol'].match(line)
if m: symbol = m.group(2).strip()
#$ \underline{\phantom{ab}46.21\% dvmDecodeIndirectRef(Thread*, \_jobject*)} $
pipe_part_f = pipe_part
if pipe_part_f == None:
pipe_part_f = ''
if len(pipe_part_f.strip()) == 0:
pipe_part_f = pipe_part_f[:-1] + '|'
print "{}{} {}".format(pipe_part_f,percentage, symbol)
if pipe_part == None or len(pipe_part.strip()) == 0:
print
continue
print VSPACE
print """
\end{lstlisting}
"""
f.close()
if __name__ == '__main__':
filename = sys.argv[1]
format_to_latex(filename)