Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
| Download

William Stein -- Talk for Mathematics is a long conversation: a celebration of Barry Mazur

Views: 2342
1
#!/usr/bin/env python
2
3
# Takes a file and a phrase as input, then
4
# searches for every occurrence of the phrase in the file, and if the line
5
# doesn't contain \index{phrase}, adds that immediately after the phrase
6
# (so at most once per line).
7
8
import os, sys
9
10
if len(sys.argv) < 3:
11
sys.stderr.write("%s: [filename.tex] '[phrase]' '[phrase2]' ...\n"%(sys.argv[0]))
12
sys.exit(1)
13
14
def index_phrase(filename, phrase):
15
s = ''
16
index = '\\index{%s}'%phrase
17
phrase_lower = phrase.lower()
18
index_lower = index.lower()
19
v = []
20
for r in open(filename).readlines():
21
r_lower = r.lower()
22
if phrase_lower in r_lower and index_lower not in r_lower:
23
i = r_lower.index(phrase_lower)
24
if i != -1: # it can't be -1...
25
j = r_lower[i+len(phrase):].find(' ')
26
#print i, j
27
if j == -1:
28
j = i+len(phrase)
29
else:
30
j = i+len(phrase) + j
31
r = r[:j] + index + r[j:]
32
print r
33
v.append(r)
34
open(filename,'w').write(''.join(v))
35
36
filename = sys.argv[1]
37
for phrase in sys.argv[2:]:
38
index_phrase(filename, phrase)
39
40
41
42