-
Notifications
You must be signed in to change notification settings - Fork 4
/
imhier
99 lines (79 loc) · 2.59 KB
/
imhier
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''Utility show the hierarchy of index terms currently in use in a LaTex
document.
Copyright 2016 by Kevin A. Straight <[email protected]> under the
terms of the GNU public license.'''
import sys
import glob
# process command line options
a = sys.argv
if len(a) < 2:
print "IMHier (part of the Indexmeister v0.321 suite)"
print "Copyright 2015-2016 by Kevin A. Straight <[email protected]>"
print "under the terms of the GNU Public License"
print "Usage: "+a[0]+" filename.tex"
print ""
print "Returns a list of currently used index terms in a Latex document."
print "Note: Globing is supported for the filename."
else:
raw_idx = []
texfiles = glob.glob(a[1])
for t in texfiles:
with open(t, 'r') as T:
tf = T.read()
i = 0
while i > -1: # find other files linked to this one
i = tf.find("\input{", i) + 7
if i != 6:
j = tf.find("}", i)
w = tf[i:j]
if w not in texfiles:
texfiles.append(w)
else:
i = -1
# print "texfiles:", texfiles
for t in texfiles:
with open(t, 'r') as T:
tf = T.read()
i = 0
while i > -1: # find contents of all the index tags
i = tf.find("\index{", i) + 7
if i != 6:
j = tf.find("}", i)
w = tf[i:j]
if w not in raw_idx:
raw_idx.append(w)
else:
i = -1
# print "raw_idx:", raw_idx
# use a nested dictionary to keep track of every term used
final_idx = {}
for entry in raw_idx:
i = entry.find("!")
j = entry.find("!", i+1)
if i == -1:
i = len(entry)
if entry[:i] not in final_idx:
final_idx[entry[:i]] = {}
if j == -1:
j = len(entry)
if i != len(entry):
if entry[i+1:j] not in final_idx[entry[:i]]:
final_idx[entry[:i]][entry[i+1:j]] = {}
if j != len(entry):
if entry[j+1:] not in final_idx[entry[:i]][entry[i+1:j]]:
final_idx[entry[:i]][entry[i+1:j]][entry[j+1:]] = {}
# Now sort and print
level1 = final_idx.keys()
level1.sort()
for i in level1:
print i
level2 = final_idx[i].keys()
level2.sort()
for j in level2:
print "->", j
level3 = final_idx[i][j].keys()
level3.sort()
for k in level3:
print "-->", k