-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrs.py
executable file
·617 lines (544 loc) · 12.1 KB
/
rs.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
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
#!/usr/bin/python
import sys
import re
ifile = ""
mainfile = ""
oneWordSubs = {"<br>":"\\\\"}
subs = { "part":["=","=","\\section{","}"]
,"section":["==","==","\\section{","}"]
,"subsection":["===","===","\\subsection{","}"]
,"subsubsection":["====","====","\\subsubsection{","}"]
,"paragraph":["=====","=====","\\paragraph{","}"]
,"boitalic":["\'\'\'\'\'","\'\'\'\'\'","\\texttt{\\textit{","}}"]
,"bold":["\'\'\'","\'\'\'","\\texttt{","}"]
,"ttverb":["<tt>\\verb","</tt>","\\texttt{\\verb","}"]
,"tt":["<tt>","</tt>","\\texttt{","}"]
,"source":["<source>","</source>","\\begin{lstlisting}","\\end{lstlistin}"]
,"math":["<math>","</math>","$","$"]
,"ref":["[[","]]","\\nameref{","}"]
,"code":["<code>","</code>","\\verb|","|"]
,"italic":["\'\'","\'\'","\\textit{","}"]}
buf = []
verbatim = []
math = []
source = []
curly = []
idx = 0
def printBuf():
global buf
print("buffer begin")
for i in buf:
sys.stdout.write(i)
print("buffer end")
def readFile():
global buf
for line in ifile:
buf.append(line)
def beginList(state):
if state == 1:
return "\\begin{itemize}\n"
elif state == 2:
return "\\begin{enumerate}\n"
elif state == 3:
return "\\begin{description}\n"
def endList(state):
if state == 1:
return "\\end{itemize}\n"
elif state == 2:
return "\\end{enumerate}\n"
elif state == 3:
return "\\end{description}\n"
def lists():
global buf
global idx
state = 0 # 1 itemize, 2 enumerate, 3 description
count = 0
line = buf[idx]
if line[0] == '*':
state = 1
elif line[0] == '#':
state = 2
else:
return
buf.insert(idx, beginList(state))
idx+=1
while line[0] == '*' or line[0] == '#':
if line[1] != ' ':
buf[idx] = "\\item " + line[1:]
else:
buf[idx] = "\\item" + line[1:]
idx+=1
if idx < len(buf):
line = buf[idx]
else:
break
if idx < len(buf):
buf.insert(idx, endList(state))
else:
buf.append(endList(state))
def description():
global buf
global idx
line = buf[idx]
if line[0] == ';':
buf.insert(idx, beginList(3))
else:
return
idx+=1
while line[0] == ';':
tmp = "\\item[" + line[1:]
it = tmp.find(' ')
tmp = tmp[:it] + "] " + tmp[it:]
buf[idx] = tmp
idx+=1
if idx < len(buf):
line = buf[idx]
else:
break
if idx < len(buf):
buf.insert(idx, endList(3))
else:
buf.append(endList(3))
def subtitute(toSubB, toSubE, toSubWithB, toSubWithE):
global buf
global idx
#print(44, line)
retLine = ""
lowIdx = 2
line = ""
count = 0
print("curline",buf[idx])
while lowIdx != -1 and idx < len(buf):
count += 1
line = buf[idx]
if line[:8] == "argsNoSub":
return
lowIdx = line.find(toSubB)
if lowIdx != -1:
print("found low: ", lowIdx)
highIdx = line.find(toSubE, lowIdx+len(toSubB))
print("ret : ", retLine)
while highIdx == -1:
line += buf[idx+1]
del buf[idx+1]
buf[idx] = line
highIdx = line.find(toSubE, lowIdx+len(toSubB))
print("ret : ", retLine)
print("found high: ", highIdx)
if lowIdx != 0:
print("retT : ", line[0:lowIdx-1] + toSubWithB)
retLine = line[0:lowIdx-1] + toSubWithB
else:
retLine = toSubWithB
print("ret : ", retLine)
retLine += line[lowIdx+len(toSubB):highIdx].strip()
print("ret : ", retLine)
retLine += toSubWithE
print("ret : ", retLine)
retLine += line[highIdx+len(toSubE):len(line)]
print("ret : ", retLine)
buf[idx] = retLine
print("buf : ", buf[idx])
else:
break
print("done ",count)
def removeVerbatim():
global buf
global verbatim
global idx
idx = 0
vList = []
while idx < len(buf):
line = buf[idx]
if line == "\\begin{verbatim}":
vList.append(line)
del buf[idx]
line = buf[idx]
while line != "\\end{verbatim}":
vList.append(line)
del buf[idx]
line = buf[idx]
vList.append(line)
del buf[idx]
buf.insert(idx, "argsNoSubVerbatim")
verbatim.append(vList)
vList = []
else:
idx+=1
def pre():
global idx
global buf
'''
idx = 0
while idx < len(buf):
line = buf[idx]
it = line[1:].find('*')
if it != -1:
buf[idx] = line[:it] + "\n"
buf.insert(idx+1, line[it:].strip()+ "\n")
idx+=1
print("pre 1")
idx = 0
while idx < len(buf):
line = buf[idx]
it = line[1:].find('#')
if it != -1:
buf[idx] = line[:it] + "\n"
buf.insert(idx+1, line[it:].strip()+ "\n")
idx+=1
print("pre 2")
idx = 0
while idx < len(buf):
line = buf[idx]
it = line[1:].find(';')
if it != -1:
buf[idx] = line[:it] + "\n"
buf.insert(idx+1, line[it:].strip()+ "\n")
idx+=1
print("pre 3")
'''
idx = 0
while idx < len(buf):
line = buf[idx]
it = line.find("<pre>")
if it != -1:
buf[idx] = line[:it] + "\n"
buf.insert(idx+1, "\\begin{verbatim}")
buf.insert(idx+2, line[it+6:].strip()+ "\n")
idx+=1
print("pre 4")
idx = 0
while idx < len(buf):
line = buf[idx]
it = line.find("</pre>")
if it != -1:
buf[idx] = line[:it] + "\n"
buf.insert(idx+1, "\\end{verbatim}")
buf.insert(idx+2, line[it+7:].strip()+ "\n")
idx+=1
print("pre done")
def sourceReplace():
global idx
global buf
global source
global math
idx = 0
while idx < len(buf):
line = buf[idx]
it = line.find("<source")
if it == -1:
idx+=1
continue
jt = line.find("</source>")
while jt == -1:
tmp = buf[idx+1]
line = line[:len(line)] + tmp
buf[idx] = line
del buf[idx+1]
jt = line.find("</source>")
source.append(line[it:jt+len("</source>")])
if line[it:jt+len("</source>")] == None or len(line[it:jt+len("</source>")]) < 2:
print(idx)
done()
buf[idx] = line[:it] + line[jt+len("</source>"):]
buf.insert(idx, " argsNoSubSource ")
def LatexLatex():
global idx
global buf
global verbatim
global source
global curly
idx = 0
while idx < len(buf):
line = buf[idx]
it = line.find("{{")
if it == -1:
idx+=1
continue
jt = line.find("}}")
while jt == -1:
tmp = buf[idx+1]
line = line[:len(line)-1] + tmp
buf[idx] = line
del buf[idx+1]
jt = line.find("}}")
print(line)
curly.append(line[it:jt+2])
buf[idx] = line[:it] + line[jt+2:]
buf.insert(idx, " argsNoSubCurly ")
def tableChange():
global idx
global buf
idx = 0
while idx < len(buf):
line = buf[idx]
if line[:2] == "{|":
del buf[idx]
buf.insert(idx, "\\begin{tabular}{ } \\hline\n")
idx+=1
line = buf[idx]
last = False
while line[:2] != "|}":
print(line)
if line[:2] == "|-":
buf.insert(idx, "\\\\ \\hline\n")
idx+=1
last = False
else:
if last:
buf.insert(idx,"& " + line[1:])
else:
buf.insert(idx,line[1:])
last = True
idx+=1
del buf[idx]
line = buf[idx]
del buf[idx]
buf.insert(idx, "\\end{tabular}\n")
idx+=1
def exampleChange():
global idx
global buf
eList = []
idx = 0
while idx < len(buf):
line = buf[idx]
it = line.find("{{LaTeX/Example")
if it != -1:
eList.append("\\begin{tabular}\n")
eList.append("\\begin{verbatim}\n")
del buf[idx]
line = buf[idx]
it = line.find("}}")
while it == -1:
if -1 == line.find("render") or -1 == line.find("math"):
if -1 == line.find("render"):
eList.append("\\end{verbatim}\n")
if -1 == line.find("math"):
eList.append("$\n")
else:
eList.append(line)
del buf[idx]
if idx >= len(buf):
print(eList)
done()
line = buf[idx]
eList = []
else:
idx+=1
def makeUnderScore(name):
ret = ""
for x in name:
if x == ' ':
ret += '_'
else:
ret += x
return ret
def removeStickImage(line):
#print(line)
it = line.find(".png")
if it == -1:
it = line.find(".PNG")
if it == -1:
it = line.find(".jpg")
if it == -1:
it = line.find(".JPG")
if it == -1:
it = line.find(".svg")
if it == -1:
it = line.find(".SVG")
if it == -1:
return line
return line[:it+4]
def imageChange():
global idx
global buf
eList = []
idx = 0
while idx < len(buf):
line = buf[idx]
it = line.find("[[File:")
if it == -1:
idx+=1
continue
jt = line.find(".png")
if jt == -1:
jt = line.find(".PNG")
if jt == -1:
jt = line.find(".jpg")
if jt == -1:
jt = line.find(".JPG")
if jt == -1:
jt = line.find(".svg")
if jt == -1:
jt = line.find(".SVG")
if jt == -1:
continue
tmp = line[:it]
tmp += "\\begin{figure}[!h]\n"
tmp += "\\includegraphics[width=0.5\\textwidth]{"
image = removeStickImage(makeUnderScore(line[it+len("[[File:"):jt]))
image = image[:len(image)]
tmp += image
tmp += "}\n\\end{figure}\n"
tmp += line[jt+len("]]"):]
buf[idx] = tmp
def refChange():
global idx
global buf
eList = []
idx = 0
while idx < len(buf):
line = buf[idx]
it = line.find("[[")
if it == -1:
idx+=1
continue
jt = line.find("]]", it)
tmp = line[:it]
tmp += line[it+len("[["):jt]
tmp += line[jt+len("]]"):]
buf[idx] = tmp
def subSingleBackslash():
global idx
global buf
idx = 0
while idx < len(buf):
line = buf[idx]
new = re.sub(r"[^>]\\([^\s\",]+)", "\\\\verb|\\\\\g<1>|", line)
while line != new:
new = re.sub(r"[^>]\\([^\s\",]+)", "\\\\verb|\\\\\g<1>|", line)
line = new
print(line)
buf[idx] = line
idx+=1
def sub(sub):
global idx
idx = 0
while idx < len(buf):
subtitute(sub[0], sub[1], sub[2], sub[3])
idx+=1
print("sub ", idx)
def subList():
global idx
idx = 0
while idx < len(buf):
lists()
idx+=1
idx = 0
while idx < len(buf):
description()
idx+=1
def done():
ifile.close()
ofile.close()
sys.exit()
def writeOut():
global verbatim
global math
global source
global idx
global buf
idx = 0
while idx < len(buf):
line = buf[idx]
it = line.find("argsNoSubCurly")
while it != -1:
line = line[:it] + getNextCurly() + line[it+len("argsNoSubCurly")]
it = line.find("argsNoSubCurly")
buf[idx] = line
idx+=1
idx = 0
while idx < len(buf):
line = buf[idx]
it = line.find("argsNoSubSource")
while it != -1:
if line[:it] == None:
print(line)
done()
line = line[:it] + getNextSource() + line[it+len("argsNoSubSource"):]
it = line.find("argsNoSubSource")
buf[idx] = line
idx+=1
for line in buf:
if line == "argsNoSubVerbatim":
tmp = verbatim[0]
del verbatim[0]
for j in tmp:
ofile.write(j)
continue
ofile.write(line)
def getNextSource():
global source
tmp = source[0]
del source[0]
print(tmp)
ret = "\\begin{lstlisting}\n"
it = tmp.find(">")
ret += tmp[it+1:len(tmp)-len("</source>")] + "\n"
ret += "\\end{lstlisting}\n"
print(ret)
return ret
def getNextCurly():
global curly
tmp = curly[0]
del curly[0]
return processCurly(tmp)
def processCurly(cur):
if cur[:len("{{LaTeX/LaTeX|code=")] == "{{LaTeX/LaTeX|code=":
return "\\verb|"+cur[len("{{LaTeX/LaTeX|code="):len(cur)-2]+"|"
if cur[:len("{{LaTeX/Usage|code=")] == "{{LaTeX/Usage|code=":
return "\\verb|"+cur[len("{{LaTeX/Usage|code="):len(cur)-2]+"|"
if cur[:len("{{LaTeX/Environment|")] == "{{LaTeX/Environment|":
return "\\verb|"+cur[len("{{LaTeX/Environment|"):len(cur)-2]+"|"
if cur[:len("{{LaTeX/Parameter|")] == "{{LaTeX/Parameter":
return "\\texttt{"+cur[len("{{LaTeX/Parameter"):len(cur)-2]+"}"
if cur[:len("{{LaTeX/Package|")] == "{{LaTeX/Package|":
return "\\textit{"+cur[len("{{LaTeX/Package|"):len(cur)-2]+"}"
if cur[:len("{{LaTeX/Example|code=\\[")] == "{{LaTeX/Example|code=\\[":
retLine = "\\begin{tabular}{c} \\begin{verbatim}"
it = cur.find("|render=")
retLine += cur[len("{{LaTeX/Example|code=\\["):it-2] + "\\end{verbatim}"
retLine += "\\\\ \\hline\n"
retLine += "$ " + cur[it+len("|render=<math>"):len(cur)-len("</math>")-2] + " $ \\end{tabular}"
return retLine
else:
return cur
def main():
global ifile
global ofile
if(len(sys.argv) == 2):
ifile = open(sys.argv[1] + ".w", "r")
ofile = open(sys.argv[1] + ".tex", "w")
readFile()
print("after read file")
LatexLatex()
print("after latex latex")
pre()
#exampleChange()
removeVerbatim()
sourceReplace()
subSingleBackslash()
tableChange()
sub(subs["paragraph"])
sub(subs["subsubsection"])
sub(subs["subsection"])
sub(subs["section"])
sub(subs["part"])
sub(subs["boitalic"])
#sub(subs["bold"])
sub(subs["italic"])
sub(subs["ttverb"])
sub(subs["tt"])
#sub(subs["ref"])
sub(subs["code"])
#subList()
imageChange()
refChange()
print(buf)
writeOut()
print("\n\n\n")
for x in curly:
print(processCurly(x))
if __name__ == "__main__":
main()