-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-index.py
282 lines (227 loc) · 6.95 KB
/
build-index.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
279
280
281
282
import argparse
import os
import pandas as pd
from docx import Document
from docx.enum.text import WD_TAB_ALIGNMENT, WD_TAB_LEADER
from docx.oxml.ns import qn
from docx.shared import Pt, Inches, Cm
from books import books
from topics import topics
from webindex import generate_web_index
INTRO_PAGES = 4
LEFT_ALIGNMENT = 0
GROUP_CORRECTIONS = {
'Armour': 'Personal Protection',
'Augment': 'Augmentations',
'Augments': 'Augmentations',
'Augmentation': 'Augmentations',
'Characteristic': 'Characteristics',
# don't nest corps
'Corporation': '',
'Corporations': '',
'Drone': 'Drones',
'Robot': 'Robots',
'Ship': 'Ships',
'Vehicle': 'Vehicles',
}
# Bulk correct entries with the incorrect type
TYPE_CORRECTIONS = {
'Adventure': 'Adventures',
'Armour': 'Personal Protection',
'Career': 'Careers',
# Not sure a list helps people, so moving them to setting
'Corporation': 'Setting',
'Megacorporation': 'Setting',
'Megacorporations': 'Setting',
# all drones are robots
'Drone': 'Robots',
'Drones': 'Robots',
"K'Kree": "K'kree",
# Not sure person needs to be its own category; adding them to setting for the time being
'Person': 'Setting',
'Robot': 'Robots',
# Don't want these pulled out, but might someday, so....
'Sectors': 'Setting',
'Sector': 'Setting',
'Subsectors': 'Setting',
'Subsector': 'Setting',
'Ship': 'Ships',
'Skill': 'Skills',
'small craft': 'Small Craft',
'Small craft': 'Small Craft',
'Sophont': 'Sophonts',
'System': 'Systems',
'Vehicle': 'Vehicles',
'Weapons': 'Weapon',
}
def has_page_break(para):
for run in para.runs:
if 'w:br' in run._element.xml and 'w:type="page"' in run._element.xml:
return True
return False
def delete_existing_entries(document):
page_count = 1
position_found = False
for para in document.paragraphs:
if position_found:
p = para._element
p.getparent().remove(p)
else:
if has_page_break(para):
page_count += 1
position_found = page_count >= INTRO_PAGES
def add_type_paragraph(document, type_name):
document.add_page_break()
paragraph = document.add_paragraph()
run = paragraph.add_run(type_name)
run.bold = True
run.font.size = Pt(12)
paragraph.alignment = LEFT_ALIGNMENT
return paragraph
def add_page_text(paragraph, topic):
paragraph.add_run('\t')
books = sorted(topic['entries'].keys())
for i, book in enumerate(books):
pages = topic['entries'][book]
p = ','.join(str(page['page']) for page in pages)
entry_text = paragraph.add_run(f"{book} {p}")
entry_text.font.size = Pt(8)
if i < len(books) - 1:
comma = paragraph.add_run(", ")
comma.font.size = Pt(8)
paragraph.alignment = LEFT_ALIGNMENT
def split_subject(subject):
if len(subject) > 35:
split_pos = subject.rfind(' ', 0, 35)
part1 = subject[:split_pos].strip()
part2 = subject[split_pos:].strip()
return [part1, part2]
else:
return [subject]
def add_subject(document, subject, indent):
parts = split_subject(subject)
max_length = 35 if indent else 40
if len(parts[-1]) > max_length:
font_size = Pt(8)
elif len(parts[-1]) > max_length-5:
font_size = Pt(9)
else:
font_size = Pt(10)
if len(parts) == 2:
paragraph = document.add_paragraph()
if indent:
paragraph.paragraph_format.left_indent = Cm(indent)
subject_text = paragraph.add_run(parts[0])
subject_text.font.size = font_size
paragraph = document.add_paragraph()
subject_text = paragraph.add_run(parts[-1])
subject_text.font.size = font_size
if indent:
paragraph.paragraph_format.left_indent = Cm(indent)
return paragraph
def add_index_line(document, topic):
subject = topic['topic']
paragraph = add_subject(document, subject, None)
if len(topic['entries']) > 0:
paragraph.paragraph_format.tab_stops.add_tab_stop(
Cm(8.5), WD_TAB_ALIGNMENT.RIGHT, WD_TAB_LEADER.DOTS
)
add_page_text(paragraph, topic)
for child_key in sorted(topic['children'].keys()):
child = topic['children'][child_key]
subject = child['topic']
paragraph = add_subject(document, subject, 0.5)
paragraph.paragraph_format.tab_stops.add_tab_stop(
Cm(8.5), WD_TAB_ALIGNMENT.RIGHT, WD_TAB_LEADER.DOTS
)
add_page_text(paragraph, child)
def create_traveller_index(topics, filename):
document = Document(filename)
delete_existing_entries(document)
section = document.add_section(0)
sectPr = section._sectPr
cols = sectPr.xpath("./w:cols")[0]
cols.set(qn('w:num'), '2')
last_type = None
for index, topic in enumerate(topics):
if topic['type'] != last_type:
add_type_paragraph(document, topic['type'])
last_type = topic['type']
add_index_line(document, topic)
document.save(filename)
def add_topic(key, row, topics):
if not key in topics:
topics[key] = {
'children': {},
'entries': {},
'topic': row['Topic'],
'type': row['Type'],
}
book = row['Document']
entry = topics[key]['entries']
if not book in entry:
entry[book] = []
entry[book].append({
"page": row['Page'],
"primary": row['Primary'] != 'No',
})
def parse_topics(source, topics):
df = pd.read_csv(source, delimiter='\t')
df = df.replace({pd.NA: None, pd.NaT: None, float('nan'): None})
for index, row in df.iterrows():
# adjust Mongoose's proper use of ’
row['Type'] = row['Type'].replace('’', "'")
row['Topic'] = row['Topic'].replace('’', "'")
for key in TYPE_CORRECTIONS:
if row['Type'] == key:
row['Type'] = TYPE_CORRECTIONS[key]
for key in GROUP_CORRECTIONS:
if row.get('Group', None) == key:
row['Group'] = GROUP_CORRECTIONS[key]
subject = row['Topic']
group = row.get('Group', None)
if group:
# adjust Mongoose's proper use of ’
group = group.replace('’', "'")
group_key = (row['Type'], group)
if group_key not in topics:
topics[group_key] = {
'children': {},
'topic': group,
'type': row['Type'],
'entries': {},
}
else:
group_key = None
key = (row['Type'], subject)
if group_key:
add_topic(key, row, topics[group_key]['children'])
if row['Type'] == 'Setting':
add_topic(key, row, topics)
else:
add_topic(key, row, topics)
def main():
parser = argparse.ArgumentParser(description="Mongoose Traveller Index Generator")
parser.add_argument('-f', '--file', nargs='+', help='File to process', required=False, default=[])
parser.add_argument('-d', '--dir', nargs='+', help='Directory to process', required=False, default=[])
parser.add_argument('-w', '--word', help='Output Word document')
parser.add_argument('--html', help='HTML document')
args = parser.parse_args()
source_topics = {}
for file in args.file:
parse_topics(file, source_topics)
for directory in args.dir:
for root, _, filenames in os.walk(directory):
for filename in filenames:
if filename.endswith('.tsv'):
full_path = os.path.join(root, filename)
parse_topics(full_path, source_topics)
sorted_topics = []
for key in sorted(source_topics.keys()):
sorted_topics.append(source_topics[key])
if args.word:
create_traveller_index(sorted_topics, args.word)
if args.html:
generate_web_index(topics, books, sorted_topics, args.html)
if __name__ == "__main__":
main()