-
Notifications
You must be signed in to change notification settings - Fork 5
/
lin.py
executable file
·184 lines (159 loc) · 4.89 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
#!/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
# 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]",
"\\emph{" : "SELF",
"\\texttt{" : "SELF",
"\\textit{" : "SELF",
"\\camera{" : "SELF",
"\\edb{" : "SELF",
"\\myparagraph{" : "[ "+"SELF"+" :]"
}
KEYWORDSUB = {
"\\system" : "SYSTEM",
"\\eg" : "e.g.,",
"\\ie" : "i.e.,",
# paper-specific
"\\overcommitname" : "informed overcommitment",
"\\Overcommitname" : "Informed overcommitment"
}
def ppword(l):
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 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 c in KEYWORDSUB:
x = l.find(c)
if x>=0:
sub = l[0:x]+KEYWORDSUB[c]+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