-
Notifications
You must be signed in to change notification settings - Fork 141
/
tr2tex
executable file
·328 lines (294 loc) · 7.8 KB
/
tr2tex
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env python3
import os
import re
import sys
def end(l):
# look for " .", etc.
p = re.compile(r'\s+([\.,:;])')
l = p.sub(r'\1', l)
# look for ) (
p = re.compile(r'\s+\)([\.,]*) \(\n')
m = p.search(l)
if m != None:
l = p.sub(r'', l)
if len(m.groups()) > 0:
l = "(" + l + ")%s\n" % m.groups()[0]
else:
l = "(" + l + ")\n"
# look for '' ``
p = re.compile(r"\s+''([\.,]*) ``\n")
m = p.search(l)
if m != None:
l = p.sub(r'', l)
if len(m.groups()) > 0:
l = "``" + l + "''%s\n" % m.groups()[0]
else:
l = "``" + l + "''\n"
# check for " )"
p = re.compile(r'\s+([\)])')
l = p.sub(r'\1', l)
return l
def ig2dotdot():
line = f.readline()
while line:
if line.startswith('..'):
return
print("%", line, end="")
line = f.readline()
def chapter(l):
p = re.compile(r'.chapter ([:\w]+) "(.+)"')
l = p.sub(r'\\chapter{\2}\n\\label{\1}', l)
print(l, end="")
def chapter1(l):
p = re.compile(r'.chapterlike "(.+)"')
l = p.sub(r'\\chapter*{\1}\n', l)
print(l, end="")
def comment(l):
p = re.compile(r'\.\\"(.*)')
l = p.sub(r'%% \1', l)
print(l, end="")
def index(l):
p = re.compile(r'.index ([-\w]+)')
l = p.sub(r'\\index{\1}', l)
p = re.compile(r'.index "(.*)"')
l = p.sub(r'\\index{\1}', l)
l = end(l)
print(l, end="")
def defindex(l):
p = re.compile(r'.italic-index ([-\w]+)')
l = p.sub(r'\\textit{\1}\\index{\1}', l)
p = re.compile(r'.italic-index "(.*)"')
l = p.sub(r'\\textit{\1}\\index{\1}', l)
l = end(l)
print(l, end="")
codep = r'([\/\.\->_\w\&<\(\)\#\[\]\|=\*%\!]+)'
def code(l):
p = re.compile(r'.code "(.*)"')
l = p.sub(r'\\lstinline{\1}', l)
p = re.compile(r'.code ' + codep)
l = p.sub(r'\\lstinline{\1}', l)
l = end(l)
print(l, end="")
def codeindex(l):
p = re.compile(r'.code-index ' + codep)
l = p.sub(r'\\lstinline{\1}\\index{\1@\\lstinline{\1}}', l)
p = re.compile(r'.code-index "(.*)"')
l = p.sub(r'\\lstinline{\1}\\index{\1@\\lstinline{\1}}', l)
l = end(l)
print(l, end="")
def section(l):
p = re.compile(r'.section "(.+)"')
l = p.sub(r'\\section{\1}', l)
print(l, end="")
def figref(l):
p = re.compile(r'.figref ([:\w]+)')
m = p.search(l)
if p != None:
n = m.groups()[0].split(':')
if len(n) > 1:
n = n[1]
else:
n = n[0]
l = p.sub(r'Figure~\\ref{fig:%s}' % n, l)
l = end(l)
print(l, end="")
def caption(fname):
with open(fname) as f:
line = f.readline()
while line:
if not line.startswith("."):
return line.rstrip('\n')
line = f.readline()
def istable(fname):
with open(fname) as f:
line = f.readline()
while line:
if line.startswith(".TS"):
return True
line = f.readline()
return False
def table(fname):
try:
with open(fname) as f:
print(r'\begin{tabular}{ll}')
line = f.readline() # F1
line = f.readline() # .TS
line = f.readline() # center
line = f.readline() # lb lb
line = f.readline() # l l.
line = f.readline() # heading
p = re.compile(r'([\w\ ]+)\t+([\w]+)\n')
line = p.sub(r'{\\bf \1} & {\\bf \2} \\\\', line)
print(line)
print("\midrule")
# table
line = f.readline()
while line:
if line.startswith(".TE"):
break
p = re.compile(r'([\(\)\w]+)\t+(.+)\n')
line = p.sub(r'\1 & \2 \\\\', line)
print(line)
line = f.readline()
line = f.readline() # F2
line = f.readline() # caption
print("\end{tabular}")
print("\caption{" + line.strip("\n") + "}")
except IOError:
print("error: couldn't open %s" % fname, file=sys.stderr)
def insert(l):
l = l.rstrip('\n')
fname = l.split(' ')[1]
try:
with open(fname) as f:
line = f.readline()
while line:
print(line, end="")
line = f.readline()
except IOError:
print("error: couldn't open %s" % fname, file=sys.stderr)
def listing(l):
print(r'\begin{lstlisting}[]')
line = f.readline()
while line:
if line.startswith(".so"):
insert(line)
elif line.startswith(".P2"):
break
else:
print(line, end="")
line = f.readline()
print("\end{lstlisting}");
def figure(l):
p = re.compile(r'.figure (.+)\n')
l = p.sub(r'\1', l)
name = "fig/%s.t" % l
print("")
print(r'\begin{figure}[t]')
print("\center")
if istable(name):
table(name)
else:
print("\includegraphics[scale=0.5]{fig/%s.eps}" % l)
print("\caption{%s}" % caption(name))
print("\label{fig:%s}" % l)
print("\end{figure}")
def italic(l):
p = re.compile(r'.italic ([\w]+)')
l = p.sub(r'\\textit{\1}', l)
p = re.compile(r'.italic "(.*)"')
l = p.sub(r'\\textit{\1}', l)
l = end(l)
print(l, end="")
def address(l):
p = re.compile(r'.address (\w+)')
l = p.sub(r'\\texttt{\1}', l)
l = end(l)
print(l, end="")
def register(l):
p = re.compile(r'.register (\w+)')
l = p.sub(r'\\texttt{\%\1}', l)
l = end(l)
print(l, end="")
def lineref(l):
l = l.replace('!', '\\')
p = re.compile(r'.line (.*):\/(.*)\/([+-]?\d+)?')
l = p.sub(r'\\lineref{\1:/\2/\3}', l)
p = re.compile(r'.line (.*):(\d+)')
l = p.sub(r'\\lineref{\1:\2}', l)
l = end(l)
print(l, end="")
def linerefs(l):
l = l.replace('!', '\\')
p = re.compile(r'.lines (.*):\/(.*)\/([+-]?\d+)?,\/(.*)\/([+-]?\d+)?')
l = p.sub(r'\\linerefs{\1:/\2/\3,/\4/\5}', l)
l = end(l)
print(l, end="")
def indent(l):
p = re.compile(r'.IP (.*)\n')
l = p.sub(r'\\paragraph{\\textbullet}', l)
l = end(l)
print(l, end="")
def file(l):
p = re.compile(r'.file ([-\w\/\.]+)')
l = p.sub(r'\\fileref{\1}', l)
p = re.compile(r'.file "(.*)"')
l = p.sub(r'\\fileref{\1}', l)
l = end(l)
print(l, end="")
def doref(l):
# replace references in a line
p = re.compile(r' \\\*\[([\w:]*)\]')
l = p.sub(r'~\\ref{\1}', l)
return l
def doquote(l):
# replace references in a line
p = re.compile(r'"([\w]+)"')
l = p.sub(r"``\1''", l)
return l
def docarrot(l):
p = re.compile(r'(\d+)\^(\d+)(-\d+)*')
l = p.sub(r'$\1^{\2}\3$', l)
return l
def dourl(l):
p = re.compile(r'https://([\w\.\/]+)')
l = p.sub(r'\\url{https://\1}', l)
return l
def tr2tex(l):
if l.startswith(". "):
l = "." + l[2:]
if l.startswith(".ig"):
ig2dotdot()
elif l.startswith(".chapter "):
chapter(l)
elif l.startswith(".chapterlike"):
chapter1(l)
elif l.startswith(".PP"):
print("")
elif l.startswith(".code "):
code(l)
elif l.startswith(".italic-index"):
defindex(l)
elif l.startswith(".index"):
index(l)
elif l.startswith(".code-index"):
codeindex(l)
elif l.startswith(".section"):
section(l)
elif l.startswith(r'.\"'):
comment(l)
elif l.startswith(r'.figref'):
figref(l)
elif l.startswith(r'.figure '):
figure(l)
elif l.startswith(r'.italic'):
italic(l)
elif l.startswith(r'.address '):
address(l)
elif l.startswith(r'.register '):
register(l)
elif l.startswith(r'.line '):
lineref(l)
elif l.startswith(r'.lines '):
linerefs(l)
elif l.startswith(r'.P1'):
listing(l)
elif l.startswith(r'.IP'):
indent(l)
elif l.startswith(r'.file'):
file(l)
elif l.startswith(r'.sheet'):
return
elif l.startswith(r"."):
print("TODO", l)
else:
l = docarrot(l)
l = doref(l)
l = doquote(l)
l = dourl(l)
print(l, end="")
with open(sys.argv[1]) as f:
line = f.readline()
while line:
tr2tex(line)
line = f.readline()