-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmindex.py
executable file
·278 lines (231 loc) · 8.55 KB
/
mindex.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Mindex 1.0.0 - a miniature index creator
This is a simple program that uses LaTeX to create a small printed index from a
mindex file, a form of tab-separated text file. The details of the file format
and the usage of Mindex are described in the README included with this program.
Mindex uses the MIT license; see the LICENSE file for details.
Usage: mindex FILENAME
"""
import argparse
import os
import subprocess
import tempfile
import sys
import shutil
from string import Template
VERSION = "1.1.0"
PAPER_DIMS = {'X': 8.5, 'Y': 11.0}
DEFAULT_CLOSING = "Automatically generated by Mindex %s on \\today." % VERSION
DEFAULT_GUTTER = "0.75em"
DEFAULT_INDENT = "0.75em"
TMP_FNAME = "mindex"
LATEXSTR = Template(r"""
\documentclass{article}
\usepackage[top=${margin_y}in, bottom=${margin_y}in, right=${margin_x}in, left=${margin_x}in, showframe]{geometry}
\usepackage[utf8x]{inputenc}
\usepackage{multicol}
\usepackage[columns=${cols}, indentunit=${indent}, columnsep=${gutter}, font=footnotesize, justific=raggedright]{idxlayout}
\usepackage[sc, osf]{mathpazo}
\usepackage{titlesec}
\renewcommand{\indexname}{\vskip -0.55in}
\begin{document}
\pagestyle{empty}
\begin{center}\small \emph{${title}}\end{center}
\begin{theindex}
${content}
\end{theindex}
\vfill
\begin{center}\small \emph{${closing}}\end{center}
\end{document}
""")
def splash():
print(("Mindex %s – the automatic miniature index printer" % VERSION))
print("Copyright 2014, 2016, 2019 Soren Bjornstad. See LICENSE for details.")
print("")
def getPaperSize(which):
while True:
ps = input("%s dimension of the finished index (inches): " % which)
try:
ps = float(ps)
except ValueError:
print("Please enter a number (decimals are okay).")
else:
if ps > PAPER_DIMS[which]:
print(("I can't print an index larger than the paper "
"(%.01f inches wide)!" % PAPER_DIMS[which]))
elif ps <= 0:
print("Think you're being smart, huh? "
"These are supposed to be *positive* numbers.")
elif PAPER_DIMS[which] - ps < 1.0:
print("Please provide at least half an inch of margin to "
"ensure the printer prints all of the page.")
else:
return ps
def getBasicParams(fname):
print("I just need a few parameters before we get started.")
title = input("Title of this index: ")
xdim = getPaperSize('X')
ydim = getPaperSize('Y')
return {'title': title, 'xdim': xdim, 'ydim': ydim, 'fname': fname}
def calcMargins(params):
x = (PAPER_DIMS['X'] - float(params['xdim'])) / 2
y = (PAPER_DIMS['Y'] - float(params['ydim'])) / 2
return x, y
def calcColumns(xdim):
cols = xdim / 1.5
cols = int(cols)
return cols
def readContent(fname):
data = []
errors = []
sortDict = {}
with open(fname) as f:
for entry in f:
# abort if this is a comment or newline
if entry[0] == "#":
continue
if entry == "\n":
continue
# process as entry
entry = entry.split('\t')
if len(entry) == 3 and entry[2].strip() != "":
# sort key specified
sortDict[entry[2].strip()] = entry[0].strip()
data.append([entry[2].strip(), entry[1].strip()])
elif (len(entry) == 3 and entry[2].strip() == "") or \
(len(entry) == 2):
# normal
data.append([entry[0].strip(), entry[1].strip()])
else:
# invalid
errors.append(entry)
continue
# the data list is now composed of sort keys; sort by these, then replace
# the sort keys with the actual content if necessary
data.sort(key=lambda x: x[0].lower())
for i in data[:]:
if i[0] in sortDict:
i[0] = sortDict[i[0]]
if errors:
print("")
print("The following lines are invalid and were ignored:")
print("----- BEGIN MINDEX FILE ERRORS -----")
for i in errors:
print(i)
print("----- END MINDEX FILE ERRORS -----")
input("(press any key to continue)")
return data
def formatIndex(data):
index = ""
for i in data:
index += "\\item ~%s, %s" % (i[0], i[1])
return index
def prepLaTeX():
tdir = tempfile.mkdtemp()
os.chdir(tdir)
# we are now in tdir for the rest of the program
return tdir
def outputLaTeX(params):
with open("%s.tex" % TMP_FNAME, 'w') as f:
f.write(LATEXSTR.substitute(params))
try:
subprocess.check_output(
['pdflatex', '-interaction=nonstopmode', TMP_FNAME],
stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
print("An error occurred while compiling your index.")
yn = input("Would you like to see the TeX output (y/n)?")
if yn.lower()[0] == 'y':
print("----- BEGIN pdfLaTeX OUTPUT -----")
print((e.output))
print("----- END pdfLaTeX OUTPUT -----")
input("(press any key to continue)")
else:
ofile = "%s.pdf" % TMP_FNAME
if sys.platform.startswith('linux'):
subprocess.call(["xdg-open", ofile])
elif sys.platform == "darwin":
os.system("open %s" % ofile)
elif sys.platform == "win32":
os.startfile(ofile)
else:
print("Unable to automatically open the output. Please"
"browse manually to %s." % ofile)
def clearscreen():
"""Clear the console screen."""
print("") # sometimes the display ends up off by a line if you don't do this
if os.name == "posix":
os.system('clear')
elif os.name in ("nt", "dos", "ce"):
os.system('CLS')
else:
print(('\n' * 100))
def modificationLoop(params):
while True:
clearscreen()
print("Mindex Tweaks Menu")
print("Num\tOption\t\t\tCurrent Value")
print("---------------------------------------------")
print(("(1)\tTextblock Width\t\t%.02f in" % params['xdim']))
print(("(2)\tTextblock Height\t%.02f in" % params['ydim']))
print(("(3)\tNumber of Columns\t%i" % params['cols']))
print(("(4)\tTitle\t\t\t%s" % params['title']))
print(("(5)\tFooter\t\t\t%s" % params['closing']))
print(("(6)\tGutter Width\t\t%s" % params['gutter']))
print(("(7)\tIndent Width\t\t%s" % params['indent']))
print("(0)\tQuit Mindex")
num = input(">>> ")
if num == "0":
return
elif num == "1":
params['xdim'] = getPaperSize('X')
params['margin_x'], params['margin_y'] = calcMargins(params)
elif num == "2":
params['ydim'] = getPaperSize('Y')
params['margin_x'], params['margin_y'] = calcMargins(params)
elif num == "3":
ri = input("New number of columns: ")
try:
ri = int(ri)
except ValueError:
print("Number of columns must be an integer.")
input("(press any key to continue)")
else:
params['cols'] = ri
elif num == "4":
params['title'] = input("New title: ")
elif num == "5":
params['closing'] = input("New footer: ")
elif num == "6":
params['gutter'] = input("New gutter width (include unit): ")
elif num == "7":
params['indent'] = input("New indent width (include unit): ")
print("Rerunning LaTeX...")
outputLaTeX(params)
if __name__ == "__main__":
if len(sys.argv) <= 1:
print("Usage: mindex FILENAME")
sys.exit(1)
else:
filename = sys.argv[1]
if not os.path.isfile(filename):
print("Usage: mindex FILENAME")
print("(The file you specified does not exist.)")
sys.exit(2)
splash()
params = getBasicParams(filename)
params['margin_x'], params['margin_y'] = calcMargins(params)
params['cols'] = calcColumns(params['xdim'])
params['closing'] = DEFAULT_CLOSING
params['gutter'] = DEFAULT_GUTTER
params['indent'] = DEFAULT_INDENT
data = readContent(params['fname'])
params['content'] = formatIndex(data)
tdir = prepLaTeX()
outputLaTeX(params)
yn = input("Would you like to tweak the output (y/n)? ")
if yn and yn.lower()[0] == 'y':
modificationLoop(params)
shutil.rmtree(tdir)