-
Notifications
You must be signed in to change notification settings - Fork 0
/
neatlatex3.py
309 lines (276 loc) · 9.73 KB
/
neatlatex3.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
#!/usr/bin/env python3
import argparse as argp
import shutil as sh
import subprocess as sp
import bibtexparser
import sys
import logging
from pathlib import Path
log = logging.getLogger(__name__)
def clear_bib(bibFile, intermDir, poplist, verbose, strsubList):
if verbose:
log.info('Removing following fields from {}:\n{}'
.format(bibFile, ', '.join(poplist)))
if not intermDir.is_dir():
intermDir.mkdir()
try:
sh.copyfile(bibFile, Path(intermDir, bibFile.name + '.bak'))
except Exception as e:
log.warning('Could not create a backup of {}:\n{}'
.format(bibFile, e))
try:
with open(bibFile, 'r') as bf:
bibDB = bibtexparser.load(bf)
except Exception as e:
log.critical('Could not read {}:\n{}'.format(bibFile, e))
return -1
if hasattr(bibDB, 'comments'):
if 'Cleared by NeatLatex' in bibDB.comments:
log.info('The bibtex file appears to be cleaned before. Skipping.')
return
else:
bibDB.comments = ['Cleared by NeatLatex']
else:
log.info('Comments section not available in bibtex. Adding.')
bibDB.comments = ['Cleared by NeatLatex']
for e in bibDB.entries:
for f in poplist:
try:
if e['ENTRYTYPE'] == 'misc' and f == 'url':
continue
e.pop(f)
except:
continue
if len(strsubList) > 0:
for e in bibDB.entries:
if 'url' in e.keys():
for s in strsubList:
e['url'] = e['url'].replace(s[0],s[1])
try:
with open(bibFile,'w') as bf:
bibtexparser.dump(bibDB, bf)
except Exception as e:
log.info('Error occurred while writing {}!\n'.format(bibFile))
return -1
log.info('Bibliography file {} cleaned up.'.format(bibFile))
def clear_wb(outDir, intermDir, allExts):
pwdFileList = [f for f in Path().iterdir()]
allExtTuples = tuple(e for e in allExts)
if len(pwdFileList) == 0 or len(allExts) == 0:
return
cleaned = True
dirs = [outDir, intermDir]
for d in dirs:
try:
sh.rmtree(d)
except Exception as e:
cleaned = False
log.error('Could not remove {}\n{}'.format(d, e))
for f in pwdFileList:
if f.name.endswith(allExtTuples):
try:
sh.os.remove(f)
except Exception as e:
cleaned = False
log.error('Could not remove {}\n{}'.format(f, e))
if cleaned:
log.info('Working directory cleaned up.')
else:
log.warning('Some files/directories could not be removed.')
def makepdf(pname, verbose):
if verbose:
try:
proc = sp.Popen('pdflatex '+pname, shell=True, stderr=sp.STDOUT)
proc.wait()
log.info('[done] pdflatex pass 1')
if Path(pname+'.bcf') in Path().iterdir():
proc = sp.Popen('biber '+pname, shell=True, stderr=sp.STDOUT)
proc.wait()
log.info('[done] biber')
else:
proc = sp.Popen('bibtex '+pname, shell=True, stderr=sp.STDOUT)
proc.wait()
log.info('[done] bibtex')
proc = sp.Popen('pdflatex '+pname, shell=True, stderr=sp.STDOUT)
proc.wait()
log.info('[done] pdflatex pass 2')
proc = sp.Popen('pdflatex '+pname, shell=True, stderr=sp.STDOUT)
proc.wait()
log.info('[done] pdflatex pass 3')
except KeyboardInterrupt:
sys.stdout.flush()
sys.exit(-1)
except Exception as e:
log.critical('{}'.format(e))
return -1
else:
try:
proc = sp.Popen(['pdflatex', pname], stdout=sp.PIPE)
proc.communicate()
log.info('[done] pdflatex pass 1')
if Path(pname+'.bcf') in Path().iterdir():
proc = sp.Popen(['biber', pname], stdout=sp.PIPE)
proc.communicate()
log.info('[done] biber')
else:
proc = sp.Popen(['bibtex', pname], stdout=sp.PIPE)
proc.communicate()
log.info('[done] bibtex')
proc = sp.Popen(['pdflatex', pname], stdout=sp.PIPE)
proc.communicate()
log.info('[done] pdflatex pass 2')
proc = sp.Popen(['pdflatex', pname], stdout=sp.PIPE)
proc.communicate()
log.info('[done] pdflatex pass 3')
except KeyboardInterrupt:
sys.stdout.flush()
sys.exit(-1)
except Exception as e:
log.critical('{}'.format(e))
return -1
def tidyup(outDir, outputExts, intermDir, intermExts, verbose):
log.info('Cleaning up the working directory...')
fList = [f for f in outDir.parent.iterdir()]
outputExtTuples = tuple(e for e in outputExts)
intermExtTuples = tuple(e for e in intermExts)
moveFailed = False
for f in fList:
if f.name.endswith(outputExtTuples):
try:
sh.move(f, Path(outDir, f))
except Exception as e:
log.error('Could not move output files to {}\n{}'.format(outDir, e))
moveFailed = True
if not moveFailed:
log.debug('All {} file(s) moved to {}'
.format(','.join(outputExts), outDir))
moveFailed = False
for f in fList:
if f.name.endswith(intermExtTuples):
try:
sh.move(f, Path(intermDir, f))
except Exception as e:
log.error('Could not move intermediate files to {}\n{}'
.format(intermDir, e))
moveFailed = True
if not moveFailed:
log.debug('All {} file(s) moved to {}'
.format(','.join(intermExts), intermDir))
def main():
logLevels = {
'critical': logging.CRITICAL,
'error': logging.ERROR,
'warn': logging.WARNING,
'warning': logging.WARNING,
'info': logging.INFO,
'debug': logging.DEBUG
}
ap = argp.ArgumentParser(description = 'Neatly compiles LaTex projects.',
prog = 'neatlatex')
ap.add_argument('-p', '--proj', help = 'Project name to comile')
ap.add_argument('-c', '--clear',
help = 'Clear the project directory from Aux or Log files.',
action = 'store_true')
ap.add_argument('-l', '--log', help = 'Indicate log level',
choices = logLevels.keys(), default = 'info')
ap.add_argument('-b', '--bibfile',
help = 'Bibtex file auto-generated by Mendeley.')
ap.add_argument('-v', '--verbose', help = 'Toggles verbosity',
action = 'store_true')
args = ap.parse_args()
logLevel = logLevels.get(args.log.lower())
if logLevel is None:
raise ValueError('Log level must be one of{}'
.format(' | '.join(logLevels.keys())))
logging.basicConfig(level = logLevel, format='%(levelname)s: %(message)s')
log.debug('Log level: {}'.format(logLevel))
verbose = args.verbose
outputExts = ['.pdf']
intermExts = ['.aux', '.dvi', '.log', '.out', '.xcp', '.bbl', '.blg',
'.lof', '.lot', '.run.xml', '.bcf', '.toc', '.fdb_latexmk',
'.fls', '.out.ps', '.tdo']
bibexclude = ['abstract', 'keywords', 'file', 'comment', 'url']
strSubList = [('{~}','~'), ('{\&}','&'), ('{\_}','_'), ('{\%}','%'),
('%20',' '), ('%5F', '_'), ('%7E', '~'),('%3D', '='),
('%2F', '/'), ('%2B', '+'),('%3B', ';')]
allExts = outputExts + intermExts
if args.proj:
proj_dir = Path(args.proj).parent
else:
proj_dir = Path('.')
outDir = Path(proj_dir, 'output')
intermDir = Path(proj_dir, 'auxlog')
if args.clear:
clear_wb(outDir, intermDir, allExts)
return
elif args.bibfile:
bibFile = Path(args.bibfile)
res = clear_bib(bibFile, intermDir, bibexclude, verbose, strSubList)
return
if not args.proj:
log.critical('Must indicate main project .tex file to compile.')
return
if len(args.proj) <= 0:
log.critical('Invalid project name.')
return
if not (Path(args.proj).exists() or
Path(args.proj + '.tex').exists()):
log.critical('File does not exist.')
return
if not outDir.is_dir():
outDir.mkdir()
if not intermDir.is_dir():
intermDir.mkdir()
pname = args.proj.strip('.tex')
res = makepdf(pname, verbose)
if res == -1:
return res
tidyup(outDir, outputExts, intermDir, intermExts, verbose)
# # Passive aggressive helper
# if verbose:
# while True:
# seepdf = False
# wannasee = input('Do you want to see the output pdf file or what? (Yes/no): ')
# if wannasee == '' or wannasee == None:
# print('No answer!.. OK. Here it is just in case.')
# seepdf = True
# elif wannasee in ['Yes', 'yes', 'y', 'Y']:
# print('\nHere is your gloriuos work.')
# seepdf = True
# elif wannasee == 'YES':
# print('\nAlright man.. Calm down! Here\'s your damn paper.. Have a blast!')
# seepdf = True
# elif wannasee in ['No', 'no', 'N', 'n']:
# print('\nSure.. you made me do all that and you don\'t even wanna see it.. No problem! It\'s fine.. It\'s totally fine..')
# elif wannasee == 'NO':
# print('\nYou know what! I don\'t wanna see it either! I don\'t even CARE what it looks like!')
# elif wannasee.lower() == 'or what':
# print('\nCome come, I clap for you.. you veeery funny ah!')
# else:
# print('\nDid you really think the person who wrote this script made me smart enough to understand what you\'re saying? Yes or No man.. YES or NO!\nDamn it.. every day a smart ass! every damn day!')
# continue
# Passive aggressive helper
if verbose:
while True:
seepdf = False
wannasee = input('Open output pdf file? (Yes/no): ')
if wannasee == '' or wannasee == None:
seepdf = True
elif wannasee.lower() in ['yes', 'y']:
print('\nHere is your gloriuos work!')
seepdf = True
elif wannasee.lower() in ['no', 'n']:
break
else:
print('Invalid input.')
continue
if seepdf:
sh.os.popen('xdg-open ' + Path(outDir, pname + '.pdf').as_posix())
break
else:
break
else:
log.info('Done! Find {} files(s) in {} directory.'
.format(','.join(outputExts), outDir))
if __name__ == '__main__':
main()