-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathmerge.py
39 lines (35 loc) · 1008 Bytes
/
merge.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
"""Merges a chapter back into the main file."""
import sys
def insert(tex):
chap = False
for line in open(tex):
# don't copy preamble or end document
if line.startswith("\\chapter"):
chap = True
if line.startswith("\\end{document}"):
chap = False
if chap:
print line,
def main(tex, num):
src = open("thinkjava.tex")
# copy the preamble
for line in src:
print line,
if line.startswith("\\begin{document}"):
break
# copy each chapter
i = -1
for line in src:
if line.startswith("\\backmatter") or line.startswith("\\chapter"):
i += 1
# insert new chapter
if i == num + 1:
insert(tex)
# skip old chapter
if i != num:
print line,
if __name__ == "__main__":
if len(sys.argv) == 3:
main(sys.argv[1], int(sys.argv[2]))
else:
print "Usage: python merge.py CHAP.tex NUMBER"