-
Notifications
You must be signed in to change notification settings - Fork 0
/
latexmerge
50 lines (44 loc) · 1.38 KB
/
latexmerge
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
#! /usr/bin/env python3
#encoding: utf-8
import time,os,sys,re
rex=re.compile('{([^{}]+)}')
def include(l):
if l.strip().find('\\input{')==0: return True
elif l.strip().find('\\include{')==0: return True
return False
def addFile(inputline, fw, fnm=None, apdx='.tex'):
try:
print('Parsing:', inputline)
if fnm is None:
m=rex.search(inputline)
if m is None: return
pre,ext=os.path.splitext(m.group(1))
if ext==apdx: fnm=pre
elif ext=='': fnm=pre+apdx
else: return
with open(fnm) as fr:
for l in fr:
if include(l): addFile(l, fw)
else: fw.write(l)
fw.write('\n\n')
except Exception as e: print(e)
def parseMain(mainfnm):
prefix, ext= os.path.splitext(mainfnm)
if ext!='.tex' and ext!='':
print('Error! Not a tex file! Exit!')
return
fnm='%s_t%d.tex' % (prefix,int(time.time()))
print('Writing to:', fnm)
with open(prefix+'.tex') as fr, open(fnm, 'w') as fw:
fw.write('%% This file is automatically generated by LatexMerge program.\n')
fw.write('%% Visiting https://github.com/zzjjzzgggg/ubuntu_tools for details.\n')
for l in fr:
l=l.strip()
if include(l): addFile(l, fw)
elif l.find('\\bibliography{')==0: addFile(l, fw, prefix+'.bbl')
else: fw.write(l+os.linesep)
if __name__=='__main__':
if len(sys.argv)<2:
print('Merge several latex files into one file.\nUsage:\n\tlatexmerge XX[.tex]')
else:
parseMain(sys.argv[1])