-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlin.py
executable file
·260 lines (221 loc) · 6.82 KB
/
lin.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
#!/usr/bin/python3
###################################
# Linearlize all latex files into a single file.
# - strips lines that start with %
# - paragraph on a single line (necessary for including in word)
#
# Usage
# ./scripts/lin.py <top-file.tex> [OPTION]
#
# Base mode - when there are no option
# - generate a file arxiv.tex suitable for upload into arxiv, TOCS, etc
# - note: does not inline the bibliography
# - note: does not renumber the figures
# - normally called by the target "make arxiv"
# Mode #2: OPTION = word
# - applies the logic of base mode
# - further remove Citations, labels and refs
# - output file is called latexforword.txt
# - normally called by the target "make spell"
# Revision history
# - 2023-12 -- rewrite from the totally obsolete perl script
import sys,os
# global
inbody = 0
# flattens tree of strings into a list of strings
def flatten(tree,outlist):
for l in tree:
if isinstance(l,list):
flatten(l,outlist)
else:
outlist.append(l)
return outlist
# called to expand \\input{filename} (if present) during parsing
def expandlineinput(l):
par = []
x = l.find("\\input{")
while x>=0:
if x>0:
par.append(l[0:x])
l = l[x+len("\\input{"):]
y = l.find("}")
if y>0:
filename = l[0:y]
print("Expanding input",filename)
tree = readfile(filename)
tree = [expandlineinput(l) for l in tree]
par.append("%BEGIN("+filename+")")
par.append(tree)
par.append("%END("+filename+")")
else:
sys.exit("unbalanced filename"+l)
l = l[y+1:]
x = l.find("\\input{")
par.append(l)
return par
#
def removecomments(l):
x = l.find("%")
if x<0:
return l
if l[x-1:x]=="\\":
# \% is not a comment
return l[0:x+1] + removecomments(l[x+1:])
return l[0:x]
# Read and parse latex input file, applying the following logic
# - remove all comments
# - strip leading and trailing blanks
# - merge consective non-blank lines (before removal of comments) into a single line that corresponds to the paragraph
# - split paragraphs if lines ends with \\
def readfile(filename):
if os.path.isfile(filename):
print ("READING FILENAME: ",filename)
elif os.path.isfile(filename+".tex"):
print ("READING FILENAME: ",filename,"(.tex)")
filename = filename+".tex"
else:
sys.exit("could not find file ",filaname)
lines = [line.strip().lstrip() for line in open(filename)]
p = []
tree = []
for l in lines:
if len(l)==0: # blank line, new paragraph
tree.append(" ".join(p))
tree.append("") # insert blank line to split paragraphs
p = []
else:
l = removecomments(l)
if len(l)>1 and l[len(l)-2:]=="\\\\": # ends with \\ split
print("line ends with \\\\",l)
tree.append(" ".join(p))
tree.append(l)
p = []
else:
p.append(l)
tree.append(" ".join(p))
return tree
# Substitutions that make it easier to use the MS Word grammar checker
COMMANDSUB = {
"~\\cite{" : " [CITE]",
"~\\ref{" : " [REF]",
"\\S\\ref{" : " [SECREF]",
"\\autoref{" : "[AUTOREF]",
"\\label{" : " [LABEL]",
"\\includegraphics{" : " [GRAPHICS]",
"\\emph{" : "SELF",
"\\texttt{" : "SELF",
"\\textit{" : "SELF",
"\\textbf{" : "SELF",
"\\camera{" : "SELF",
"\\caption{" : "SELF",
"\\textsf{" : "CAPS",
"\\section{" : "CAPS",
"\\edb{" : "SELF",
"\\myparagraph{" : "["+"SELF"+":]",
"\\new{" : "SELF",
"\\system{": "SYSTEM",
#paper-specific
"\\td{" : "TD"
}
KEYWORDSUB = {
"\\eg" : "e.g.,",
"\\ie" : "i.e.,",
"\\etc" : "etc.",
"\\&" : "&",
# paper-specific
"\\overcommitname" : "informed overcommitment",
"\\Overcommitname" : "Informed overcommitment",
"\\system" : "SYSTEM"
# Arm architecture
}
## custom macros
CUSTOMMACROS = {}
## one day, will figure out regex in python
def findcustommacros(l):
global CUSTOMMACROS
x = l.find("\\newcommand{")
if x>=0:
l2 = l[x+len("\\newcommand{"):]
x2 = l2.find("}{")
if x2>0:
key = l2[0:x2]
l3 = l2[x2+2:]
for c in ['\\textsc{','\\textsf{']:
x3 = l3.find(c)
if x3==0:
x4 = l3.find("}")
if x4>0:
value = l3[len(c):x4]
x5 = value.find("\\xspace")
if x5>0:
value = value[0:x4]
if key in KEYWORDSUB:
print("HARD MACRO |"+key+"|"+value+"|")
else:
print("CUSTOM MACROS |"+key+"|"+value+"|"+value.upper())
CUSTOMMACROS[key+"{}"] = value.upper()
return findcustommacros(l[x+len("\\newcommand{"):])
else:
return l
def ppword(l):
global inbody
global CUSTOMMACROS
if inbody == 0:
l = findcustommacros(l)
for c in ["\\title","\\begin{document}"]:
x = l.find(c)
if x>=0:
inbody = 1
return ppword(l)
return ""
for cc in CUSTOMMACROS:
x = l.find(cc)
if x>=0:
sub = l[0:x]+CUSTOMMACROS[cc]+l[x+len(cc):]
return ppword(sub)
for c in COMMANDSUB:
x = l.find(c)
if x>=0:
l2 = l[x+len(c):]
y = l2.find("}")
if y>=0:
pos = COMMANDSUB[c].find("SELF")
if COMMANDSUB[c]=="CAPS":
sub = l[0:x]+ l2[:y]+l2[y+1:]
elif pos>=0:
prefix = COMMANDSUB[c][:pos]
postfix = COMMANDSUB[c][pos+4:]
sub = l[0:x]+prefix + l2[:y]+ postfix +l2[y+1:]
else:
sub = l[0:x]+COMMANDSUB[c]+l2[y+1:]
return ppword(sub)
for cc in KEYWORDSUB:
c2 = cc+"{}"
for c in [c2,cc]:
x = l.find(c)
if x>=0:
sub = l[0:x]+KEYWORDSUB[cc]+l[x+len(c):]
return ppword(sub)
return l
main = sys.argv[1]
tree = readfile(main)
tree = [expandlineinput(l) for l in tree]
tree = flatten(tree,[])
if len(sys.argv)>2 and sys.argv[2]=="word":
tree = [ppword(l) for l in tree]
outname = "latexforword.txt"
else:
outname = "arxiv.tex"
print("Generating "+outname)
F = open(outname,"w")
wasblank = 0
for l in tree:
while len(l)>=1 and l[0:1]==" ":
l = l[1:]
if l=="":
if wasblank == 0:
F.write("\n")
wasblank = 1
else:
F.write(l+"\n")
wasblank = 0