texincludes
3.41 KB
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
#!/usr/bin/env python
import sys,re,glob,StringIO,os,tempfile,filecmp,shutil
from optparse import OptionParser
parser = OptionParser()
(options, args) = parser.parse_args()
if len(args) < 1:
sys.exit(1)
files = glob.glob("*.tex")
if len(files) == 0:
sys.exit(0)
outfile = tempfile.NamedTemporaryFile(delete=False)
docfile = re.compile(r"""(?m)^(?!\s*%).*\\begin\{document\}""")
inputs = re.compile(r"""(?m)^(?!\s*%).*\\input{(.*)}""")
bibs = re.compile(r"""(?m)^(?!\s*%).*\\bibliography\{(.*)\}""")
citations = re.compile(r"""^(?m)^(?!\s*%).*\\(?:no|n)?cite""")
graphics = re.compile(r"""(?m)^(?!\s*%).*\\includegraphics(\[.*?\])?\{(.*?)\}""")
withpdf = re.compile(r"^.*\.pdf$")
nobibtex = re.compile(r"""(?m)^% !NOBIBTEX!""")
nobibtexs = {}
output = StringIO.StringIO()
allnames = []
for f in files:
lines = open(f, "r").read()
if not docfile.search(lines):
continue
input_files = []
bib_files = []
graphic_files = []
toprocess = [f]
docitations = False
dontbibtex = False
fbasename = os.path.splitext(f)[0]
while len(toprocess) > 0:
try:
lines = open(toprocess[0], "r").read()
if nobibtex.search(lines):
nobibtexs[toprocess[0]] = True
else:
nobibtexs[toprocess[0]] = False
if len(citations.findall(lines)) > 0:
docitations = True
toprocess += inputs.findall(lines)
b = bibs.finditer(lines)
for m in b:
allbibs = m.group(1).split(",")
for bib in allbibs:
bib_files.append(bib + ".bib")
g = graphics.finditer(lines)
for m in g:
if withpdf.match(m.group(2)):
graphic_files.append(m.group(2))
else:
path, ext = os.path.splitext(m.group(2))
if ext == '':
graphic_files.append(path + ".pdf")
else:
graphic_files.append(m.group(2))
except Exception, e:
pass
input_files.append(toprocess.pop(0))
all_files = input_files
all_files.extend(graphic_files)
all_files.extend(bib_files)
for file in args[1:]:
all_files.append(file)
allnames.append(fbasename)
tex_files = [all_file for all_file in all_files if all_file.endswith(".tex")]
print >>output, "%s_TEXFILES=%s" % (fbasename.upper(), " ".join(tex_files),)
print >>output, "%s : LOG := %s.log" % (fbasename, fbasename)
print >>output, "%s : PDF := %s.pdf" % (fbasename, fbasename)
print >>output, "%s : $(START) %s.pdf $(END)" % (fbasename, fbasename)
print >>output, "%s.ps : %s.pdf" % (fbasename, fbasename)
print >>output, "%s.pdf %s.blg : .deps %s" % (fbasename, fbasename, " ".join(all_files))
if docitations and not nobibtexs[f]:
print >>output, "\txelatex -shell-escape %s" % (f)
print >>output, "\tbibtex %s" % (fbasename)
print >>output, "\txelatex -shell-escape %s" % (f)
print >>output, "\txelatex -shell-escape %s" % (f)
else:
print >>output, "\txelatex -shell-escape %s" % (f)
print >>output, "\txelatex -shell-escape %s" % (f)
print >>output, "spell-%s : %s" % (fbasename, " ".join(tex_files),)
print >>output, "\t@hunspell -t -l -p $(PWD)/.okwords %s | sort -f | uniq" % (" ".join(tex_files),)
print >>output, "open-%s : %s" % (fbasename, " ".join(tex_files),)
print >>output, "\t@$(PYTEXOPEN) %s.pdf 1>/dev/null 2>/dev/null &" % (fbasename,)
print >>outfile, output.getvalue(),
print >>outfile, "PDFS = %s" % (" ".join([n + ".pdf" for n in allnames]))
outfile.close()
shutil.move(outfile.name, args[0])